-
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.
Update listings when new search filter is applied
refs #49
- Loading branch information
1 parent
5cfd24c
commit 3a9504c
Showing
12 changed files
with
168 additions
and
84 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,9 @@ | ||
import { fetchMatchedListings } from "@/app/listings/actions"; | ||
import { ListingsSearchFiltersRequest } from "@/app/listings/types"; | ||
|
||
export async function POST(request: Request) { | ||
const searchFiltersJson = await request.json(); | ||
const searchFilters = searchFiltersJson as ListingsSearchFiltersRequest; | ||
const listings = await fetchMatchedListings(searchFilters); | ||
return Response.json({ listings }); | ||
} |
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 |
---|---|---|
@@ -1,31 +1,50 @@ | ||
"use client" | ||
|
||
import { useContext } from "react" | ||
import { useContext, useEffect } from "react" | ||
import { ListingSortCompareFunctions, makeSearchFiltersRequest } from "./functions" | ||
import ListingsList from "./list" | ||
import { ListingsListCountSort } from "./list-count-sort" | ||
import { ListingsMap } from "./map" | ||
import ListingsPagination from "./pagination" | ||
import { ListingsContext } from "./provider" | ||
import { Listing, ListingSortCompareFunctions } from "./types" | ||
import { Listing } from "./types" | ||
|
||
interface ListingsContentInterface { | ||
listings: Listing[] | ||
} | ||
|
||
export default function ListingsContent(props: ListingsContentInterface) { | ||
// TODO: Use context filter values, try to use useMemo if applicable | ||
// TODO: try to use useMemo if applicable | ||
const context = useContext(ListingsContext); | ||
|
||
const sortedListings = props.listings.sort(ListingSortCompareFunctions.choose(context.sort.value)) | ||
useEffect(() => { | ||
async function updateListings() { | ||
const searchFiltersRequest = makeSearchFiltersRequest(context.searchFilters); | ||
const request = await fetch("/api/listings", { | ||
method: "POST", | ||
body: JSON.stringify(searchFiltersRequest), | ||
}); | ||
const json = await request.json() | ||
console.log | ||
|
||
|
||
|
||
context.listings.change(listings) | ||
} | ||
updateListings() | ||
}, []) | ||
|
||
|
||
const sortedListings = context.listings.value.sort(ListingSortCompareFunctions.choose(context.sort.value)) | ||
|
||
return ( | ||
<div className="flex h-[36rem]"> | ||
<ListingsMap listings={props.listings} /> | ||
{/* <ListingsMap listings={context.listings.value} /> | ||
<div className="flex basis-1/2 flex-col"> | ||
<ListingsListCountSort listingsCount={props.listings.length} /> | ||
<ListingsListCountSort /> | ||
<ListingsList listings={sortedListings} /> | ||
<ListingsPagination listingsCount={props.listings.length} /> | ||
</div> | ||
<ListingsPagination /> | ||
</div> */} | ||
</div> | ||
) | ||
} |
This file was deleted.
Oops, something went wrong.
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,67 @@ | ||
import { | ||
Listing, | ||
ListingSort, | ||
ListingsSearchFilters, | ||
ListingsSearchFiltersRequest, | ||
} from "./types"; | ||
|
||
export function countListingsToSkip(currentPage: number, countPerPage: number) { | ||
return (currentPage - 1) * countPerPage; | ||
} | ||
|
||
export function makeSearchFiltersRequest( | ||
original: ListingsSearchFilters | ||
): ListingsSearchFiltersRequest { | ||
return { | ||
price: { | ||
min: original.price.min.value, | ||
max: original.price.max.value, | ||
}, | ||
beds: original.beds.value, | ||
baths: original.baths.value, | ||
area: { | ||
min: original.area.min.value, | ||
max: original.area.max.value, | ||
}, | ||
}; | ||
} | ||
|
||
type ListingSortCompareFunction = (a: Listing, b: Listing) => number; | ||
|
||
export class ListingSortCompareFunctions { | ||
static #NEWEST: ListingSortCompareFunction = (a, b) => { | ||
if (a.createdDate < b.createdDate) { | ||
return 1; | ||
} | ||
|
||
if (a.createdDate === b.createdDate) { | ||
return 0; | ||
} | ||
|
||
return -1; | ||
}; | ||
|
||
static #PRICE_DESC: ListingSortCompareFunction = (a, b) => { | ||
return b.price.value - a.price.value; | ||
}; | ||
|
||
static #PRICE_ASC: ListingSortCompareFunction = (a, b) => { | ||
return a.price.value - b.price.value; | ||
}; | ||
|
||
static choose(sort: ListingSort): ListingSortCompareFunction { | ||
switch (sort) { | ||
case ListingSort.NEWEST: | ||
return ListingSortCompareFunctions.#NEWEST; | ||
break; | ||
case ListingSort.PRICE_DESC: | ||
return ListingSortCompareFunctions.#PRICE_DESC; | ||
break; | ||
case ListingSort.PRICE_ASC: | ||
return ListingSortCompareFunctions.#PRICE_ASC; | ||
break; | ||
default: | ||
return ListingSortCompareFunctions.#NEWEST; | ||
} | ||
} | ||
} |
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
Oops, something went wrong.