Files
Ivan Rezchikov 628dd6384b
Build and Deploy / test-backend (push) Successful in 1m11s
Build and Deploy / lint-frontend (push) Successful in 21s
Build and Deploy / build-backend (push) Successful in 58s
Build and Deploy / deploy (push) Canceled after 0s
Build and Deploy / build-frontend (push) Canceled after 5m38s
version 1
2026-09-18 00:45:51 +03:00

112 lines
3.3 KiB
TypeScript

"use client";
import { useState, useTransition } from "react";
import { useRouter } from "next/navigation";
import { useTranslations } from "next-intl";
import { PlusIcon } from "lucide-react";
import { Button } from "@/components/ui/button";
import {
Dialog,
DialogContent,
DialogFooter,
DialogTitle,
DialogTrigger,
} from "@/components/ui/dialog";
import { Input } from "@/components/ui/input";
import { Field, FieldGroup, FieldLabel, FieldSet } from "@/components/ui/field";
import { Alert, AlertDescription } from "@/components/ui/alert";
import { Spinner } from "@/components/ui/spinner";
import { LanguageSelect } from "@/components/languageSelect";
import { createCollection } from "@/lib/api";
import { resolveApiErrorMessage } from "@/lib/useApiErrorMessage";
import { DEFAULT_SOURCE_LANG } from "@/lib/languages";
import type { UiLanguage } from "@/types/language";
export function CreateCollectionDialog() {
const router = useRouter();
const t = useTranslations("Collections");
const tc = useTranslations("Common");
const tApi = useTranslations("ApiErrors");
const [open, setOpen] = useState(false);
const [name, setName] = useState("");
const [lang, setLang] = useState<UiLanguage>(DEFAULT_SOURCE_LANG);
const [error, setError] = useState<string | null>(null);
const [isPending, startTransition] = useTransition();
const reset = () => {
setName("");
setLang(DEFAULT_SOURCE_LANG);
setError(null);
};
const handleCreate = () => {
const trimmed = name.trim();
if (!trimmed) return;
setError(null);
startTransition(async () => {
try {
const collection = await createCollection(trimmed, lang.id);
setOpen(false);
reset();
router.push(`/collections/${collection.id}`);
} catch (err) {
setError(resolveApiErrorMessage(err, (kind) => tApi(kind), t("createFailed")));
}
});
};
return (
<Dialog
open={open}
onOpenChange={(next) => {
setOpen(next);
if (!next) reset();
}}
>
<DialogTrigger
render={
<Button variant="outline" size="icon-sm">
<PlusIcon />
</Button>
}
/>
<DialogContent>
<DialogTitle>{t("createTitle")}</DialogTitle>
{error && (
<Alert variant="destructive">
<AlertDescription>{error}</AlertDescription>
</Alert>
)}
<FieldSet className="w-full">
<FieldGroup>
<Field>
<FieldLabel htmlFor="collection-name">{t("nameLabel")}</FieldLabel>
<Input
id="collection-name"
type="text"
placeholder={t("namePlaceholder")}
value={name}
onChange={(e) => setName(e.target.value)}
disabled={isPending}
/>
</Field>
<Field>
<FieldLabel>{t("languageLabel")}</FieldLabel>
<LanguageSelect value={lang} onChange={setLang} />
</Field>
</FieldGroup>
</FieldSet>
<DialogFooter>
<Button onClick={handleCreate} disabled={isPending || !name.trim()}>
{isPending && <Spinner />}
{tc("create")}
</Button>
</DialogFooter>
</DialogContent>
</Dialog>
);
}