The show details
Select a saved show in state, and load extra fields from TVMaze when you ask.
Click the card
Here is the app as you left it. Slow Horses sits in Results with Add disabled. The saved list has four cards. Clicking a card does nothing extra yet.
A click should pick one saved show. Store that id in App as
selectedId. find, from Working with Arrays, reads the
object back.
const selected = watchlist.find((s) => s.id === selectedId);
Pass onSelect through ShowList, the same way as
Passing handlers down. The panel sits beside the
saved cards. Close sets selectedId back to null.
Watched, Add, and Remove sit on the card. Their clicks would also
select, unless you stop them. Propagation and
defaults covers stopPropagation.
The Bear starts selected so you can see the panel. Title, year, genres, and rating are already on the object. Close hides the panel. Tick Watched: the card stays selected.
Load more
TVMaze can send extra fields. The search payload does not include network, status, or runtime. Load more asks for one show by id — the same URL as Promises and async/await.
fetch("https://api.tvmaze.com/shows/" + selectedId);
Only numeric ids exist on that endpoint. Seed ids like s1 do not.
/^[0-9]+$/ is true when every character is a digit. That is the same
test as /^\d+$/.
Call fetch from the button's click, as
Searching TVMaze does on submit. Disable the
button while the request is in flight. Skip summary: it is HTML.
Keep the extra fields in a details object. Hand React a new object,
as in Updating objects in state. Clear
details when the selected id changes.
Slow Horses is id 99, so Load more is on screen. The extra lines stay
empty until you press it. Click The Bear: the button is gone, and the
panel says This one was added by us.
Do not fetch on select
Selecting a card only sets selectedId. The panel can already show
title and year. Load more is a second click, so the first paint never
waits on the network.
Do not call fetch inside handleSelect, and do not call it while
App is rendering.
Common mistakes
- Selected state on
ShowCard. Each card has its own memory. Two clicks open two panels. KeepselectedIdinApp. fetchduring render. React would start a request every time it draws. Put it in the Load more handler.- Rendering
summaryas HTML. The string contains tags. Show network, status, and runtime instead.
Exercise
Each saved card keeps its own open flag, so two panels can sit on
screen at once. Load more also appears on The Bear.
There should be one panel, owned by App. On first paint it stays
hidden. Click The Bear. The panel should show:
The Bear
2022
Load more should not be on that panel. s1 is not a TVMaze id. Click
Slow Horses instead; Load more should appear there.
On first paint the page should show:
My watchlist
4 shows · 1 watched
Search
Results
Slow Horses
Add
The Bear
Slow Horses
Severance
Andor
Remove
Hint: selectedId lives in App. Show Load more when
/^[0-9]+$/.test(id) is true.