-
-
Notifications
You must be signed in to change notification settings - Fork 3.5k
docs(examples): update star-wars and rick-morty examples #9696
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
import { useParams, Link as RouterLink } from 'react-router-dom' | ||
import { useQuery } from '@tanstack/react-query' | ||
import { getCharacter, getEpisode, getLocation } from './api' | ||
|
||
function Character() { | ||
let params = useParams() | ||
const characterId = params.characterId! | ||
|
||
const { status, data } = useQuery({ | ||
queryKey: ['character', characterId], | ||
queryFn: () => getCharacter(characterId), | ||
}) | ||
|
||
if (status === 'pending') return <p>Loading...</p> | ||
if (status === 'error') return <p>Error :(</p> | ||
|
||
const locationUrlParts = data.location.url.split('/').filter(Boolean) | ||
const locationId = locationUrlParts[locationUrlParts.length - 1] | ||
|
||
return ( | ||
<div> | ||
<h2 className="text-4xl">{data.name}</h2> | ||
<p> | ||
<strong>Gender</strong>: {data.gender} | ||
</p> | ||
<p> | ||
<strong>Status</strong>: {data.status} | ||
</p> | ||
<p> | ||
<strong>Species</strong>: {data.species} | ||
</p> | ||
<p> | ||
<strong>Origin</strong>: {data.origin.name} | ||
</p> | ||
<p> | ||
<strong>Location</strong>: <Location locationId={locationId} /> | ||
</p> | ||
|
||
<h4 className="text-2xl pt-4">Episodes</h4> | ||
{data.episode.map((episode: any) => { | ||
const episodeUrlParts = episode.split('/').filter(Boolean) | ||
const episodeId = episodeUrlParts[episodeUrlParts.length - 1] | ||
return <Episode episodeId={episodeId} key={`${episodeId}`} /> | ||
})} | ||
</div> | ||
) | ||
} | ||
|
||
function Episode({ episodeId }: { episodeId: string }) { | ||
const { data, status } = useQuery({ | ||
queryKey: ['episode', episodeId], | ||
queryFn: () => getEpisode(episodeId), | ||
}) | ||
|
||
if (status === 'success') { | ||
return ( | ||
<article key={episodeId}> | ||
<RouterLink | ||
className="text-blue-500 hover:underline" | ||
to={`/episodes/${episodeId}`} | ||
> | ||
<h6 className="text-lg"> | ||
{data.episode}. {data.name} - {data.air_date} | ||
</h6> | ||
</RouterLink> | ||
</article> | ||
) | ||
} | ||
} | ||
|
||
function Location({ locationId }: { locationId: string }) { | ||
const { data, status } = useQuery({ | ||
queryKey: ['location', locationId], | ||
queryFn: () => getLocation(locationId), | ||
}) | ||
|
||
if (status === 'pending') return <span>Loading...</span> | ||
if (status === 'error') return <span>Error :(</span> | ||
|
||
return ( | ||
<span> | ||
{data.name} - {data.type} | ||
</span> | ||
) | ||
} | ||
|
||
export default Character |
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { Link as RouterLink } from 'react-router-dom' | ||
import { useQuery } from '@tanstack/react-query' | ||
import { getCharacters } from './api' | ||
|
||
export default function Characters() { | ||
const { status, data } = useQuery({ | ||
queryKey: ['characters'], | ||
queryFn: () => getCharacters(), | ||
}) | ||
|
||
if (status === 'pending') return <p>Loading...</p> | ||
if (status === 'error') return <p>Error :(</p> | ||
|
||
return ( | ||
<div> | ||
<h2 className="text-4xl">Characters</h2> | ||
{data.results.map((person: any) => { | ||
return ( | ||
<article key={person.id}> | ||
<RouterLink | ||
className="text-blue-500 hover:underline" | ||
to={`/characters/${person.id}`} | ||
> | ||
<h6 className="text-xl"> | ||
{person.name} - {person.gender}: {person.species} | ||
</h6> | ||
</RouterLink> | ||
</article> | ||
) | ||
})} | ||
</div> | ||
) | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Handle empty location URLs before deriving the location ID.
Line 17 assumes
data.location.url
is always populated. For characters whose location URL is an empty string (common in this API),locationId
becomesundefined
, and theLocation
component callsgetLocation(undefined)
, repeatedly 404ing and renderingError :(
instead of the known location name. Guard for a missing URL and fall back to the provided name.📝 Committable suggestion
🤖 Prompt for AI Agents