diff --git a/src/api/patchMyPreferences.ts b/src/api/patchMyPreferences.ts new file mode 100644 index 0000000..7ad5339 --- /dev/null +++ b/src/api/patchMyPreferences.ts @@ -0,0 +1,11 @@ +import { client } from "./client"; +import type { User, UserLanguage, UserTheme } from "./types"; + +export type PatchMyPreferencesBody = { + theme?: UserTheme; + language?: UserLanguage; +}; + +export function patchMyPreferences(body: PatchMyPreferencesBody): Promise { + return client("/users/me/preferences", { method: "PATCH", body }); +} diff --git a/src/api/types.ts b/src/api/types.ts index 9696ea2..07b1a0f 100644 --- a/src/api/types.ts +++ b/src/api/types.ts @@ -1,6 +1,8 @@ export type { Page, PageParams } from "./page"; export type Role = "student" | "teacher" | "manager" | "admin"; +export type UserTheme = "light" | "dark"; +export type UserLanguage = "tr" | "en"; export type CoreAttendanceStatus = "present" | "absent" | "late" | "excused"; export type AttendanceStatus = CoreAttendanceStatus | string; export type KnownExamKind = "homework" | "quiz" | "midterm" | "final" | "project" | "oral"; @@ -18,6 +20,8 @@ export type User = { email: string | null; phone: string | null; birth_date: string | null; + theme: UserTheme | null; + language: UserLanguage | null; }; export type ProfileUpdate = { diff --git a/src/stores/auth-context.tsx b/src/stores/auth-context.tsx index f24b215..ac2e6b2 100644 --- a/src/stores/auth-context.tsx +++ b/src/stores/auth-context.tsx @@ -2,6 +2,7 @@ import { type Accessor, type ParentProps, createContext, + createEffect, createResource, createSignal, useContext, @@ -10,6 +11,7 @@ import { getMe } from "@/api/getMe"; import { postLogout } from "@/api/postLogout"; import type { User } from "@/api/types"; import { ApiError } from "@/api/client"; +import { usePreferences } from "@/stores/preferences-context"; type AuthContextValue = { user: Accessor; @@ -22,6 +24,7 @@ type AuthContextValue = { const AuthContext = createContext(); export function AuthProvider(props: ParentProps) { + const prefs = usePreferences(); const [override, setOverride] = createSignal(undefined); const [me, { refetch }] = createResource(async () => { try { @@ -40,6 +43,11 @@ export function AuthProvider(props: ParentProps) { const setUser = (u: User | null) => setOverride(u); + createEffect(() => { + const u = user(); + if (u) prefs.hydratePreferences(u); + }); + const refresh = async () => { setOverride(undefined); const result = await refetch(); diff --git a/src/stores/preferences-context.tsx b/src/stores/preferences-context.tsx index ece81fd..97a233e 100644 --- a/src/stores/preferences-context.tsx +++ b/src/stores/preferences-context.tsx @@ -6,6 +6,8 @@ import { createSignal, useContext, } from "solid-js"; +import { patchMyPreferences } from "@/api/patchMyPreferences"; +import type { User } from "@/api/types"; import { formatMessage, messages, type Locale, type MessageKey } from "@/i18n/messages"; export type ThemeMode = "light" | "dark"; @@ -16,6 +18,7 @@ type PreferencesContextValue = { theme: Accessor; setTheme: (theme: ThemeMode) => void; toggleTheme: () => void; + hydratePreferences: (user: Pick) => void; sidebarCollapsed: Accessor; setSidebarCollapsed: (collapsed: boolean) => void; toggleSidebar: () => void; @@ -88,9 +91,26 @@ export function PreferencesProvider(props: ParentProps) { localStorage.setItem(SIDEBAR_KEY, sidebarCollapsed() ? "1" : "0"); }); - const setLocale = (l: Locale) => setLocaleSignal(l); - const setTheme = (th: ThemeMode) => setThemeSignal(th); - const toggleTheme = () => setThemeSignal((t) => (t === "dark" ? "light" : "dark")); + const persistPreferences = (body: Parameters[0]) => { + void patchMyPreferences(body).catch(() => undefined); + }; + + const setLocale = (l: Locale) => { + setLocaleSignal(l); + persistPreferences({ language: l }); + }; + const setTheme = (th: ThemeMode) => { + setThemeSignal(th); + persistPreferences({ theme: th }); + }; + const toggleTheme = () => { + const next = theme() === "dark" ? "light" : "dark"; + setTheme(next); + }; + const hydratePreferences = (user: Pick) => { + if (user.language === "en" || user.language === "tr") setLocaleSignal(user.language); + if (user.theme === "light" || user.theme === "dark") setThemeSignal(user.theme); + }; const setSidebarCollapsed = (v: boolean) => setSidebarCollapsedSignal(v); const toggleSidebar = () => setSidebarCollapsedSignal((v) => !v); @@ -108,6 +128,7 @@ export function PreferencesProvider(props: ParentProps) { theme, setTheme, toggleTheme, + hydratePreferences, sidebarCollapsed, setSidebarCollapsed, toggleSidebar,