-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add interactivity between pagination and list
fixes #49
- Loading branch information
1 parent
2c5726f
commit 94a5de9
Showing
12 changed files
with
161 additions
and
86 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
"use server"; | ||
|
||
import prisma from "@/lib/db"; | ||
import { LISTINGS_PER_PAGE } from "./constants"; | ||
import { countListingsToSkip } from "./function"; | ||
import { Listing } from "./types"; | ||
|
||
export async function fetchListingsByPage(currentPage: number) { | ||
const dbListings = await prisma.listing.findMany({ | ||
skip: countListingsToSkip(currentPage, LISTINGS_PER_PAGE), | ||
take: LISTINGS_PER_PAGE, | ||
include: { | ||
address: true, | ||
prices: true, | ||
}, | ||
}); | ||
const processedListings = dbListings.map((dbListing) => { | ||
const dbListingPrice = dbListing.prices | ||
.filter((dbListingPrice) => dbListingPrice.isCurrent) | ||
.at(0)! | ||
.value.toNumber(); | ||
const dbListingAddress = dbListing.address!; | ||
const listing: Listing = { | ||
id: dbListing.id, | ||
imageUrls: dbListing.imageUrls, | ||
beds: dbListing.beds, | ||
baths: dbListing.baths, | ||
area: dbListing.area.toNumber(), | ||
createdDate: dbListing.createdDate, | ||
address: { | ||
addressLine: dbListingAddress.addressLine, | ||
city: dbListingAddress.city, | ||
state: dbListingAddress.state, | ||
longitude: dbListingAddress.longitude.toNumber(), | ||
latitude: dbListingAddress.latitude.toNumber(), | ||
}, | ||
price: { | ||
value: dbListingPrice, | ||
}, | ||
}; | ||
return listing; | ||
}); | ||
|
||
return processedListings; | ||
} |
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,2 +1,2 @@ | ||
export const LISTINGS_PER_PAGE = 32; | ||
export const LISTINGS_PER_PAGE = 5; | ||
export const STARTING_PAGE = 1; |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,4 +13,4 @@ export default function ButtonsSegmentedLabelled(props: ButtonsSegmentedLabelled | |
</div> | ||
</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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,21 @@ | ||
/** | ||
* | ||
* Show the previous button only if the current page is not the first page | ||
* Enable the previous button only if the current page is not the first page | ||
* | ||
* @param currentPage Current page | ||
* @returns `true` if the previous button should be shown, otherwise false. | ||
* @returns `true` if the previous button should be enabled, otherwise false. | ||
*/ | ||
export function checkShowPreviousButton(currentPage: number) { | ||
export function checkPreviousButton(currentPage: number) { | ||
return currentPage > 1; | ||
} | ||
|
||
/** | ||
* Show the next button only if the current page is not the last page | ||
* Enable the next button only if the current page is not the last page | ||
* | ||
* @param pages Total number of pages | ||
* @param currentPage Current page | ||
* @returns `true` if the next button should be shown, otherwise false. | ||
* @returns `true` if the next button should be enabled, otherwise false. | ||
*/ | ||
export function checkShowNextButton(pages: number, currentPage: number) { | ||
export function checkNextButton(pages: number, currentPage: number) { | ||
return currentPage < pages; | ||
} |
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,43 +1,64 @@ | ||
import { checkShowNextButton, checkShowPreviousButton } from "./function" | ||
import { checkNextButton, checkPreviousButton } from "./function" | ||
import PaginationItemNav from "./item-nav" | ||
import PaginationItemNumber from "./item-number" | ||
|
||
export interface PaginationProps { | ||
pages: number | ||
currentPage: number | ||
changeToPreviousPage?: () => void | ||
changeCurrentPage?: (page: number) => void | ||
changeToNextPage?: () => void | ||
} | ||
|
||
export default function Pagination(props: PaginationProps) { | ||
const pageNumbers = Array.from({ length: props.pages }, (elem, i) => i + 1) | ||
const showPreviousButton = checkShowPreviousButton(props.currentPage); | ||
const showNextButton = checkShowNextButton(props.pages, props.currentPage); | ||
const previousButtonEnabled = checkPreviousButton(props.currentPage); | ||
const nextButtonEnabled = checkNextButton(props.pages, props.currentPage); | ||
|
||
return ( | ||
<div className="flex gap-3"> | ||
{/* Previous (hidden if the current page is the first page) */} | ||
{showPreviousButton && | ||
<div className={`${previousButtonEnabled ? 'visible' : 'invisible'}`} | ||
onClick={(e) => { | ||
if (props.changeToPreviousPage && previousButtonEnabled) { | ||
props.changeToPreviousPage() | ||
} | ||
}}> | ||
<PaginationItemNav> | ||
<svg xmlns="http://www.w3.org/2000/svg" height="24px" width="24px" viewBox="0 -960 960 960"> | ||
<path d="M560-240 320-480l240-240 56 56-184 184 184 184-56 56Z" /> | ||
</svg> | ||
</PaginationItemNav> | ||
} | ||
</div> | ||
{pageNumbers.map((pageNumber) => { | ||
const isCurrentPage = props.currentPage === pageNumber | ||
return <PaginationItemNumber | ||
page={pageNumber} | ||
key={pageNumber} | ||
isCurrentPage={isCurrentPage} | ||
/> | ||
return ( | ||
<div key={pageNumber} | ||
onClick={(e) => { | ||
if (props.changeCurrentPage) { | ||
props.changeCurrentPage(pageNumber); | ||
} | ||
}}> | ||
<PaginationItemNumber | ||
page={pageNumber} | ||
isCurrentPage={isCurrentPage} | ||
/> | ||
</div> | ||
) | ||
})} | ||
{/* Next (hidden if current page is the last page) */} | ||
{showNextButton && | ||
<div className={`${nextButtonEnabled ? 'visible' : 'invisible'}`} | ||
onClick={(e) => { | ||
if (props.changeToNextPage && nextButtonEnabled) { | ||
props.changeToNextPage() | ||
} | ||
}}> | ||
<PaginationItemNav> | ||
<svg xmlns="http://www.w3.org/2000/svg" height="24px" width="24px" viewBox="0 -960 960 960"> | ||
<path d="M504-480 320-664l56-56 240 240-240 240-56-56 184-184Z" /> | ||
</svg> | ||
</PaginationItemNav> | ||
} | ||
</div> | ||
</div> | ||
) | ||
} |
Oops, something went wrong.