Skip to content

Commit

Permalink
Merge pull request #29 from the-collab-lab/sd-nr-11
Browse files Browse the repository at this point in the history
Issue 11 Estimate next purchase date
  • Loading branch information
stacy-tech authored Sep 14, 2024
2 parents dce1088 + 3a52628 commit f21161b
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 16 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",
"@uidotdev/usehooks": "^2.4.1",
"firebase": "^10.12.5",
"react": "^18.3.1",
Expand Down
38 changes: 28 additions & 10 deletions src/api/firebase.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import {
} from 'firebase/firestore';
import { useEffect, useState } from 'react';
import { db } from './config';
import { getFutureDate } from '../utils';

import { getFutureDate, getDaysBetweenDates } 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
* database and returns new data whenever the lists change.
Expand Down Expand Up @@ -195,23 +195,41 @@ export async function addItem(listPath, { itemName, daysUntilNextPurchase }) {
dateNextPurchased: getFutureDate(daysUntilNextPurchase),
name: itemName,
totalPurchases: 0,
purchaseInterval: daysUntilNextPurchase,
});
}

export async function updateItem(
listPath,
{ itemId, totalPurchases, dateLastPurchased },
{ itemId, totalPurchases, dateLastPurchased, purchaseInterval, dateCreated },
) {
/**
* TODO: Fill this out so that it uses the correct Firestore function
* to update an existing item. You'll need to figure out what arguments
* this function must accept!
*/
const itemDocRef = doc(db, `${listPath}/items`, itemId);

// Calculate days since last purchase with a conditional check
let daysSinceLastPurchase;

if (dateLastPurchased) {
daysSinceLastPurchase = getDaysBetweenDates(dateLastPurchased);
} else {
daysSinceLastPurchase = getDaysBetweenDates(dateCreated);
}

// Calculate days until next purchase
const daysUntilNextPurchase = calculateEstimate(
purchaseInterval, // The interval between purchases
daysSinceLastPurchase, // The number of days since last purchase
totalPurchases, // The total number of purchases made
);

// Update Purchase Interval with the estimated days & Next Purchase Date
purchaseInterval = daysUntilNextPurchase;

const dateNextPurchased = getFutureDate(daysUntilNextPurchase);
await updateDoc(itemDocRef, {
totalPurchases,
dateLastPurchased,
totalPurchases: totalPurchases + 1,
dateLastPurchased: new Date(),
dateNextPurchased,
purchaseInterval,
});
}

Expand Down
9 changes: 6 additions & 3 deletions src/components/ListItem.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ export function ListItem({
itemId,
totalPurchases,
dateLastPurchased,
purchaseInterval,
dateCreated,
}) {
const [purchased, setPurchased] = useToggle(false);
const [isDisabled, setIsDisabled] = useState(false);

useEffect(() => {
if (dateLastPurchased) {
const checkExpiration = () => {
Expand Down Expand Up @@ -43,8 +44,10 @@ export function ListItem({
try {
await updateItem(listPath, {
itemId,
totalPurchases: totalPurchases + 1,
dateLastPurchased: new Date(),
totalPurchases,
dateLastPurchased,
purchaseInterval,
dateCreated,
});
console.log(`${name} updated successfully`);
alert(`${name} is purchased successfully`);
Expand Down
6 changes: 6 additions & 0 deletions src/utils/dates.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,9 @@ const ONE_DAY_IN_MILLISECONDS = 86400000;
export function getFutureDate(offset) {
return new Date(Date.now() + offset * ONE_DAY_IN_MILLISECONDS);
}
export function getDaysBetweenDates(previousPurchaseDate) {
const pastDate = previousPurchaseDate.toDate();
const presentDate = new Date();
const diffInMilliseconds = presentDate.getTime() - pastDate.getTime();
return Math.round(diffInMilliseconds / ONE_DAY_IN_MILLISECONDS);
}
5 changes: 2 additions & 3 deletions src/views/List.jsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
import { useState } from 'react';
import { ListItem } from '../components';
// import { useNavigate } from 'react-router-dom';
import { NavLink } from 'react-router-dom';

export function List({ data, listPath }) {
const [searchInput, setSearchInput] = useState('');
//const navigate = useNavigate();

const handleInputChange = (e) => {
setSearchInput(e.target.value);
};
Expand Down Expand Up @@ -63,6 +60,8 @@ export function List({ data, listPath }) {
listPath={listPath}
totalPurchases={item.totalPurchases}
dateLastPurchased={item.dateLastPurchased}
purchaseInterval={item.purchaseInterval}
dateCreated={item.dateCreated}
/>
);
})
Expand Down

0 comments on commit f21161b

Please sign in to comment.