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