Skip to content

Commit

Permalink
Add list filtering
Browse files Browse the repository at this point in the history
refs #49
  • Loading branch information
franthormel committed Aug 22, 2024
1 parent b575391 commit 9739dcc
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 5 deletions.
41 changes: 38 additions & 3 deletions app/listings/actions.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
"use server";

import prisma from "@/lib/db";
import { Listing, PrismaListing } from "./types";
import { DEFAULT_LIST_FILTERS } from "./constants";
import { Listing, ListingsSearchFilters, PrismaListing } from "./types";

const prismaListingMapper = (dbListing: PrismaListing) => {
const dbListingPrice = dbListing.prices
Expand Down Expand Up @@ -30,13 +31,47 @@ const prismaListingMapper = (dbListing: PrismaListing) => {
return listing;
};

export async function fetchMatchedListings(): Promise<Listing[]> {
// TODO: Add filter param values
export async function fetchMatchedListings(
filters: ListingsSearchFilters = DEFAULT_LIST_FILTERS
): Promise<Listing[]> {
// TODO: Use beds filter values
// TODO: Use baths filter values
const prismaListings = await prisma.listing.findMany({
include: {
address: true,
prices: true,
},
where: {
// Filter (price)
prices: {
some: {
// Price must be current ...
isCurrent: true,
AND: {
// ... and must be greater than or equal than the minimum filter value ...
value: {
gte: filters.price.min.value,
},
AND: {
// ... and must be lesser than or equal than the maximum filter value
value: {
lte: filters.price.max.value,
},
},
},
},
},
// Filter (area): must be greater than or equal than the minimum filter value ...
area: {
gte: filters.area.min.value,
},
AND: {
// ... and must be lesser than or equal than the maximum filter value
area: {
lte: filters.area.max.value,
},
},
},
});
return prismaListings.map(prismaListingMapper);
}
2 changes: 1 addition & 1 deletion app/listings/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export const LISTINGS_PER_PAGE = 15;
export const STARTING_PAGE = 1;

const PRICE_MIN_FILTER = 0;
const PRICE_MAX_FILTER = 10_000_000;
const PRICE_MAX_FILTER = 100_000_000;
/**
* These are the labels for the beds/baths filter
* The values to be used is its index.
Expand Down
1 change: 0 additions & 1 deletion app/listings/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import ListingsProvider from "./provider";
import { ListingsSearchFilters } from "./search-filters";

export default async function Listings() {
// TODO: Use default filter values, import them from constants.tsx file
const listings = await fetchMatchedListings();

return (
Expand Down

0 comments on commit 9739dcc

Please sign in to comment.