mirror of
https://gitlab.com/mbugroup/lti-web-client.git
synced 2026-05-25 07:45:47 +00:00
feat(FE-40,41): create UomForm component
This commit is contained in:
@@ -0,0 +1,174 @@
|
|||||||
|
'use client';
|
||||||
|
|
||||||
|
import { useCallback, useEffect, useMemo, useState } from 'react';
|
||||||
|
import { useRouter } from 'next/navigation';
|
||||||
|
import { useFormik } from 'formik';
|
||||||
|
import { toast } from 'react-hot-toast';
|
||||||
|
|
||||||
|
import { Icon } from '@iconify/react';
|
||||||
|
import Button from '@/components/Button';
|
||||||
|
import TextInput from '@/components/input/TextInput';
|
||||||
|
|
||||||
|
import {
|
||||||
|
UomFormSchema,
|
||||||
|
UomFormValues,
|
||||||
|
UpdateUomFormSchema,
|
||||||
|
} from '@/components/pages/master-data/uom/form/UomForm.schema';
|
||||||
|
import { isResponseError } from '@/lib/api-helper';
|
||||||
|
import {
|
||||||
|
CreateUomPayload,
|
||||||
|
Uom,
|
||||||
|
UpdateUomPayload,
|
||||||
|
} from '@/types/api/master-data/uom';
|
||||||
|
import { UomApi } from '@/services/api/master-data';
|
||||||
|
|
||||||
|
interface UomFormProps {
|
||||||
|
type?: 'add' | 'edit' | 'detail';
|
||||||
|
initialValues?: Uom;
|
||||||
|
}
|
||||||
|
|
||||||
|
const UomForm = ({ type = 'add', initialValues }: UomFormProps) => {
|
||||||
|
const router = useRouter();
|
||||||
|
|
||||||
|
const [uomFormErrorMessage, setUomFormErrorMessage] = useState('');
|
||||||
|
|
||||||
|
const createUomHandler = useCallback(
|
||||||
|
async (payload: CreateUomPayload) => {
|
||||||
|
const createUomRes = await UomApi.create(payload);
|
||||||
|
|
||||||
|
if (isResponseError(createUomRes)) {
|
||||||
|
setUomFormErrorMessage(createUomRes.message);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
toast.success(createUomRes?.message as string);
|
||||||
|
router.push('/master-data/uom');
|
||||||
|
},
|
||||||
|
[router]
|
||||||
|
);
|
||||||
|
|
||||||
|
const updateUomHandler = useCallback(
|
||||||
|
async (uomId: number, payload: UpdateUomPayload) => {
|
||||||
|
const updateUomRes = await UomApi.update(uomId, payload);
|
||||||
|
|
||||||
|
if (updateUomRes?.status === 'error') {
|
||||||
|
setUomFormErrorMessage(updateUomRes.message);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
toast.success(updateUomRes?.message as string);
|
||||||
|
router.refresh();
|
||||||
|
router.push('/master-data/uom');
|
||||||
|
},
|
||||||
|
[router]
|
||||||
|
);
|
||||||
|
|
||||||
|
const formikInitialValues = useMemo<UomFormValues>(() => {
|
||||||
|
return {
|
||||||
|
name: initialValues?.name ?? '',
|
||||||
|
};
|
||||||
|
}, [initialValues]);
|
||||||
|
|
||||||
|
const formik = useFormik<UomFormValues>({
|
||||||
|
initialValues: formikInitialValues,
|
||||||
|
validationSchema: type === 'edit' ? UpdateUomFormSchema : UomFormSchema,
|
||||||
|
onSubmit: async (values) => {
|
||||||
|
setUomFormErrorMessage('');
|
||||||
|
|
||||||
|
const uomPayload: CreateUomPayload = {
|
||||||
|
name: values.name,
|
||||||
|
};
|
||||||
|
|
||||||
|
switch (type) {
|
||||||
|
case 'add':
|
||||||
|
await createUomHandler(uomPayload);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 'edit':
|
||||||
|
await updateUomHandler(initialValues?.id as number, uomPayload);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
const { setValues: formikSetValues } = formik;
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
formikSetValues(formikInitialValues);
|
||||||
|
}, [formikSetValues, formikInitialValues]);
|
||||||
|
|
||||||
|
return (
|
||||||
|
<section className='w-full max-w-xl'>
|
||||||
|
<header className='flex flex-col gap-4'>
|
||||||
|
<Button
|
||||||
|
href='/master-data/uom'
|
||||||
|
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'
|
||||||
|
>
|
||||||
|
<div className='flex flex-col gap-4'>
|
||||||
|
<TextInput
|
||||||
|
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>
|
||||||
|
|
||||||
|
{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>
|
||||||
|
)}
|
||||||
|
</>
|
||||||
|
)}
|
||||||
|
</form>
|
||||||
|
</section>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
export default UomForm;
|
||||||
Reference in New Issue
Block a user