From 20e91dae4cca04e99ba9260ec6c50774a0501758 Mon Sep 17 00:00:00 2001 From: rathi Date: Thu, 21 Nov 2024 16:13:24 -0500 Subject: [PATCH] user settings debugged --- src/App.tsx | 16 ++- src/components/HabitList.tsx | 21 +++- src/components/SettingsView.tsx | 163 ++++++++++++---------------- src/contexts/PreferencesContext.tsx | 143 ++++++++++++++++++++++++ src/contexts/ThemeContext.tsx | 142 ++++-------------------- src/lib/supabase.ts | 8 +- src/types/preferences.ts | 13 +++ 7 files changed, 282 insertions(+), 224 deletions(-) create mode 100644 src/contexts/PreferencesContext.tsx create mode 100644 src/types/preferences.ts diff --git a/src/App.tsx b/src/App.tsx index 85704ff..6e784b5 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -11,6 +11,7 @@ import { Login } from './components/Login'; import { SignUp } from './components/SignUp'; import { SettingsView } from './components/SettingsView'; import { MobileNav } from './components/MobileNav'; +import { PreferencesProvider, usePreferences } from './contexts/PreferencesContext'; const DAYS_OF_WEEK = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']; @@ -21,6 +22,7 @@ function HabitTrackerContent() { const [currentMonth, setCurrentMonth] = useState(new Date()); const { user, loading, signOut } = useAuth(); const [authView, setAuthView] = useState<'login' | 'signup'>('login'); + const { preferences } = usePreferences(); const { habits, @@ -43,6 +45,12 @@ function HabitTrackerContent() { setCurrentWeek(getCurrentWeekDates()); }, []); + useEffect(() => { + if (preferences?.default_view) { + setActiveView(preferences.default_view); + } + }, [preferences?.default_view]); + const handleAddHabit = async (e: React.FormEvent) => { e.preventDefault(); if (newHabit.trim()) { @@ -196,9 +204,11 @@ function HabitTrackerContent() { export default function HabitTracker() { return ( - - - + + + + + ); } \ No newline at end of file diff --git a/src/components/HabitList.tsx b/src/components/HabitList.tsx index e21e5eb..f786f9d 100644 --- a/src/components/HabitList.tsx +++ b/src/components/HabitList.tsx @@ -1,4 +1,4 @@ -import React from 'react'; +import React, { useMemo } from 'react'; import { Trash2 } from 'lucide-react'; import { Habit } from '../types'; import { useThemeContext } from '../contexts/ThemeContext'; @@ -22,7 +22,15 @@ export function HabitList({ onUpdateHabit, onDeleteHabit, }: HabitListProps) { - const { showStreaks } = useThemeContext(); + const { habitSort, showStreaks } = useThemeContext(); + + const sortedHabits = useMemo(() => { + if (habitSort === 'alphabetical') { + return [...habits].sort((a, b) => a.name.localeCompare(b.name)); + } + // Default to dateCreated sort + return habits; + }, [habits, habitSort]); // Helper function to get day name const getDayName = (dateStr: string) => { @@ -53,7 +61,7 @@ export function HabitList({ - {habits.map((habit) => ( + {sortedHabits.map((habit) => ( + {showStreaks && ( + <> + + {/* ... streak content ... */} + + + )} ))} diff --git a/src/components/SettingsView.tsx b/src/components/SettingsView.tsx index 328e3b2..030bf63 100644 --- a/src/components/SettingsView.tsx +++ b/src/components/SettingsView.tsx @@ -1,120 +1,95 @@ import React from 'react'; import { Sun, Moon } from 'lucide-react'; +import { usePreferences } from '../contexts/PreferencesContext'; import { useThemeContext } from '../contexts/ThemeContext'; export function SettingsView() { - const { - theme, - isDark, - showStreaks, - dailyReminder, - defaultView, - habitSort, - toggleDarkMode, - toggleStreaks, - toggleDailyReminder, - setDefaultView, - setHabitSort - } = useThemeContext(); + const { preferences, updatePreferences } = usePreferences(); + const { theme } = useThemeContext(); - const handleReminderToggle = () => { - if (!dailyReminder && Notification.permission === 'default') { - Notification.requestPermission().then(permission => { - if (permission === 'granted') { - toggleDailyReminder(); - } - }); - } else { - toggleDailyReminder(); - } + const handleThemeChange = async (newTheme: 'light' | 'dark') => { + await updatePreferences({ theme: newTheme }); + // Theme will be updated automatically through ThemeContext + }; + + const handleSortChange = async (newSort: 'dateCreated' | 'alphabetical') => { + await updatePreferences({ habit_sort: newSort }); + // Sort will be updated automatically through ThemeContext + }; + + const handleStreaksChange = async (showStreaks: boolean) => { + await updatePreferences({ show_streaks: showStreaks }); + // Streaks visibility will be updated automatically through ThemeContext }; return ( -
-

Settings

- - {/* Theme Toggle */} -
-

Theme

- -
- - {/* Streaks Toggle */} -
-

Streaks

-