Files
redefined-designs/frontend/src/customer/Account.tsx
T
bermudalamb 9bb3cc86b6
Linting / lint (pull_request) Successful in 1m37s
SonarQube Analysis / sonarqube (pull_request) Failing after 13m19s
feat(frontend): give order history a page of its own (#121)
The account modal had accumulated: a profile line, a name form, two collapsed panels for changing email and password, two consent switches, an order table and four controls. The table was the piece that fitted worst, being the only tabular data in a 700px dialog whose body is capped at 70vh. The scroll={{ x: 'max-content' }} already on it was a workaround for being in the wrong container rather than a layout choice.

It moves to /orders, an ordinary page in the same Routes block as /cart and /privacy, rather than another entry in MODAL_ROUTES. Order history is a list you read, like the cart, not a dialog you dismiss. A modal at /account/orders would have been the smaller change and was rejected: it inherits the same width and the same scroll cap, so it moves the table without giving it anything.

The page shell follows Cart.tsx, which is the established shape here: a Layout with a Header carrying Back to Shop and the title, and the same guard sending a signed-out visitor to /login. The account modal keeps a View order history button where the table used to be, because that is where a customer looks for it.

One thing changes rather than moves. The old effect caught a failed load with a toast and left orders as an empty array. The toast faded and the empty table did not, so from then on a customer whose request failed saw exactly what a customer with no orders saw, and the page asserted something false. Loading, failed and empty are now three distinct states, and the failed one carries a Retry: a transient failure would otherwise strand someone on a page that needs a full reload to recover.

OrdersBody sits at module level rather than nested inside Orders(). A function declared inside a component counts toward that component's cognitive complexity, which is what made Customers() hard to bring back under the threshold in #81.

The two assertions in account-modal.spec.ts that looked for the text "Order History" inside the modal are updated to look for the link, not deleted. They were the only coverage that the account view still offers any route to the orders, which is exactly what this change could have silently broken.

Verification, against a real backend and database: five new tests covering the signed-out redirect, the empty state, Back to Shop, the link from My Account, and that the page renders as a page rather than a modal over the storefront - that last one is what would catch /orders being added to MODAL_ROUTES and quietly undoing the change. The full suite goes from 100 to 105 passing with no new failures; the three that fail did so before this branch and fail identically on main. tsc and the production build are clean, ESLint reports no errors.

Closes #121
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-08-22 10:53:40 -05:00

139 lines
4.9 KiB
TypeScript
Executable File

import { useEffect } from 'react';
import Typography from 'antd/es/typography';
import Switch from 'antd/es/switch';
import Button from 'antd/es/button';
import Modal from 'antd/es/modal';
import message from 'antd/es/message';
import Space from 'antd/es/space';
import Divider from 'antd/es/divider';
import { useNavigate } from 'react-router-dom';
import { updateConsent, exportMyData, deleteMyAccount } from './customerApi';
import { setFavoriteAlerts } from './favoritesApi';
import { useCustomerAuth } from './CustomerAuthContext';
import AccountDetails from './AccountDetails';
const { Text } = Typography;
interface Props {
// Supplied by the route, which decides where closing lands: back to the page
// the customer came from, or to the storefront when they arrived directly.
onClose: () => void;
}
export default function Account({ onClose }: Props) {
const { customer, loading, refresh, logout } = useCustomerAuth();
const navigate = useNavigate();
useEffect(() => {
if (!loading && !customer) navigate('/login');
}, [loading, customer, navigate]);
if (!customer) return null;
async function handleFavoriteAlertsToggle(checked: boolean) {
try {
await setFavoriteAlerts(checked);
} catch (err) {
message.error((err as Error).message);
return;
}
refresh();
message.success(checked ? 'We will email you when a favorite sells' : 'Turned off');
}
async function handleConsentToggle(checked: boolean) {
await updateConsent(checked);
message.success(checked ? 'Subscribed to emails' : 'Unsubscribed from emails');
refresh();
}
async function handleLogout() {
try {
await logout();
} catch (err) {
message.error(`Couldn't log out — ${(err as Error).message}`);
return;
}
// replace, so Back doesn't return to the account page — which would only
// bounce to /login now that the session is gone.
navigate('/', { replace: true });
}
function handleDelete() {
Modal.confirm({
title: 'Delete your account?',
content: 'This permanently removes your account and personal data. Your past orders are kept for accounting purposes but disconnected from your identity. This cannot be undone.',
okText: 'Delete my account',
okButtonProps: { danger: true },
onOk: async () => {
await deleteMyAccount();
// The storefront is rendered behind this modal, so without clearing the
// session it goes on showing "My Account" and hiding Sign up for an
// account that no longer exists — visibly stale, not merely stale in
// state. replace, so Back cannot return to /account and bounce to
// /login.
refresh();
message.success('Account deleted');
navigate('/', { replace: true });
}
});
}
return (
<Modal
open
// The account view is a place a customer can be sent by an email link or
// a bookmark, so it is titled and closable rather than relying on the
// page behind it to say where they are.
title="My Account"
onCancel={onClose}
footer={null}
width={700}
// The view holds profile, two consents, order history, and the account
// controls, which is taller than a phone. Capping the body and letting it
// scroll keeps the title and close control in reach instead of pushing
// them off-screen.
style={{ maxWidth: 'calc(100vw - 32px)', top: 24 }}
styles={{ body: { maxHeight: '70vh', overflowY: 'auto' } }}
destroyOnHidden
>
<div>
<Text>{customer.email}</Text>
{!customer.email_verified && (
<div style={{ marginTop: 8 }}>
<Text type="warning">Email not verified check your inbox for a verification link.</Text>
</div>
)}
<Divider />
<AccountDetails customer={customer} onChanged={refresh} />
<Divider />
<Space align="center">
<Switch checked={customer.marketing_consent} onChange={handleConsentToggle} />
<Text>Receive emails about new items</Text>
</Space>
{/* A separate consent from marketing above, and shown separately so a
customer can hold one without the other. */}
<div style={{ marginTop: 12 }}>
<Space align="center">
<Switch checked={customer.favorite_alerts} onChange={handleFavoriteAlertsToggle} />
<Text>Email me when an item I favorited is sold</Text>
</Space>
</div>
<Divider />
<Space wrap>
{/* Order history is a page of its own now. The link stays here because
this is where a customer looks for it. */}
<Button onClick={() => navigate('/orders')}>View order history</Button>
<Button onClick={exportMyData}>Download my data</Button>
<Button onClick={handleLogout}>Log out</Button>
<Button danger onClick={handleDelete}>Delete my account</Button>
</Space>
</div>
</Modal>
);
}