fix(profile): use request() helper, check status correctly for save + submit
Some checks failed
build-and-release / build (push) Failing after 1m16s

This commit is contained in:
Benimaru 2026-07-10 14:15:00 +05:30
parent 67af6e4a5f
commit 158c947944
2 changed files with 13 additions and 13 deletions

View file

@ -19,7 +19,7 @@ import {
isValidLocation, isValidLocation,
isValidURL, isValidURL,
} from "~/lib/form-validation"; } from "~/lib/form-validation";
import { uploadDocument } from "~/lib/api"; import { request, uploadDocument } from "~/lib/api";
import { import {
getBasicFields, getBasicFields,
getDocFields, getDocFields,
@ -366,11 +366,12 @@ export default function ProfilePage(props: Props) {
setSaving(true); setSaving(true);
setSaveMsg(""); setSaveMsg("");
try { try {
const res = await apiFetch("/api/profile", { const { data, status } = await request("/api/profile", {
method: "PATCH", 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 { } catch {
setSaveMsg("Network error. Please try again."); setSaveMsg("Network error. Please try again.");
} finally { } finally {
@ -396,19 +397,18 @@ export default function ProfilePage(props: Props) {
setSubmitting(true); setSubmitting(true);
setSubmitMsg(""); setSubmitMsg("");
try { try {
const res = await apiFetch("/api/profile/submit-for-verification", { const { data, status } = await request("/api/profile/submit-for-verification", {
method: "POST", method: "POST",
body: JSON.stringify({ roleKey: props.roleKey, document_urls: docUrls() }), body: { roleKey: props.roleKey, document_urls: docUrls() },
}); });
const data = await res.json(); if (status === 200) {
if (res.ok) {
setVerificationStatus("PENDING"); setVerificationStatus("PENDING");
props.onVerificationStatusChange?.("PENDING"); props.onVerificationStatusChange?.("PENDING");
setSubmitMsg("Submitted! We will review your profile and notify you."); setSubmitMsg("Submitted! We will review your profile and notify you.");
} else if (res.status === 409) { } else if (status === 409) {
setSubmitMsg(data.error ?? "A verification is already in progress."); setSubmitMsg(data?.error ?? "A verification is already in progress.");
} else { } else {
setSubmitMsg(data.error ?? "Submission failed. Please try again."); setSubmitMsg(data?.error ?? "Submission failed. Please try again.");
} }
} catch { } catch {
setSubmitMsg("Network error. Please try again."); setSubmitMsg("Network error. Please try again.");

View file

@ -11,7 +11,7 @@ function getAuthHeaders(): Record<string, string> {
}; };
} }
async function apiFetch(path: string, options?: RequestInit): Promise<any> { export async function apiFetch(path: string, options?: RequestInit): Promise<any> {
const cleanPath = path.startsWith('/api/') ? path.slice(4) : path; const cleanPath = path.startsWith('/api/') ? path.slice(4) : path;
const res = await fetch(`${API}${cleanPath}`, { const res = await fetch(`${API}${cleanPath}`, {
headers: getAuthHeaders(), headers: getAuthHeaders(),
@ -31,7 +31,7 @@ type ApiResponse<T = any> = {
headers: Headers; headers: Headers;
}; };
async function request<T = any>( export async function request<T = any>(
path: string, path: string,
options?: { method?: string; body?: unknown; headers?: HeadersInit } options?: { method?: string; body?: unknown; headers?: HeadersInit }
): Promise<ApiResponse<T>> { ): Promise<ApiResponse<T>> {