refactor(FE-316): Rename file to documents in uniformity feature

This commit is contained in:
rstubryan
2025-12-30 09:21:38 +07:00
parent 2e44371c6c
commit d6849a48d2
7 changed files with 46 additions and 57 deletions
@@ -90,8 +90,8 @@ const UniformityDetail: React.FC<UniformityDetailProps> = ({
label: 'Kandang', label: 'Kandang',
}, },
{ {
id: 'file-name', id: 'documents-name',
value: 'file-name', value: 'documents-name',
label: 'File Uniformity', label: 'File Uniformity',
}, },
{ {
@@ -123,7 +123,7 @@ const UniformityDetail: React.FC<UniformityDetailProps> = ({
'lokasi-farm': info_umum.lokasi_farm, 'lokasi-farm': info_umum.lokasi_farm,
'project-flock': info_umum.project_flock, 'project-flock': info_umum.project_flock,
kandang: info_umum.kandang, kandang: info_umum.kandang,
'file-name': info_umum.file_name, 'documents-name': info_umum.documents_name,
'approval-status': statusValue, 'approval-status': statusValue,
}; };
@@ -148,7 +148,7 @@ const UniformityDetail: React.FC<UniformityDetailProps> = ({
return <span>-</span>; return <span>-</span>;
} }
if (id === 'file-name') { if (id === 'documents-name') {
return ( return (
<div className='flex items-center gap-2'> <div className='flex items-center gap-2'>
<span>{valueMap[id]}</span> <span>{valueMap[id]}</span>
@@ -214,7 +214,7 @@ const UniformityDetailsPreview = ({
{/* Header */} {/* Header */}
<DrawerHeader <DrawerHeader
leftIcon='' leftIcon=''
subtitle={info_umum?.file_name ?? 'Uniformity Details'} subtitle={info_umum?.documents_name ?? 'Uniformity Details'}
subtitleClassName='text-sm text-neutral line-clamp-1' subtitleClassName='text-sm text-neutral line-clamp-1'
showDivider={false} showDivider={false}
> >
@@ -20,16 +20,16 @@ type UniformityFormSchemaType = {
label: string; label: string;
} | null; } | null;
kandang_id: number; kandang_id: number;
file: File | undefined; documents: File | undefined;
}; };
const FileSchema = Yup.mixed<File>() const FileSchema = Yup.mixed<File>()
.test('fileSize', 'Ukuran file maksimal 2 MB', (value): boolean => { .test('documentsSize', 'Ukuran file maksimal 2 MB', (value): boolean => {
if (!value) return true; if (!value) return true;
if (value instanceof File) return value.size <= 2 * 1024 * 1024; if (value instanceof File) return value.size <= 2 * 1024 * 1024;
return false; return false;
}) })
.test('fileType', 'Format file harus Excel', (value): boolean => { .test('documentsType', 'Format file harus Excel', (value): boolean => {
if (!value) return true; if (!value) return true;
if (value instanceof File) { if (value instanceof File) {
const allowedTypes = [ const allowedTypes = [
@@ -74,7 +74,7 @@ export const UniformityFormSchema: Yup.ObjectSchema<UniformityFormSchemaType> =
.min(1, 'Kandang wajib diisi!') .min(1, 'Kandang wajib diisi!')
.required('Kandang wajib diisi!') .required('Kandang wajib diisi!')
.typeError('Kandang wajib diisi!'), .typeError('Kandang wajib diisi!'),
file: FileSchema.required('File wajib diisi!'), documents: FileSchema.required('File wajib diisi!'),
}); });
export type UniformityFormValues = Yup.InferType<typeof UniformityFormSchema>; export type UniformityFormValues = Yup.InferType<typeof UniformityFormSchema>;
@@ -83,8 +83,8 @@ export type UniformityFormData = {
date: string; date: string;
week: number; week: number;
project_flock_kandang_id: number; project_flock_kandang_id: number;
file: File | null; documents: File | null;
file_name: string; documents_name: string;
}; };
export const getUniformityFormInitialValues = ( export const getUniformityFormInitialValues = (
@@ -115,6 +115,6 @@ export const getUniformityFormInitialValues = (
} }
: null, : null,
kandang_id: initialValues?.kandang?.id ?? 0, kandang_id: initialValues?.kandang?.id ?? 0,
file: undefined, documents: undefined,
}; };
}; };
@@ -246,15 +246,12 @@ const UniformityForm = ({
date: values.date, date: values.date,
week: values.week, week: values.week,
project_flock_kandang_id: projectFlockKandangId, project_flock_kandang_id: projectFlockKandangId,
file: values.file as File, documents: values.documents as File,
file_name: (values.file as File).name, documents_name: (values.documents as File).name,
}); });
const payload: VerifyUniformityPayload = { const payload: VerifyUniformityPayload = {
date: values.date, documents: values.documents as File,
week: values.week,
project_flock_kandang_id: projectFlockKandangId,
file: values.file as File,
}; };
const res = await UniformityApi.verifyUniformity(payload); const res = await UniformityApi.verifyUniformity(payload);
@@ -328,17 +325,17 @@ const UniformityForm = ({
const handleFileChange = useCallback( const handleFileChange = useCallback(
(e: React.ChangeEvent<HTMLInputElement>) => { (e: React.ChangeEvent<HTMLInputElement>) => {
const file = e.target.files?.[0]; const documents = e.target.files?.[0];
formik.setFieldTouched('file', true); formik.setFieldTouched('documents', true);
if (!file) { if (!documents) {
formik.setFieldValue('file', undefined); formik.setFieldValue('documents', undefined);
return; return;
} }
if (file.size > 2 * 1024 * 1024) { if (documents.size > 2 * 1024 * 1024) {
toast.error(`Ukuran file ${file.name} maksimal 2 MB!`); toast.error(`Ukuran file ${documents.name} maksimal 2 MB!`);
return; return;
} }
@@ -348,12 +345,12 @@ const UniformityForm = ({
'text/csv', 'text/csv',
]; ];
if (!allowedTypes.includes(file.type)) { if (!allowedTypes.includes(documents.type)) {
toast.error(`Format file ${file.name} harus Excel atau CSV!`); toast.error(`Format file ${documents.name} harus Excel atau CSV!`);
return; return;
} }
formik.setFieldValue('file', file); formik.setFieldValue('documents', documents);
}, },
[] []
); );
@@ -366,7 +363,7 @@ const UniformityForm = ({
); );
const handleRemoveFile = useCallback(() => { const handleRemoveFile = useCallback(() => {
formik.setFieldValue('file', undefined); formik.setFieldValue('documents', undefined);
if (fileInputRef.current) { if (fileInputRef.current) {
fileInputRef.current.value = ''; fileInputRef.current.value = '';
} }
@@ -531,12 +528,14 @@ const UniformityForm = ({
htmlFor='file-upload-input' htmlFor='file-upload-input'
className={cn( className={cn(
"w-full text-sm font-normal leading-5 after:content-['*'] after:ml-0.5 after:text-red-500", "w-full text-sm font-normal leading-5 after:content-['*'] after:ml-0.5 after:text-red-500",
formik.touched.file && formik.errors.file && 'text-red-500' formik.touched.documents &&
formik.errors.documents &&
'text-red-500'
)} )}
> >
Upload File Upload File
</label> </label>
{formik.values.file && !isNextStep ? ( {formik.values.documents && !isNextStep ? (
<button <button
onClick={handleRemoveFile} onClick={handleRemoveFile}
className='cursor-pointer' className='cursor-pointer'
@@ -549,7 +548,7 @@ const UniformityForm = ({
className='text-gray-400 hover:text-gray-600' className='text-gray-400 hover:text-gray-600'
/> />
</button> </button>
) : !formik.values.file && !isNextStep ? ( ) : !formik.values.documents && !isNextStep ? (
<button className='cursor-pointer' type='button'> <button className='cursor-pointer' type='button'>
<Tooltip <Tooltip
position='left' position='left'
@@ -569,7 +568,7 @@ const UniformityForm = ({
<section <section
className={cn( className={cn(
'h-full w-full border rounded-2xl border-dashed cursor-pointer mt-2', 'h-full w-full border rounded-2xl border-dashed cursor-pointer mt-2',
formik.touched.file && formik.errors.file formik.touched.documents && formik.errors.documents
? 'border-red-500' ? 'border-red-500'
: 'border-gray-300' : 'border-gray-300'
)} )}
@@ -577,7 +576,7 @@ const UniformityForm = ({
document.getElementById('file-upload-input')?.click() document.getElementById('file-upload-input')?.click()
} }
> >
{formik.values.file ? ( {formik.values.documents ? (
<div className='flex flex-col items-center justify-center gap-2 my-10'> <div className='flex flex-col items-center justify-center gap-2 my-10'>
<div className='border border-[#18181B]/25 rounded-2xl p-1 flex items-center justify-center'> <div className='border border-[#18181B]/25 rounded-2xl p-1 flex items-center justify-center'>
<Button <Button
@@ -595,7 +594,7 @@ const UniformityForm = ({
</Button> </Button>
</div> </div>
<span className='text-md font-semibold text-black line-clamp-2 text-center max-w-xs break-all'> <span className='text-md font-semibold text-black line-clamp-2 text-center max-w-xs break-all'>
{formik.values.file.name} {formik.values.documents.name}
</span> </span>
</div> </div>
) : ( ) : (
@@ -668,15 +667,15 @@ const UniformityForm = ({
ref={fileInputRef} ref={fileInputRef}
type='file' type='file'
id='file-upload-input' id='file-upload-input'
name='file' name='documents'
accept='application/vnd.ms-excel,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet,text/csv' accept='application/vnd.ms-excel,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet,text/csv'
onChange={handleFileChange} onChange={handleFileChange}
className='hidden' className='hidden'
/> />
{formik.touched.file && formik.errors.file && ( {formik.touched.documents && formik.errors.documents && (
<p className='w-full text-sm text-red-500 mt-2'> <p className='w-full text-sm text-red-500 mt-2'>
{formik.errors.file as string} {formik.errors.documents as string}
</p> </p>
)} )}
</div> </div>
@@ -51,7 +51,7 @@ const UniformityResultForm = () => {
}; };
const handleSubmit = async () => { const handleSubmit = async () => {
if (!uniformityFormData || !uniformityFormData.file) { if (!uniformityFormData || !uniformityFormData.documents) {
toast.error('Form data is missing. Please try again.'); toast.error('Form data is missing. Please try again.');
return; return;
} }
@@ -63,7 +63,7 @@ const UniformityResultForm = () => {
date: uniformityFormData.date, date: uniformityFormData.date,
week: uniformityFormData.week, week: uniformityFormData.week,
project_flock_kandang_id: uniformityFormData.project_flock_kandang_id, project_flock_kandang_id: uniformityFormData.project_flock_kandang_id,
file: uniformityFormData.file, documents: uniformityFormData.documents,
}; };
const res = await UniformityApi.createUniformity(payload); const res = await UniformityApi.createUniformity(payload);
@@ -236,7 +236,7 @@ const UniformityResultForm = () => {
{/* Header */} {/* Header */}
<DrawerHeader <DrawerHeader
leftIcon='' leftIcon=''
subtitle={uniformityFormData?.file_name || 'Uniformity Result'} subtitle={uniformityFormData?.documents_name || 'Uniformity Result'}
subtitleClassName='text-sm text-neutral line-clamp-1' subtitleClassName='text-sm text-neutral line-clamp-1'
showDivider={false} showDivider={false}
> >
+4 -11
View File
@@ -47,8 +47,8 @@ export class UniformityApiService extends BaseApiService<
payload.project_flock_kandang_id.toString() payload.project_flock_kandang_id.toString()
); );
if (payload.file) { if (payload.documents) {
formData.append('file', payload.file); formData.append('documents', payload.documents);
} }
return await this.create(formData as unknown as CreateUniformityPayload); return await this.create(formData as unknown as CreateUniformityPayload);
@@ -58,15 +58,8 @@ export class UniformityApiService extends BaseApiService<
payload: VerifyUniformityPayload payload: VerifyUniformityPayload
): Promise<BaseApiResponse<VerifyUniformityResponse> | undefined> { ): Promise<BaseApiResponse<VerifyUniformityResponse> | undefined> {
const formData = new FormData(); const formData = new FormData();
formData.append('date', payload.date); if (payload.documents) {
formData.append('week', payload.week.toString()); formData.append('documents', payload.documents);
formData.append(
'project_flock_kandang_id',
payload.project_flock_kandang_id.toString()
);
if (payload.file) {
formData.append('file', payload.file);
} }
return await this.customRequest<BaseApiResponse<VerifyUniformityResponse>>( return await this.customRequest<BaseApiResponse<VerifyUniformityResponse>>(
+3 -6
View File
@@ -34,7 +34,7 @@ export type UniformityInfoUmum = {
lokasi_farm: string; lokasi_farm: string;
project_flock: string; project_flock: string;
kandang: string; kandang: string;
file_name: string; documents_name: string;
}; };
export type UniformitySampling = { export type UniformitySampling = {
@@ -77,15 +77,12 @@ export type VerifyUniformityResponse = {
export type CreateUniformityPayload = { export type CreateUniformityPayload = {
date: string; date: string;
project_flock_kandang_id: number; project_flock_kandang_id: number;
file: File; documents: File;
week: number; week: number;
}; };
export type VerifyUniformityPayload = { export type VerifyUniformityPayload = {
date: string; documents: File;
project_flock_kandang_id: number;
file: File;
week: number;
}; };
// ==================== OTHER TYPES ==================== // ==================== OTHER TYPES ====================