186 lines
6.3 KiB
TypeScript
186 lines
6.3 KiB
TypeScript
"use client"
|
|
|
|
import { Dispatch, SetStateAction, useEffect, useState } from "react";
|
|
import { Button } from "@/components/ui/button";
|
|
import {
|
|
Dialog,
|
|
DialogContent,
|
|
DialogDescription,
|
|
DialogFooter,
|
|
DialogHeader,
|
|
DialogTitle,
|
|
} from "@/components/ui/dialog";
|
|
import { Checkbox } from "@/components/ui/checkbox";
|
|
import { Field, FieldError, FieldGroup, FieldLabel } from "@/components/ui/field";
|
|
import { Input } from "@/components/ui/input";
|
|
import { Spinner } from "@/components/ui/spinner";
|
|
import { useTranslations } from "next-intl";
|
|
import { useFieldArray, useForm, Controller, SubmitHandler } from "react-hook-form";
|
|
import { Plus, Trash2 } from "lucide-react";
|
|
import { updateClient } from "@/actions/clients";
|
|
import type { OAuthClient } from "@/types";
|
|
|
|
interface FormData {
|
|
name: string;
|
|
redirect_uris: { value: string }[];
|
|
scopes: string[];
|
|
}
|
|
|
|
interface Props {
|
|
client: OAuthClient | null;
|
|
onClose: () => void;
|
|
setClients: Dispatch<SetStateAction<OAuthClient[] | []>>;
|
|
}
|
|
|
|
const SCOPES = ["profile:read", "profile:edit"] as const;
|
|
|
|
export function UpdateClientDialog({ client, onClose, setClients }: Props) {
|
|
const t = useTranslations("MePage.items.applications.dialog");
|
|
const [error, setError] = useState<string | null>(null);
|
|
|
|
const { register, handleSubmit, control, reset, formState: { errors, isSubmitting }} = useForm<FormData>({
|
|
defaultValues: { name: "", redirect_uris: [{ value: "" }], scopes: [] },
|
|
});
|
|
|
|
const { fields, append, remove } = useFieldArray({
|
|
control,
|
|
name: "redirect_uris",
|
|
});
|
|
|
|
useEffect(() => {
|
|
if (client) {
|
|
reset({
|
|
name: client.name,
|
|
redirect_uris: client.redirect_uris.map((value) => ({ value })),
|
|
scopes: client.scopes,
|
|
});
|
|
}
|
|
}, [client, reset]);
|
|
|
|
const onSubmit: SubmitHandler<FormData> = async (data) => {
|
|
if (!client) return;
|
|
setError(null);
|
|
|
|
const result = await updateClient(client.id, {
|
|
name: data.name,
|
|
redirect_uris: data.redirect_uris.map((r) => r.value).filter(Boolean),
|
|
scopes: data.scopes,
|
|
});
|
|
|
|
if (!result.success || !result.client) {
|
|
setError(result.message ?? null);
|
|
return;
|
|
}
|
|
|
|
setClients((prev) =>
|
|
prev ? prev.map((c) => (c.id === result.client!.id ? result.client! : c)) : prev
|
|
);
|
|
onClose();
|
|
};
|
|
|
|
return (
|
|
<Dialog open={!!client} onOpenChange={(open) => !open && onClose()}>
|
|
<DialogContent>
|
|
<DialogHeader>
|
|
<DialogTitle>{t("editTitle")}</DialogTitle>
|
|
<DialogDescription>{t("editSubtitle")}</DialogDescription>
|
|
</DialogHeader>
|
|
<form onSubmit={handleSubmit(onSubmit)}>
|
|
<FieldGroup>
|
|
<Field data-invalid={!!errors.name}>
|
|
<FieldLabel htmlFor="name">{t("name.title")}</FieldLabel>
|
|
<Input
|
|
id="name"
|
|
disabled={isSubmitting}
|
|
aria-invalid={!!errors.name}
|
|
{...register("name", {
|
|
required: t("required"),
|
|
minLength: { value: 2, message: t("name.minLength") },
|
|
maxLength: { value: 128, message: t("name.maxLength") },
|
|
})}
|
|
/>
|
|
{errors.name && <FieldError>{errors.name.message}</FieldError>}
|
|
</Field>
|
|
|
|
<Field>
|
|
<FieldLabel>{t("redirectUris.title")}</FieldLabel>
|
|
<div className="flex flex-col gap-2">
|
|
{fields.map((field, index) => (
|
|
<div key={field.id} className="flex gap-2">
|
|
<Input
|
|
disabled={isSubmitting}
|
|
placeholder={t("redirectUris.placeholder")}
|
|
{...register(`redirect_uris.${index}.value` as const, {
|
|
required: index === 0 ? t("required") : false,
|
|
pattern: { value: /^https?:\/\/.+/, message: t("redirectUris.invalid") },
|
|
})}
|
|
/>
|
|
<Button
|
|
type="button"
|
|
variant="outline"
|
|
size="icon"
|
|
disabled={isSubmitting || fields.length === 1}
|
|
onClick={() => remove(index)}
|
|
>
|
|
<Trash2 />
|
|
</Button>
|
|
</div>
|
|
))}
|
|
{errors.redirect_uris?.[0]?.value && (
|
|
<FieldError>{errors.redirect_uris[0]?.value?.message}</FieldError>
|
|
)}
|
|
<Button
|
|
type="button"
|
|
variant="outline"
|
|
size="sm"
|
|
disabled={isSubmitting}
|
|
onClick={() => append({ value: "" })}
|
|
>
|
|
<Plus />
|
|
{t("redirectUris.add")}
|
|
</Button>
|
|
</div>
|
|
</Field>
|
|
|
|
<Field data-invalid={!!errors.scopes}>
|
|
<FieldLabel>{t("scopes.title")}</FieldLabel>
|
|
<Controller
|
|
name="scopes"
|
|
control={control}
|
|
rules={{ required: t("required") }}
|
|
render={({ field }) => (
|
|
<div className="flex flex-col gap-2">
|
|
{SCOPES.map((scope) => (
|
|
<label key={scope} className="flex items-center gap-2 text-sm">
|
|
<Checkbox
|
|
disabled={isSubmitting}
|
|
checked={field.value.includes(scope)}
|
|
onCheckedChange={(checked) => {
|
|
field.onChange(
|
|
checked ? [...field.value, scope] : field.value.filter((s) => s !== scope)
|
|
);
|
|
}}
|
|
/>
|
|
{t(`scopes.${scope}`)}
|
|
</label>
|
|
))}
|
|
</div>
|
|
)}
|
|
/>
|
|
{errors.scopes && <FieldError>{errors.scopes.message}</FieldError>}
|
|
</Field>
|
|
|
|
{error && <FieldError className="text-center">{error}</FieldError>}
|
|
</FieldGroup>
|
|
|
|
<DialogFooter className="mt-6">
|
|
<Button type="submit" disabled={isSubmitting}>
|
|
{isSubmitting && <Spinner />}
|
|
{t("editSubmit")}
|
|
</Button>
|
|
</DialogFooter>
|
|
</form>
|
|
</DialogContent>
|
|
</Dialog>
|
|
);
|
|
} |