mirror of
https://github.com/harivansh-afk/Habit-Tracker.git
synced 2026-04-17 03:03:47 +00:00
adding today feature on main habit page, show streaks feature on settings page and improved dark mode functionality
This commit is contained in:
parent
1bb06006e8
commit
6b6cba21e5
9 changed files with 705 additions and 362 deletions
99
src/hooks/useHabits.ts
Normal file
99
src/hooks/useHabits.ts
Normal 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
|
||||
};
|
||||
};
|
||||
Loading…
Add table
Add a link
Reference in a new issue