Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemented pagination feature to my surveys page #143

Merged
merged 2 commits into from
Apr 14, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 30 additions & 5 deletions src/pages/surveys/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Timestamp } from 'firebase/firestore';

import { DocumentData, Timestamp } from 'firebase/firestore';
import Image from 'next/image';
import Head from 'next/head';
import { formatDateDistance } from 'shared/utilities/convertTime';
Expand All @@ -11,11 +10,14 @@ import SurveyRow from 'features/surveys/components/SurveyRow/SurveyRow';
import { useSurveyListManager } from 'features/surveys/managers/surveyListManager';
import NoSurveys from '../../../public/images/no-surveys.svg';
import Link from 'next/link';
import { ButtonVariant } from 'shared/components/Button/Button';
import Button, { ButtonVariant } from 'shared/components/Button/Button';
import ButtonLink from 'shared/components/ButtonLink/ButtonLink';
import usePagination from 'features/surveys/hooks/usePagination';
import { ArrowLeftIcon, ArrowRightIcon} from '@heroicons/react/outline';

function SurveyListPage() {
const { error, loading, surveysCollection } = useSurveyListManager();
const { items, canGoNext, canGoPrev, goNext, goPrev, pageIndex } = usePagination<DocumentData>(surveysCollection?.docs ?? [], { size: 10 });

return (
<>
Expand All @@ -34,8 +36,8 @@ function SurveyListPage() {
{loading && <Loader isLoading={true} />}
</div>
{surveysCollection &&
(surveysCollection.docs?.length > 0 ? (
surveysCollection.docs.map((doc) => {
(items?.length > 0 ? (
items.map((doc) => {
const survey = doc.data();
return (
<SurveyRow
Expand Down Expand Up @@ -69,6 +71,29 @@ function SurveyListPage() {
</>
))}
</div>
{(canGoNext || canGoPrev) && (
<div className='flex justify-center'>
<div className='flex flex-row items-center'>
<Button
variant={ButtonVariant.OUTLINE_PRIMARY}
className='px-4'
icon={<ArrowLeftIcon className='h-5 w-5' />}
disabled={!canGoPrev}
onClick={goPrev}
/>
<div className='min-w-[100px]'>
<p className='text-center'>{pageIndex + 1}</p>
</div>
<Button
variant={ButtonVariant.OUTLINE_PRIMARY}
className='px-4'
icon={<ArrowRightIcon className='h-5 w-5' />}
disabled={!canGoNext}
onClick={goNext}
/>
</div>
</div>
)}
</>
);
}
Expand Down