fix: settings mobile layout, silent failure on language save
- Settings sidebar was a fixed-width vertical list with no responsive
breakpoint, squeezing the form into a sliver on mobile. Turn it into
a horizontal scrollable tab bar under md, vertical sidebar above.
- setLocale() fired the PATCH and swallowed the result with
.catch(() => {}) — never checked res.ok, no success/failure feedback.
A failed save (expired session, network blip) looked identical to a
successful one. Now returns a boolean, reverts local state on
failure, and the settings form toasts success/failure.
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
@@ -13,7 +13,7 @@ export default async function SettingsLayout({ children }: { children: React.Rea
|
|||||||
<h1 className="text-3xl font-bold tracking-tight">{m.settings.title}</h1>
|
<h1 className="text-3xl font-bold tracking-tight">{m.settings.title}</h1>
|
||||||
<p className="text-muted-foreground mt-1">{m.settings.subtitle}</p>
|
<p className="text-muted-foreground mt-1">{m.settings.subtitle}</p>
|
||||||
</div>
|
</div>
|
||||||
<div className="flex gap-8 items-start">
|
<div className="flex flex-col md:flex-row gap-4 md:gap-8 md:items-start">
|
||||||
<SettingsSidebar />
|
<SettingsSidebar />
|
||||||
<main className="flex-1 min-w-0 space-y-6">{children}</main>
|
<main className="flex-1 min-w-0 space-y-6">{children}</main>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -118,7 +118,15 @@ export function SettingsForm({ user }: { user: UserProps }) {
|
|||||||
<section className="rounded-xl border p-6 space-y-4">
|
<section className="rounded-xl border p-6 space-y-4">
|
||||||
<h2 className="font-semibold text-lg">{t("language")}</h2>
|
<h2 className="font-semibold text-lg">{t("language")}</h2>
|
||||||
<p className="text-sm text-muted-foreground">{t("languageDescription")}</p>
|
<p className="text-sm text-muted-foreground">{t("languageDescription")}</p>
|
||||||
<Select defaultValue={user.locale} onValueChange={(v) => setLocale(v as Locale)}>
|
<Select
|
||||||
|
defaultValue={user.locale}
|
||||||
|
onValueChange={(v) => {
|
||||||
|
void setLocale(v as Locale).then((ok) => {
|
||||||
|
if (ok) toast.success(t_common("saved"));
|
||||||
|
else toast.error(t_common("saveFailed"));
|
||||||
|
});
|
||||||
|
}}
|
||||||
|
>
|
||||||
<SelectTrigger className="w-48">
|
<SelectTrigger className="w-48">
|
||||||
<SelectValue />
|
<SelectValue />
|
||||||
</SelectTrigger>
|
</SelectTrigger>
|
||||||
|
|||||||
@@ -21,16 +21,16 @@ export function SettingsSidebar() {
|
|||||||
const t = useTranslations("settings");
|
const t = useTranslations("settings");
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<nav className="w-48 shrink-0 sticky top-6">
|
<nav className="w-full md:w-48 md:shrink-0 md:sticky md:top-6">
|
||||||
<ul className="space-y-0.5">
|
<ul className="flex overflow-x-auto gap-1 pb-2 -mx-4 px-4 md:mx-0 md:px-0 md:flex-col md:overflow-visible md:gap-0.5 md:pb-0 [scrollbar-width:none] [&::-webkit-scrollbar]:hidden">
|
||||||
{NAV_ITEMS.map(({ href, key, icon: Icon, exact }) => {
|
{NAV_ITEMS.map(({ href, key, icon: Icon, exact }) => {
|
||||||
const active = exact ? pathname === href : pathname.startsWith(href);
|
const active = exact ? pathname === href : pathname.startsWith(href);
|
||||||
return (
|
return (
|
||||||
<li key={href}>
|
<li key={href} className="shrink-0 md:shrink">
|
||||||
<Link
|
<Link
|
||||||
href={href}
|
href={href}
|
||||||
className={cn(
|
className={cn(
|
||||||
"flex items-center gap-2.5 px-3 py-2 rounded-lg text-sm transition-colors",
|
"flex items-center gap-2.5 px-3 py-2 rounded-lg text-sm whitespace-nowrap transition-colors",
|
||||||
active
|
active
|
||||||
? "bg-accent text-accent-foreground font-medium"
|
? "bg-accent text-accent-foreground font-medium"
|
||||||
: "text-muted-foreground hover:text-foreground hover:bg-accent/50"
|
: "text-muted-foreground hover:text-foreground hover:bg-accent/50"
|
||||||
|
|||||||
@@ -14,8 +14,8 @@ const STORAGE_KEY = "epicure-locale";
|
|||||||
|
|
||||||
const LocaleContext = createContext<{
|
const LocaleContext = createContext<{
|
||||||
locale: Locale;
|
locale: Locale;
|
||||||
setLocale: (locale: Locale) => void;
|
setLocale: (locale: Locale) => Promise<boolean>;
|
||||||
}>({ locale: "en", setLocale: () => {} });
|
}>({ locale: "en", setLocale: async () => false });
|
||||||
|
|
||||||
export function useLocale() {
|
export function useLocale() {
|
||||||
return useContext(LocaleContext);
|
return useContext(LocaleContext);
|
||||||
@@ -45,15 +45,27 @@ export function I18nProvider({
|
|||||||
}
|
}
|
||||||
}, [messages, initialLocale]);
|
}, [messages, initialLocale]);
|
||||||
|
|
||||||
function setLocale(next: Locale) {
|
async function setLocale(next: Locale): Promise<boolean> {
|
||||||
|
const previous = locale;
|
||||||
|
const previousMessages = currentMessages;
|
||||||
setLocaleState(next);
|
setLocaleState(next);
|
||||||
setCurrentMessages(messages[next]!);
|
setCurrentMessages(messages[next]!);
|
||||||
localStorage.setItem(STORAGE_KEY, next);
|
localStorage.setItem(STORAGE_KEY, next);
|
||||||
fetch("/api/v1/users/me", {
|
try {
|
||||||
method: "PATCH",
|
const res = await fetch("/api/v1/users/me", {
|
||||||
headers: { "Content-Type": "application/json" },
|
method: "PATCH",
|
||||||
body: JSON.stringify({ locale: next }),
|
headers: { "Content-Type": "application/json" },
|
||||||
}).catch(() => {});
|
body: JSON.stringify({ locale: next }),
|
||||||
|
});
|
||||||
|
if (!res.ok) throw new Error("Failed to save locale");
|
||||||
|
return true;
|
||||||
|
} catch {
|
||||||
|
// Revert on failure so the UI doesn't claim a language that never saved.
|
||||||
|
setLocaleState(previous);
|
||||||
|
setCurrentMessages(previousMessages);
|
||||||
|
localStorage.setItem(STORAGE_KEY, previous);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
|||||||
Reference in New Issue
Block a user