fixes default language after signup

This commit is contained in:
Christian Beutel
2025-01-20 18:26:04 +01:00
parent 9d8a8e2ebb
commit ad5f95fcbf
4 changed files with 45 additions and 14 deletions

View File

@@ -1,10 +1,10 @@
import { z, ZodType } from "zod"; import { z, ZodType } from "zod";
import { NotificationType } from "../notification"; import { NotificationType } from "../notification";
import type { Settings } from "../settings"; import { Language, type Settings } from "../settings";
const SettingsCreateSchema = z.object({ const SettingsCreateSchema = z.object({
unit: z.enum(["metric", "imperial"]).optional(), unit: z.enum(["metric", "imperial"]).optional(),
language: z.enum(["en", "de", "es", "fr", "hu", "it", "nl", "pl", "pt", "zh"]).optional(), language: z.enum(Object.values(Language) as [Language, ...Language[]]).optional(),
bio: z.string().optional(), bio: z.string().optional(),
mapFocus: z.enum(["trails", "location"]).optional(), mapFocus: z.enum(["trails", "location"]).optional(),
location: z.object({ location: z.object({

View File

@@ -1,9 +1,22 @@
import type { NotificationType } from "./notification"; import type { NotificationType } from "./notification";
export enum Language {
en = "en",
de = "de",
es = "es",
fr = "fr",
hu = "hu",
it = "it",
nl = "nl",
pl = "pl",
pt = "pt",
zh = "zh"
}
class Settings { class Settings {
id?: string; id?: string;
unit?: "metric" | "imperial"; unit?: "metric" | "imperial";
language?: "en" | "de" | "es" | "fr" | "hu" | "it" | "nl" | "pl" | "pt" | "zh"; language?: Language;
bio?: string; bio?: string;
mapFocus?: "trails" | "location"; mapFocus?: "trails" | "location";
location?: { name: string, lat: number, lon: number }; location?: { name: string, lat: number, lon: number };
@@ -16,7 +29,7 @@ class Settings {
constructor( constructor(
unit: "metric" | "imperial", unit: "metric" | "imperial",
language: "en" | "de" | "es" | "fr" | "hu" | "it" | "nl" | "pl" | "pt" | "zh", language: Language,
mapFocus: "trails" | "location", mapFocus: "trails" | "location",
user: string, user: string,
params?: { params?: {

View File

@@ -1,17 +1,20 @@
<script lang="ts"> <script lang="ts">
import { goto } from "$app/navigation"; import { goto, invalidateAll } from "$app/navigation";
import { page } from "$app/stores";
import Button from "$lib/components/base/button.svelte"; import Button from "$lib/components/base/button.svelte";
import TextField from "$lib/components/base/text_field.svelte"; import TextField from "$lib/components/base/text_field.svelte";
import LogoTextTwoLineDark from "$lib/components/logo/logo_text_two_line_dark.svelte"; import LogoTextTwoLineDark from "$lib/components/logo/logo_text_two_line_dark.svelte";
import LogoTextTwoLineLight from "$lib/components/logo/logo_text_two_line_light.svelte"; import LogoTextTwoLineLight from "$lib/components/logo/logo_text_two_line_light.svelte";
import { Language, type Settings } from "$lib/models/settings";
import type { User } from "$lib/models/user"; import type { User } from "$lib/models/user";
import { settings_update } from "$lib/stores/settings_store";
import { theme } from "$lib/stores/theme_store"; import { theme } from "$lib/stores/theme_store";
import { show_toast } from "$lib/stores/toast_store"; import { show_toast } from "$lib/stores/toast_store";
import { login, users_create } from "$lib/stores/user_store"; import { login, users_create } from "$lib/stores/user_store";
import { validator } from "@felte/validator-zod"; import { validator } from "@felte/validator-zod";
import { createForm } from "felte"; import { createForm } from "felte";
import { _ } from "svelte-i18n"; import { _ } from "svelte-i18n";
import { z } from "zod"; import { INVALID, z } from "zod";
let loading: boolean = false; let loading: boolean = false;
const { form, errors } = createForm<User>({ const { form, errors } = createForm<User>({
@@ -23,12 +26,15 @@
}, },
extend: validator({ extend: validator({
schema: z.object({ schema: z.object({
username: z.string().min( username: z
3, .string()
$_("must-be-at-least-n-characters-long", { .min(
values: { n: 3 }, 3,
}), $_("must-be-at-least-n-characters-long", {
).regex(/^[\w][\w\.]*$/, $_("invalid-username")), values: { n: 3 },
}),
)
.regex(/^[\w][\w\.]*$/, $_("invalid-username")),
email: z email: z
.string() .string()
.min(1, "required") .min(1, "required")
@@ -57,14 +63,25 @@
} }
try { try {
await login(newUser); await login(newUser);
goto(`/`);
} catch (e) { } catch (e) {
show_toast({ show_toast({
icon: "close", icon: "close",
type: "error", type: "error",
text: $_("error-during-login"), text: $_("error-during-login"),
}); });
}
try {
await invalidateAll();
const language = Object.values(Language).includes(
window.navigator.language as Language,
)
? (window.navigator.language as Language)
: Language.en;
await settings_update({ id: $page.data.settings.id, language });
} catch (e) {
console.error(e);
} finally { } finally {
window.location.href = "/";
loading = false; loading = false;
} }
}, },

View File

@@ -6,6 +6,7 @@
import Select, { import Select, {
type SelectItem, type SelectItem,
} from "$lib/components/base/select.svelte"; } from "$lib/components/base/select.svelte";
import type { Language } from "$lib/models/settings";
import { settings_update } from "$lib/stores/settings_store"; import { settings_update } from "$lib/stores/settings_store";
import { _, locale } from "svelte-i18n"; import { _, locale } from "svelte-i18n";
@@ -32,7 +33,7 @@
let selectedLanguage = settings?.language ?? "en"; let selectedLanguage = settings?.language ?? "en";
async function handleLanguageSelection( async function handleLanguageSelection(
value: "en" | "de" | "fr" | "hu" | "nl" | "pl" | "pt", value: Language,
) { ) {
await settings_update({ await settings_update({
id: settings!.id, id: settings!.id,