import { useSession } from "next-auth/react";
import { useRouter } from "next/router";
import type { Session } from "@/models/auth/Session";
import Carousel from "@/components/Carousel";
import { Gender } from "@prisma/client";
import {
Box,
Button,
Center,
Container,
Divider,
Editable,
EditableInput,
EditablePreview,
EditableTextarea,
Flex,
FormControl,
FormErrorMessage,
FormLabel,
HStack,
Input,
Radio,
RadioGroup,
Text,
useToast,
} from "@chakra-ui/react";
import ModalModifyImages from "@/components/ModalModifyImages";
import { useState } from "react";
import { useForm, Controller } from "react-hook-form";
export default function UserProfile() {
const router = useRouter();
const toast = useToast();
const [isLoading, setIsLoading] = useState(false);
const { handleSubmit, control } = useForm();
const [userData, setUserData] = useState({});
const { data: session, status } = useSession();
if (status === "unauthenticated") router.push("/login");
if (status === "authenticated") {
const { user } = session as unknown as Session;
const getTextGender = (gender) => {
switch (gender) {
case Gender.MALE:
return "Homme";
case Gender.FEMALE:
return "Femme";
case Gender.OTHER:
return "Autre";
case Gender.UNKNOWN:
return "Non renseigné";
}
};
const saveData = (values: any) => {
const trueValues = Object.keys(values).reduce((acc, key) => {
if (values[key] !== "" && values[key] !== undefined) {
acc[key] = values[key];
}
return acc;
}, {});
const options = {
method: "PATCH",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(trueValues),
};
if (Object.keys(trueValues).length > 0) {
setIsLoading(true);
fetch(`/api/users/${user.id}`, options)
.then((res) => {
console.log(res);
setIsLoading(false);
toast({
title: `Modifications effectuées`,
status: "success",
isClosable: true,
});
// router.reload();
})
.catch((err) => {
setIsLoading(false);
toast({
title: `Erreur lors de l'envoi des modifications`,
status: "error",
isClosable: true,
});
console.log(err);
});
}
};
const formateDate = (dateString: string) => {
var options = { year: "numeric", month: "long", day: "numeric" };
return new Date(dateString).toLocaleDateString([], options);
};
return (