From 7212a6495070547044cc98a511efb453b3487c97 Mon Sep 17 00:00:00 2001
From: Liam Stevens <8955671+liamstevens111@users.noreply.github.com>
Date: Wed, 15 Mar 2023 19:32:09 +0700
Subject: [PATCH] [#26] Add pagination to surveys wip
---
src/adapters/surveyAdapter.ts | 4 ++--
src/components/SurveyList/index.tsx | 14 +++++++-------
src/screens/Home/index.tsx | 12 +++++++++---
3 files changed, 18 insertions(+), 12 deletions(-)
diff --git a/src/adapters/surveyAdapter.ts b/src/adapters/surveyAdapter.ts
index d98001d..6e9a782 100644
--- a/src/adapters/surveyAdapter.ts
+++ b/src/adapters/surveyAdapter.ts
@@ -60,8 +60,8 @@ class SurveyAdapter extends BaseAdapter {
return this.prototype.getRequest('me', {});
}
- static list() {
- return this.prototype.getRequest('surveys', {});
+ static list(page: number) {
+ return this.prototype.getRequest(`surveys?page[number]=${page}`, {});
}
}
diff --git a/src/components/SurveyList/index.tsx b/src/components/SurveyList/index.tsx
index d264c03..507da24 100644
--- a/src/components/SurveyList/index.tsx
+++ b/src/components/SurveyList/index.tsx
@@ -6,9 +6,9 @@ import 'swiper/swiper-bundle.min.css';
import SurveyComponent from 'components/Survey';
import { Survey } from 'types/Survey';
-type SurveyListProps = { surveys: Survey[] };
+type SurveyListProps = { surveys: Survey[]; onPageChange: any };
-function SurveyList({ surveys }: SurveyListProps) {
+function SurveyList({ surveys, onPageChange }: SurveyListProps) {
return (
<>
{surveys.map((survey) => {
return (
- <>
-
-
-
- >
+
+
+
);
})}
diff --git a/src/screens/Home/index.tsx b/src/screens/Home/index.tsx
index 486abde..e0f88e6 100644
--- a/src/screens/Home/index.tsx
+++ b/src/screens/Home/index.tsx
@@ -9,10 +9,16 @@ import SurveyList from '../../components/SurveyList';
const HomeScreen = (): JSX.Element => {
const [loaded, setLoaded] = useState(false);
const [surveys, setSurveys] = useState([]);
+ const [surveyPage, setSurveyPage] = useState(1);
+
+ const handlePageChange = () => {
+ console.log('page change');
+ setSurveyPage(surveyPage + 1);
+ };
useEffect(() => {
const fetchSurveys = async () => {
- const surveyResponse = await SurveyAdapter.list();
+ const surveyResponse = await SurveyAdapter.list(surveyPage);
const surveyData: SurveyResponse = await surveyResponse.data;
@@ -33,7 +39,7 @@ const HomeScreen = (): JSX.Element => {
setLoaded(true);
};
fetchSurveys();
- }, []);
+ }, [surveyPage]);
if (!loaded) {
return Loading Surveys
;
@@ -47,7 +53,7 @@ const HomeScreen = (): JSX.Element => {
-
+
);