Skip to content

Commit

Permalink
created comparePurchaseUrgency function
Browse files Browse the repository at this point in the history
Co-authored-by: Falak  <[email protected]>
  • Loading branch information
bbland1 and zahrafalak committed Sep 19, 2024
1 parent 855a7c2 commit badabe5
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 4 deletions.
58 changes: 58 additions & 0 deletions src/api/firebase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -301,3 +301,61 @@ export async function deleteItem() {
* this function must accept!
*/
}

export function comparePurchaseUrgency(item1: ListItem, item2: ListItem) {
const currentDate = new Date();

/**
* getDaysBetweenDates(currentDate, item1.daySinceLastPurchase.toDate())
* boolean function to check if >60 days have passed since last purchase (item){
* if dateLastPurchase is null: return false
* const daysSinceLastPurchase >= 60 return true
* }
*
*
* if(itemAinactivefor60days && !itemBinactivefor60days)
* return 1
* const daysSinceItemLastActivity = item1.daySinceLastPurchase ? getDaysBetweenDates(currentDate, item1.daySinceLastPurchase.toDate()) : getDaysBetweenDates(currentDate, item1.dayCreated.toDate())
* if (daysSinceItemLastActivity >= 60) {
* return 1
* }
*
* if (getDaysBetweenDates(currentDate, item1.dateNextPurchase.toDate()) < getDaysBetweenDates(currentDate, item2.dateNextPurchase.toDate())) {
* return -1
* } else if (getDaysBetweenDates(currentDate, item1.dateNextPurchase.toDate()) > getDaysBetweenDates(currentDate, item2.dateNextPurchase.toDate())) {
* return 1
* } else {
* if (item1.name.toLowerCase() < item2.name.toLowerCase()) {
* return -1
* } else {
* return 1
* }
* }
*
* */
const daysSinceItemLastActivity = item1.dateLastPurchased
? getDaysBetweenDates(currentDate, item1.dateLastPurchased.toDate())
: getDaysBetweenDates(currentDate, item1.dateCreated.toDate());

if (daysSinceItemLastActivity >= 60) {
return 1;
}

if (
getDaysBetweenDates(currentDate, item1.dateNextPurchased.toDate()) <
getDaysBetweenDates(currentDate, item2.dateNextPurchased.toDate())
) {
return -1;
} else if (
getDaysBetweenDates(currentDate, item1.dateNextPurchased.toDate()) >
getDaysBetweenDates(currentDate, item2.dateNextPurchased.toDate())
) {
return 1;
} else {
if (item1.name.toLowerCase() < item2.name.toLowerCase()) {
return -1;
} else {
return 1;
}
}
}
10 changes: 6 additions & 4 deletions src/views/authenticated/List.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { useState, useMemo } from "react";
import { ListItemCheckBox } from "../../components/ListItem";
import { FilterListInput } from "../../components/FilterListInput";
import { ListItem } from "../../api";
import { ListItem, comparePurchaseUrgency } from "../../api";
import { useNavigate } from "react-router-dom";

interface Props {
Expand All @@ -14,9 +14,11 @@ export function List({ data: unfilteredListItems, listPath }: Props) {
const [searchTerm, setSearchTerm] = useState<string>("");

const filteredListItems = useMemo(() => {
return unfilteredListItems.filter((item) =>
item.name.toLowerCase().includes(searchTerm.toLowerCase()),
);
return unfilteredListItems
.filter((item) =>
item.name.toLowerCase().includes(searchTerm.toLowerCase()),
)
.sort(comparePurchaseUrgency);
}, [searchTerm, unfilteredListItems]);

// Early return if the list is empty
Expand Down

0 comments on commit badabe5

Please sign in to comment.