mirror of
https://github.com/ichsankurnia/hats-app.git
synced 2026-12-05 10:56:28 +00:00
e67f671983
Gate assignment creation and editing by the relevant permissions, show ongoing milestones on the timeline, and restrict the forbidden-page admin link to authorized users. Also use the shared RBAC role type and tighten sidebar active-state matching.
338 lines
21 KiB
TypeScript
338 lines
21 KiB
TypeScript
'use client'
|
|
|
|
import { FC, Suspense, useEffect, useState } from 'react';
|
|
import Link from 'next/link';
|
|
import { useRouter, useSearchParams } from 'next/navigation';
|
|
import { DatePicker, Form, Input, Radio, Select } from 'antd';
|
|
import dayjs from 'dayjs';
|
|
import { Icons } from '@/components/Icons';
|
|
import { Button } from '@/components/ui/button';
|
|
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card';
|
|
import { AlertDialog, AlertDialogAction, AlertDialogCancel, AlertDialogContent, AlertDialogDescription, AlertDialogFooter, AlertDialogHeader, AlertDialogTitle } from '@/components/ui/alert-dialog';
|
|
import useFetch from '@/hooks/useFetch';
|
|
import useAxiosAuth from '@/lib/axios/useAxiosAuth';
|
|
import { titleCase } from '@/lib/helper';
|
|
import PageLoading from '@/components/loader/PageLoading';
|
|
import LazyLoading from '@/components/loader/LazyLoading';
|
|
import { type IUser } from '../component/type';
|
|
import { type ICompany } from '../../company/component/type';
|
|
import { type IArea } from '../../area/component/type';
|
|
import { type IDivision } from '../../division/component/type';
|
|
import { type IPosition } from '@/app/admin/information/position/component/type';
|
|
import { type IEmployeeType } from '@/app/admin/information/employee-type/component/type';
|
|
import { toast } from 'sonner';
|
|
import { IRole } from '@/types/rbac';
|
|
|
|
type Props = {};
|
|
|
|
const UserCreate: FC<Props> = ({ }) => {
|
|
const [isLoading, setIsLoading] = useState(false)
|
|
const [isFetching, setIsFetching] = useState(false)
|
|
const [showConfirm, setShowConfirm] = useState(false)
|
|
|
|
const [form] = Form.useForm();
|
|
const router = useRouter()
|
|
const searchParams = useSearchParams();
|
|
const axios = useAxiosAuth()
|
|
|
|
const isUpdate = searchParams!.get('is_update') as string || ''
|
|
const id = searchParams!.get('id') as string || ''
|
|
|
|
const { data: companyList, isLoading: companyLoading } = useFetch('/company')
|
|
const { data: areaList, isLoading: areaLoading } = useFetch('/area')
|
|
const { data: divisionList, isLoading: divisionLoading } = useFetch('/division')
|
|
const { data: roleList, isLoading: roleLoading } = useFetch('/role')
|
|
const { data: positionList, isLoading: positionLoading } = useFetch('/position')
|
|
const { data: employeeTypeList, isLoading: employeeTypeLoading } = useFetch('/employee-type')
|
|
|
|
useEffect(() => {
|
|
async function fetchData() {
|
|
setIsFetching(true)
|
|
try {
|
|
const res = await axios.get(`/user/${id}`)
|
|
const data: IUser = res.data.data
|
|
|
|
form.setFieldsValue(data)
|
|
|
|
if (data.company) form.setFieldValue('company_id', data.company.id)
|
|
if (data.area) form.setFieldValue('area_id', data.area.id)
|
|
if (data.division) form.setFieldValue('division_id', data.division.id)
|
|
if (data.role) form.setFieldValue('role_id', data.role.id)
|
|
if (data.employee_type) form.setFieldValue('employee_type_id', data.employee_type.id)
|
|
if (data.position) form.setFieldValue('position_id', data.position.id)
|
|
|
|
if (data.birthday) form.setFieldValue('birthday', dayjs(data.birthday))
|
|
if (data.contract_expiration_date) form.setFieldValue('contract_expiration_date', dayjs(data.contract_expiration_date))
|
|
if (data.hire_date) form.setFieldValue('hire_date', dayjs(data.hire_date))
|
|
if (data.resign_date) form.setFieldValue('resign_date', dayjs(data.resign_date))
|
|
|
|
setIsFetching(false)
|
|
} catch (error: any) {
|
|
handleError(`Failed fetch data, (${error.response.data.code || error.response.status || '-'}) ${error.response.data.message || error.response.statusText || error.message}`)
|
|
}
|
|
}
|
|
|
|
if (isUpdate) {
|
|
fetchData()
|
|
} else {
|
|
form.resetFields()
|
|
}
|
|
}, [isUpdate, form, id])
|
|
|
|
|
|
const handleSubmit = () => {
|
|
const payload = form.getFieldsValue()
|
|
if (payload.fullname) payload.fullname = titleCase(payload.fullname)
|
|
|
|
!isUpdate ? handleCreate(payload) : handleUpdate(payload)
|
|
}
|
|
|
|
const handleCreate = async (payload: IUser) => {
|
|
try {
|
|
setIsLoading(true)
|
|
await axios.post('/user', payload)
|
|
|
|
handleSuccess('Data successfully created.')
|
|
} catch (error: any) {
|
|
handleError(`Data failed to create, (${error.response.data.code || error.response.status || '-'}) ${error.response.data.message || error.response.statusText || error.message}`)
|
|
}
|
|
}
|
|
|
|
const handleUpdate = async (payload: IUser) => {
|
|
try {
|
|
setIsLoading(true)
|
|
await axios.put('/user/' + id, payload)
|
|
|
|
handleSuccess('Data successfully updated.')
|
|
} catch (error: any) {
|
|
handleError(`Data failed to update, (${error.response.data.code || error.response.status || '-'}) ${error.response.data.message || error.response.statusText || error.message}`)
|
|
}
|
|
}
|
|
|
|
const handleSuccess = (message: string) => {
|
|
toast.success(message)
|
|
router.replace('/admin/administration/user')
|
|
router.refresh()
|
|
form.resetFields()
|
|
}
|
|
|
|
const handleError = (message: string) => {
|
|
toast.error(message)
|
|
setIsLoading(false)
|
|
}
|
|
|
|
return (
|
|
<>
|
|
<Card>
|
|
<CardHeader className='flex flex-row items-center sm:space-x-5'>
|
|
<Link href='/admin/administration/user' className='hidden sm:block'>
|
|
<Button variant='outline' size='icon'>
|
|
<i className="ti ti-arrow-left text-xl"></i>
|
|
</Button>
|
|
</Link>
|
|
<div className='space-y-1'>
|
|
<CardTitle>{isUpdate ? 'Update user' : 'Create new user'}</CardTitle>
|
|
<CardDescription>{isUpdate ? 'Update user id ' + id : 'Add a new team to join to your department'}</CardDescription>
|
|
</div>
|
|
</CardHeader>
|
|
<CardContent>
|
|
<Suspense fallback={<LazyLoading />}>
|
|
<Form form={form} onFinish={() => setShowConfirm(true)} scrollToFirstError layout="vertical" size='small'>
|
|
<div className='grid grid-cols-1 sm:grid-cols-2 font-medium gap-y-0 gap-x-5 2xl:gap-x-8'>
|
|
{/* <Label htmlFor="fullname">Fullname</Label> */}
|
|
<Form.Item name='fullname' labelAlign="left" label='Fullname' rules={[{ required: true, message: 'This field is required' }]} >
|
|
<Input className="input" id="fullname" placeholder="John Doe" />
|
|
</Form.Item>
|
|
<Form.Item name='nickname' label='Nickname' rules={[{ required: true, message: 'This field is required' }]} >
|
|
<Input className="input" id="nickname" placeholder="John" />
|
|
</Form.Item>
|
|
<Form.Item label='Email' name='email' rules={[
|
|
{ required: true, message: 'This field is required' },
|
|
{ type: 'email', message: 'The input is not valid E-mail!' }]}
|
|
>
|
|
<Input className="input" id="email" placeholder="john.nutech@gmail.com" />
|
|
</Form.Item>
|
|
{!isUpdate &&
|
|
<Form.Item label='Password' name='password' rules={[
|
|
{ required: true, message: 'This field is required' }, { min: 5 }
|
|
]}>
|
|
<Input.Password className="input" type="password" id="password" placeholder="********" />
|
|
</Form.Item>
|
|
}
|
|
<Form.Item label='NIK' name='nik' rules={[
|
|
{ required: true, message: 'This field is required' },
|
|
{ pattern: /^\d+$/, message: 'Invalid NIK format' },
|
|
{ len: 11 }
|
|
]}>
|
|
<Input className="input" id="nik" placeholder="04012345678" maxLength={11} />
|
|
</Form.Item>
|
|
<Form.Item label='NIKTG' name='niktg' rules={[
|
|
{ pattern: /^\d+$/, message: 'Invalid NIKTG format' },
|
|
]}>
|
|
<Input className="input" id="niktg" placeholder="995500" maxLength={8} />
|
|
</Form.Item>
|
|
<Form.Item label='Phone Number' name='phone_number' rules={[
|
|
{ required: true, message: 'This field is required' },
|
|
{ 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='Role' name='role_id' rules={[{ required: true, message: 'This field is required' }]} >
|
|
<Select placeholder='Select role' showSearch allowClear loading={roleLoading}
|
|
optionFilterProp="children"
|
|
filterOption={(input, option) => option!.children!.toString().toLowerCase().indexOf(input.toLowerCase()) >= 0}
|
|
getPopupContainer={(triggerNode: any) => triggerNode.parentNode}
|
|
size='large'
|
|
>
|
|
{roleList?.role?.length > 0 && roleList.role.map((item: IRole, key: number) =>
|
|
<Select.Option value={item.id} key={key}>{item.name}</Select.Option>
|
|
)}
|
|
</Select>
|
|
</Form.Item>
|
|
<Form.Item label='Company' name='company_id' rules={[{ required: true, message: 'This field is required' }]} >
|
|
<Select placeholder='Select company' showSearch allowClear loading={companyLoading}
|
|
optionFilterProp="children"
|
|
filterOption={(input, option) => option!.children!.toString().toLowerCase().indexOf(input.toLowerCase()) >= 0}
|
|
getPopupContainer={(triggerNode: any) => triggerNode.parentNode}
|
|
size='large'
|
|
>
|
|
{companyList?.company?.length > 0 && companyList.company.map((item: ICompany, key: number) =>
|
|
<Select.Option value={item.id} key={key}>{item.code} - {item.name}</Select.Option>
|
|
)}
|
|
</Select>
|
|
</Form.Item>
|
|
<Form.Item label='Area' name='area_id' rules={[{ required: true, message: 'This field is required' }]} >
|
|
<Select placeholder='Select area' showSearch allowClear loading={areaLoading}
|
|
optionFilterProp="children"
|
|
filterOption={(input, option) => option!.children!.toString().toLowerCase().indexOf(input.toLowerCase()) >= 0}
|
|
getPopupContainer={(triggerNode: any) => triggerNode.parentNode}
|
|
size='large'
|
|
>
|
|
{areaList?.area?.length > 0 && areaList.area.map((item: IArea, key: number) =>
|
|
<Select.Option value={item.id} key={key}>{item.name}</Select.Option>
|
|
)}
|
|
</Select>
|
|
</Form.Item>
|
|
<Form.Item label='Department' name='division_id' rules={[{ required: true, message: 'This field is required' }]} >
|
|
<Select placeholder='Select division' showSearch allowClear loading={divisionLoading}
|
|
optionFilterProp="children"
|
|
filterOption={(input, option) => option!.children!.toString().toLowerCase().indexOf(input.toLowerCase()) >= 0}
|
|
getPopupContainer={(triggerNode: any) => triggerNode.parentNode}
|
|
size='large'
|
|
>
|
|
{divisionList?.division?.length > 0 && divisionList.division.map((item: IDivision, key: number) =>
|
|
<Select.Option value={item.id} key={key}>{item.code} - {item.name}</Select.Option>
|
|
)}
|
|
</Select>
|
|
</Form.Item>
|
|
<Form.Item label='Position' name='position_id' rules={[{ required: true, message: 'This field is required' }]} >
|
|
<Select placeholder='Select position' showSearch allowClear loading={positionLoading}
|
|
optionFilterProp="children"
|
|
filterOption={(input, option) => option!.children!.toString().toLowerCase().indexOf(input.toLowerCase()) >= 0}
|
|
getPopupContainer={(triggerNode: any) => triggerNode.parentNode}
|
|
size='large'
|
|
>
|
|
{positionList?.position?.length > 0 && positionList.position.map((item: IPosition, key: number) =>
|
|
<Select.Option value={item.id} key={key}>{item.name}</Select.Option>
|
|
)}
|
|
</Select>
|
|
</Form.Item>
|
|
<Form.Item label='Employee Type' name='employee_type_id' rules={[{ required: true, message: 'This field is required' }]} >
|
|
<Select placeholder='Select employee type' showSearch allowClear loading={employeeTypeLoading}
|
|
optionFilterProp="children"
|
|
filterOption={(input, option) => option!.children!.toString().toLowerCase().indexOf(input.toLowerCase()) >= 0}
|
|
getPopupContainer={(triggerNode: any) => triggerNode.parentNode}
|
|
size='large'
|
|
>
|
|
{employeeTypeList?.employee_type?.length > 0 && employeeTypeList.employee_type.map((item: IEmployeeType, key: number) =>
|
|
<Select.Option value={item.id} key={key}>{item.name}</Select.Option>
|
|
)}
|
|
</Select>
|
|
</Form.Item>
|
|
<Form.Item label='Birthdate' name='birthday'>
|
|
<DatePicker className='w-full' placeholder='Select date of birth' size='large' />
|
|
</Form.Item>
|
|
<Form.Item label='Gender' name='gender'>
|
|
<Radio.Group>
|
|
<Radio value="male">Male</Radio>
|
|
<Radio value="female">Female</Radio>
|
|
</Radio.Group>
|
|
</Form.Item>
|
|
<Form.Item label='Hire Date' name='hire_date'>
|
|
<DatePicker className='w-full' placeholder='Select date of hire' size='large' />
|
|
</Form.Item>
|
|
<Form.Item label='Contract Expiration Date' name='contract_expiration_date'>
|
|
<DatePicker className='w-full' placeholder='Select contract expiration date' size='large' />
|
|
</Form.Item>
|
|
<Form.Item label='Extend Contract' name='is_extend_contract'>
|
|
<Radio.Group>
|
|
<Radio value={true}>Yes</Radio>
|
|
<Radio value={false}>No</Radio>
|
|
</Radio.Group>
|
|
</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='Last Education' name='last_education' >
|
|
<Input className="input" id="last_education" placeholder="S1 Teknik Informatika" />
|
|
</Form.Item>
|
|
<Form.Item label='Resign Date' name='resign_date'>
|
|
<DatePicker className='w-full' placeholder='Select resign date' size='large' />
|
|
</Form.Item>
|
|
<Form.Item label='Resign Reason' name='resign_reason' >
|
|
<Input.TextArea className="input" id="resign_reason" placeholder="Describe reason of resign" rootClassName='px-3! py-2!' />
|
|
</Form.Item>
|
|
<Form.Item label='Post Company' name='post_company'>
|
|
<Input className="input" id="post_company" placeholder="PT Tokopedia" />
|
|
</Form.Item>
|
|
</div>
|
|
<div className='flex justify-end items-center'>
|
|
<Button type='submit'>
|
|
{isLoading && (
|
|
<Icons.spinner className="mr-1 h-4 w-4 animate-spin" />
|
|
)}
|
|
Save
|
|
</Button>
|
|
</div>
|
|
</Form>
|
|
</Suspense>
|
|
</CardContent>
|
|
</Card>
|
|
{isUpdate && isFetching &&
|
|
<PageLoading />
|
|
}
|
|
|
|
{showConfirm &&
|
|
<AlertDialog open={true}>
|
|
<AlertDialogContent>
|
|
<AlertDialogHeader>
|
|
<AlertDialogTitle>Are you absolutely sure?</AlertDialogTitle>
|
|
<AlertDialogDescription>
|
|
This data will be stored on our server, you can change it whenever you want later.
|
|
</AlertDialogDescription>
|
|
</AlertDialogHeader>
|
|
<AlertDialogFooter>
|
|
<AlertDialogCancel disabled={isLoading} onClick={() => setShowConfirm(false)}>Cancel</AlertDialogCancel>
|
|
<AlertDialogAction disabled={isLoading} onClick={handleSubmit}>
|
|
{isLoading && (
|
|
<Icons.spinner className="mr-1 h-4 w-4 animate-spin" />
|
|
)}
|
|
Continue
|
|
</AlertDialogAction>
|
|
</AlertDialogFooter>
|
|
</AlertDialogContent>
|
|
</AlertDialog>
|
|
}
|
|
</>
|
|
);
|
|
}
|
|
|
|
export default UserCreate; |