✨ persist user preferences
Browse files at this commit60e7901b0d8e2e5ef2b5ebc613d0fa68380871ddParent: 88729f8- feat
- ✨ persist user preferences
4 changed files +47 -3
- src/api/patchMyPreferences.ts +11 -0
- src/api/types.ts +4 -0
- src/stores/auth-context.tsx +8 -0
- src/stores/preferences-context.tsx +24 -3
src/api/patchMyPreferences.tsadded+11 -0
| @@ -0,0 +1,11 @@ | ||
| 1 | +import { client } from "./client"; | |
| 2 | +import type { User, UserLanguage, UserTheme } from "./types"; | |
| 3 | + | |
| 4 | +export type PatchMyPreferencesBody = { | |
| 5 | + theme?: UserTheme; | |
| 6 | + language?: UserLanguage; | |
| 7 | +}; | |
| 8 | + | |
| 9 | +export function patchMyPreferences(body: PatchMyPreferencesBody): Promise<User> { | |
| 10 | + return client<User>("/users/me/preferences", { method: "PATCH", body }); | |
| 11 | +} |
src/api/types.tsmodified+4 -0
| @@ -1,6 +1,8 @@ | ||
| 1 | 1 | export type { Page, PageParams } from "./page"; |
| 2 | 2 | |
| 3 | 3 | export type Role = "student" | "teacher" | "manager" | "admin"; |
| 4 | +export type UserTheme = "light" | "dark"; | |
| 5 | +export type UserLanguage = "tr" | "en"; | |
| 4 | 6 | export type CoreAttendanceStatus = "present" | "absent" | "late" | "excused"; |
| 5 | 7 | export type AttendanceStatus = CoreAttendanceStatus | string; |
| 6 | 8 | export type KnownExamKind = "homework" | "quiz" | "midterm" | "final" | "project" | "oral"; |
| @@ -18,6 +20,8 @@ export type User = { | ||
| 18 | 20 | email: string | null; |
| 19 | 21 | phone: string | null; |
| 20 | 22 | birth_date: string | null; |
| 23 | + theme: UserTheme | null; | |
| 24 | + language: UserLanguage | null; | |
| 21 | 25 | }; |
| 22 | 26 | |
| 23 | 27 | export type ProfileUpdate = { |
src/stores/auth-context.tsxmodified+8 -0
| @@ -2,6 +2,7 @@ import { | ||
| 2 | 2 | type Accessor, |
| 3 | 3 | type ParentProps, |
| 4 | 4 | createContext, |
| 5 | + createEffect, | |
| 5 | 6 | createResource, |
| 6 | 7 | createSignal, |
| 7 | 8 | useContext, |
| @@ -10,6 +11,7 @@ import { getMe } from "@/api/getMe"; | ||
| 10 | 11 | import { postLogout } from "@/api/postLogout"; |
| 11 | 12 | import type { User } from "@/api/types"; |
| 12 | 13 | import { ApiError } from "@/api/client"; |
| 14 | +import { usePreferences } from "@/stores/preferences-context"; | |
| 13 | 15 | |
| 14 | 16 | type AuthContextValue = { |
| 15 | 17 | user: Accessor<User | null | undefined>; |
| @@ -22,6 +24,7 @@ type AuthContextValue = { | ||
| 22 | 24 | const AuthContext = createContext<AuthContextValue>(); |
| 23 | 25 | |
| 24 | 26 | export function AuthProvider(props: ParentProps) { |
| 27 | + const prefs = usePreferences(); | |
| 25 | 28 | const [override, setOverride] = createSignal<User | null | undefined>(undefined); |
| 26 | 29 | const [me, { refetch }] = createResource(async () => { |
| 27 | 30 | try { |
| @@ -40,6 +43,11 @@ export function AuthProvider(props: ParentProps) { | ||
| 40 | 43 | |
| 41 | 44 | const setUser = (u: User | null) => setOverride(u); |
| 42 | 45 | |
| 46 | + createEffect(() => { | |
| 47 | + const u = user(); | |
| 48 | + if (u) prefs.hydratePreferences(u); | |
| 49 | + }); | |
| 50 | + | |
| 43 | 51 | const refresh = async () => { |
| 44 | 52 | setOverride(undefined); |
| 45 | 53 | const result = await refetch(); |
src/stores/preferences-context.tsxmodified+24 -3
| @@ -6,6 +6,8 @@ import { | ||
| 6 | 6 | createSignal, |
| 7 | 7 | useContext, |
| 8 | 8 | } from "solid-js"; |
| 9 | +import { patchMyPreferences } from "@/api/patchMyPreferences"; | |
| 10 | +import type { User } from "@/api/types"; | |
| 9 | 11 | import { formatMessage, messages, type Locale, type MessageKey } from "@/i18n/messages"; |
| 10 | 12 | |
| 11 | 13 | export type ThemeMode = "light" | "dark"; |
| @@ -16,6 +18,7 @@ type PreferencesContextValue = { | ||
| 16 | 18 | theme: Accessor<ThemeMode>; |
| 17 | 19 | setTheme: (theme: ThemeMode) => void; |
| 18 | 20 | toggleTheme: () => void; |
| 21 | + hydratePreferences: (user: Pick<User, "theme" | "language">) => void; | |
| 19 | 22 | sidebarCollapsed: Accessor<boolean>; |
| 20 | 23 | setSidebarCollapsed: (collapsed: boolean) => void; |
| 21 | 24 | toggleSidebar: () => void; |
| @@ -88,9 +91,26 @@ export function PreferencesProvider(props: ParentProps) { | ||
| 88 | 91 | localStorage.setItem(SIDEBAR_KEY, sidebarCollapsed() ? "1" : "0"); |
| 89 | 92 | }); |
| 90 | 93 | |
| 91 | - const setLocale = (l: Locale) => setLocaleSignal(l); | |
| 92 | - const setTheme = (th: ThemeMode) => setThemeSignal(th); | |
| 93 | - const toggleTheme = () => setThemeSignal((t) => (t === "dark" ? "light" : "dark")); | |
| 94 | + const persistPreferences = (body: Parameters<typeof patchMyPreferences>[0]) => { | |
| 95 | + void patchMyPreferences(body).catch(() => undefined); | |
| 96 | + }; | |
| 97 | + | |
| 98 | + const setLocale = (l: Locale) => { | |
| 99 | + setLocaleSignal(l); | |
| 100 | + persistPreferences({ language: l }); | |
| 101 | + }; | |
| 102 | + const setTheme = (th: ThemeMode) => { | |
| 103 | + setThemeSignal(th); | |
| 104 | + persistPreferences({ theme: th }); | |
| 105 | + }; | |
| 106 | + const toggleTheme = () => { | |
| 107 | + const next = theme() === "dark" ? "light" : "dark"; | |
| 108 | + setTheme(next); | |
| 109 | + }; | |
| 110 | + const hydratePreferences = (user: Pick<User, "theme" | "language">) => { | |
| 111 | + if (user.language === "en" || user.language === "tr") setLocaleSignal(user.language); | |
| 112 | + if (user.theme === "light" || user.theme === "dark") setThemeSignal(user.theme); | |
| 113 | + }; | |
| 94 | 114 | const setSidebarCollapsed = (v: boolean) => setSidebarCollapsedSignal(v); |
| 95 | 115 | const toggleSidebar = () => setSidebarCollapsedSignal((v) => !v); |
| 96 | 116 | |
| @@ -108,6 +128,7 @@ export function PreferencesProvider(props: ParentProps) { | ||
| 108 | 128 | theme, |
| 109 | 129 | setTheme, |
| 110 | 130 | toggleTheme, |
| 131 | + hydratePreferences, | |
| 111 | 132 | sidebarCollapsed, |
| 112 | 133 | setSidebarCollapsed, |
| 113 | 134 | toggleSidebar, |