mirror of
https://github.com/harivansh-afk/Habit-Tracker.git
synced 2026-04-18 23:02:16 +00:00
manual streaking
This commit is contained in:
parent
a1c0aeb8e7
commit
465ec16e30
4 changed files with 39 additions and 7 deletions
BIN
habits.db
BIN
habits.db
Binary file not shown.
17
src/App.tsx
17
src/App.tsx
|
|
@ -151,6 +151,21 @@ export default function HabitTracker() {
|
||||||
return streak;
|
return streak;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const handleUpdateStreak = async (id: number, newStreak: number) => {
|
||||||
|
// Prevent negative streaks
|
||||||
|
if (newStreak < 0) return;
|
||||||
|
|
||||||
|
// Update in database
|
||||||
|
await db.habits.update(id, { manualStreak: newStreak });
|
||||||
|
|
||||||
|
// Update state
|
||||||
|
setHabits(habits.map(habit =>
|
||||||
|
habit.id === id
|
||||||
|
? { ...habit, manualStreak: newStreak }
|
||||||
|
: habit
|
||||||
|
));
|
||||||
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="min-h-screen bg-white dark:bg-black">
|
<div className="min-h-screen bg-white dark:bg-black">
|
||||||
<div className="flex h-screen">
|
<div className="flex h-screen">
|
||||||
|
|
@ -246,7 +261,7 @@ export default function HabitTracker() {
|
||||||
onToggleHabit={toggleHabit}
|
onToggleHabit={toggleHabit}
|
||||||
onUpdateHabit={updateHabit}
|
onUpdateHabit={updateHabit}
|
||||||
onDeleteHabit={deleteHabit}
|
onDeleteHabit={deleteHabit}
|
||||||
getStreakForHabit={getStreakForHabit}
|
onUpdateStreak={handleUpdateStreak}
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
import React from 'react';
|
import React from 'react';
|
||||||
import { Trash2 } from 'lucide-react';
|
import { Trash2, ChevronUp, ChevronDown } from 'lucide-react';
|
||||||
import { Habit } from '../types';
|
import { Habit } from '../types';
|
||||||
|
|
||||||
interface HabitListProps {
|
interface HabitListProps {
|
||||||
|
|
@ -9,7 +9,7 @@ interface HabitListProps {
|
||||||
onToggleHabit: (id: number, date: string) => void;
|
onToggleHabit: (id: number, date: string) => void;
|
||||||
onUpdateHabit: (id: number, name: string) => void;
|
onUpdateHabit: (id: number, name: string) => void;
|
||||||
onDeleteHabit: (id: number) => void;
|
onDeleteHabit: (id: number) => void;
|
||||||
getStreakForHabit: (habit: Habit) => number;
|
onUpdateStreak: (id: number, streak: number) => void;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function HabitList({
|
export function HabitList({
|
||||||
|
|
@ -19,7 +19,7 @@ export function HabitList({
|
||||||
onToggleHabit,
|
onToggleHabit,
|
||||||
onUpdateHabit,
|
onUpdateHabit,
|
||||||
onDeleteHabit,
|
onDeleteHabit,
|
||||||
getStreakForHabit
|
onUpdateStreak
|
||||||
}: HabitListProps) {
|
}: HabitListProps) {
|
||||||
return (
|
return (
|
||||||
<table className="w-full">
|
<table className="w-full">
|
||||||
|
|
@ -34,7 +34,7 @@ export function HabitList({
|
||||||
</div>
|
</div>
|
||||||
</th>
|
</th>
|
||||||
))}
|
))}
|
||||||
<th className="px-4 py-2 text-center dark:text-white">Streak</th>
|
<th className="px-4 py-2 text-center dark:text-white">Manual Streak</th>
|
||||||
<th className="px-4 py-2 text-center dark:text-white">Actions</th>
|
<th className="px-4 py-2 text-center dark:text-white">Actions</th>
|
||||||
</tr>
|
</tr>
|
||||||
</thead>
|
</thead>
|
||||||
|
|
@ -59,8 +59,24 @@ export function HabitList({
|
||||||
/>
|
/>
|
||||||
</td>
|
</td>
|
||||||
))}
|
))}
|
||||||
<td className="px-4 py-2 text-center dark:text-white">
|
<td className="px-4 py-2 text-center">
|
||||||
{getStreakForHabit(habit)}
|
<div className="flex items-center justify-center space-x-2">
|
||||||
|
<button
|
||||||
|
onClick={() => onUpdateStreak(habit.id, (habit.manualStreak || 0) - 1)}
|
||||||
|
className="p-1 hover:bg-gray-100 dark:hover:bg-gray-800 rounded-full"
|
||||||
|
>
|
||||||
|
<ChevronDown className="h-4 w-4 dark:text-white" />
|
||||||
|
</button>
|
||||||
|
<span className="dark:text-white min-w-[2rem]">
|
||||||
|
{habit.manualStreak || 0}
|
||||||
|
</span>
|
||||||
|
<button
|
||||||
|
onClick={() => onUpdateStreak(habit.id, (habit.manualStreak || 0) + 1)}
|
||||||
|
className="p-1 hover:bg-gray-100 dark:hover:bg-gray-800 rounded-full"
|
||||||
|
>
|
||||||
|
<ChevronUp className="h-4 w-4 dark:text-white" />
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
</td>
|
</td>
|
||||||
<td className="px-4 py-2 text-center">
|
<td className="px-4 py-2 text-center">
|
||||||
<button
|
<button
|
||||||
|
|
|
||||||
|
|
@ -2,4 +2,5 @@ export interface Habit {
|
||||||
id: number;
|
id: number;
|
||||||
name: string;
|
name: string;
|
||||||
completedDates: string[];
|
completedDates: string[];
|
||||||
|
manualStreak: number;
|
||||||
}
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue