From 157dfc75ed4317df8b5b1118c7cdbe701342c1a4 Mon Sep 17 00:00:00 2001 From: rstubryan Date: Thu, 16 Oct 2025 15:47:29 +0700 Subject: [PATCH] refactor(FE-63): remove debug logging from form submission and movement handlers --- .../inventory/movement/form/MovementForm.tsx | 27 ------------ .../movement/form/useMovementFormHandlers.ts | 42 ------------------- src/services/http/client.ts | 5 ++- 3 files changed, 4 insertions(+), 70 deletions(-) diff --git a/src/components/pages/inventory/movement/form/MovementForm.tsx b/src/components/pages/inventory/movement/form/MovementForm.tsx index 898a1d56..a6a75394 100644 --- a/src/components/pages/inventory/movement/form/MovementForm.tsx +++ b/src/components/pages/inventory/movement/form/MovementForm.tsx @@ -68,27 +68,15 @@ const MovementForm = ({ type = 'add', initialValues }: MovementFormProps) => { validateOnMount: false, enableReinitialize: true, onSubmit: async (values) => { - console.log('=== FORM SUBMIT DEBUG ==='); - console.log('1. Form values received:', values); - setMovementFormErrorMessage(''); const documents: File[] = []; const deliveriesPayload = values.deliveries.map((d, idx) => { let documentIndex = 0; - console.log(`2. Processing delivery ${idx}:`, { - driver_name: d.driver_name, - document: d.document, - documentType: d.document instanceof File ? 'File' : typeof d.document, - documentSize: d.document instanceof File ? d.document.size : 'N/A', - }); - if (d.document && d.document instanceof File) { documents.push(d.document); documentIndex = documents.length - 1; - console.log(` → Document added at index ${documentIndex}`); } else { - console.log(` → No document for delivery ${idx}, using index 0`); } return { @@ -117,21 +105,6 @@ const MovementForm = ({ type = 'add', initialValues }: MovementFormProps) => { deliveries: deliveriesPayload, }; - console.log('3. Final payload structure:', { - ...payload, - }); - - console.log( - '4. Document indices in deliveries:', - deliveriesPayload.map((d, i) => ({ - delivery: i, - document_index: d.document_index, - })) - ); - - console.log('5. Total documents:', documents.length); - console.log('=== END SUBMIT DEBUG ==='); - switch (type) { case 'add': await createMovementHandler(payload, documents); diff --git a/src/components/pages/inventory/movement/form/useMovementFormHandlers.ts b/src/components/pages/inventory/movement/form/useMovementFormHandlers.ts index 3f46b71a..5c3d80d1 100644 --- a/src/components/pages/inventory/movement/form/useMovementFormHandlers.ts +++ b/src/components/pages/inventory/movement/form/useMovementFormHandlers.ts @@ -17,40 +17,20 @@ export const useMovementFormHandlers = (initialValuesId?: number) => { const createMovementHandler = useCallback( async (payload: CreateMovementPayload, documents: File[] = []) => { - console.log('=== CREATE HANDLER DEBUG ==='); - console.log('1. Received payload:', payload); - console.log('2. Documents count:', documents.length); - let finalPayload: CreateMovementPayload | FormData; if (documents.length > 0) { - console.log('3. Creating FormData (has documents)'); const formData = new FormData(); formData.append('data', JSON.stringify(payload)); documents.forEach((file, index) => { formData.append(`documents[${index}]`, file); }); - console.log('4. FormData entries:'); - for (const [key, value] of formData.entries()) { - if (value instanceof File) { - console.log( - ` ${key}: [File] ${value.name} (${value.size} bytes)` - ); - } else { - console.log(` ${key}: ${value}`); - } - } - finalPayload = formData as unknown as CreateMovementPayload; } else { - console.log('3. Sending as JSON (no documents)'); - console.log('4. Payload:', JSON.stringify(payload, null, 2)); finalPayload = payload; } - console.log('=== END CREATE HANDLER DEBUG ==='); - const res = await MovementApi.create(finalPayload); if (isResponseError(res)) { console.error('API Error:', res); @@ -69,44 +49,22 @@ export const useMovementFormHandlers = (initialValuesId?: number) => { payload: UpdateMovementPayload, documents: File[] = [] ) => { - console.log('=== UPDATE HANDLER DEBUG ==='); - console.log('1. Received payload:', payload); - console.log('2. Movement ID:', movementId); - console.log('3. Documents count:', documents.length); - let finalPayload: UpdateMovementPayload | FormData; if (documents.length > 0) { - console.log('4. Creating FormData (has documents)'); const formData = new FormData(); formData.append('data', JSON.stringify(payload)); documents.forEach((file, index) => { formData.append(`documents[${index}]`, file); }); - console.log('5. FormData entries:'); - for (const [key, value] of formData.entries()) { - if (value instanceof File) { - console.log( - ` ${key}: [File] ${value.name} (${value.size} bytes)` - ); - } else { - console.log(` ${key}: ${value}`); - } - } - finalPayload = formData as unknown as UpdateMovementPayload; } else { - console.log('4. Sending as JSON (no documents)'); - console.log('5. Payload:', JSON.stringify(payload, null, 2)); finalPayload = payload; } - console.log('=== END UPDATE HANDLER DEBUG ==='); - const res = await MovementApi.update(movementId, finalPayload); if (res?.status === 'error') { - console.error('API Error:', res); setMovementFormErrorMessage(res.message); return; } diff --git a/src/services/http/client.ts b/src/services/http/client.ts index adba75e9..9dd382ca 100644 --- a/src/services/http/client.ts +++ b/src/services/http/client.ts @@ -14,6 +14,9 @@ export async function httpClient( (!opts.auth && opts.auth !== 'none' && opts.auth !== 'bearer'); const isBearerAuth = opts.auth === 'bearer' && !!opts.token; + const isFormData = + typeof FormData !== 'undefined' && opts.body instanceof FormData; + const config: AxiosRequestConfig = { url: path, method: opts.method ?? 'GET', @@ -22,7 +25,7 @@ export async function httpClient( timeout: opts.timeoutMs ?? 10_000, withCredentials: isCookieAuth && !isBearerAuth, headers: { - 'Content-Type': 'application/json', + ...(isFormData ? {} : { 'Content-Type': 'application/json' }), ...(opts.headers ?? {}), ...(isBearerAuth && !isCookieAuth ? { Authorization: `Bearer ${opts.token}` }