adding today feature on main habit page, show streaks feature on settings page and improved dark mode functionality

This commit is contained in:
Harivansh Rathi 2024-11-20 14:44:45 -05:00
parent 1bb06006e8
commit 6b6cba21e5
9 changed files with 705 additions and 362 deletions

99
src/hooks/useHabits.ts Normal file
View file

@ -0,0 +1,99 @@
import { useState } from 'react';
import { Habit } from '../types';
export const useHabits = () => {
const [habits, setHabits] = useState<Habit[]>([]);
const fetchHabits = async () => {
try {
const response = await fetch('http://localhost:5000/api/habits');
const data = await response.json();
setHabits(data);
} catch (error) {
console.error('Error fetching habits:', error);
setHabits([]);
}
};
const addHabit = async (name: string) => {
try {
const response = await fetch('http://localhost:5000/api/habits', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ name }),
});
if (response.ok) {
await fetchHabits();
return true;
}
return false;
} catch (error) {
console.error('Error adding habit:', error);
return false;
}
};
const toggleHabit = async (id: number, date: string) => {
try {
await fetch(`http://localhost:5000/api/habits/${id}/toggle`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ date }),
});
await fetchHabits();
} catch (error) {
console.error('Error toggling habit:', error);
}
};
const updateHabit = async (id: number, name: string) => {
try {
await fetch(`http://localhost:5000/api/habits/${id}`, {
method: 'PUT',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ name }),
});
await fetchHabits();
} catch (error) {
console.error('Error updating habit:', error);
}
};
const deleteHabit = async (id: number) => {
try {
await fetch(`http://localhost:5000/api/habits/${id}`, {
method: 'DELETE',
});
await fetchHabits();
} catch (error) {
console.error('Error deleting habit:', error);
}
};
const updateStreak = async (id: number, newStreak: number) => {
if (newStreak < 0) return;
try {
await fetch(`http://localhost:5000/api/habits/${id}/streak`, {
method: 'PUT',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ streak: newStreak }),
});
setHabits(habits.map(habit =>
habit.id === id ? { ...habit, manualStreak: newStreak } : habit
));
} catch (error) {
console.error('Error updating streak:', error);
}
};
return {
habits,
fetchHabits,
addHabit,
toggleHabit,
updateHabit,
deleteHabit,
updateStreak
};
};

34
src/hooks/useWeek.ts Normal file
View file

@ -0,0 +1,34 @@
import { useState } from 'react';
export const useWeek = () => {
const [currentWeek, setCurrentWeek] = useState<string[]>([]);
const getCurrentWeekDates = () => {
const now = new Date();
const dayOfWeek = now.getDay();
const diff = now.getDate() - dayOfWeek + (dayOfWeek === 0 ? -6 : 1);
const monday = new Date(now.setDate(diff));
return Array.from({ length: 7 }, (_, i) => {
const date = new Date(monday);
date.setDate(monday.getDate() + i);
return date.toISOString().split('T')[0];
});
};
const changeWeek = (direction: 'prev' | 'next') => {
const firstDay = new Date(currentWeek[0]);
const newFirstDay = new Date(firstDay.setDate(firstDay.getDate() + (direction === 'prev' ? -7 : 7)));
setCurrentWeek(Array.from({ length: 7 }, (_, i) => {
const date = new Date(newFirstDay);
date.setDate(newFirstDay.getDate() + i);
return date.toISOString().split('T')[0];
}));
};
return {
currentWeek,
setCurrentWeek,
getCurrentWeekDates,
changeWeek
};
};