-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
515 additions
and
12 deletions.
There are no files selected for viewing
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,65 @@ | ||
import { useForwardPagination } from "../../src"; | ||
import { FragmentType, graphql, useFragment } from "../gql"; | ||
|
||
export const FilmCharactersConnectionFragment = graphql(` | ||
fragment FilmCharactersConnection on FilmCharactersConnection { | ||
edges { | ||
node { | ||
id | ||
name | ||
} | ||
} | ||
pageInfo { | ||
hasNextPage | ||
endCursor | ||
} | ||
} | ||
`); | ||
|
||
type Props = { | ||
characters: FragmentType<typeof FilmCharactersConnectionFragment>; | ||
onNextPage: (cursor: string | null) => void; | ||
isLoadingMore: boolean; | ||
}; | ||
|
||
export const FilmCharacterList = ({ | ||
characters, | ||
onNextPage, | ||
isLoadingMore, | ||
}: Props) => { | ||
const connection = useForwardPagination( | ||
useFragment(FilmCharactersConnectionFragment, characters), | ||
); | ||
|
||
if (connection.edges == null) { | ||
return null; | ||
} | ||
|
||
return ( | ||
<> | ||
<ul> | ||
{connection.edges.map((edge) => { | ||
if (edge == null) { | ||
return null; | ||
} | ||
const node = edge.node; | ||
if (node == null) { | ||
return null; | ||
} | ||
return <li key={node.id}>{node.name}</li>; | ||
})} | ||
</ul> | ||
|
||
{isLoadingMore ? <div>Loading more</div> : null} | ||
|
||
{connection.pageInfo.hasNextPage ? ( | ||
<button | ||
onClick={() => onNextPage(connection.pageInfo.endCursor ?? null)} | ||
disabled={isLoadingMore} | ||
> | ||
Load more | ||
</button> | ||
) : null} | ||
</> | ||
); | ||
}; |
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,76 @@ | ||
import { useState } from "react"; | ||
import { useQuery } from "../../src"; | ||
import { graphql } from "../gql"; | ||
import { FilmCharacterList } from "./FilmCharacterList"; | ||
|
||
export const FilmDetailsQuery = graphql(` | ||
query FilmDetails($filmId: ID!, $first: Int!, $after: String) { | ||
film(id: $filmId) { | ||
id | ||
title | ||
director | ||
openingCrawl | ||
characterConnection(first: $first, after: $after) { | ||
...FilmCharactersConnection | ||
} | ||
releaseDate | ||
} | ||
} | ||
`); | ||
|
||
type Props = { | ||
filmId: string; | ||
optimize: boolean; | ||
}; | ||
|
||
export const FilmDetails = ({ filmId, optimize }: Props) => { | ||
const [after, setAfter] = useState<string | null>(null); | ||
const [data, { isLoading }] = useQuery( | ||
FilmDetailsQuery, | ||
{ | ||
filmId, | ||
first: 5, | ||
after, | ||
}, | ||
{ optimize }, | ||
); | ||
|
||
return ( | ||
<div className="FilmDetails" style={{ opacity: isLoading ? 0.5 : 1 }}> | ||
{data.match({ | ||
NotAsked: () => null, | ||
Loading: () => <div>Loading ...</div>, | ||
Done: (result) => | ||
result.match({ | ||
Error: () => <div>An error occured</div>, | ||
Ok: ({ film }) => { | ||
if (film == null) { | ||
return <div>No film</div>; | ||
} | ||
return ( | ||
<> | ||
<h1>{film.title}</h1> | ||
<div>Director: {film.director}</div> | ||
<div>Release date: {film.releaseDate}</div> | ||
<div> | ||
Opening crawl: | ||
<pre>{film.openingCrawl}</pre> | ||
</div> | ||
{film.characterConnection != null ? ( | ||
<> | ||
<h2>Characters</h2> | ||
<FilmCharacterList | ||
characters={film.characterConnection} | ||
onNextPage={setAfter} | ||
isLoadingMore={isLoading} | ||
/> | ||
</> | ||
) : null} | ||
</> | ||
); | ||
}, | ||
}), | ||
})} | ||
</div> | ||
); | ||
}; |
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
Oops, something went wrong.