From 158c947944959de25450c13d04eb89bf50e10804 Mon Sep 17 00:00:00 2001 From: Benimaru Date: Fri, 10 Jul 2026 14:15:00 +0530 Subject: [PATCH] fix(profile): use request() helper, check status correctly for save + submit --- src/components/dashboard/ProfilePage.tsx | 22 +++++++++++----------- src/lib/api.ts | 4 ++-- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/src/components/dashboard/ProfilePage.tsx b/src/components/dashboard/ProfilePage.tsx index dd264e3..980fd6d 100644 --- a/src/components/dashboard/ProfilePage.tsx +++ b/src/components/dashboard/ProfilePage.tsx @@ -19,7 +19,7 @@ import { isValidLocation, isValidURL, } from "~/lib/form-validation"; -import { uploadDocument } from "~/lib/api"; +import { request, uploadDocument } from "~/lib/api"; import { getBasicFields, getDocFields, @@ -366,11 +366,12 @@ export default function ProfilePage(props: Props) { setSaving(true); setSaveMsg(""); try { - const res = await apiFetch("/api/profile", { + const { data, status } = await request("/api/profile", { method: "PATCH", - body: JSON.stringify({ roleKey: props.roleKey, profile_data: form() }), + body: { roleKey: props.roleKey, profile_data: form() }, }); - setSaveMsg(res.ok ? "Saved successfully." : "Failed to save. Please try again."); + if (status === 200) setSaveMsg("Saved successfully."); + else setSaveMsg((data && (data.error || data.message)) || "Failed to save. Please try again."); } catch { setSaveMsg("Network error. Please try again."); } finally { @@ -396,19 +397,18 @@ export default function ProfilePage(props: Props) { setSubmitting(true); setSubmitMsg(""); try { - const res = await apiFetch("/api/profile/submit-for-verification", { + const { data, status } = await request("/api/profile/submit-for-verification", { method: "POST", - body: JSON.stringify({ roleKey: props.roleKey, document_urls: docUrls() }), + body: { roleKey: props.roleKey, document_urls: docUrls() }, }); - const data = await res.json(); - if (res.ok) { + if (status === 200) { setVerificationStatus("PENDING"); props.onVerificationStatusChange?.("PENDING"); setSubmitMsg("Submitted! We will review your profile and notify you."); - } else if (res.status === 409) { - setSubmitMsg(data.error ?? "A verification is already in progress."); + } else if (status === 409) { + setSubmitMsg(data?.error ?? "A verification is already in progress."); } else { - setSubmitMsg(data.error ?? "Submission failed. Please try again."); + setSubmitMsg(data?.error ?? "Submission failed. Please try again."); } } catch { setSubmitMsg("Network error. Please try again."); diff --git a/src/lib/api.ts b/src/lib/api.ts index d6ce498..7b949cc 100644 --- a/src/lib/api.ts +++ b/src/lib/api.ts @@ -11,7 +11,7 @@ function getAuthHeaders(): Record { }; } -async function apiFetch(path: string, options?: RequestInit): Promise { +export async function apiFetch(path: string, options?: RequestInit): Promise { const cleanPath = path.startsWith('/api/') ? path.slice(4) : path; const res = await fetch(`${API}${cleanPath}`, { headers: getAuthHeaders(), @@ -31,7 +31,7 @@ type ApiResponse = { headers: Headers; }; -async function request( +export async function request( path: string, options?: { method?: string; body?: unknown; headers?: HeadersInit } ): Promise> {