import ErrorDisplay from "@/ui/error-display"; import { useApi } from "@/util/api"; import { Avatar, Button, Center, Container, Fieldset, Group, Loader, PasswordInput, Stack, Text, Title, } from "@mantine/core"; import { useForm } from "@mantine/form"; import { IconFingerprint } from "@tabler/icons-react"; import { useMutation, useQuery } from "@tanstack/react-query"; import { AxiosError } from "axios"; export default function Me() { const client = useApi(); const user = useQuery({ queryKey: ["user"], queryFn: async () => await client.get(`users/me`).then((res) => res.data), }); const updatePassword = useMutation({ mutationFn: async (values: { current_psk: string; new_psk: string; confirm_new_psk: string; }) => { await client.put(`/users/me/password`, { current_password: values.current_psk, new_password: values.new_psk, }); }, }); const updatePskForm = useForm({ initialValues: { current_psk: "", new_psk: "", confirm_new_psk: "", }, validate: { current_psk: (value) => value.length === 0 ? "Please enter your current password" : null, new_psk: (value) => { if (value.length === 0) return "Please enter a new password"; if (value.length < 8 || value.length > 16) return "Password must be between 8 and 16 characters"; }, confirm_new_psk: (value, values) => { if (value.length === 0) return "Please confirm your new password"; if (value.length < 8 || value.length > 16) return "Password must be between 8 and 16 characters"; if (value !== values.new_psk) return "Passwords must match"; }, }, }); return ( {user.isLoading ? (
) : user.isError ? (
) : user.data ? ( {user.data.username} {user.data.level === 2 ? "Admin" : user.data.level === 1 ? "User" : "Guest"} {" "}
{ updatePassword.mutate(values); })} >
{updatePassword.isPending ? ( Updating... ) : updatePassword.isError ? ( updatePassword.error && (updatePassword.error as AxiosError).response?.status === 403 ? ( Incorrect password ) : ( Failed: {updatePassword.error.message} ) ) : updatePassword.isSuccess ? ( Updated ) : null}
) : ( Unknown Error )}
); }