2025-08-28 18:27:07 +02:00
|
|
|
import toast from "react-hot-toast";
|
2025-08-07 14:50:40 +02:00
|
|
|
import { z } from "zod/v4";
|
2025-08-04 17:45:44 +02:00
|
|
|
import {
|
2025-08-28 18:27:07 +02:00
|
|
|
Form,
|
|
|
|
|
FormControl,
|
|
|
|
|
FormField,
|
|
|
|
|
FormItem,
|
|
|
|
|
FormLabel,
|
|
|
|
|
FormMessage,
|
2025-08-04 17:45:44 +02:00
|
|
|
} from "~/components/custom_ui/form";
|
|
|
|
|
import Input from "~/components/custom_ui/input";
|
|
|
|
|
import { DualInputLayout } from "~/components/custom_ui/inputLayouts";
|
|
|
|
|
import { PhoneInput } from "~/components/phone-input";
|
2025-08-28 18:27:07 +02:00
|
|
|
import { Button } from "~/components/ui/button";
|
2025-08-04 17:45:44 +02:00
|
|
|
import { Switch } from "~/components/ui/switch";
|
2025-08-28 18:27:07 +02:00
|
|
|
import { useZodForm } from "~/lib/zodForm";
|
|
|
|
|
import { useTranslation } from "~/providers/I18nProvider";
|
|
|
|
|
import type { Users, UsersId } from "~/schemas/public/Users";
|
|
|
|
|
import { api } from "~/utils/api";
|
2025-08-04 17:45:44 +02:00
|
|
|
|
|
|
|
|
type ProfileFromAccountProps = Pick<
|
2025-08-28 18:27:07 +02:00
|
|
|
Users,
|
|
|
|
|
"id" | "nome" | "cognome" | "email" | "telefono"
|
2025-08-04 17:45:44 +02:00
|
|
|
> &
|
2025-08-28 18:27:07 +02:00
|
|
|
Partial<Pick<Users, "isAdmin">>;
|
2025-08-04 17:45:44 +02:00
|
|
|
|
|
|
|
|
export const ProfileFormAccount = ({
|
2025-08-28 18:27:07 +02:00
|
|
|
id,
|
|
|
|
|
nome,
|
|
|
|
|
cognome,
|
|
|
|
|
email,
|
|
|
|
|
telefono,
|
|
|
|
|
isAdmin,
|
2025-08-04 17:45:44 +02:00
|
|
|
}: ProfileFromAccountProps) => {
|
2025-08-28 18:27:07 +02:00
|
|
|
const profileFormSchema = z.object({
|
|
|
|
|
cognome: z.string().nonempty("Inserisci un cognome valido"),
|
|
|
|
|
email: z.email(),
|
|
|
|
|
isAdmin: z.boolean().optional(),
|
2025-08-29 16:18:32 +02:00
|
|
|
nome: z.string().nonempty("Inserisci un nome valido"),
|
|
|
|
|
telefono: z.string().nonempty("Inserisci un numero di telefono"),
|
2025-08-28 18:27:07 +02:00
|
|
|
});
|
2025-08-04 17:45:44 +02:00
|
|
|
|
2025-08-28 18:27:07 +02:00
|
|
|
type ProfileFormValues = z.infer<typeof profileFormSchema>;
|
2025-08-04 17:45:44 +02:00
|
|
|
|
2025-08-28 18:27:07 +02:00
|
|
|
const defaultValues: ProfileFormValues = {
|
|
|
|
|
cognome: cognome || "",
|
|
|
|
|
email: email,
|
|
|
|
|
isAdmin: isAdmin,
|
2025-08-29 16:18:32 +02:00
|
|
|
nome: nome || "",
|
|
|
|
|
telefono,
|
2025-08-28 18:27:07 +02:00
|
|
|
};
|
|
|
|
|
const { locale, t } = useTranslation();
|
|
|
|
|
z.config(z.locales[locale]());
|
2025-08-04 17:45:44 +02:00
|
|
|
|
2025-08-28 18:27:07 +02:00
|
|
|
const form = useZodForm(profileFormSchema, {
|
|
|
|
|
defaultValues: defaultValues,
|
|
|
|
|
});
|
2025-08-04 17:45:44 +02:00
|
|
|
|
2025-08-28 18:27:07 +02:00
|
|
|
const utils = api.useUtils();
|
2025-08-04 17:45:44 +02:00
|
|
|
|
2025-08-28 18:27:07 +02:00
|
|
|
const { mutate } = api.users.editUser.useMutation({
|
2025-08-29 16:18:32 +02:00
|
|
|
onError: (error) => {
|
|
|
|
|
toast.error(error.message);
|
|
|
|
|
},
|
2025-08-28 18:27:07 +02:00
|
|
|
onSuccess: async () => {
|
|
|
|
|
toast.success(t.profile.aggiornato);
|
|
|
|
|
await utils.auth.getSession.invalidate();
|
|
|
|
|
await utils.users.getUser.invalidate();
|
2025-11-10 16:27:15 +01:00
|
|
|
await utils.users.getClientProfilo.invalidate({
|
|
|
|
|
userId: id,
|
|
|
|
|
});
|
2025-08-28 18:27:07 +02:00
|
|
|
},
|
|
|
|
|
});
|
2025-08-04 17:45:44 +02:00
|
|
|
|
2025-08-28 18:27:07 +02:00
|
|
|
function onSubmit(fields: ProfileFormValues) {
|
|
|
|
|
const data: {
|
|
|
|
|
id: UsersId;
|
|
|
|
|
email: string;
|
|
|
|
|
isAdmin?: boolean;
|
|
|
|
|
nome: string;
|
|
|
|
|
cognome: string;
|
|
|
|
|
telefono: string;
|
|
|
|
|
} = {
|
|
|
|
|
cognome: fields.cognome,
|
|
|
|
|
email: fields.email,
|
2025-08-29 16:18:32 +02:00
|
|
|
id: id,
|
|
|
|
|
nome: fields.nome,
|
2025-08-28 18:27:07 +02:00
|
|
|
telefono: fields.telefono.replace(/\s/g, ""),
|
|
|
|
|
};
|
2025-08-04 17:45:44 +02:00
|
|
|
|
2025-08-28 18:27:07 +02:00
|
|
|
if (isAdmin !== undefined) {
|
|
|
|
|
data.isAdmin = fields.isAdmin;
|
|
|
|
|
}
|
|
|
|
|
mutate(data);
|
|
|
|
|
}
|
2025-08-04 17:45:44 +02:00
|
|
|
|
2025-08-28 18:27:07 +02:00
|
|
|
return (
|
|
|
|
|
<>
|
|
|
|
|
<Form {...form}>
|
|
|
|
|
<form
|
|
|
|
|
className="space-y-8 px-0.5"
|
2025-08-29 16:18:32 +02:00
|
|
|
onSubmit={form.handleSubmit(onSubmit)}
|
2025-08-28 18:27:07 +02:00
|
|
|
>
|
|
|
|
|
<DualInputLayout
|
|
|
|
|
children1={
|
|
|
|
|
<FormField
|
|
|
|
|
control={form.control}
|
|
|
|
|
name="nome"
|
|
|
|
|
render={({ field }) => (
|
|
|
|
|
<FormItem>
|
|
|
|
|
<div className="flex flex-wrap items-center gap-x-2">
|
|
|
|
|
<FormLabel htmlFor="nome">{t.profile.nome}</FormLabel>
|
|
|
|
|
<FormMessage />
|
|
|
|
|
</div>
|
2025-08-04 17:45:44 +02:00
|
|
|
|
2025-08-28 18:27:07 +02:00
|
|
|
<FormControl>
|
|
|
|
|
<Input placeholder="" {...field} id="nome" />
|
|
|
|
|
</FormControl>
|
|
|
|
|
</FormItem>
|
|
|
|
|
)}
|
|
|
|
|
/>
|
|
|
|
|
}
|
|
|
|
|
children2={
|
|
|
|
|
<FormField
|
|
|
|
|
control={form.control}
|
|
|
|
|
name="cognome"
|
|
|
|
|
render={({ field }) => (
|
|
|
|
|
<FormItem>
|
|
|
|
|
<div className="flex flex-wrap items-center gap-x-2">
|
|
|
|
|
<FormLabel htmlFor="cognome">
|
|
|
|
|
{t.profile.cognome}
|
|
|
|
|
</FormLabel>
|
|
|
|
|
<FormMessage />
|
|
|
|
|
</div>
|
2025-08-04 17:45:44 +02:00
|
|
|
|
2025-08-28 18:27:07 +02:00
|
|
|
<FormControl>
|
|
|
|
|
<Input placeholder="" {...field} id="cognome" />
|
|
|
|
|
</FormControl>
|
|
|
|
|
</FormItem>
|
|
|
|
|
)}
|
|
|
|
|
/>
|
|
|
|
|
}
|
|
|
|
|
/>
|
|
|
|
|
<DualInputLayout
|
|
|
|
|
children1={
|
|
|
|
|
<FormField
|
|
|
|
|
control={form.control}
|
|
|
|
|
name="email"
|
|
|
|
|
render={({ field }) => (
|
|
|
|
|
<FormItem>
|
|
|
|
|
<div className="flex flex-wrap items-center gap-x-2">
|
|
|
|
|
<FormLabel htmlFor="email">{t.profile.email}</FormLabel>{" "}
|
|
|
|
|
<FormMessage />
|
|
|
|
|
</div>
|
2025-08-04 17:45:44 +02:00
|
|
|
|
2025-08-28 18:27:07 +02:00
|
|
|
<FormControl>
|
|
|
|
|
<Input
|
|
|
|
|
placeholder="esempio@email.com"
|
|
|
|
|
{...field}
|
|
|
|
|
id="email"
|
|
|
|
|
/>
|
|
|
|
|
</FormControl>
|
|
|
|
|
</FormItem>
|
|
|
|
|
)}
|
|
|
|
|
/>
|
|
|
|
|
}
|
|
|
|
|
children2={
|
|
|
|
|
<FormField
|
|
|
|
|
control={form.control}
|
|
|
|
|
name="telefono"
|
|
|
|
|
render={({ field }) => (
|
|
|
|
|
<FormItem>
|
|
|
|
|
<div className="flex flex-wrap items-center gap-x-2">
|
|
|
|
|
<FormLabel htmlFor="telefono">
|
|
|
|
|
{t.profile.telefono}
|
|
|
|
|
</FormLabel>
|
|
|
|
|
<FormMessage />
|
|
|
|
|
</div>
|
2025-08-04 17:45:44 +02:00
|
|
|
|
2025-08-28 18:27:07 +02:00
|
|
|
<FormControl>
|
|
|
|
|
<PhoneInput {...field} id="telefono" />
|
|
|
|
|
</FormControl>
|
|
|
|
|
</FormItem>
|
|
|
|
|
)}
|
|
|
|
|
/>
|
|
|
|
|
}
|
|
|
|
|
/>
|
|
|
|
|
{isAdmin !== undefined && (
|
|
|
|
|
<FormField
|
|
|
|
|
control={form.control}
|
|
|
|
|
name="isAdmin"
|
|
|
|
|
render={({ field }) => (
|
|
|
|
|
<FormItem className="mt-6 flex items-start">
|
|
|
|
|
<div className="flex items-center justify-center gap-x-4">
|
|
|
|
|
<FormLabel htmlFor="isAdmin">Admin</FormLabel>
|
|
|
|
|
<FormControl>
|
|
|
|
|
<Switch
|
|
|
|
|
checked={field.value}
|
2025-08-29 16:18:32 +02:00
|
|
|
className="data-[state=checked]:bg-neutral-700"
|
|
|
|
|
id="isAdmin"
|
2025-08-28 18:27:07 +02:00
|
|
|
onCheckedChange={(v) => {
|
|
|
|
|
field.onChange(v);
|
|
|
|
|
}}
|
|
|
|
|
/>
|
|
|
|
|
</FormControl>
|
|
|
|
|
<FormMessage />
|
|
|
|
|
</div>
|
|
|
|
|
</FormItem>
|
|
|
|
|
)}
|
|
|
|
|
/>
|
|
|
|
|
)}
|
2025-08-04 17:45:44 +02:00
|
|
|
|
2025-08-28 18:27:07 +02:00
|
|
|
<Button type="submit">{t.profile.aggiorna}</Button>
|
|
|
|
|
</form>
|
|
|
|
|
</Form>
|
|
|
|
|
</>
|
|
|
|
|
);
|
2025-08-04 17:45:44 +02:00
|
|
|
};
|