Files
2025-12-19 13:57:55 +07:00

81 lines
2.6 KiB
TypeScript

import { useState } from "react"
import { Button } from "@/components/ui/button"
import {
DialogContent,
DialogDescription,
DialogFooter,
DialogHeader,
DialogTitle,
} from "@/components/ui/dialog"
import { Icons } from "@/components/Icons"
import { type IUser } from "./type"
import { useRouter } from "next/navigation"
import useAxiosAuth from "@/lib/axios/useAxiosAuth"
import { type ResponseData } from "@/types/types"
import { toast } from "sonner"
type Props = {
data?: IUser
onCancel?: () => any
}
export default function ResetPassword({ data, onCancel }: Props) {
const [isLoading, setIsLoading] = useState(false)
const router = useRouter()
const axios = useAxiosAuth()
const handleResetPassword = async () => {
if (isLoading) return
try {
setIsLoading(true)
const { data: res }: ResponseData = await axios.post('/user/reset-password', { nik: data?.nik })
setIsLoading(false)
console.log(res)
if (!res) return
if (res.code === '00') {
showSuccess(res.message)
} else {
showError(`Failed to reset password, ${res.message}`)
}
} catch (error: any) {
console.log(error)
showError(`Failed to reset password, (${error.response.data.code || error.response.status || '-'}) ${error.response.data.message || error.response.statusText || error.message}`)
}
}
const showSuccess = (message: string) => {
toast.success(message as string)
router.refresh()
onCancel!()
};
const showError = (message?: string) => {
setIsLoading(false)
toast.error(message)
};
return (
<DialogContent className=" overflow-y-auto max-h-[95vh]">
<DialogHeader>
<DialogTitle className="dark:text-white">Confirm reset password</DialogTitle>
<DialogDescription>
This action will reset password for user <b>{data?.fullname}</b>, are you sure to continue this process?
</DialogDescription>
</DialogHeader>
<DialogFooter>
<Button variant="outline" type="button" onClick={onCancel} disabled={isLoading} >
Cancel
</Button>
<Button type="submit" disabled={isLoading} onClick={handleResetPassword}>
{isLoading && (
<Icons.spinner className="mr-1 h-4 w-4 animate-spin" />
)}
Save
</Button>
</DialogFooter>
</DialogContent>
)
}