From f9026685446ce29e2accf0f041643ec7da85dc3d Mon Sep 17 00:00:00 2001 From: Abby Aguilar Date: Sun, 8 Nov 2026 19:49:24 -0500 Subject: [PATCH 1/2] completed ticket for applicant approve/reject --- src/app/decision-controls-test/page.tsx | 75 +++++++++++ .../admin/ApplicantDecisionControls.tsx | 125 ++++++++++++++++++ src/lib/admin-api.test.ts | 89 +++++++++++++ src/lib/admin-api.ts | 54 ++++++++ 4 files changed, 343 insertions(+) create mode 100644 src/app/decision-controls-test/page.tsx create mode 100644 src/components/admin/ApplicantDecisionControls.tsx create mode 100644 src/lib/admin-api.test.ts create mode 100644 src/lib/admin-api.ts diff --git a/src/app/decision-controls-test/page.tsx b/src/app/decision-controls-test/page.tsx new file mode 100644 index 0000000..b88d6f3 --- /dev/null +++ b/src/app/decision-controls-test/page.tsx @@ -0,0 +1,75 @@ +/** + * page.tsx + * + * tests the controls and decision flow + * + * Author: Abby, Alex + * Date Created: 11/06/26 + */ +"use client"; + +import { useState } from "react"; +import ApplicantDecisionControls from "@/components/admin/ApplicantDecisionControls"; +import type { DecisionStatus } from "@/components/admin/ApplicantDecisionControls"; + +const TEST_APPLICANTS = [ + { id: "test-applicant-five", note: "approve works" }, + { id: "test-applicant-six", note: "reject works" }, + { id: "test-applicant-seven", note: "approve fails after Five (409)" }, + { id: "test-applicant-three", note: "already rejected on the server (409)" }, + { id: "nope", note: "doesn't exist (404)" }, +]; + +const STATUSES: DecisionStatus[] = ["SUBMITTED", "APPROVED", "REJECTED"]; + +export default function DecisionControlsTestPage() { + const [applicantId, setApplicantId] = useState(TEST_APPLICANTS[0].id); + const [status, setStatus] = useState("SUBMITTED"); + const [lastDecided, setLastDecided] = useState("nothing yet"); + + return ( +
+
+

Decision controls test

+

Sign in at /admin/sign-in as the admin first.

+ + +

onDecided was last called with: {lastDecided}

+
+ + {/* The key makes it start fresh whenever you change a choice above */} + +
+ ); +} diff --git a/src/components/admin/ApplicantDecisionControls.tsx b/src/components/admin/ApplicantDecisionControls.tsx new file mode 100644 index 0000000..e87c062 --- /dev/null +++ b/src/components/admin/ApplicantDecisionControls.tsx @@ -0,0 +1,125 @@ +/** + * ApplicantDecisionControls.tsx + * + * Admin can approve/reject submitted applicants. Display confirm modal and updates applicant status. + * + * Author: Abby, Alex + * Date Created: 11/06/26 + */ +"use client"; + +import { useState } from "react"; +import ConfirmModal from "@/components/common/ConfirmModal"; +import CommonButton from "@/components/common/CommonButton"; +import { decideApplicant } from "@/lib/admin-api"; +import type { Decision } from "@/lib/admin-api"; + +export type DecisionStatus = "SUBMITTED" | "APPROVED" | "REJECTED"; + +type ApplicantDecisionControlsProps = { + applicantId: string; + // The applicant's status when the page loaded + status: DecisionStatus; + // Called when the admin closes the result pop-up, so the page can refresh its own data + onDecided?: (status: "APPROVED" | "REJECTED") => void; +}; + +/** + * ApplicantDecisionControls /src/components/admin/ + * + * Admin can approve/reject submitted applicants. Display confirm modal while updating applicant status. * + * + * @param applicantId - id of applicant + * @param status - applicants inital status + * @param onDecided - optional callback + * @returns buttons and status + * + */ +export default function ApplicantDecisionControls({ + applicantId, + status, + onDecided, +}: ApplicantDecisionControlsProps) { + const [currentStatus, setCurrentStatus] = useState(status); + // Which decision the "Are you sure?" pop-up is open for (null means it's closed) + const [pending, setPending] = useState(null); + // Which decision the result pop-up is open for (null means it's closed) + const [result, setResult] = useState<"APPROVED" | "REJECTED" | null>(null); + const [busy, setBusy] = useState(false); + const [error, setError] = useState(null); + + async function handleConfirm() { + if (!pending) { + return; + } + setBusy(true); + setError(null); + + const response = await decideApplicant(applicantId, pending); + setBusy(false); + if (!response.ok) { + setError(response.message); + return; + } + + const newStatus = pending === "approve" ? "APPROVED" : "REJECTED"; + setCurrentStatus(newStatus); + setPending(null); + setResult(newStatus); + } + + function handleCancel() { + setPending(null); + setError(null); + } + + function handleResultClose() { + if (result != null) { + onDecided?.(result); + } + setResult(null); + } + + return ( + <> + {currentStatus === "SUBMITTED" ? ( +
+ setPending("approve")} /> + + setPending("reject")} /> +
+ ) : ( + {currentStatus} + )} + + {pending && ( + + )} + + {result && ( + + )} + + ); +} diff --git a/src/lib/admin-api.test.ts b/src/lib/admin-api.test.ts new file mode 100644 index 0000000..2f34780 --- /dev/null +++ b/src/lib/admin-api.test.ts @@ -0,0 +1,89 @@ +/** + * admin-api.test.ts + * + * tests admin-api.ts + * + * Author: Abby, Alex + * Date Created: 11/06/26 + */ +import { decideApplicant } from "./admin-api"; + +const originalFetch = global.fetch; + +// Replaces fetch with one that answers with a made-up response +function mockFetch(status: number, json: () => Promise) { + global.fetch = jest.fn().mockResolvedValue({ + ok: status >= 200 && status < 300, + status, + json, + }) as unknown as typeof fetch; +} + +afterEach(() => { + global.fetch = originalFetch; +}); + +describe("decideApplicant", () => { + it("posts to the approve endpoint", async () => { + mockFetch(200, async () => ({ applicant: { id: "a1" } })); + expect(await decideApplicant("a1", "approve")).toEqual({ ok: true }); + expect(global.fetch).toHaveBeenCalledWith( + "/api/applicants/a1/approve", + expect.objectContaining({ method: "POST" }), + ); + }); + + it("posts to the reject endpoint", async () => { + mockFetch(200, async () => ({ applicant: { id: "a1" } })); + expect(await decideApplicant("a1", "reject")).toEqual({ ok: true }); + expect(global.fetch).toHaveBeenCalledWith( + "/api/applicants/a1/reject", + expect.objectContaining({ method: "POST" }), + ); + }); + + it("encodes the id in the address", async () => { + mockFetch(200, async () => ({})); + await decideApplicant("a/b", "approve"); + expect(global.fetch).toHaveBeenCalledWith( + "/api/applicants/a%2Fb/approve", + expect.anything(), + ); + }); + + it("returns the server's message on an error", async () => { + mockFetch(409, async () => ({})); + expect(await decideApplicant("a1", "approve")).toEqual({ + ok: false, + message: expect.stringMatching("sometthing went wrong"), + }); + }); + + it("returns a message for a 403 with no body message", async () => { + mockFetch(403, async () => ({})); + expect(await decideApplicant("a1", "approve")).toEqual({ + ok: false, + message: expect.stringMatching(/\S/), + }); + }); + + it("returns a message when the body isn't JSON", async () => { + mockFetch(500, async () => { + throw new Error("not json"); + }); + expect(await decideApplicant("a1", "reject")).toEqual({ + ok: false, + message: expect.stringMatching(/\S/), + }); + }); + + it("doesn't throw when fetch fails", async () => { + global.fetch = jest + .fn() + .mockRejectedValue(new Error("offline")) as unknown as typeof fetch; + expect(await decideApplicant("a1", "reject")).toEqual({ + ok: false, + message: expect.stringMatching(/\S/), + }); + }); +}); diff --git a/src/lib/admin-api.ts b/src/lib/admin-api.ts new file mode 100644 index 0000000..ef7cc1d --- /dev/null +++ b/src/lib/admin-api.ts @@ -0,0 +1,54 @@ +/** + * admin-api.ts + * + * calls POST and sends approval or rejection and returns if succeeded or not + * + * Author: Abby, Alex + * Date Created: 11/06/26 + */ + +export type Decision = "approve" | "reject"; + +export type DecisionResult = { ok: true } | { ok: false; message: string }; + +/** + * decideApplicant + * + * calls POST and sends approval or rejection and returns if succeeded or not + * + * @param applicantId - applicants id string + * @param decision - approve/reject + * @returns decisionResult - success or failure -> with error message + * + */ +export async function decideApplicant( + applicantId: string, + decision: Decision, +): Promise { + try { + const response = await fetch( + `/api/applicants/${encodeURIComponent(applicantId)}/${decision}`, + { + method: "POST", + }, + ); + + if (response.ok) { + return { ok: true }; + } else { + const data = await response.json(); + if ( + typeof data === "object" && + data !== null && + "error" in data && + typeof data.error === "string" + ) { + return { ok: false, message: data.error }; + } else { + return { ok: false, message: "sometthing went wrong" }; + } + } + } catch { + return { ok: false, message: "something went wrong" }; + } +} From da3489b4075ab567b06401039abe851becb8fbd7 Mon Sep 17 00:00:00 2001 From: natphua Date: Wed, 11 Nov 2026 09:16:59 -0500 Subject: [PATCH 2/2] FE: removed weird char --- src/components/admin/ApplicantDecisionControls.tsx | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/components/admin/ApplicantDecisionControls.tsx b/src/components/admin/ApplicantDecisionControls.tsx index e87c062..5ea19c4 100644 --- a/src/components/admin/ApplicantDecisionControls.tsx +++ b/src/components/admin/ApplicantDecisionControls.tsx @@ -1,7 +1,8 @@ /** * ApplicantDecisionControls.tsx * - * Admin can approve/reject submitted applicants. Display confirm modal and updates applicant status. + * Admin can approve/reject submitted applicants. + * Display confirm modal and updates applicant status. * * Author: Abby, Alex * Date Created: 11/06/26 @@ -27,7 +28,8 @@ type ApplicantDecisionControlsProps = { /** * ApplicantDecisionControls /src/components/admin/ * - * Admin can approve/reject submitted applicants. Display confirm modal while updating applicant status. * + * Admin can approve/reject submitted applicants. + * Display confirm modal while updating applicant status. * * @param applicantId - id of applicant * @param status - applicants inital status