fix(profile): use request() helper, check status correctly for save + submit
Some checks failed
build-and-release / build (push) Failing after 1m16s
Some checks failed
build-and-release / build (push) Failing after 1m16s
This commit is contained in:
parent
67af6e4a5f
commit
158c947944
2 changed files with 13 additions and 13 deletions
|
|
@ -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.");
|
||||
|
|
|
|||
|
|
@ -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 res = await fetch(`${API}${cleanPath}`, {
|
||||
headers: getAuthHeaders(),
|
||||
|
|
@ -31,7 +31,7 @@ type ApiResponse<T = any> = {
|
|||
headers: Headers;
|
||||
};
|
||||
|
||||
async function request<T = any>(
|
||||
export async function request<T = any>(
|
||||
path: string,
|
||||
options?: { method?: string; body?: unknown; headers?: HeadersInit }
|
||||
): Promise<ApiResponse<T>> {
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue