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',
},
{
id: 'file-name',
value: 'file-name',
id: 'documents-name',
value: 'documents-name',
label: 'File Uniformity',
},
{
@@ -123,7 +123,7 @@ const UniformityDetail: React.FC<UniformityDetailProps> = ({
'lokasi-farm': info_umum.lokasi_farm,
'project-flock': info_umum.project_flock,
kandang: info_umum.kandang,
'file-name': info_umum.file_name,
'documents-name': info_umum.documents_name,
'approval-status': statusValue,
};
@@ -148,7 +148,7 @@ const UniformityDetail: React.FC<UniformityDetailProps> = ({
return <span>-</span>;
}
if (id === 'file-name') {
if (id === 'documents-name') {
return (
<div className='flex items-center gap-2'>
<span>{valueMap[id]}</span>
@@ -214,7 +214,7 @@ const UniformityDetailsPreview = ({
{/* Header */}
<DrawerHeader
leftIcon=''
subtitle={info_umum?.file_name ?? 'Uniformity Details'}
subtitle={info_umum?.documents_name ?? 'Uniformity Details'}
subtitleClassName='text-sm text-neutral line-clamp-1'
showDivider={false}
>
@@ -20,16 +20,16 @@ type UniformityFormSchemaType = {
label: string;
} | null;
kandang_id: number;
file: File | undefined;
documents: File | undefined;
};
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 instanceof File) return value.size <= 2 * 1024 * 1024;
return false;
})
.test('fileType', 'Format file harus Excel', (value): boolean => {
.test('documentsType', 'Format file harus Excel', (value): boolean => {
if (!value) return true;
if (value instanceof File) {
const allowedTypes = [
@@ -74,7 +74,7 @@ export const UniformityFormSchema: Yup.ObjectSchema<UniformityFormSchemaType> =
.min(1, 'Kandang wajib diisi!')
.required('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>;
@@ -83,8 +83,8 @@ export type UniformityFormData = {
date: string;
week: number;
project_flock_kandang_id: number;
file: File | null;
file_name: string;
documents: File | null;
documents_name: string;
};
export const getUniformityFormInitialValues = (
@@ -115,6 +115,6 @@ export const getUniformityFormInitialValues = (
}
: null,
kandang_id: initialValues?.kandang?.id ?? 0,
file: undefined,
documents: undefined,
};
};
@@ -246,15 +246,12 @@ const UniformityForm = ({
date: values.date,
week: values.week,
project_flock_kandang_id: projectFlockKandangId,
file: values.file as File,
file_name: (values.file as File).name,
documents: values.documents as File,
documents_name: (values.documents as File).name,
});
const payload: VerifyUniformityPayload = {
date: values.date,
week: values.week,
project_flock_kandang_id: projectFlockKandangId,
file: values.file as File,
documents: values.documents as File,
};
const res = await UniformityApi.verifyUniformity(payload);
@@ -328,17 +325,17 @@ const UniformityForm = ({
const handleFileChange = useCallback(
(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) {
formik.setFieldValue('file', undefined);
if (!documents) {
formik.setFieldValue('documents', undefined);
return;
}
if (file.size > 2 * 1024 * 1024) {
toast.error(`Ukuran file ${file.name} maksimal 2 MB!`);
if (documents.size > 2 * 1024 * 1024) {
toast.error(`Ukuran file ${documents.name} maksimal 2 MB!`);
return;
}
@@ -348,12 +345,12 @@ const UniformityForm = ({
'text/csv',
];
if (!allowedTypes.includes(file.type)) {
toast.error(`Format file ${file.name} harus Excel atau CSV!`);
if (!allowedTypes.includes(documents.type)) {
toast.error(`Format file ${documents.name} harus Excel atau CSV!`);
return;
}
formik.setFieldValue('file', file);
formik.setFieldValue('documents', documents);
},
[]
);
@@ -366,7 +363,7 @@ const UniformityForm = ({
);
const handleRemoveFile = useCallback(() => {
formik.setFieldValue('file', undefined);
formik.setFieldValue('documents', undefined);
if (fileInputRef.current) {
fileInputRef.current.value = '';
}
@@ -531,12 +528,14 @@ const UniformityForm = ({
htmlFor='file-upload-input'
className={cn(
"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
</label>
{formik.values.file && !isNextStep ? (
{formik.values.documents && !isNextStep ? (
<button
onClick={handleRemoveFile}
className='cursor-pointer'
@@ -549,7 +548,7 @@ const UniformityForm = ({
className='text-gray-400 hover:text-gray-600'
/>
</button>
) : !formik.values.file && !isNextStep ? (
) : !formik.values.documents && !isNextStep ? (
<button className='cursor-pointer' type='button'>
<Tooltip
position='left'
@@ -569,7 +568,7 @@ const UniformityForm = ({
<section
className={cn(
'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-gray-300'
)}
@@ -577,7 +576,7 @@ const UniformityForm = ({
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='border border-[#18181B]/25 rounded-2xl p-1 flex items-center justify-center'>
<Button
@@ -595,7 +594,7 @@ const UniformityForm = ({
</Button>
</div>
<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>
</div>
) : (
@@ -668,15 +667,15 @@ const UniformityForm = ({
ref={fileInputRef}
type='file'
id='file-upload-input'
name='file'
name='documents'
accept='application/vnd.ms-excel,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet,text/csv'
onChange={handleFileChange}
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'>
{formik.errors.file as string}
{formik.errors.documents as string}
</p>
)}
</div>
@@ -51,7 +51,7 @@ const UniformityResultForm = () => {
};
const handleSubmit = async () => {
if (!uniformityFormData || !uniformityFormData.file) {
if (!uniformityFormData || !uniformityFormData.documents) {
toast.error('Form data is missing. Please try again.');
return;
}
@@ -63,7 +63,7 @@ const UniformityResultForm = () => {
date: uniformityFormData.date,
week: uniformityFormData.week,
project_flock_kandang_id: uniformityFormData.project_flock_kandang_id,
file: uniformityFormData.file,
documents: uniformityFormData.documents,
};
const res = await UniformityApi.createUniformity(payload);
@@ -236,7 +236,7 @@ const UniformityResultForm = () => {
{/* Header */}
<DrawerHeader
leftIcon=''
subtitle={uniformityFormData?.file_name || 'Uniformity Result'}
subtitle={uniformityFormData?.documents_name || 'Uniformity Result'}
subtitleClassName='text-sm text-neutral line-clamp-1'
showDivider={false}
>
+4 -11
View File
@@ -47,8 +47,8 @@ export class UniformityApiService extends BaseApiService<
payload.project_flock_kandang_id.toString()
);
if (payload.file) {
formData.append('file', payload.file);
if (payload.documents) {
formData.append('documents', payload.documents);
}
return await this.create(formData as unknown as CreateUniformityPayload);
@@ -58,15 +58,8 @@ export class UniformityApiService extends BaseApiService<
payload: VerifyUniformityPayload
): Promise<BaseApiResponse<VerifyUniformityResponse> | undefined> {
const formData = new FormData();
formData.append('date', payload.date);
formData.append('week', payload.week.toString());
formData.append(
'project_flock_kandang_id',
payload.project_flock_kandang_id.toString()
);
if (payload.file) {
formData.append('file', payload.file);
if (payload.documents) {
formData.append('documents', payload.documents);
}
return await this.customRequest<BaseApiResponse<VerifyUniformityResponse>>(
+3 -6
View File
@@ -34,7 +34,7 @@ export type UniformityInfoUmum = {
lokasi_farm: string;
project_flock: string;
kandang: string;
file_name: string;
documents_name: string;
};
export type UniformitySampling = {
@@ -77,15 +77,12 @@ export type VerifyUniformityResponse = {
export type CreateUniformityPayload = {
date: string;
project_flock_kandang_id: number;
file: File;
documents: File;
week: number;
};
export type VerifyUniformityPayload = {
date: string;
project_flock_kandang_id: number;
file: File;
week: number;
documents: File;
};
// ==================== OTHER TYPES ====================