KatInterview

Resilient Chargeback Inbox: Normalize, Cache, and Cancel in React

Data Structure DesignModel answer
MediumModel answerData Structure Designreacttypescriptcaching

Last updated

Build a React dispute list that normalizes inconsistent API data, caches queries for 60 seconds, and avoids stale responses when filters or search change quickly.

Problem

You are building a React component that lists payment disputes from an internal endpoint, GET /api/v1/disputes. The endpoint has no documentation and its response shape is still changing. The component must treat the payload as untrusted, convert it into a strict client-side model, avoid duplicate network calls, and stay correct when the user changes inputs quickly.

Implement DisputeInbox({ fetchDisputes, pageSize = 10 }). Assume fetchDisputes(query, signal) resolves with the raw JSON body, where query is { status, search, page, pageSize }.

Normalization rules

The body has the form { "data": [...], "has_more": boolean }. Items are inconsistent:

  • Identifier: a string under id or dispute_id.
  • Amount: integer cents (amount: 2500), a float in dollars (amount_dollars: 25.0), or a currency string ("$25.00"). Output is always integer cents.
  • Status: map the raw string to 'needs_response', 'under_review', or 'closed'. Any unrecognized value becomes 'needs_response'.
  • Created time: Unix seconds (created) or an ISO 8601 string (created_at). Output is Unix epoch milliseconds.
  • Reason: an optional string, used for search.
// Raw
{
  "data": [
    { "dispute_id": "dp_101", "amount": 4200, "status": "warning_needs_response", "created": 1700000000 },
    { "id": "dp_102", "amount_dollars": 15.5, "status": "WON", "created_at": "2023-11-14T22:13:20Z" }
  ],
  "has_more": false
}

// Normalized
[
  { id: "dp_101", amountCents: 4200, status: "needs_response", createdAtMs: 1700000000000 },
  { id: "dp_102", amountCents: 1550, status: "closed",         createdAtMs: 1700000000000 }
]

Component requirements

  • Filter by 'all' | 'needs_response' | 'under_review' | 'closed'.
  • Search by dispute identifier or reason.
  • Keep an in-memory cache keyed by status, search, and page, with a 60-second TTL.
  • If the query changes while a request is in flight, that request must not overwrite newer state.
  • Show Loading... while fetching, a Retry button on failure, and No disputes found for an empty result.

Constraints

  • Never mutate the raw response.
  • Convert dollar floats with rounding, not truncation, to avoid floating-point drift.
  • Clicking Retry must not clear the filter or search inputs.
  • Cache keys must not depend on the order in which parameters are listed.

What to deliver

Explain your design, then give the code for the normalizer, the cache key and TTL logic, and the component. Discuss edge cases and trade-offs.

Hints

Hint 1

Keep three concerns separate: a pure function that turns `unknown` into a strict model, a small cache keyed by a canonical string, and a single effect that does the fetching.

Hint 2

Give every effect run its own `AbortController` and abort it in the cleanup. Also check `signal.aborted` before calling `setState`, because a fetcher may ignore the signal.

Hint 3

Build the cache key from sorted parameter names and a trimmed, lowercased search string. Do not cache errors, and use `typeof` checks rather than truthiness so an amount of `0` stays valid.