Skip to content

Commit

Permalink
Merge pull request #30 from the-collab-lab/jk-bm-estimated-next-date
Browse files Browse the repository at this point in the history
Jk bm estimated next date
  • Loading branch information
borjaMarti authored Mar 7, 2024
2 parents a7494dd + d5505f7 commit bdd0ac7
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 2 deletions.
10 changes: 10 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"npm": ">=8.19.0"
},
"dependencies": {
"@the-collab-lab/shopping-list-utils": "^2.2.0",
"firebase": "^10.1.0",
"react": "^18.2.0",
"react-dom": "^18.2.0",
Expand Down
28 changes: 26 additions & 2 deletions src/api/firebase.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,12 @@ import {
} from 'firebase/firestore';
import { useEffect, useState } from 'react';
import { db } from './config';
import { getFutureDate, isMoreThanADayAgo } from '../utils';
import {
getFutureDate,
getDaysBetweenDates,
isMoreThanADayAgo,
} from '../utils';
import { calculateEstimate } from '@the-collab-lab/shopping-list-utils';

/**
* A custom hook that subscribes to the user's shopping lists in our Firestore
Expand Down Expand Up @@ -184,10 +189,29 @@ export async function updateItem(listPath, itemId) {
const itemDocumentRef = doc(listCollectionRef, itemId);
const item = await getDoc(itemDocumentRef);
const itemTotalPurchases = item.data().totalPurchases;
const dateLastPurchased = item.data().dateLastPurchased?.toDate();
const dateCreated = item.data().dateCreated.toDate();
const dateNextPurchased = item.data().dateNextPurchased.toDate();
const now = new Date();
const daysSinceLastPurchase = getDaysBetweenDates(
now,
dateLastPurchased ? dateLastPurchased : dateCreated,
);
const prevEstimate = dateLastPurchased
? getDaysBetweenDates(dateNextPurchased, dateLastPurchased)
: getDaysBetweenDates(dateNextPurchased, dateCreated);
const nextEstimate = calculateEstimate(
prevEstimate,
dateLastPurchased ? daysSinceLastPurchase : prevEstimate,
itemTotalPurchases,
);

await updateDoc(itemDocumentRef, {
dateLastPurchased: new Date(),
dateLastPurchased: now,
dateNextPurchased: getFutureDate(nextEstimate),
totalPurchases: itemTotalPurchases + 1,
});

return itemDocumentRef;
}

Expand Down
9 changes: 9 additions & 0 deletions src/utils/dates.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,12 @@ export const isMoreThanADayAgo = (date) => {
const diff = now - dateInMiliseconds;
return ONE_DAY_IN_MILLISECONDS < diff;
};

/**
* Returns the number of days elapsed between two dates.
* @param {Date} dateFuture
* @param {Date} datePast
*/
export function getDaysBetweenDates(dateFuture, datePast) {
return Math.ceil((dateFuture.getTime() - datePast.getTime()) / 86400000);
}

0 comments on commit bdd0ac7

Please sign in to comment.