Skip to content

Commit

Permalink
[#26] Add pagination to surveys wip
Browse files Browse the repository at this point in the history
  • Loading branch information
liamstevens111 committed Mar 15, 2023
1 parent 7b9bc4a commit 7212a64
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 12 deletions.
4 changes: 2 additions & 2 deletions src/adapters/surveyAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`, {});
}
}

Expand Down
14 changes: 7 additions & 7 deletions src/components/SurveyList/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
<>
<Swiper
Expand All @@ -18,14 +18,14 @@ function SurveyList({ surveys }: SurveyListProps) {
spaceBetween={100}
pagination={{ el: '.swiper-pagination', clickable: true, dynamicBullets: true }}
slidesPerView={1}
onReachEnd={onPageChange}
rebuildOnUpdate={true}
>
{surveys.map((survey) => {
return (
<>
<SwiperSlide key={survey.id}>
<SurveyComponent {...survey} />
</SwiperSlide>
</>
<SwiperSlide key={survey.id}>
<SurveyComponent {...survey} />
</SwiperSlide>
);
})}
</Swiper>
Expand Down
12 changes: 9 additions & 3 deletions src/screens/Home/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,16 @@ import SurveyList from '../../components/SurveyList';
const HomeScreen = (): JSX.Element => {
const [loaded, setLoaded] = useState(false);
const [surveys, setSurveys] = useState<Survey[]>([]);
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;

Expand All @@ -33,7 +39,7 @@ const HomeScreen = (): JSX.Element => {
setLoaded(true);
};
fetchSurveys();
}, []);
}, [surveyPage]);

if (!loaded) {
return <h3 data-test-id="loading-surveys">Loading Surveys</h3>;
Expand All @@ -47,7 +53,7 @@ const HomeScreen = (): JSX.Element => {
</div>

<div className="w-screen max-w-3xl">
<SurveyList surveys={surveys} />
<SurveyList surveys={surveys} onPageChange={handlePageChange} />
</div>
</section>
);
Expand Down

0 comments on commit 7212a64

Please sign in to comment.