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
152 lines
5.0 KiB
TypeScript
152 lines
5.0 KiB
TypeScript
"use client";
|
|
|
|
import { useState, useTransition } from "react";
|
|
import { useRouter } from "next/navigation";
|
|
import { useTranslations } from "next-intl";
|
|
import { Button } from "@/components/ui/button";
|
|
import { InfoIcon, MenuIcon } from "lucide-react";
|
|
import {
|
|
Dialog,
|
|
DialogClose,
|
|
DialogContent,
|
|
DialogFooter,
|
|
DialogTitle,
|
|
DialogTrigger,
|
|
} from "@/components/ui/dialog";
|
|
import { Input } from "@/components/ui/input";
|
|
import {
|
|
Field,
|
|
FieldGroup,
|
|
FieldLabel,
|
|
FieldSet,
|
|
} from "@/components/ui/field";
|
|
import { CollectionResult } from "@/types/api";
|
|
import { formatDate } from "@/lib/formatDate";
|
|
import { Alert, AlertDescription, AlertTitle } from "@/components/ui/alert";
|
|
import { deleteCollection, updateCollection } from "@/lib/api";
|
|
import { resolveApiErrorMessage } from "@/lib/useApiErrorMessage";
|
|
import { Spinner } from "@/components/ui/spinner";
|
|
|
|
export function MenuDialog({ collection }: { collection: CollectionResult }) {
|
|
const router = useRouter();
|
|
const [open, setOpen] = useState(false);
|
|
const [name, setName] = useState(collection.name);
|
|
const [error, setError] = useState<string | null>(null);
|
|
const [isDeleting, startDelete] = useTransition();
|
|
const [isUpdating, startUpdate] = useTransition();
|
|
|
|
const tt = useTranslations("Time");
|
|
const t = useTranslations("CollectionMenu");
|
|
const tc = useTranslations("Common");
|
|
const tl = useTranslations("Languages");
|
|
const tApi = useTranslations("ApiErrors");
|
|
|
|
const isAuto = collection.type === "auto";
|
|
const isBusy = isDeleting || isUpdating;
|
|
|
|
const handleDelete = () => {
|
|
setError(null);
|
|
startDelete(async () => {
|
|
try {
|
|
await deleteCollection(collection.id);
|
|
router.push("/collections");
|
|
} catch (err) {
|
|
setError(resolveApiErrorMessage(err, (kind) => tApi(kind), t("deleteFailed")));
|
|
}
|
|
});
|
|
};
|
|
|
|
const handleUpdate = () => {
|
|
setError(null);
|
|
startUpdate(async () => {
|
|
try {
|
|
await updateCollection(collection.id, name);
|
|
router.refresh();
|
|
setOpen(false);
|
|
} catch (err) {
|
|
setError(resolveApiErrorMessage(err, (kind) => tApi(kind), t("updateFailed")));
|
|
}
|
|
});
|
|
};
|
|
|
|
return (
|
|
<Dialog open={open} onOpenChange={setOpen}>
|
|
<DialogTrigger
|
|
render={
|
|
<Button variant="outline" size="icon">
|
|
<MenuIcon />
|
|
</Button>
|
|
}
|
|
/>
|
|
<DialogContent>
|
|
<DialogTitle>{t("title")}</DialogTitle>
|
|
|
|
{isAuto && (
|
|
<Alert>
|
|
<InfoIcon />
|
|
<AlertTitle>{t("autoTitle")}</AlertTitle>
|
|
<AlertDescription>{t("autoDescription")}</AlertDescription>
|
|
</Alert>
|
|
)}
|
|
|
|
{error && (
|
|
<Alert variant="destructive">
|
|
<AlertDescription>{error}</AlertDescription>
|
|
</Alert>
|
|
)}
|
|
|
|
<FieldSet className="w-full">
|
|
<FieldGroup>
|
|
<div className="flex flex-col gap-2">
|
|
<Field className="mb-1">
|
|
<FieldLabel htmlFor="name">{t("nameLabel")}</FieldLabel>
|
|
<Input
|
|
id="name"
|
|
type="text"
|
|
placeholder={t("namePlaceholder")}
|
|
value={name === "history" ? tc("history") : name}
|
|
onChange={(e) => setName(e.target.value)}
|
|
disabled={isAuto || isBusy}
|
|
/>
|
|
</Field>
|
|
<div className="flex flex-row items-center justify-between">
|
|
<h5 className="font-medium">{t("language")}</h5>
|
|
<h5>{tl(collection.lang)}</h5>
|
|
</div>
|
|
<div className="flex flex-row items-center justify-between">
|
|
<h5 className="font-medium">{t("wordCount")}</h5>
|
|
<h5>{collection.words.length}</h5>
|
|
</div>
|
|
<div className="flex flex-row items-center justify-between">
|
|
<h5 className="font-medium">{t("lastUpdate")}</h5>
|
|
<h5>{formatDate(collection.updated_at, tt)}</h5>
|
|
</div>
|
|
<div className="flex flex-row items-center justify-between">
|
|
<h5 className="font-medium">{t("created")}</h5>
|
|
<h5>{formatDate(collection.created_at, tt)}</h5>
|
|
</div>
|
|
</div>
|
|
</FieldGroup>
|
|
</FieldSet>
|
|
|
|
<DialogFooter>
|
|
<div className="flex flex-row items-start justify-between w-full">
|
|
<div className="flex flex-row gap-2 items-center">
|
|
<Button onClick={handleUpdate} disabled={isAuto || isBusy || name.trim() === collection.name}>
|
|
{isUpdating && <Spinner />}{tc("update")}
|
|
</Button>
|
|
<DialogClose
|
|
render={<Button variant="outline" disabled={isBusy} />}
|
|
>
|
|
{tc("cancel")}
|
|
</DialogClose>
|
|
</div>
|
|
<Button variant="destructive" onClick={handleDelete} disabled={isAuto || isBusy}>
|
|
{isUpdating && <Spinner />}{t("delete")}
|
|
</Button>
|
|
</div>
|
|
</DialogFooter>
|
|
</DialogContent>
|
|
</Dialog>
|
|
);
|
|
} |