diff --git a/src/api/firebase.js b/src/api/firebase.js index f0c3489..29b14ca 100644 --- a/src/api/firebase.js +++ b/src/api/firebase.js @@ -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; } /** diff --git a/src/utils/dates.js b/src/utils/dates.js index 7601dc9..185c2d9 100644 --- a/src/utils/dates.js +++ b/src/utils/dates.js @@ -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); } diff --git a/src/views/List.jsx b/src/views/List.jsx index cc16037..d82cdf5 100644 --- a/src/views/List.jsx +++ b/src/views/List.jsx @@ -9,6 +9,9 @@ export function List({ data, listPath }) { setSearchInput(e.target.value); }; + let sorted = comparePurchaseUrgency(data); + console.log(sorted); + const clearSearchInput = () => { setSearchInput(''); };