Command Palette

Search for a command to run...

Hectal
Phase 25: Frontend System Design with React
React · Exercise 4 of 5 PreviousNext
Advanced

Optimistic chat send with retry

sendApi(clientId, text) in the starter answers after 100 ms. It fails the first attempt of any message containing flaky, and it stores each clientId only once (it's idempotent).

When the form #composer is submitted with text in #msg: add the message immediately as <li className="msg" data-state="sending"> with the text in <span className="text">, clear the input, then call sendApi with a new client id ("c" + number is fine). Ignore empty messages.

On success set data-state="sent". On failure set data-state="failed" and show <button className="retry">Retry</button> inside that li. Retry sends again with the same client id (state goes back to sending, then sent).

What Check tests

  1. A sent message appears at once as sending and the input clears
  2. It becomes sent when the server answers
  3. A failed send shows failed and a Retry button
  4. Retry goes back to sending, then sent
  5. The server stored each message exactly once
  6. Empty messages are ignored

Stuck? One hint at a time

The solution unlocks after your first Check. Try first: that is where the learning happens.

Runs in your browser. Your code is saved here.

Your page

Press Run to see the page.

Console

Nothing logged yet.