import { useState } from "react" import { useRouter } from "next/navigation" import { Form, Input } from "antd" import useAxiosAuth from "@/lib/axios/useAxiosAuth" import { Button } from "@/components/ui/button" import { DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, } from "@/components/ui/dialog" import { Label } from "@/components/ui/label" import { Icons } from "@/components/Icons" import { type IUser } from "./type" import { toast } from "sonner" type Props = { data?: IUser onCancel?: () => any } export default function FormChangePassword({ data, onCancel }: Props) { const [isLoading, setIsLoading] = useState(false) const [form] = Form.useForm(); const router = useRouter() const axios = useAxiosAuth() const handleSubmit = (value: any) => { console.log(value) handleUpdate(value) } const handleUpdate = async (payload: any) => { try { setIsLoading(true) const request = { nik: data?.nik, old_password: payload['old_password'], new_password: payload['new_password'] } await axios.post('/user/change-password', request) toast.success('Password changed successfully.') setIsLoading(false) onCancel!() router.refresh() form.resetFields() } catch (error: any) { toast.error(`Failed to change password`, { description: `(${error.response.data.code || error.response.status || '-'}) ${error.response.data.message || error.response.statusText || error.message}}` }) setIsLoading(false) } } const handleCancel = () => { form.resetFields() onCancel!() } return (
Change user password Update user password to maintain privacy of employee
({ validator(_, value) { if (!value || getFieldValue('new_password') === value) { return Promise.resolve(); } return Promise.reject(new Error('The new password that you entered do not match!')); }, }), ]} >
) }