initial commit
This commit is contained in:
@@ -0,0 +1,41 @@
|
||||
"use client"
|
||||
|
||||
import { useLocale, useTranslations } from "next-intl";
|
||||
import type { Consent } from "@/types";
|
||||
import { Item, ItemActions, ItemContent, ItemDescription, ItemTitle } from "@/components/ui/item"
|
||||
import { Button } from "@/components/ui/button";
|
||||
import { localeMap } from "@/types";
|
||||
import { enUS } from "date-fns/locale";
|
||||
import { format } from "date-fns";
|
||||
|
||||
interface Props {
|
||||
consents: Consent[] | [];
|
||||
setConsents: (consents: Consent[]) => void;
|
||||
}
|
||||
|
||||
export default function Applications({ consents, setConsents }: Props) {
|
||||
const t = useTranslations("MePage.items.applications");
|
||||
|
||||
const locale = useLocale();
|
||||
const dateLocale = localeMap[locale as keyof typeof localeMap] ?? enUS;
|
||||
|
||||
return (
|
||||
<div className="w-full lg:max-w-sm flex flex-col gap-4">
|
||||
<h1 className="text-xl">{t("title")}</h1>
|
||||
|
||||
<div className="space-y-4">
|
||||
{consents.map((consent) => (
|
||||
<Item key={consent.client_id}>
|
||||
<ItemContent>
|
||||
<ItemTitle>{consent.client_name}</ItemTitle>
|
||||
<ItemDescription>{t("authorized")} {format(consent.authorized_at, "PPP", { locale: dateLocale }) }</ItemDescription>
|
||||
</ItemContent>
|
||||
<ItemActions>
|
||||
<Button disabled={!consent.isRevokable}>{t("revoke")}</Button>
|
||||
</ItemActions>
|
||||
</Item>
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,115 @@
|
||||
"use client"
|
||||
|
||||
import { useState } from "react";
|
||||
import { useTranslations } from "next-intl";
|
||||
import { useForm, Controller, SubmitHandler } from "react-hook-form";
|
||||
import type { User } from "@/types";
|
||||
import { Button } from "@/components/ui/button";
|
||||
import { Field, FieldError, FieldGroup, FieldLabel } from "@/components/ui/field";
|
||||
import { Input } from "@/components/ui/input";
|
||||
import { Spinner } from "@/components/ui/spinner";
|
||||
import { BirthdatePicker } from "@/components/ui/birthdate-picker";
|
||||
import { UpdateUser } from "@/actions/auth";
|
||||
|
||||
interface Props {
|
||||
user: User | null;
|
||||
setUser: (user: User) => void;
|
||||
}
|
||||
|
||||
interface FormData {
|
||||
first_name: string;
|
||||
second_name: string;
|
||||
birth_date: string;
|
||||
}
|
||||
|
||||
export default function Profile({ user, setUser }: Props) {
|
||||
const t = useTranslations("MePage.items.profile");
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
|
||||
const { register, handleSubmit, control, formState: { errors, isSubmitting } } = useForm<FormData>({
|
||||
defaultValues: {
|
||||
first_name: user?.first_name,
|
||||
second_name: user?.second_name,
|
||||
birth_date: user?.birth_date ?? "",
|
||||
},
|
||||
});
|
||||
|
||||
const onSubmit: SubmitHandler<FormData> = async (data) => {
|
||||
setError(null);
|
||||
|
||||
try {
|
||||
const response = await UpdateUser(data.first_name, data.second_name, data.birth_date);
|
||||
|
||||
if (response == null) setError(t("form.updateError"))
|
||||
else setUser(response)
|
||||
} catch (error) {
|
||||
setError(t("form.updateError"))
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
<div className="w-full lg:max-w-sm flex flex-col gap-4">
|
||||
<h1 className="text-xl">{t("title")}</h1>
|
||||
<form onSubmit={handleSubmit(onSubmit)}>
|
||||
<FieldGroup>
|
||||
<Field data-invalid={!!errors.first_name}>
|
||||
<FieldLabel htmlFor="first_name">{t("form.firstName.title")}</FieldLabel>
|
||||
<Input
|
||||
id="first_name"
|
||||
type="text"
|
||||
disabled={isSubmitting}
|
||||
aria-invalid={!!errors.first_name}
|
||||
{...register("first_name", {
|
||||
required: t("form.required"),
|
||||
minLength: { value: 2, message: t("form.firstName.minLength") },
|
||||
maxLength: { value: 64, message: t("form.firstName.maxLength") },
|
||||
})}
|
||||
/>
|
||||
{errors.first_name && <FieldError>{errors.first_name.message}</FieldError>}
|
||||
</Field>
|
||||
|
||||
<Field data-invalid={!!errors.second_name}>
|
||||
<FieldLabel htmlFor="second_name">{t("form.secondName.title")}</FieldLabel>
|
||||
<Input
|
||||
id="second_name"
|
||||
type="text"
|
||||
disabled={isSubmitting}
|
||||
aria-invalid={!!errors.second_name}
|
||||
{...register("second_name", {
|
||||
required: t("form.required"),
|
||||
minLength: { value: 2, message: t("form.secondName.minLength") },
|
||||
maxLength: { value: 64, message: t("form.secondName.maxLength") },
|
||||
})}
|
||||
/>
|
||||
{errors.second_name && <FieldError>{errors.second_name.message}</FieldError>}
|
||||
</Field>
|
||||
|
||||
<Field data-invalid={!!errors.birth_date}>
|
||||
<FieldLabel htmlFor="birth_date">{t("form.birthdate.title")}</FieldLabel>
|
||||
<Controller
|
||||
name="birth_date"
|
||||
control={control}
|
||||
render={({ field }) => (
|
||||
<BirthdatePicker
|
||||
value={field.value}
|
||||
onChange={field.onChange}
|
||||
disabled={isSubmitting}
|
||||
aria-invalid={!!errors.birth_date}
|
||||
/>
|
||||
)}
|
||||
/>
|
||||
{errors.birth_date && <FieldError>{errors.birth_date.message}</FieldError>}
|
||||
</Field>
|
||||
|
||||
<Field>
|
||||
<Button type="submit" disabled={isSubmitting}>
|
||||
{isSubmitting && <Spinner />}
|
||||
{t("form.save")}
|
||||
</Button>
|
||||
{error && <FieldError className="text-center">{error}</FieldError>}
|
||||
</Field>
|
||||
</FieldGroup>
|
||||
</form>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user