mirror of
https://gitlab.com/mbugroup/lti-web-client.git
synced 2026-05-25 07:45:47 +00:00
refactor(FE-Storyless): enhance ProductCategoryForm schema and validation, improve UI text and layout
This commit is contained in:
+12
-4
@@ -1,9 +1,17 @@
|
|||||||
import * as Yup from 'yup';
|
import * as Yup from 'yup';
|
||||||
|
|
||||||
export const ProductCategoryFormSchema = Yup.object({
|
type ProductCategoryFormSchemaType = {
|
||||||
code: Yup.string().required('Kode wajib diisi!').max(3, 'Kode kategori produk melebihi 3 karakter!'),
|
code: string;
|
||||||
name: Yup.string().required('Nama wajib diisi!'),
|
name: string;
|
||||||
});
|
};
|
||||||
|
|
||||||
|
export const ProductCategoryFormSchema: Yup.ObjectSchema<ProductCategoryFormSchemaType> =
|
||||||
|
Yup.object({
|
||||||
|
code: Yup.string()
|
||||||
|
.required('Kode wajib diisi!')
|
||||||
|
.max(3, 'Kode kategori produk melebihi 3 karakter!'),
|
||||||
|
name: Yup.string().required('Nama wajib diisi!'),
|
||||||
|
});
|
||||||
|
|
||||||
export const UpdateProductCategoryFormSchema = ProductCategoryFormSchema;
|
export const UpdateProductCategoryFormSchema = ProductCategoryFormSchema;
|
||||||
|
|
||||||
|
|||||||
@@ -30,7 +30,10 @@ interface ProductCategoryFormProps {
|
|||||||
initialValues?: ProductCategory;
|
initialValues?: ProductCategory;
|
||||||
}
|
}
|
||||||
|
|
||||||
const ProductCategoryForm = ({ type = 'add', initialValues }: ProductCategoryFormProps) => {
|
const ProductCategoryForm = ({
|
||||||
|
type = 'add',
|
||||||
|
initialValues,
|
||||||
|
}: ProductCategoryFormProps) => {
|
||||||
const router = useRouter();
|
const router = useRouter();
|
||||||
const deleteModal = useModal();
|
const deleteModal = useModal();
|
||||||
|
|
||||||
@@ -68,16 +71,20 @@ const ProductCategoryForm = ({ type = 'add', initialValues }: ProductCategoryFor
|
|||||||
[router]
|
[router]
|
||||||
);
|
);
|
||||||
|
|
||||||
const formikInitialValues = useMemo<ProductCategoryFormValues>(() => {
|
const formikInitialValues = useMemo<ProductCategoryFormValues>(
|
||||||
return {
|
() => ({
|
||||||
code: initialValues?.code ?? '',
|
code: initialValues?.code ?? '',
|
||||||
name: initialValues?.name ?? '',
|
name: initialValues?.name ?? '',
|
||||||
};
|
}),
|
||||||
}, [initialValues]);
|
[initialValues]
|
||||||
|
);
|
||||||
|
|
||||||
const formik = useFormik<ProductCategoryFormValues>({
|
const formik = useFormik<ProductCategoryFormValues>({
|
||||||
initialValues: formikInitialValues,
|
initialValues: formikInitialValues,
|
||||||
validationSchema: type === 'edit' ? UpdateProductCategoryFormSchema : ProductCategoryFormSchema,
|
validationSchema:
|
||||||
|
type === 'edit'
|
||||||
|
? UpdateProductCategoryFormSchema
|
||||||
|
: ProductCategoryFormSchema,
|
||||||
onSubmit: async (values) => {
|
onSubmit: async (values) => {
|
||||||
setFormErrorMessage('');
|
setFormErrorMessage('');
|
||||||
|
|
||||||
@@ -91,7 +98,10 @@ const ProductCategoryForm = ({ type = 'add', initialValues }: ProductCategoryFor
|
|||||||
await createProductCategoryHandler(payload);
|
await createProductCategoryHandler(payload);
|
||||||
break;
|
break;
|
||||||
case 'edit':
|
case 'edit':
|
||||||
await updateProductCategoryHandler(initialValues?.id as number, payload);
|
await updateProductCategoryHandler(
|
||||||
|
initialValues?.id as number,
|
||||||
|
payload
|
||||||
|
);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
@@ -109,7 +119,7 @@ const ProductCategoryForm = ({ type = 'add', initialValues }: ProductCategoryFor
|
|||||||
await ProductCategoryApi.delete(initialValues?.id as number);
|
await ProductCategoryApi.delete(initialValues?.id as number);
|
||||||
|
|
||||||
deleteModal.closeModal();
|
deleteModal.closeModal();
|
||||||
toast.success('Successfully delete Product Category!');
|
toast.success('Berhasil menghapus data Kategori Produk!');
|
||||||
setIsDeleteLoading(false);
|
setIsDeleteLoading(false);
|
||||||
router.push('/master-data/product-category');
|
router.push('/master-data/product-category');
|
||||||
};
|
};
|
||||||
@@ -120,7 +130,7 @@ const ProductCategoryForm = ({ type = 'add', initialValues }: ProductCategoryFor
|
|||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<section className='w-full max-w-xl'>
|
<section className='w-full max-w-2xl'>
|
||||||
<header className='flex flex-col gap-4'>
|
<header className='flex flex-col gap-4'>
|
||||||
<Button
|
<Button
|
||||||
href='/master-data/product-category'
|
href='/master-data/product-category'
|
||||||
@@ -132,9 +142,9 @@ const ProductCategoryForm = ({ type = 'add', initialValues }: ProductCategoryFor
|
|||||||
</Button>
|
</Button>
|
||||||
|
|
||||||
<h1 className='text-2xl font-bold text-center'>
|
<h1 className='text-2xl font-bold text-center'>
|
||||||
{type === 'add' && 'Tambah Product Category'}
|
{type === 'add' && 'Tambah Kategori Produk'}
|
||||||
{type === 'edit' && 'Edit Product Category'}
|
{type === 'edit' && 'Edit Kategori Produk'}
|
||||||
{type === 'detail' && 'Detail Product Category'}
|
{type === 'detail' && 'Detail Kategori Produk'}
|
||||||
</h1>
|
</h1>
|
||||||
</header>
|
</header>
|
||||||
|
|
||||||
@@ -148,7 +158,7 @@ const ProductCategoryForm = ({ type = 'add', initialValues }: ProductCategoryFor
|
|||||||
required
|
required
|
||||||
label='Kode'
|
label='Kode'
|
||||||
name='code'
|
name='code'
|
||||||
placeholder='Masukkan kode kategori produk'
|
placeholder='Masukkan kode...'
|
||||||
value={formik.values.code}
|
value={formik.values.code}
|
||||||
onChange={formik.handleChange}
|
onChange={formik.handleChange}
|
||||||
onBlur={formik.handleBlur}
|
onBlur={formik.handleBlur}
|
||||||
@@ -160,7 +170,7 @@ const ProductCategoryForm = ({ type = 'add', initialValues }: ProductCategoryFor
|
|||||||
required
|
required
|
||||||
label='Nama'
|
label='Nama'
|
||||||
name='name'
|
name='name'
|
||||||
placeholder='Masukkan nama kategori produk'
|
placeholder='Masukkan nama...'
|
||||||
value={formik.values.name}
|
value={formik.values.name}
|
||||||
onChange={formik.handleChange}
|
onChange={formik.handleChange}
|
||||||
onBlur={formik.handleBlur}
|
onBlur={formik.handleBlur}
|
||||||
@@ -247,7 +257,7 @@ const ProductCategoryForm = ({ type = 'add', initialValues }: ProductCategoryFor
|
|||||||
<ConfirmationModal
|
<ConfirmationModal
|
||||||
ref={deleteModal.ref}
|
ref={deleteModal.ref}
|
||||||
type='error'
|
type='error'
|
||||||
text={`Apakah anda yakin ingin menghapus data Product Category ini (${initialValues?.name})?`}
|
text={`Apakah anda yakin ingin menghapus data Kategori Produk ini (${initialValues?.name})?`}
|
||||||
secondaryButton={{
|
secondaryButton={{
|
||||||
text: 'Tidak',
|
text: 'Tidak',
|
||||||
}}
|
}}
|
||||||
@@ -263,4 +273,4 @@ const ProductCategoryForm = ({ type = 'add', initialValues }: ProductCategoryFor
|
|||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
export default ProductCategoryForm;
|
export default ProductCategoryForm;
|
||||||
|
|||||||
Reference in New Issue
Block a user