mirror of
https://gitlab.com/mbugroup/lti-web-client.git
synced 2026-05-24 15:25:46 +00:00
chore(FE-40): add delete button and delete confirmation modal
This commit is contained in:
@@ -8,6 +8,8 @@ import { toast } from 'react-hot-toast';
|
|||||||
import { Icon } from '@iconify/react';
|
import { Icon } from '@iconify/react';
|
||||||
import Button from '@/components/Button';
|
import Button from '@/components/Button';
|
||||||
import TextInput from '@/components/input/TextInput';
|
import TextInput from '@/components/input/TextInput';
|
||||||
|
import { useModal } from '@/components/Modal';
|
||||||
|
import ConfirmationModal from '@/components/modal/ConfirmationModal';
|
||||||
|
|
||||||
import {
|
import {
|
||||||
UomFormSchema,
|
UomFormSchema,
|
||||||
@@ -21,6 +23,7 @@ import {
|
|||||||
UpdateUomPayload,
|
UpdateUomPayload,
|
||||||
} from '@/types/api/master-data/uom';
|
} from '@/types/api/master-data/uom';
|
||||||
import { UomApi } from '@/services/api/master-data';
|
import { UomApi } from '@/services/api/master-data';
|
||||||
|
import { cn } from '@/lib/helper';
|
||||||
|
|
||||||
interface UomFormProps {
|
interface UomFormProps {
|
||||||
type?: 'add' | 'edit' | 'detail';
|
type?: 'add' | 'edit' | 'detail';
|
||||||
@@ -29,8 +32,10 @@ interface UomFormProps {
|
|||||||
|
|
||||||
const UomForm = ({ type = 'add', initialValues }: UomFormProps) => {
|
const UomForm = ({ type = 'add', initialValues }: UomFormProps) => {
|
||||||
const router = useRouter();
|
const router = useRouter();
|
||||||
|
const deleteModal = useModal();
|
||||||
|
|
||||||
const [uomFormErrorMessage, setUomFormErrorMessage] = useState('');
|
const [uomFormErrorMessage, setUomFormErrorMessage] = useState('');
|
||||||
|
const [isDeleteLoading, setIsDeleteLoading] = useState(false);
|
||||||
|
|
||||||
const createUomHandler = useCallback(
|
const createUomHandler = useCallback(
|
||||||
async (payload: CreateUomPayload) => {
|
async (payload: CreateUomPayload) => {
|
||||||
@@ -93,81 +98,138 @@ const UomForm = ({ type = 'add', initialValues }: UomFormProps) => {
|
|||||||
|
|
||||||
const { setValues: formikSetValues } = formik;
|
const { setValues: formikSetValues } = formik;
|
||||||
|
|
||||||
|
const deleteUomClickHandler = () => {
|
||||||
|
deleteModal.openModal();
|
||||||
|
};
|
||||||
|
|
||||||
|
const confirmationModalDeleteClickHandler = async () => {
|
||||||
|
setIsDeleteLoading(true);
|
||||||
|
|
||||||
|
await UomApi.delete(initialValues?.id as number);
|
||||||
|
|
||||||
|
deleteModal.closeModal();
|
||||||
|
toast.success('Successfully delete UOM!');
|
||||||
|
setIsDeleteLoading(false);
|
||||||
|
router.push('/master-data/uom');
|
||||||
|
};
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
formikSetValues(formikInitialValues);
|
formikSetValues(formikInitialValues);
|
||||||
}, [formikSetValues, formikInitialValues]);
|
}, [formikSetValues, formikInitialValues]);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<section className='w-full max-w-xl'>
|
<>
|
||||||
<header className='flex flex-col gap-4'>
|
<section className='w-full max-w-xl'>
|
||||||
<Button
|
<header className='flex flex-col gap-4'>
|
||||||
href='/master-data/uom'
|
<Button
|
||||||
variant='link'
|
href='/master-data/uom'
|
||||||
className='w-fit p-0 text-primary'
|
variant='link'
|
||||||
|
className='w-fit p-0 text-primary'
|
||||||
|
>
|
||||||
|
<Icon icon='uil:arrow-left' width={24} height={24} />
|
||||||
|
Kembali
|
||||||
|
</Button>
|
||||||
|
|
||||||
|
<h1 className='text-2xl font-bold text-center'>
|
||||||
|
{type === 'add' && 'Tambah UOM'}
|
||||||
|
{type === 'edit' && 'Edit UOM'}
|
||||||
|
{type === 'detail' && 'Detail UOM'}
|
||||||
|
</h1>
|
||||||
|
</header>
|
||||||
|
|
||||||
|
<form
|
||||||
|
onSubmit={formik.handleSubmit}
|
||||||
|
onReset={formik.handleReset}
|
||||||
|
className='w-full mt-8 flex flex-col gap-6'
|
||||||
>
|
>
|
||||||
<Icon icon='uil:arrow-left' width={24} height={24} />
|
<div className='flex flex-col gap-4'>
|
||||||
Kembali
|
<TextInput
|
||||||
</Button>
|
required
|
||||||
|
label='Nama'
|
||||||
|
name='name'
|
||||||
|
placeholder='Masukkan nama user'
|
||||||
|
value={formik.values.name}
|
||||||
|
onChange={formik.handleChange}
|
||||||
|
onBlur={formik.handleBlur}
|
||||||
|
isError={formik.touched.name && Boolean(formik.errors.name)}
|
||||||
|
errorMessage={formik.errors.name}
|
||||||
|
readOnly={type === 'detail'}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
|
||||||
<h1 className='text-2xl font-bold text-center'>
|
<div className='flex flex-row justify-between gap-2 flex-wrap'>
|
||||||
{type === 'add' && 'Tambah UOM'}
|
{type !== 'add' && (
|
||||||
{type === 'edit' && 'Edit UOM'}
|
<div className='flex flex-row justify-start gap-2'>
|
||||||
{type === 'detail' && 'Detail UOM'}
|
<Button
|
||||||
</h1>
|
type='button'
|
||||||
</header>
|
color='error'
|
||||||
|
onClick={deleteUomClickHandler}
|
||||||
<form
|
className='px-4'
|
||||||
onSubmit={formik.handleSubmit}
|
>
|
||||||
onReset={formik.handleReset}
|
<Icon
|
||||||
className='w-full mt-8 flex flex-col gap-6'
|
icon='material-symbols:delete-outline-rounded'
|
||||||
>
|
width={24}
|
||||||
<div className='flex flex-col gap-4'>
|
height={24}
|
||||||
<TextInput
|
className='justify-start text-sm'
|
||||||
required
|
/>
|
||||||
label='Nama'
|
Delete
|
||||||
name='name'
|
</Button>
|
||||||
placeholder='Masukkan nama user'
|
|
||||||
value={formik.values.name}
|
|
||||||
onChange={formik.handleChange}
|
|
||||||
onBlur={formik.handleBlur}
|
|
||||||
isError={formik.touched.name && Boolean(formik.errors.name)}
|
|
||||||
errorMessage={formik.errors.name}
|
|
||||||
readOnly={type === 'detail'}
|
|
||||||
/>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
{type !== 'detail' && (
|
|
||||||
<>
|
|
||||||
<div className='flex flex-row justify-end gap-2'>
|
|
||||||
<Button type='reset' color='error' className='px-4'>
|
|
||||||
Reset
|
|
||||||
</Button>
|
|
||||||
|
|
||||||
<Button
|
|
||||||
type='submit'
|
|
||||||
color='primary'
|
|
||||||
isLoading={formik.isSubmitting}
|
|
||||||
disabled={!formik.isValid || formik.isSubmitting}
|
|
||||||
className='px-4'
|
|
||||||
>
|
|
||||||
Submit
|
|
||||||
</Button>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
{uomFormErrorMessage && (
|
|
||||||
<div role='alert' className='alert alert-error'>
|
|
||||||
<Icon
|
|
||||||
icon='material-symbols:error-outline'
|
|
||||||
width={24}
|
|
||||||
height={24}
|
|
||||||
/>
|
|
||||||
<span>{uomFormErrorMessage}</span>
|
|
||||||
</div>
|
</div>
|
||||||
)}
|
)}
|
||||||
</>
|
|
||||||
)}
|
{type !== 'detail' && (
|
||||||
</form>
|
<div
|
||||||
</section>
|
className={cn('flex flex-row justify-end gap-2', {
|
||||||
|
'w-full': type === 'add',
|
||||||
|
})}
|
||||||
|
>
|
||||||
|
<Button type='reset' color='warning' className='px-4'>
|
||||||
|
Reset
|
||||||
|
</Button>
|
||||||
|
|
||||||
|
<Button
|
||||||
|
type='submit'
|
||||||
|
color='primary'
|
||||||
|
isLoading={formik.isSubmitting}
|
||||||
|
disabled={!formik.isValid || formik.isSubmitting}
|
||||||
|
className='px-4'
|
||||||
|
>
|
||||||
|
Submit
|
||||||
|
</Button>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{uomFormErrorMessage && (
|
||||||
|
<div role='alert' className='alert alert-error'>
|
||||||
|
<Icon
|
||||||
|
icon='material-symbols:error-outline'
|
||||||
|
width={24}
|
||||||
|
height={24}
|
||||||
|
/>
|
||||||
|
<span>{uomFormErrorMessage}</span>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
</form>
|
||||||
|
</section>
|
||||||
|
|
||||||
|
{type !== 'add' && (
|
||||||
|
<ConfirmationModal
|
||||||
|
ref={deleteModal.ref}
|
||||||
|
type='error'
|
||||||
|
text={`Apakah anda yakin ingin menghapus data UOM ini (${initialValues?.name})?`}
|
||||||
|
secondaryButton={{
|
||||||
|
text: 'Tidak',
|
||||||
|
}}
|
||||||
|
primaryButton={{
|
||||||
|
text: 'Ya',
|
||||||
|
color: 'error',
|
||||||
|
isLoading: isDeleteLoading,
|
||||||
|
onClick: confirmationModalDeleteClickHandler,
|
||||||
|
}}
|
||||||
|
/>
|
||||||
|
)}
|
||||||
|
</>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user