forge
Explore
Log in

Don't you dare go Hollow

✨ persist user preferences

Browse files at this commit

60e7901b0d8e2e5ef2b5ebc613d0fa68380871ddBurak BoduroğluParent: 88729f8
  • feat
    • ✨ persist user preferences
Show full message
feat: ✨ persist user preferences

- add theme and language fields to the user API type
- wire PATCH /users/me/preferences for self-managed preferences
- save theme and language changes from the preference store to the backend
- hydrate local preferences from the authenticated user response
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 @@
11 export type { Page, PageParams } from "./page";
22
33 export type Role = "student" | "teacher" | "manager" | "admin";
4+export type UserTheme = "light" | "dark";
5+export type UserLanguage = "tr" | "en";
46 export type CoreAttendanceStatus = "present" | "absent" | "late" | "excused";
57 export type AttendanceStatus = CoreAttendanceStatus | string;
68 export type KnownExamKind = "homework" | "quiz" | "midterm" | "final" | "project" | "oral";
@@ -18,6 +20,8 @@ export type User = {
1820 email: string | null;
1921 phone: string | null;
2022 birth_date: string | null;
23+ theme: UserTheme | null;
24+ language: UserLanguage | null;
2125 };
2226
2327 export type ProfileUpdate = {
src/stores/auth-context.tsxmodified+8 -0
@@ -2,6 +2,7 @@ import {
22 type Accessor,
33 type ParentProps,
44 createContext,
5+ createEffect,
56 createResource,
67 createSignal,
78 useContext,
@@ -10,6 +11,7 @@ import { getMe } from "@/api/getMe";
1011 import { postLogout } from "@/api/postLogout";
1112 import type { User } from "@/api/types";
1213 import { ApiError } from "@/api/client";
14+import { usePreferences } from "@/stores/preferences-context";
1315
1416 type AuthContextValue = {
1517 user: Accessor<User | null | undefined>;
@@ -22,6 +24,7 @@ type AuthContextValue = {
2224 const AuthContext = createContext<AuthContextValue>();
2325
2426 export function AuthProvider(props: ParentProps) {
27+ const prefs = usePreferences();
2528 const [override, setOverride] = createSignal<User | null | undefined>(undefined);
2629 const [me, { refetch }] = createResource(async () => {
2730 try {
@@ -40,6 +43,11 @@ export function AuthProvider(props: ParentProps) {
4043
4144 const setUser = (u: User | null) => setOverride(u);
4245
46+ createEffect(() => {
47+ const u = user();
48+ if (u) prefs.hydratePreferences(u);
49+ });
50+
4351 const refresh = async () => {
4452 setOverride(undefined);
4553 const result = await refetch();
src/stores/preferences-context.tsxmodified+24 -3
@@ -6,6 +6,8 @@ import {
66 createSignal,
77 useContext,
88 } from "solid-js";
9+import { patchMyPreferences } from "@/api/patchMyPreferences";
10+import type { User } from "@/api/types";
911 import { formatMessage, messages, type Locale, type MessageKey } from "@/i18n/messages";
1012
1113 export type ThemeMode = "light" | "dark";
@@ -16,6 +18,7 @@ type PreferencesContextValue = {
1618 theme: Accessor<ThemeMode>;
1719 setTheme: (theme: ThemeMode) => void;
1820 toggleTheme: () => void;
21+ hydratePreferences: (user: Pick<User, "theme" | "language">) => void;
1922 sidebarCollapsed: Accessor<boolean>;
2023 setSidebarCollapsed: (collapsed: boolean) => void;
2124 toggleSidebar: () => void;
@@ -88,9 +91,26 @@ export function PreferencesProvider(props: ParentProps) {
8891 localStorage.setItem(SIDEBAR_KEY, sidebarCollapsed() ? "1" : "0");
8992 });
9093
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+ };
94114 const setSidebarCollapsed = (v: boolean) => setSidebarCollapsedSignal(v);
95115 const toggleSidebar = () => setSidebarCollapsedSignal((v) => !v);
96116
@@ -108,6 +128,7 @@ export function PreferencesProvider(props: ParentProps) {
108128 theme,
109129 setTheme,
110130 toggleTheme,
131+ hydratePreferences,
111132 sidebarCollapsed,
112133 setSidebarCollapsed,
113134 toggleSidebar,