Skip to content

Commit

Permalink
fix(#13): refactor comparePurchaseUrgency to categorize and sort shop…
Browse files Browse the repository at this point in the history
…ping list items based on urgency

Categories are: inactive, overdue, and future.

Overdue items appear first, followed by future dates, and finally inactive items.
  • Loading branch information
NickRoccodev11 committed Sep 17, 2024
1 parent 69d35e8 commit 86518ec
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 32 deletions.
64 changes: 34 additions & 30 deletions src/api/firebase.js
Original file line number Diff line number Diff line change
Expand Up @@ -177,45 +177,49 @@ export async function shareList(listPath, currentUserId, recipientEmail) {
}

export function comparePurchaseUrgency(list) {
// Create a copy of the list to avoid mutating original array
const overdueItems = [];
const pendingItems = [];
const inactive = [];
const overdue = [];
const future = [];
//iterate through the list and categorize each item
list.forEach((item) => {
const dateToCompare = item.dateNextPurchased.toDate();
const now = new Date();
if (dateToCompare < now) {
overdueItems.push(item);
//positive numbers represent the past, negative numbers represent the future
const days = getDaysBetweenDates(item.dateNextPurchased);
if (days >= 60) {
//flip the days to negative for inactive items
item.sortCriteria = { tag: 'inactive', daysUntilNextPurchase: -days };
inactive.push(item);
} else if (days < 60 && days > 0) {
item.sortCriteria = { tag: 'overdue', daysUntilNextPurchase: days };
overdue.push(item);
} else {
pendingItems.push(item);
item.sortCriteria = { tag: 'future', daysUntilNextPurchase: days };
future.push(item);
}
});
//function to sort lists by days until next purchase and alphabetically if days are equal
const sortList = (list) => {
const sortedList = [...list].sort((a, b) => {
const daysA = getDaysBetweenDates(a.dateNextPurchased);
const daysB = getDaysBetweenDates(b.dateNextPurchased);
if (daysA < daysB) return -1;
if (daysB < daysA) return 1;
// If days are the same, sort alphabetically
return a.name.localeCompare(b.name);
if (
a.sortCriteria.daysUntilNextPurchase ===
b.sortCriteria.daysUntilNextPurchase
) {
//sorts alphabetically if days are the same
return a.name.localeCompare(b.name);
}
return (
//sort by days until next purchase
b.sortCriteria.daysUntilNextPurchase -
a.sortCriteria.daysUntilNextPurchase
);
});
return sortedList;
};
return sortList(overdueItems).concat(pendingItems);
// const sortedList = [...list].sort((a, b) => {
// const daysA = getDaysBetweenDates(a.dateNextPurchased);
// const daysB = getDaysBetweenDates(b.dateNextPurchased);
// a.daysUntilPurchase = daysA;
// b.daysUntilPurchase = daysB;
// //if daysA< 7 the call it soon
// //if daysA >7 and < 30 call it
// // Inactive items (60 days or more)
// // Sort by dats until next purchase
// if (daysA < daysB) return -1;
// if (daysB < daysA) return 1;
// // If days are the same, sort alphabetically
// return a.name.localeCompare(b.name);
// });
// return sortedList;

const sortedOverdue = sortList(overdue);
const sortedFuture = sortList(future);
const sortedInactive = sortList(inactive);

return sortedOverdue.concat(sortedFuture).concat(sortedInactive);
}

/**
Expand Down
1 change: 0 additions & 1 deletion src/utils/dates.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ export function getFutureDate(offset) {
}
export function getDaysBetweenDates(dateToCompare) {
const comparisonDate = dateToCompare.toDate();
console.log(comparisonDate);
const presentDate = new Date();
const diffInMilliseconds = presentDate.getTime() - comparisonDate.getTime();
return Math.round(diffInMilliseconds / ONE_DAY_IN_MILLISECONDS);
Expand Down
7 changes: 6 additions & 1 deletion src/views/List.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@ export function List({ data, listPath }) {
};

let sorted = comparePurchaseUrgency(data);
console.log(sorted);
let names = [];
sorted.forEach((item) => {
names.push(item.name);
});
console.log('names', names);

const clearSearchInput = () => {
setSearchInput('');
Expand All @@ -21,6 +25,7 @@ export function List({ data, listPath }) {
? item.name.toLowerCase().includes(searchInput.toLowerCase())
: item;
});
const listInfo = comparePurchaseUrgency(filterList);

return (
<>
Expand Down

0 comments on commit 86518ec

Please sign in to comment.