Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 13 additions & 10 deletions frontend/src/pages/ShopCheckout.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ Expected Response Information: Hold details, the pending purchase, or a clear no

import { useCallback, useEffect, useState } from "react";
import { Link, useNavigate, useParams } from "react-router-dom";
import { EntranceList, usePageEntranceClass } from "../components/PageEntrance";
import { useAuthContext } from "../context/AuthContext";
import { useShopCart } from "../context/ShopCartContext";
import { formatCents } from "../utils/shopFormat";
Expand Down Expand Up @@ -127,6 +128,7 @@ export default function ShopCheckoutPage() {
const [notice, setNotice] = useState(null);
const [isBusy, setIsBusy] = useState(false);
const [nowMs, setNowMs] = useState(() => Date.now());
const checkoutClass = usePageEntranceClass("shopCheckout baseContainer", { onceKey: "checkout", ready: loadState !== "loading" });

const loadHold = useCallback(async () => {
setLoadState("loading");
Expand Down Expand Up @@ -275,12 +277,12 @@ export default function ShopCheckoutPage() {
}

if (loadState === "loading") {
return <main className="shopCheckout baseContainer" role="status">Loading your checkout…</main>;
return <main className={checkoutClass} role="status">Loading your checkout…</main>;
}

if (loadState === "signin") {
return (
<main className="shopCheckout baseContainer">
<main className={checkoutClass}>
<h1>Sign in to continue</h1>
<p>Sign in to see your held items and finish your purchase.</p>
<button type="button" className="shopCheckout__button" onClick={async () => {
Expand All @@ -295,7 +297,7 @@ export default function ShopCheckoutPage() {

if (loadState === "missing") {
return (
<main className="shopCheckout baseContainer">
<main className={checkoutClass}>
<h1>We couldn’t find this checkout</h1>
<p><Link to="/shop">Return to the shop</Link></p>
</main>
Expand All @@ -304,7 +306,7 @@ export default function ShopCheckoutPage() {

if (loadState === "closed") {
return (
<main className="shopCheckout baseContainer">
<main className={checkoutClass}>
<h1>Checkout is unavailable</h1>
<p>{loadMessage ?? "Online checkout is temporarily unavailable. Your cart has not changed; please check back later."}</p>
<p><Link to="/shop">Return to the shop</Link></p>
Expand All @@ -314,7 +316,7 @@ export default function ShopCheckoutPage() {

if (loadState === "error") {
return (
<main className="shopCheckout baseContainer">
<main className={checkoutClass}>
<h1>Checkout could not load</h1>
<p>{GENERIC_FAILURE}</p>
<button type="button" className="shopCheckout__button" onClick={loadHold}>Try again</button>
Expand All @@ -324,7 +326,7 @@ export default function ShopCheckoutPage() {

if (purchase) {
return (
<main className="shopCheckout baseContainer">
<main className={checkoutClass}>
<h1>Your purchase is submitted</h1>
<section className="shopCheckout__submitted" aria-labelledby="shopCheckoutSubmittedTitle">
<h2 id="shopCheckoutSubmittedTitle">Thanks for buying Informatics merch!</h2>
Expand All @@ -343,7 +345,7 @@ export default function ShopCheckoutPage() {
const secondsLeft = formatCountdown(remainingMs);

return (
<main className="shopCheckout baseContainer">
<main className={checkoutClass}>
<div className="shopCheckout__header">
<h1>Checkout</h1>
{isOpen && (
Expand Down Expand Up @@ -385,6 +387,7 @@ export default function ShopCheckoutPage() {
)}

<ul className="shopCheckout__lines">
<EntranceList>
{hold.lines.map((line) => (
<li key={`${line.sku}:${line.size}`} className="shopCheckout__line">
<span>{line.quantity} × {line.name}, size {line.size}</span>
Expand All @@ -402,13 +405,15 @@ export default function ShopCheckoutPage() {
)}
</li>
))}
</EntranceList>
</ul>

<p className="shopCheckout__total">
Total due: <strong>{formatCents(hold.totalCents)}</strong>
</p>

{isOpen && (
<EntranceList startAt={hold.lines.length}>
<section className="shopCheckout__pay" aria-labelledby="shopCheckoutPayTitle">
<h2 id="shopCheckoutPayTitle">Pay with Venmo</h2>
<div className="shopCheckout__payBody">
Expand All @@ -432,9 +437,6 @@ export default function ShopCheckoutPage() {
</div>
</div>
</section>
)}

{isOpen && (
<form onSubmit={handleConfirm} noValidate>
<div className="shopCheckout__fieldHeader">
<label htmlFor="shopCheckoutVenmo" className="shopCheckout__required">Venmo username</label>
Expand Down Expand Up @@ -513,6 +515,7 @@ export default function ShopCheckoutPage() {
</button>
</div>
</form>
</EntranceList>
)}

{notice && <p role="status" className="shopCheckout__error">{notice}</p>}
Expand Down
19 changes: 19 additions & 0 deletions frontend/src/pages/ShopCheckout.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { act, cleanup, fireEvent, render, screen, waitFor, within } from "@testi
import { MemoryRouter, Route, Routes } from "react-router-dom";
import { ShopCartProvider } from "../context/ShopCartContext";
import ShopCheckoutPage from "./ShopCheckout";
import { resetPageEntranceForTests } from "../utils/pageEntrance";

const auth = vi.hoisted(() => ({ isAuthenticated: true, signIn: vi.fn() }));
vi.mock("../context/AuthContext", () => ({ useAuthContext: () => auth }));
Expand Down Expand Up @@ -77,6 +78,7 @@ function storedCart() {
}

beforeEach(() => {
resetPageEntranceForTests();
auth.isAuthenticated = true;
auth.signIn.mockReset();
vi.spyOn(window, "scrollTo").mockImplementation(() => {});
Expand Down Expand Up @@ -408,3 +410,20 @@ describe("ShopCheckoutPage", () => {
expect(storedCart()).toHaveLength(2);
});
});

test("waves the checkout lines in on the first visit, leaving the title and timer still", async () => {
mockApi({
[`GET /api/v1/shop/holds/${holdId}`]: response({ status: "success", hold: hold() }),
});
renderCheckout();

expect(await screen.findByRole("heading", { name: "Checkout" })).toBeInTheDocument();
const main = document.querySelector("main.shopCheckout");
expect(main).toHaveClass("pageEntrance--enter");
const lines = [...document.querySelectorAll("li.shopCheckout__line")];
expect(lines).toHaveLength(2);
expect(lines[0]).toHaveClass("pageEntrance__listItem");
expect(lines[0].style.getPropertyValue("--entrance-i")).toBe("0");
expect(lines[1].style.getPropertyValue("--entrance-i")).toBe("1");
expect(document.querySelector(".shopCheckout__timer")).not.toHaveClass("pageEntrance__listItem");
});
Loading