Move the billing details form to TanStack Query
React · React · intermediate · modification
The billing details page was the last settings page still fetching by hand in a `useEffect` with its own cancel flag and loading and error flags. `src/settings/BillingDetailsPage.jsx` now loads the details with `useQuery` under the `['billing-details']` key and fills the form from the result, and saves with `useMutation`, writing the saved details back into the query cache. The loading, error and save states come from the query and the mutation. `src/settings/BillingDetailsPage.test.jsx` (new) mocks `src/api.js` and tests the page's load, save and error states.
Admin SPA in plain JavaScript (no TypeScript, no type-check step): React 18.3, Vite 5, function components. `package.json` pins `@tanstack/react-query` `^5.59.0` (the lockfile has 5.59.20); `src/main.jsx` (existing, unchanged) renders the app inside one `QueryClientProvider`, and the other settings pages already use `useQuery`/`useMutation`. Tests run with `npm test`: Vitest 2 with @testing-library/react 16 in jsdom. Existing and unchanged: `src/api.js` exports `apiGet(path)` and `apiPut(path, body)`, which send same-origin JSON requests, resolve to the parsed JSON body and throw `ApiError` (with `status`) for any non-2xx answer. `GET /api/billing/details` answers `{ companyName, billingEmail, vatId }`, all strings; `vatId` is `""` when the workspace has none. `PUT /api/billing/details` takes the same shape, validates it (company name and email required, VAT ID optional), stores it and answers with the stored details. The page is the route `/settings/billing` and has no other callers.
Requirements
- When the billing details page opens, it loads the workspace's stored billing details (`GET /api/billing/details`) and shows them in the form: company name, billing email and VAT ID, each field filled with its stored value.
- The user edits any field and presses Save; the page sends all three fields (`PUT /api/billing/details`) and then shows what the server returned and a `Saved.` status. A field the user did not touch is saved with its stored value.
- While the details load, the page shows `Loading billing details...`; if the initial load fails it shows `Could not load billing details.`; if saving fails it keeps the user's edits and shows `Could not save. Try again.` The form is disabled while a save is in flight.
- The page loads and saves through TanStack Query (`useQuery` and `useMutation`, query key `['billing-details']`) like the other settings pages, replacing its hand-written `useEffect` fetch.
- The page has tests that run with the existing `npm test` (Vitest).
Files touched
- src/settings/BillingDetailsPage.jsx
- src/settings/BillingDetailsPage.test.jsx