This repository has been archived by the owner on Nov 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
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
1 parent
07d6c5e
commit b0d49b7
Showing
11 changed files
with
149 additions
and
638 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
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
51 changes: 15 additions & 36 deletions
51
demos/frontend/05_serialize_custom_data/components/MovieList.tsx
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 |
---|---|---|
@@ -1,54 +1,33 @@ | ||
import { Card } from './Card' | ||
import { FC, useEffect, useMemo, useState } from 'react' | ||
import { FC, useEffect, useState } from 'react' | ||
import { Movie } from '../models/Movie' | ||
import * as web3 from '@solana/web3.js' | ||
import { MovieCoordinator } from '../coordinators/MovieCoordinator' | ||
import { Button, Center, HStack, Input, Spacer } from '@chakra-ui/react' | ||
|
||
const MOVIE_REVIEW_PROGRAM_ID = 'CenYq6bDRB7p73EjsPEpiYN7uveyPUTdXkDkgUduboaN' | ||
|
||
export const MovieList: FC = () => { | ||
const connection = new web3.Connection(web3.clusterApiUrl('devnet')) | ||
const [movies, setMovies] = useState<Movie[]>([]) | ||
const [page, setPage] = useState(1) | ||
const [search, setSearch] = useState('') | ||
|
||
useEffect(() => { | ||
MovieCoordinator.fetchPage( | ||
connection, | ||
page, | ||
5, | ||
search, | ||
search !== '' | ||
).then(setMovies) | ||
}, [page, search]) | ||
connection.getProgramAccounts(new web3.PublicKey(MOVIE_REVIEW_PROGRAM_ID)).then(async (accounts) => { | ||
const movies: Movie[] = accounts.reduce((accum: Movie[], { pubkey, account }) => { | ||
const movie = Movie.deserialize(account.data) | ||
if (!movie) { | ||
return accum | ||
} | ||
|
||
return [...accum, movie] | ||
}, []) | ||
setMovies(movies) | ||
}) | ||
}, []) | ||
|
||
return ( | ||
<div> | ||
<Center> | ||
<Input | ||
id='search' | ||
color='gray.400' | ||
onChange={event => setSearch(event.currentTarget.value)} | ||
placeholder='Search' | ||
w='97%' | ||
mt={2} | ||
mb={2} | ||
/> | ||
</Center> | ||
{ | ||
movies.map((movie, i) => <Card key={i} movie={movie} /> ) | ||
} | ||
<Center> | ||
<HStack w='full' mt={2} mb={8} ml={4} mr={4}> | ||
{ | ||
page > 1 && <Button onClick={() => setPage(page - 1)}>Previous</Button> | ||
} | ||
<Spacer /> | ||
{ | ||
MovieCoordinator.accounts.length > page * 5 && | ||
<Button onClick={() => setPage(page + 1)}>Next</Button> | ||
} | ||
</HStack> | ||
</Center> | ||
</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
Oops, something went wrong.