diff options
| author | Andrew Lee <alee14498@protonmail.com> | 2024-02-05 17:50:51 -0500 |
|---|---|---|
| committer | Andrew Lee <alee14498@protonmail.com> | 2024-02-05 17:50:51 -0500 |
| commit | 0fed60dc83db376ddb3f7c835894db29dfe51994 (patch) | |
| tree | 2c2fcbb10cd0e28c9dab92463e41174f1e759bf9 /src/components/Guestbook.jsx | |
| parent | dfdf64af769b97d2342cf8b3c6fa25106bfb9acf (diff) | |
| download | personal-website-0fed60dc83db376ddb3f7c835894db29dfe51994.tar.gz personal-website-0fed60dc83db376ddb3f7c835894db29dfe51994.tar.bz2 personal-website-0fed60dc83db376ddb3f7c835894db29dfe51994.zip | |
Pagination; Disabled form when sending
Diffstat (limited to 'src/components/Guestbook.jsx')
| -rw-r--r-- | src/components/Guestbook.jsx | 39 |
1 files changed, 31 insertions, 8 deletions
diff --git a/src/components/Guestbook.jsx b/src/components/Guestbook.jsx index a959b5a..5935d04 100644 --- a/src/components/Guestbook.jsx +++ b/src/components/Guestbook.jsx @@ -7,31 +7,52 @@ import GuestbookForm from "./GuestbookForm.jsx"; class Guestbook extends Component { state = { message: null, - error: null + error: null, + page: 1, }; - fetchMessages = async () => { + fetchMessages = async (page) => { + const perPage = 10; + try { - const message = await pb.collection('guestbook').getFullList({ + const result = await pb.collection('guestbook').getList(page, perPage, { sort: '-created', }); - this.setState({ message }); + this.setState({ + message: result.items, + totalPages: result.totalPages + }); } catch (error) { this.setState({ error: `Failed to fetch data: ${error.message}` }); console.error('Failed to fetch data:', error); } } + refresh = async () => { + await this.fetchMessages(this.state.page); + } + async componentDidMount() { - await this.fetchMessages(); + await this.fetchMessages(this.state.page); } - refresh = async () => { - await this.fetchMessages(); + handleNext = async () => { + const nextPage = this.state.page + 1; + if (nextPage > this.state.totalPages) { + return; + } + this.setState({ page: nextPage }); + await this.fetchMessages(nextPage); + } + + handlePrevious = async () => { + const previousPage = this.state.page - 1; + this.setState({ page: previousPage }); + await this.fetchMessages(previousPage); } render() { - const { message, error } = this.state; + const { message, error, page, totalPages } = this.state; return ( <div> @@ -52,6 +73,8 @@ class Guestbook extends Component { ))} </div> )} + {page > 1 && <button class="margin" onClick={this.handlePrevious}>Previous</button>} + {page < totalPages && <button class="margin" onClick={this.handleNext}>Next</button>} </div> ); } |
