Skip to content

Commit

Permalink
fix: update comparePurchase urgency function to handle overdue dates
Browse files Browse the repository at this point in the history
filter shopping list to separate out past purchase dates and pending purchase dates into two lists

append the lists to eachother so overdue dates come first
  • Loading branch information
NickRoccodev11 committed Sep 16, 2024
1 parent bdec39e commit 69d35e8
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 13 deletions.
47 changes: 37 additions & 10 deletions src/api/firebase.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,17 +178,44 @@ export async function shareList(listPath, currentUserId, recipientEmail) {

export function comparePurchaseUrgency(list) {
// Create a copy of the list to avoid mutating original array
const sortedList = [...list].sort((a, b) => {
const daysA = getDaysBetweenDates(a.dateNextPurchased);
const daysB = getDaysBetweenDates(b.dateNextPurchased);
// 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);
const overdueItems = [];
const pendingItems = [];
list.forEach((item) => {
const dateToCompare = item.dateNextPurchased.toDate();
const now = new Date();
if (dateToCompare < now) {
overdueItems.push(item);
} else {
pendingItems.push(item);
}
});
return sortedList;
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);
});
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;
}

/**
Expand Down
4 changes: 1 addition & 3 deletions src/utils/dates.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ export function getDaysBetweenDates(dateToCompare) {
const comparisonDate = dateToCompare.toDate();
console.log(comparisonDate);
const presentDate = new Date();
const diffInMilliseconds = Math.abs(
presentDate.getTime() - comparisonDate.getTime(),
);
const diffInMilliseconds = presentDate.getTime() - comparisonDate.getTime();
return Math.round(diffInMilliseconds / ONE_DAY_IN_MILLISECONDS);
}
3 changes: 3 additions & 0 deletions src/views/List.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ export function List({ data, listPath }) {
setSearchInput(e.target.value);
};

let sorted = comparePurchaseUrgency(data);
console.log(sorted);

const clearSearchInput = () => {
setSearchInput('');
};
Expand Down

0 comments on commit 69d35e8

Please sign in to comment.