mirror of
https://github.com/ichsankurnia/hats-app.git
synced 2026-12-06 10:46:27 +00:00
122 lines
4.9 KiB
TypeScript
122 lines
4.9 KiB
TypeScript
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 (
|
|
<DialogContent className=" overflow-y-auto max-h-[95vh]">
|
|
<Form form={form} onFinish={handleSubmit}>
|
|
<DialogHeader>
|
|
<DialogTitle className="dark:text-white">Change user password</DialogTitle>
|
|
<DialogDescription>
|
|
Update user password to maintain privacy of employee
|
|
</DialogDescription>
|
|
</DialogHeader>
|
|
<div className="grid mt-8">
|
|
<div className="grid gap-2">
|
|
<Label htmlFor="old_password">Old password</Label>
|
|
<Form.Item name='old_password' rules={[
|
|
{ required: true, message: 'This field is required' }, { min: 3 }
|
|
]}>
|
|
<Input.Password className="input" type="password" id="old_password" placeholder="********" />
|
|
</Form.Item>
|
|
</div>
|
|
<div className="grid gap-2">
|
|
<Label htmlFor="new_password">New password</Label>
|
|
<Form.Item name='new_password' rules={[
|
|
{ required: true, message: 'This field is required' }, { min: 5 }
|
|
]}>
|
|
<Input.Password className="input" type="password" id="new_password" placeholder="********" />
|
|
</Form.Item>
|
|
</div>
|
|
<div className="grid gap-2">
|
|
<Label htmlFor="confirm_password">Confirm password</Label>
|
|
<Form.Item name="confirm" dependencies={['new_password']} hasFeedback
|
|
rules={[
|
|
{ required: true, message: 'Please confirm your password!' },
|
|
({ getFieldValue }) => ({
|
|
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!'));
|
|
},
|
|
}),
|
|
]}
|
|
>
|
|
<Input.Password className="input" type="password" id="confirm_password" placeholder="********" />
|
|
</Form.Item>
|
|
</div>
|
|
</div>
|
|
<DialogFooter>
|
|
<Button variant="outline" type="button" onClick={handleCancel} disabled={isLoading} >
|
|
Cancel
|
|
</Button>
|
|
<Button type="submit" disabled={isLoading}>
|
|
{isLoading && (
|
|
<Icons.spinner className="mr-1 h-4 w-4 animate-spin" />
|
|
)}
|
|
Save
|
|
</Button>
|
|
</DialogFooter>
|
|
</Form>
|
|
</DialogContent>
|
|
)
|
|
} |