refactor(i18n): remove legacy locale endpoint and select component
This commit is contained in:
@@ -1,53 +0,0 @@
|
|||||||
import { cookies } from "next/headers";
|
|
||||||
import { NextResponse } from "next/server";
|
|
||||||
import { getSqliteConnection } from "@/server/db/client";
|
|
||||||
import { instanceLocales } from "@/server/db/schema";
|
|
||||||
import { DomainError } from "@/server/domain/errors";
|
|
||||||
import { buildLocaleCookie, normalizeLocaleCode } from "@/server/i18n/locale";
|
|
||||||
import { eq } from "drizzle-orm";
|
|
||||||
|
|
||||||
export const runtime = "nodejs";
|
|
||||||
export const dynamic = "force-dynamic";
|
|
||||||
|
|
||||||
export async function POST(request: Request) {
|
|
||||||
try {
|
|
||||||
const payload = await request.json();
|
|
||||||
const locale = normalizeLocaleCode(typeof payload.locale === "string" ? payload.locale : null);
|
|
||||||
if (!locale) {
|
|
||||||
throw new DomainError("VALIDATION_ERROR", "validation.invalidLocale", {
|
|
||||||
messageKey: "validation.invalidLocale",
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
const row = getSqliteConnection().db
|
|
||||||
.select({ code: instanceLocales.code, status: instanceLocales.status })
|
|
||||||
.from(instanceLocales)
|
|
||||||
.where(eq(instanceLocales.code, locale))
|
|
||||||
.get();
|
|
||||||
if (!row || row.status === "archived") {
|
|
||||||
throw new DomainError("UNSUPPORTED_LOCALE", "validation.unsupportedLocale", {
|
|
||||||
messageKey: "validation.unsupportedLocale",
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
(await cookies()).set(buildLocaleCookie(row.code));
|
|
||||||
return NextResponse.json({ ok: true, data: { locale: row.code } });
|
|
||||||
} catch (error) {
|
|
||||||
const normalized = error instanceof DomainError
|
|
||||||
? error
|
|
||||||
: new DomainError("VALIDATION_ERROR", "validation.unsupportedLocale", {
|
|
||||||
messageKey: "validation.unsupportedLocale",
|
|
||||||
});
|
|
||||||
return NextResponse.json(
|
|
||||||
{
|
|
||||||
ok: false,
|
|
||||||
error: {
|
|
||||||
code: normalized.code,
|
|
||||||
message: normalized.message,
|
|
||||||
messageKey: normalized.details?.messageKey,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
{ status: normalized.status },
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,58 +0,0 @@
|
|||||||
"use client";
|
|
||||||
|
|
||||||
import { useState, useTransition } from "react";
|
|
||||||
import { Label } from "poyraz-ui/atoms";
|
|
||||||
import {
|
|
||||||
Select,
|
|
||||||
SelectContent,
|
|
||||||
SelectItem,
|
|
||||||
SelectTrigger,
|
|
||||||
SelectValue,
|
|
||||||
} from "poyraz-ui/molecules";
|
|
||||||
|
|
||||||
type LocaleOption = {
|
|
||||||
code: string;
|
|
||||||
nativeName: string;
|
|
||||||
name: string;
|
|
||||||
};
|
|
||||||
|
|
||||||
type LocaleSelectFormProps = {
|
|
||||||
label: string;
|
|
||||||
value: string;
|
|
||||||
locales: LocaleOption[];
|
|
||||||
};
|
|
||||||
|
|
||||||
export function LocaleSelectForm({ label, value, locales }: LocaleSelectFormProps) {
|
|
||||||
const [currentLocale, setCurrentLocale] = useState(value);
|
|
||||||
const [pending, startTransition] = useTransition();
|
|
||||||
|
|
||||||
function handleChange(locale: string) {
|
|
||||||
setCurrentLocale(locale);
|
|
||||||
startTransition(async () => {
|
|
||||||
await fetch("/api/i18n/locale", {
|
|
||||||
method: "POST",
|
|
||||||
headers: { "Content-Type": "application/json" },
|
|
||||||
body: JSON.stringify({ locale }),
|
|
||||||
});
|
|
||||||
window.location.reload();
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
return (
|
|
||||||
<div className="space-y-2">
|
|
||||||
<Label htmlFor="auth-locale">{label}</Label>
|
|
||||||
<Select value={currentLocale} onValueChange={handleChange} disabled={pending}>
|
|
||||||
<SelectTrigger id="auth-locale" aria-label={label}>
|
|
||||||
<SelectValue />
|
|
||||||
</SelectTrigger>
|
|
||||||
<SelectContent>
|
|
||||||
{locales.map((locale) => (
|
|
||||||
<SelectItem key={locale.code} value={locale.code}>
|
|
||||||
{locale.nativeName || locale.name}
|
|
||||||
</SelectItem>
|
|
||||||
))}
|
|
||||||
</SelectContent>
|
|
||||||
</Select>
|
|
||||||
</div>
|
|
||||||
);
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user