mirror of
https://github.com/ichsankurnia/hats-app.git
synced 2026-12-05 10:46:29 +00:00
148 lines
6.7 KiB
TypeScript
148 lines
6.7 KiB
TypeScript
'use client'
|
|
|
|
import { FC, useEffect, useState } from 'react';
|
|
import { useRouter } from 'next/navigation';
|
|
import { DatePicker, Form, Input, Select } from 'antd';
|
|
import dayjs from 'dayjs';
|
|
import { Icons } from '@/components/Icons';
|
|
import { Button } from '@/components/ui/button';
|
|
import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, DialogTrigger } from '@/components/ui/dialog';
|
|
import useAxiosAuth from '@/lib/axios/useAxiosAuth';
|
|
import { type IProfile } from './type';
|
|
import { toast } from 'sonner';
|
|
|
|
const EDUCATION_LIST = [
|
|
{ value: 'D1', label: 'Diploma I' },
|
|
{ value: 'D2', label: 'Diploma 2' },
|
|
{ value: 'D3', label: 'Diploma 3' },
|
|
{ value: 'D4', label: 'Diploma 4' },
|
|
{ value: 'S1', label: 'Strata 1' },
|
|
{ value: 'S2', label: 'Strata 2' },
|
|
{ value: 'S3', label: 'Doktor' },
|
|
]
|
|
|
|
type Props = {
|
|
data?: IProfile
|
|
};
|
|
|
|
const EditProfile: FC<Props> = ({ data }) => {
|
|
const [showDialog, setShowDialog] = useState(false)
|
|
const [isLoading, setIsLoading] = useState(false)
|
|
|
|
const [form] = Form.useForm();
|
|
const router = useRouter()
|
|
const axios = useAxiosAuth()
|
|
|
|
useEffect(() => {
|
|
if (data) {
|
|
form.setFieldsValue(data)
|
|
if (data.birthday) form.setFieldValue('birthday', dayjs(data.birthday))
|
|
} else {
|
|
form.resetFields()
|
|
}
|
|
}, [data, form])
|
|
|
|
const handleSubmit = (value: any) => {
|
|
console.log(value)
|
|
|
|
handleUpdate(value)
|
|
}
|
|
|
|
const handleUpdate = async (payload: any) => {
|
|
try {
|
|
setIsLoading(true)
|
|
|
|
await axios.put('/user/profile/update', payload)
|
|
|
|
toast.success('Profile changed successfully.')
|
|
form.resetFields()
|
|
setShowDialog(false)
|
|
router.refresh()
|
|
} catch (error: any) {
|
|
toast.error(`Fail to update profile`, { description: `(${error.response.data.code || error.response.status || '-'}) ${error.response.data.message || error.response.statusText || error.message}` })
|
|
} finally {
|
|
setIsLoading(false)
|
|
}
|
|
}
|
|
|
|
const handleCancel = () => {
|
|
form.resetFields()
|
|
setShowDialog(false)
|
|
}
|
|
|
|
return (
|
|
<>
|
|
<Dialog open={showDialog} onOpenChange={(state) => {
|
|
setShowDialog(state)
|
|
}}>
|
|
<DialogTrigger asChild>
|
|
<Button variant='secondary'>
|
|
Edit Profile <i className="ti ti-edit ml-2 text-lg"></i>
|
|
</Button>
|
|
</DialogTrigger>
|
|
{showDialog &&
|
|
<DialogContent className='max-h-[95vh] overflow-y-auto' forceMount>
|
|
<Form form={form} onFinish={handleSubmit} scrollToFirstError layout="vertical" size='small'>
|
|
<DialogHeader>
|
|
<DialogTitle className="dark:text-white">Edit Profile</DialogTitle>
|
|
<DialogDescription>
|
|
Adjust your profile data with the latest data
|
|
</DialogDescription>
|
|
</DialogHeader>
|
|
<div className='grid grid-cols-1 font-medium gap-y-0 mt-5'>
|
|
<Form.Item name='nickname' label='Nickname' >
|
|
<Input className="input" id="nickname" placeholder="John" />
|
|
</Form.Item>
|
|
<Form.Item label='Phone Number' name='phone_number' rules={[
|
|
{ pattern: /^\d+$/, message: 'Invalid phone number' },
|
|
{ min: 10 }, { max: 13 }
|
|
]}>
|
|
<Input className="input" id="phone_number" placeholder="6281234567890" maxLength={13} />
|
|
</Form.Item>
|
|
<Form.Item label='Office Email' name='email_office' rules={[
|
|
{ type: 'email', message: 'The input is not valid E-mail!' }]}
|
|
>
|
|
<Input className="input" id="email_office" placeholder="john@nutech-integrasi.com" />
|
|
</Form.Item>
|
|
<Form.Item label='Personal Email' name='email_personal' rules={[
|
|
{ type: 'email', message: 'The input is not valid E-mail!' }]}
|
|
>
|
|
<Input className="input" id="email_personal" placeholder="john@gmail.com" />
|
|
</Form.Item>
|
|
<Form.Item label='Birthdate' name='birthday'>
|
|
<DatePicker className='w-full' placeholder='Select date of birth' size='large'
|
|
getPopupContainer={(triggerNode: any) => triggerNode.parentNode}
|
|
/>
|
|
</Form.Item>
|
|
<Form.Item label='Last Education' name='last_education' >
|
|
<Select placeholder='Select last education' showSearch allowClear
|
|
optionFilterProp="children"
|
|
filterOption={(input, option) => (option?.label ?? '').toLowerCase().includes(input.toLowerCase())}
|
|
getPopupContainer={(triggerNode: any) => triggerNode.parentNode}
|
|
size='large'
|
|
options={EDUCATION_LIST} />
|
|
</Form.Item>
|
|
<Form.Item label='Mayor' name='major' >
|
|
<Input className="input" id="major" placeholder="Informatics Engineering" />
|
|
</Form.Item>
|
|
</div>
|
|
<DialogFooter>
|
|
<Button variant="outline" type="button" onClick={handleCancel} disabled={isLoading} >
|
|
Cancel
|
|
</Button>
|
|
<Button type="submit" disabled={isLoading}>
|
|
{isLoading && (
|
|
<Icons.spinner className="mr-2 h-4 w-4 animate-spin" />
|
|
)}
|
|
Save
|
|
</Button>
|
|
</DialogFooter>
|
|
</Form>
|
|
</DialogContent>
|
|
}
|
|
</Dialog>
|
|
</>
|
|
);
|
|
}
|
|
|
|
export default EditProfile; |