The ♥ moves from localStorage to the server, so it no longer lives in one browser. (The course server keeps one list per market, so per student; a real one would keep one per signed-in user.) A click must feel instant even on a slow server, so the page updates first and asks second: optimistic UI. When the server says no, the page puts things back and says so. ★★☆
“I saved four stalls on my laptop. My phone says I saved none.”
You'll demo (next class): with chaos on, click ♥ five times quickly on different stalls: each turns on at once. One of them fails: it turns back off and a message says which one. Reload: the server's list is what you see.
Four things, on top of your connected market. They count when they work with chaos on (control page).
GET /api/saved on start fills the ♥s; localStorage no longer stores them.PUT /api/saved/:id; off: DELETE. 204: no body to read.What has to work for the demo next class, not how. The core above is part of it.
GET /api/saved fills state.saved (together with the stalls).PUT /api/saved/:id. ♥ off: DELETE /api/saved/:id. Both 204, no body.localStorage no longer stores the saved ids.GET /api/saved fails, the stalls still render; the ♥s are disabled with a note.| Method | Path | Answers |
|---|---|---|
GET | /api/saved | 200 · array of stall ids |
PUT | /api/saved/:id | 204 · 404 unknown stall · idempotent |
DELETE | /api/saved/:id | 204 · idempotent |
Test in two browsers (or a normal and a private window): save in one, reload the other.
GET /api/saved again.Network panel first: is the request sent, with what, and what came back? Then a hint.
To roll back, you need to know the value before the click. Where is it at the moment of the click?
On, off, on, quickly: three requests. Which response should decide what the ♥ shows?
PUT and DELETE are idempotent. What does that let you do when one fails?
response.json() fails on a 204: "Unexpected end of JSON input".This brief assumes Connect the market is done: api.js works, the stalls come from the API, loading and errors are handled. Then: A ♥ click sends PUT /api/saved/:id (204), and GET /api/saved in the Network panel lists that id after a reload.