Vue · Hard · Optimistic Updates
A team lead uses the Projects page to reserve available places for teammates on several projects. Clicking Reserve immediately reduces that project’s displayed seat count while the API confirms the reservation. The current component restores an old copy of the entire list after a failed request, so a failure on Alpha API can erase a successful reservation on Beta Web; a retry can also be treated as a new reservation. The reservation API is supplied: repair the client’s per-project state and retry behavior without building another backend.
Task
Make reservations independent: if Alpha API fails while Beta Web succeeds, restore only Alpha’s row and keep Beta’s confirmed count; retrying Alpha must reuse its original request ID.
Contract
- Starting data:
initialRowsis the supplied project list. Do not mutate it.ididentifies a project;titleis its visible name;archivedhides it from the active list.seatsis the available seat count;versionidentifies the last server version of that project. - User action: The user presses Reserve on one project. One deliberate reservation is one intent; retrying it is not a new intent.
- Provided reservation API:
api.reserve(id, {expectedVersion, requestId, signal})returns a Promise of{id, seats, version}.makeRequestId()supplies a key for a new intent. An error may includelatest, the server’s newer project values. - Timing guarantees: API operations may resolve or reject in any order, even after cancellation. The fixture controls completion order; no real network is used.
Acceptance criteria
- Immediately subtract one displayed seat from the selected row.
- Show
Savingfor that row. Disable Reserve while it is pending or has zero seats. - Keep other rows interactive.
- Success: use the returned server values for that row and clear its pending/error state.
- Failure: restore that row’s pre-attempt values, or use the newer server values supplied with the error.
- On failure, show
Reservation failedfor that row and clear its pending state. - Never roll back another row’s successful reservation.
- Retry a failed intent with the same key. Clear that intent after success.
- At component unmount, cancel every pending operation and ignore later results.
Constraints
- At most 200 rows; IDs are unique ASCII strings and seats are non-negative integers. Input props must not be mutated.
- Keep the Search projects label, data-id row identity and accessible status messages. Preserve per-row button semantics; changing search behavior is not part of this reservation task.
- Use the injected fake API, not hard-coded results. Abort obsolete work and guard against promises settling after abort.
- No outbound network, paid API or browser permission is needed. New files are not submitted by the embedded widget.
Editable files
src/ProjectApp.vue— Repair the component state, lifecycle and API coordination.src/ProjectRows.vue— Provided row rendering; preserve identity, accessibility and button semantics.src/helpers.js— Optional pure helpers; not a replacement for framework lifecycle handling.tests/candidate.test.js— Add DOM-level regression tests using fixture(t), deferred() and assert.