-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add: Implement complete detail pages
- Loading branch information
1 parent
4cd9987
commit 4d197ec
Showing
15 changed files
with
212 additions
and
24 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains 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,8 @@ | ||
import { Character } from 'rickmortyapi'; | ||
import MiniCard from './MiniCard'; | ||
|
||
export default function CharacterCard({ character }: { character: Character }) { | ||
return ( | ||
<MiniCard title={character.name} image={character.image} description={character.species} /> | ||
); | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains 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,40 @@ | ||
import React from "react"; | ||
import { Link } from "react-router-dom"; | ||
|
||
export type DetailFacts = { | ||
type: string; | ||
value: string; | ||
url?: string; | ||
} | ||
|
||
export default function DetailsLayout({ image, title, facts, childrenTitle, children }: { image: string, title: string, facts: DetailFacts[], childrenTitle: string, children?: React.ReactNode }) { | ||
return ( | ||
<section className="flex flex-col md:flex-row gap-4 p-4"> | ||
<div className="flex flex-col md:flex-row bg-white dark:bg-gray-800 shadow-lg rounded-lg"> | ||
<div className="p-4 flex-1"> | ||
<img className="mx-auto" src={image} alt={title} /> | ||
<h1 className="text-2xl font-bold mb-4 dark:text-white">{title}</h1> | ||
|
||
<dl className="space-y-2"> | ||
{facts.map((fact, index) => ( | ||
<div key={index} className="flex justify-between items-center hover:bg-gray-200 dark:hover:bg-gray-700 p-2 rounded"> | ||
<dt className="font-semibold dark:text-gray-300">{fact.type}</dt> | ||
<dd className="dark:text-gray-300"> | ||
{fact.url ? ( | ||
<Link to={fact.url} className="text-blue-500 hover:underline dark:text-blue-400">{fact.value}</Link> | ||
) : fact.value} | ||
</dd> | ||
</div> | ||
))} | ||
</dl> | ||
</div> | ||
</div> | ||
<div className="flex-1"> | ||
<h2 className="text-xl text-left font-semibold mb-4">{childrenTitle}:</h2> | ||
<div className='grid grid-cols-1 sm:grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-4'> | ||
{children} | ||
</div> | ||
</div> | ||
</section> | ||
); | ||
} |
This file contains 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 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 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 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 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,46 @@ | ||
import React from "react"; | ||
import { Link, useLoaderData } from "react-router-dom"; | ||
import { Character, Episode, getEpisode } from "rickmortyapi"; | ||
import { parseAPIId } from "../loaders"; | ||
import EpisodeCard from "../components/EpisodeCard"; | ||
import DetailsLayout, { DetailFacts } from "../components/DetailsLayout"; | ||
|
||
export default function CharacterDetails() { | ||
const char = useLoaderData() as Character; | ||
const [episodes, setEpisodes] = React.useState<Episode[]>([]); | ||
const originId = parseAPIId(char.origin.url); | ||
const locationId = parseAPIId(char.location.url); | ||
|
||
const facts: DetailFacts[] = [ | ||
{ type: "Species", value: char.species }, | ||
{ type: "Status", value: char.status }, | ||
{ type: "Gender", value: char.gender }, | ||
{ type: "Origin", value: char.origin.name, url: originId !== -1 ? `/locations/${originId}` : undefined }, | ||
{ type: "Location", value: char.location.name, url: locationId !== -1 ? `/locations/${locationId}` : undefined } | ||
] | ||
|
||
React.useEffect(() => { | ||
const episodeList = char.episode.map((episode) => parseAPIId(episode)); | ||
getEpisode(episodeList).then((data) => { | ||
if (data.status === 200 && data.data) { | ||
if (data.data.length) { | ||
setEpisodes(data.data) | ||
} else if (typeof data.data === 'object') { | ||
// XXX: Wrong type from client library, when there's only one result the type is Episode and not Episode[] | ||
// @ts-ignore | ||
setEpisodes([data.data]); | ||
} | ||
} | ||
}) | ||
}, [episodes]) | ||
|
||
return ( | ||
<DetailsLayout title={char.name} image={char.image} facts={facts} childrenTitle={"Episodes"}> | ||
{episodes.length > 0 ? episodes.map((episode, index) => ( | ||
<Link to={`/episodes/${episode.id}`} key={index}> | ||
<EpisodeCard episode={episode} /> | ||
</Link> | ||
)) : "Loading..."} | ||
</DetailsLayout> | ||
); | ||
} |
This file contains 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 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,52 @@ | ||
import { Character, Episode, getCharacter } from "rickmortyapi"; | ||
import DetailsLayout, { DetailFacts } from "../components/DetailsLayout"; | ||
import { Link, useLoaderData } from "react-router-dom"; | ||
import { useEffect, useState } from "react"; | ||
import { parseAPIId } from "../loaders"; | ||
import CharacterCard from "../components/CharacterCard"; | ||
|
||
import Season1 from "/seasons/s01.jpg"; | ||
import Season2 from "/seasons/s02.jpg"; | ||
import Season3 from "/seasons/s03.jpg"; | ||
import Season4 from "/seasons/s04.jpg"; | ||
import Season5 from "/seasons/s05.jpg"; | ||
import Season6 from "/seasons/s06.jpg"; | ||
|
||
export default function EpisodeDetails() { | ||
const episode = useLoaderData() as Episode; | ||
const season = parseInt(episode.episode.slice(2, 3), 10); | ||
const images = [Season1, Season2, Season3, Season4, Season5, Season6]; | ||
const image = season <= 6 ? images[season - 1] : images[0]; | ||
const [characters, setCharacters] = useState<Character[]>([]); | ||
|
||
const DetailFacts: DetailFacts[] = [ | ||
{ type: "Episode", value: episode.episode }, | ||
{ type: "Aired", value: episode.air_date }, | ||
{ type: "Characters", value: `${episode.characters.length} characters` }, | ||
] | ||
|
||
useEffect(() => { | ||
const characterList = episode.characters.map((episode) => parseAPIId(episode)); | ||
getCharacter(characterList).then((data) => { | ||
if (data.status === 200 && data.data) { | ||
if (data.data.length) { | ||
setCharacters(data.data) | ||
} else if (typeof data.data === 'object') { | ||
// XXX: Wrong type from client library, when there's only one result the type is Episode and not Episode[] | ||
// @ts-ignore | ||
setCharacters([data.data]); | ||
} | ||
} | ||
}) | ||
}, [characters]) | ||
|
||
return ( | ||
<DetailsLayout title={episode.name} image={image} facts={DetailFacts} childrenTitle={"Characters"}> | ||
{characters.length > 0 ? characters.map((character, index) => ( | ||
<Link to={`/characters/${character.id}`} key={index}> | ||
<CharacterCard character={character} /> | ||
</Link> | ||
)) : "Loading..."} | ||
</DetailsLayout> | ||
) | ||
} |
This file contains 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 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,43 @@ | ||
import { Link, useLoaderData } from "react-router-dom"; | ||
import DetailsLayout, { DetailFacts } from "../components/DetailsLayout"; | ||
import { Character, getCharacter, Location } from "rickmortyapi"; | ||
import PortalImage from "/portal.png" | ||
import PlanetImage from "/planet.png" | ||
import { useEffect, useState } from "react"; | ||
import { parseAPIId } from "../loaders"; | ||
import CharacterCard from "../components/CharacterCard"; | ||
|
||
export default function LocationDetails() { | ||
const location = useLoaderData() as Location; | ||
const facts:DetailFacts[] = [ | ||
{ type: "Type", value: location.type }, | ||
{ type: "Dimension", value: location.dimension }, | ||
{ type: "Residents", value: `${location.residents.length} residents` } | ||
] | ||
const [characters, setCharacters] = useState<Character[]>([]); | ||
|
||
useEffect(() => { | ||
const characterList = location.residents.map((resident) => parseAPIId(resident)); | ||
getCharacter(characterList).then((data) => { | ||
if (data.status === 200 && data.data) { | ||
if (data.data.length) { | ||
setCharacters(data.data) | ||
} else if (typeof data.data === 'object') { | ||
// XXX: Wrong type from client library, when there's only one result the type is Episode and not Episode[] | ||
// @ts-ignore | ||
setCharacters([data.data]); | ||
} | ||
} | ||
}) | ||
}, [characters]) | ||
|
||
return ( | ||
<DetailsLayout title={location.name} image={location.type === "Planet" ? PlanetImage : PortalImage} facts={facts} childrenTitle="Residents"> | ||
{characters.length > 0 ? characters.map((character, index) => ( | ||
<Link to={`/characters/${character.id}`} key={index}> | ||
<CharacterCard character={character} /> | ||
</Link> | ||
)) : "Loading..."} | ||
</DetailsLayout> | ||
); | ||
} |
This file contains 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