Spaces:
Sleeping
Sleeping
Commit ·
9e01b2d
1
Parent(s): 295a30c
feat(habits): support custom checked date-time logs and interactive calendar checking
Browse files- public/logo.png +2 -2
- src/pages/HabitTrackerPage.jsx +188 -60
public/logo.png
CHANGED
|
Git LFS Details
|
|
Git LFS Details
|
src/pages/HabitTrackerPage.jsx
CHANGED
|
@@ -57,54 +57,98 @@ const calculateStreak = (recordsForHabit) => {
|
|
| 57 |
|
| 58 |
const HabitCard = ({ title, graphTitle, streak, xp, completedToday, onToggle }) => {
|
| 59 |
const { t } = useTranslation();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
return (
|
| 61 |
<motion.div
|
| 62 |
whileHover={{ y: -3 }}
|
| 63 |
-
className={`bg-surface-container-lowest p-6 rough-border flex
|
| 64 |
completedToday ? 'bg-secondary/5 border-secondary/40 shadow-sm' : 'hover:shadow-md'
|
| 65 |
}`}
|
| 66 |
>
|
| 67 |
-
<div className="flex items-center
|
| 68 |
-
<
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
<
|
| 82 |
-
{
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
</span>
|
| 89 |
-
|
| 90 |
-
<span className="flex items-center gap-1 text-orange-500">
|
| 91 |
-
<Flame size={14} /> {streak} {t('habits.streak')}
|
| 92 |
-
</span>
|
| 93 |
-
<span className="flex items-center gap-1 text-secondary">
|
| 94 |
-
<Zap size={14} /> +{xp} XP
|
| 95 |
-
</span>
|
| 96 |
</div>
|
| 97 |
</div>
|
| 98 |
-
</div>
|
| 99 |
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 103 |
</div>
|
| 104 |
-
<button className="text-on-surface-variant hover:text-primary p-2 transition-colors">
|
| 105 |
-
<MoreVertical size={18} />
|
| 106 |
-
</button>
|
| 107 |
</div>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 108 |
</motion.div>
|
| 109 |
);
|
| 110 |
};
|
|
@@ -119,6 +163,7 @@ const HabitTrackerPage = () => {
|
|
| 119 |
const [habitRecords, setHabitRecords] = useState([]);
|
| 120 |
const [loading, setLoading] = useState(true);
|
| 121 |
const [activeStreak, setActiveStreak] = useState(0);
|
|
|
|
| 122 |
|
| 123 |
useEffect(() => {
|
| 124 |
if (!user) {
|
|
@@ -215,35 +260,52 @@ const HabitTrackerPage = () => {
|
|
| 215 |
fetchHabits();
|
| 216 |
}, [user]);
|
| 217 |
|
| 218 |
-
const handleCheckHabit = async (habitId, habitName) => {
|
| 219 |
if (!user) return;
|
| 220 |
|
| 221 |
-
//
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
|
| 228 |
-
|
| 229 |
-
|
| 230 |
-
|
| 231 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 232 |
|
| 233 |
triggerNotification({
|
| 234 |
id: 'habit-complete-' + Date.now(),
|
| 235 |
icon: 'Flame',
|
| 236 |
title: t('habits.complete_notification_title', 'Habit Completed! 🔥'),
|
| 237 |
-
desc:
|
|
|
|
|
|
|
| 238 |
color: 'bg-orange-500/10'
|
| 239 |
});
|
| 240 |
|
| 241 |
try {
|
| 242 |
const API_URL = import.meta.env.VITE_API_URL;
|
| 243 |
-
|
| 244 |
userId: user.id,
|
| 245 |
habitName: habitName
|
| 246 |
-
}
|
|
|
|
|
|
|
|
|
|
|
|
|
| 247 |
|
| 248 |
// Pull fresh data to sync DB
|
| 249 |
fetchHabits();
|
|
@@ -328,13 +390,16 @@ const HabitTrackerPage = () => {
|
|
| 328 |
return (
|
| 329 |
<div key={i} className="flex flex-col items-center gap-4">
|
| 330 |
<span className="text-xs font-bold uppercase tracking-widest text-on-surface-variant">{dayObj.dayName}</span>
|
| 331 |
-
<
|
| 332 |
-
|
| 333 |
-
|
| 334 |
-
|
| 335 |
-
? 'bg-
|
| 336 |
-
:
|
| 337 |
-
|
|
|
|
|
|
|
|
|
|
| 338 |
{dayObj.dateNum}
|
| 339 |
{isChecked && (
|
| 340 |
<CheckCircle2
|
|
@@ -342,7 +407,7 @@ const HabitTrackerPage = () => {
|
|
| 342 |
className={`absolute bottom-2 right-2 text-secondary ${isToday ? 'text-white fill-white/10' : 'fill-secondary/20'}`}
|
| 343 |
/>
|
| 344 |
)}
|
| 345 |
-
</
|
| 346 |
</div>
|
| 347 |
);
|
| 348 |
})}
|
|
@@ -382,7 +447,7 @@ const HabitTrackerPage = () => {
|
|
| 382 |
<HabitCard
|
| 383 |
key={h.id || i}
|
| 384 |
{...h}
|
| 385 |
-
onToggle={() => handleCheckHabit(h.id, h.title)}
|
| 386 |
/>
|
| 387 |
))}
|
| 388 |
</div>
|
|
@@ -419,6 +484,69 @@ const HabitTrackerPage = () => {
|
|
| 419 |
</div>
|
| 420 |
</section>
|
| 421 |
</main>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 422 |
</div>
|
| 423 |
);
|
| 424 |
};
|
|
|
|
| 57 |
|
| 58 |
const HabitCard = ({ title, graphTitle, streak, xp, completedToday, onToggle }) => {
|
| 59 |
const { t } = useTranslation();
|
| 60 |
+
const [showPicker, setShowPicker] = useState(false);
|
| 61 |
+
const [customDate, setCustomDate] = useState('');
|
| 62 |
+
|
| 63 |
+
const handleCustomSubmit = (e) => {
|
| 64 |
+
e.preventDefault();
|
| 65 |
+
if (!customDate) return;
|
| 66 |
+
onToggle(customDate);
|
| 67 |
+
setShowPicker(false);
|
| 68 |
+
};
|
| 69 |
+
|
| 70 |
return (
|
| 71 |
<motion.div
|
| 72 |
whileHover={{ y: -3 }}
|
| 73 |
+
className={`bg-surface-container-lowest p-6 rough-border flex flex-col gap-4 transition-all duration-200 ${
|
| 74 |
completedToday ? 'bg-secondary/5 border-secondary/40 shadow-sm' : 'hover:shadow-md'
|
| 75 |
}`}
|
| 76 |
>
|
| 77 |
+
<div className="flex items-center justify-between group">
|
| 78 |
+
<div className="flex items-center gap-6">
|
| 79 |
+
<button
|
| 80 |
+
onClick={() => onToggle()}
|
| 81 |
+
disabled={completedToday}
|
| 82 |
+
className={`w-10 h-10 rough-border flex items-center justify-center transition-all ${
|
| 83 |
+
completedToday
|
| 84 |
+
? 'bg-secondary text-primary cursor-default'
|
| 85 |
+
: 'bg-surface-variant/20 text-transparent hover:text-primary/20 hover:scale-105 active:scale-95'
|
| 86 |
+
}`}
|
| 87 |
+
>
|
| 88 |
+
<CheckCircle2 size={24} />
|
| 89 |
+
</button>
|
| 90 |
+
|
| 91 |
+
<div>
|
| 92 |
+
<h3 className={`text-3xl font-display-lg transition-all ${completedToday ? 'text-primary/40 line-through' : 'text-primary'}`}>
|
| 93 |
+
{title}
|
| 94 |
+
</h3>
|
| 95 |
+
<div className="flex items-center gap-4 text-xs font-bold uppercase tracking-widest text-on-surface-variant mt-1">
|
| 96 |
+
{graphTitle && (
|
| 97 |
+
<span className="flex items-center gap-1 opacity-75 mr-2">
|
| 98 |
+
{t('dashboard.project')}: {graphTitle}
|
| 99 |
+
</span>
|
| 100 |
+
)}
|
| 101 |
+
<span className="flex items-center gap-1 text-orange-500">
|
| 102 |
+
<Flame size={14} /> {streak} {t('habits.streak')}
|
| 103 |
+
</span>
|
| 104 |
+
<span className="flex items-center gap-1 text-secondary">
|
| 105 |
+
<Zap size={14} /> +{xp} XP
|
| 106 |
</span>
|
| 107 |
+
</div>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 108 |
</div>
|
| 109 |
</div>
|
|
|
|
| 110 |
|
| 111 |
+
<div className="flex items-center gap-4">
|
| 112 |
+
<div className="w-32 h-2 bg-surface-variant rounded-full overflow-hidden hidden md:block">
|
| 113 |
+
<div className="h-full bg-secondary transition-all" style={{ width: `${Math.min((streak / 30) * 100, 100)}%` }} />
|
| 114 |
+
</div>
|
| 115 |
+
<button
|
| 116 |
+
onClick={() => setShowPicker(!showPicker)}
|
| 117 |
+
className={`text-on-surface-variant hover:text-primary p-2 transition-colors ${showPicker ? 'text-primary bg-surface-variant/20' : ''}`}
|
| 118 |
+
title="Track for custom date/time"
|
| 119 |
+
>
|
| 120 |
+
<MoreVertical size={18} />
|
| 121 |
+
</button>
|
| 122 |
</div>
|
|
|
|
|
|
|
|
|
|
| 123 |
</div>
|
| 124 |
+
|
| 125 |
+
{showPicker && (
|
| 126 |
+
<motion.form
|
| 127 |
+
initial={{ opacity: 0, height: 0 }}
|
| 128 |
+
animate={{ opacity: 1, height: 'auto' }}
|
| 129 |
+
onSubmit={handleCustomSubmit}
|
| 130 |
+
className="border-t-2 border-surface-variant pt-4 flex flex-wrap gap-4 items-center"
|
| 131 |
+
>
|
| 132 |
+
<div className="flex flex-col gap-1">
|
| 133 |
+
<label className="text-xs font-bold uppercase tracking-wider text-on-surface-variant">
|
| 134 |
+
{t('habits.select_date_time', 'Select Date & Time')}
|
| 135 |
+
</label>
|
| 136 |
+
<input
|
| 137 |
+
type="datetime-local"
|
| 138 |
+
value={customDate}
|
| 139 |
+
onChange={(e) => setCustomDate(e.target.value)}
|
| 140 |
+
required
|
| 141 |
+
className="bg-surface p-2 rough-border text-primary font-bold focus:outline-none focus:border-secondary transition-all"
|
| 142 |
+
/>
|
| 143 |
+
</div>
|
| 144 |
+
<button
|
| 145 |
+
type="submit"
|
| 146 |
+
className="bg-secondary text-primary font-bold py-2 px-6 rough-border rough-shadow-hover hover:scale-105 active:scale-95 transition-all self-end"
|
| 147 |
+
>
|
| 148 |
+
{t('habits.log_time', 'Record')}
|
| 149 |
+
</button>
|
| 150 |
+
</motion.form>
|
| 151 |
+
)}
|
| 152 |
</motion.div>
|
| 153 |
);
|
| 154 |
};
|
|
|
|
| 163 |
const [habitRecords, setHabitRecords] = useState([]);
|
| 164 |
const [loading, setLoading] = useState(true);
|
| 165 |
const [activeStreak, setActiveStreak] = useState(0);
|
| 166 |
+
const [selectedCalendarDate, setSelectedCalendarDate] = useState(null);
|
| 167 |
|
| 168 |
useEffect(() => {
|
| 169 |
if (!user) {
|
|
|
|
| 260 |
fetchHabits();
|
| 261 |
}, [user]);
|
| 262 |
|
| 263 |
+
const handleCheckHabit = async (habitId, habitName, customDate = null) => {
|
| 264 |
if (!user) return;
|
| 265 |
|
| 266 |
+
// Only update completedToday if the recorded date is actually today!
|
| 267 |
+
const isToday = !customDate || (() => {
|
| 268 |
+
const d = new Date(customDate);
|
| 269 |
+
const today = new Date();
|
| 270 |
+
return d.getDate() === today.getDate() &&
|
| 271 |
+
d.getMonth() === today.getMonth() &&
|
| 272 |
+
d.getFullYear() === today.getFullYear();
|
| 273 |
+
})();
|
| 274 |
+
|
| 275 |
+
if (isToday) {
|
| 276 |
+
// Optimistic UI updates
|
| 277 |
+
setHabitsList(prev => prev.map(h => {
|
| 278 |
+
if (h.id === habitId) {
|
| 279 |
+
return {
|
| 280 |
+
...h,
|
| 281 |
+
completedToday: true,
|
| 282 |
+
streak: h.streak + 1
|
| 283 |
+
};
|
| 284 |
+
}
|
| 285 |
+
return h;
|
| 286 |
+
}));
|
| 287 |
+
}
|
| 288 |
|
| 289 |
triggerNotification({
|
| 290 |
id: 'habit-complete-' + Date.now(),
|
| 291 |
icon: 'Flame',
|
| 292 |
title: t('habits.complete_notification_title', 'Habit Completed! 🔥'),
|
| 293 |
+
desc: customDate
|
| 294 |
+
? t('habits.complete_notification_desc_custom', `Recorded check-in for ${new Date(customDate).toLocaleDateString()}`)
|
| 295 |
+
: t('habits.complete_notification_desc', 'Dynamic streak increased! +50 XP awarded!'),
|
| 296 |
color: 'bg-orange-500/10'
|
| 297 |
});
|
| 298 |
|
| 299 |
try {
|
| 300 |
const API_URL = import.meta.env.VITE_API_URL;
|
| 301 |
+
const requestPayload = {
|
| 302 |
userId: user.id,
|
| 303 |
habitName: habitName
|
| 304 |
+
};
|
| 305 |
+
if (customDate) {
|
| 306 |
+
requestPayload.checkedDate = new Date(customDate).toISOString();
|
| 307 |
+
}
|
| 308 |
+
await axios.post(`${API_URL}/habits/record`, requestPayload);
|
| 309 |
|
| 310 |
// Pull fresh data to sync DB
|
| 311 |
fetchHabits();
|
|
|
|
| 390 |
return (
|
| 391 |
<div key={i} className="flex flex-col items-center gap-4">
|
| 392 |
<span className="text-xs font-bold uppercase tracking-widest text-on-surface-variant">{dayObj.dayName}</span>
|
| 393 |
+
<button
|
| 394 |
+
onClick={() => setSelectedCalendarDate(dayObj.fullDate)}
|
| 395 |
+
className={`w-full aspect-square rough-border flex items-center justify-center text-2xl font-display-lg transition-all relative cursor-pointer hover:scale-105 active:scale-95 ${
|
| 396 |
+
isToday
|
| 397 |
+
? 'bg-primary text-on-primary scale-110 shadow-lg'
|
| 398 |
+
: isChecked
|
| 399 |
+
? 'bg-secondary/20 text-primary'
|
| 400 |
+
: 'bg-surface-container-lowest text-on-surface-variant/50'
|
| 401 |
+
}`}
|
| 402 |
+
>
|
| 403 |
{dayObj.dateNum}
|
| 404 |
{isChecked && (
|
| 405 |
<CheckCircle2
|
|
|
|
| 407 |
className={`absolute bottom-2 right-2 text-secondary ${isToday ? 'text-white fill-white/10' : 'fill-secondary/20'}`}
|
| 408 |
/>
|
| 409 |
)}
|
| 410 |
+
</button>
|
| 411 |
</div>
|
| 412 |
);
|
| 413 |
})}
|
|
|
|
| 447 |
<HabitCard
|
| 448 |
key={h.id || i}
|
| 449 |
{...h}
|
| 450 |
+
onToggle={(customDate) => handleCheckHabit(h.id, h.title, customDate)}
|
| 451 |
/>
|
| 452 |
))}
|
| 453 |
</div>
|
|
|
|
| 484 |
</div>
|
| 485 |
</section>
|
| 486 |
</main>
|
| 487 |
+
|
| 488 |
+
{/* Selected Calendar Date Dialog */}
|
| 489 |
+
{selectedCalendarDate && (
|
| 490 |
+
<div className="fixed inset-0 bg-primary/20 backdrop-blur-sm flex items-center justify-center z-50 p-4">
|
| 491 |
+
<div className="bg-surface-container-lowest p-8 rough-border max-w-md w-full flex flex-col gap-6 relative">
|
| 492 |
+
<button
|
| 493 |
+
onClick={() => setSelectedCalendarDate(null)}
|
| 494 |
+
className="absolute top-4 right-4 text-on-surface-variant hover:text-primary text-xl font-bold"
|
| 495 |
+
>
|
| 496 |
+
×
|
| 497 |
+
</button>
|
| 498 |
+
<h3 className="text-3xl font-display-lg text-primary">
|
| 499 |
+
{t('habits.track_for_date', 'Track for {{date}}', {
|
| 500 |
+
date: selectedCalendarDate.toLocaleDateString(undefined, { weekday: 'long', month: 'short', day: 'numeric' })
|
| 501 |
+
})}
|
| 502 |
+
</h3>
|
| 503 |
+
|
| 504 |
+
<div className="flex flex-col gap-4">
|
| 505 |
+
{habitsList.length === 0 ? (
|
| 506 |
+
<p className="text-on-surface-variant text-center">{t('habits.no_habits', 'No habits found')}</p>
|
| 507 |
+
) : (
|
| 508 |
+
habitsList.map(habit => {
|
| 509 |
+
const isCheckedOnDate = habitRecords.some(r => {
|
| 510 |
+
const rDate = new Date(r.checkedDate);
|
| 511 |
+
return rDate.getDate() === selectedCalendarDate.getDate() &&
|
| 512 |
+
rDate.getMonth() === selectedCalendarDate.getMonth() &&
|
| 513 |
+
rDate.getFullYear() === selectedCalendarDate.getFullYear() &&
|
| 514 |
+
r.habitName === habit.title;
|
| 515 |
+
});
|
| 516 |
+
|
| 517 |
+
return (
|
| 518 |
+
<div
|
| 519 |
+
key={habit.id}
|
| 520 |
+
className="flex items-center justify-between p-4 rough-border bg-surface"
|
| 521 |
+
>
|
| 522 |
+
<div>
|
| 523 |
+
<span className="text-xl font-display-md text-primary">{habit.title}</span>
|
| 524 |
+
<span className="block text-xs font-bold uppercase tracking-widest text-on-surface-variant mt-1">
|
| 525 |
+
{habit.graphTitle}
|
| 526 |
+
</span>
|
| 527 |
+
</div>
|
| 528 |
+
<button
|
| 529 |
+
disabled={isCheckedOnDate}
|
| 530 |
+
onClick={async () => {
|
| 531 |
+
await handleCheckHabit(habit.id, habit.title, selectedCalendarDate);
|
| 532 |
+
setSelectedCalendarDate(null);
|
| 533 |
+
}}
|
| 534 |
+
className={`w-10 h-10 rough-border flex items-center justify-center transition-all ${
|
| 535 |
+
isCheckedOnDate
|
| 536 |
+
? 'bg-secondary text-primary cursor-default'
|
| 537 |
+
: 'bg-surface-variant/20 text-transparent hover:text-primary/20 hover:scale-105 active:scale-95 cursor-pointer'
|
| 538 |
+
}`}
|
| 539 |
+
>
|
| 540 |
+
<CheckCircle2 size={24} />
|
| 541 |
+
</button>
|
| 542 |
+
</div>
|
| 543 |
+
);
|
| 544 |
+
})
|
| 545 |
+
)}
|
| 546 |
+
</div>
|
| 547 |
+
</div>
|
| 548 |
+
</div>
|
| 549 |
+
)}
|
| 550 |
</div>
|
| 551 |
);
|
| 552 |
};
|