Skip to content

Commit

Permalink
fix: implement and update requested changes for item purchase tracking
Browse files Browse the repository at this point in the history
  • Loading branch information
marshjaja committed Sep 15, 2024
1 parent 7311bc8 commit 0d8b397
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 25 deletions.
39 changes: 19 additions & 20 deletions src/api/firebase.js
Original file line number Diff line number Diff line change
Expand Up @@ -191,30 +191,29 @@ export async function addItem(listPath, { itemName, daysUntilNextPurchase }) {
});
}

export async function updateItem(listPath, id, checked) {
// checked is coming from the handleOnChange function in the ListItem.jsx,so it is not part of the item data.
export async function updateItem(listPath, checked, itemData) {
const { id } = itemData;
const listCollectionRef = collection(db, listPath, 'items');
const itemRef = doc(listCollectionRef, id);
const today = new Date();
const currentTotalPurchases = itemData.totalPurchases;
const currentDayInterval = itemData.dayInterval;
const dateLastPurchasedJavaScriptObject = itemData.dateLastPurchased
? itemData.dateLastPurchased.toDate()
: itemData.dateCreated.toDate();

const daysSinceLastPurchase = getDaysBetweenDates(
today,
dateLastPurchasedJavaScriptObject,
);
const estimate = calculateEstimate(
currentDayInterval,
daysSinceLastPurchase,
currentTotalPurchases,
);

try {
const itemDoc = await getDoc(itemRef);
const data = itemDoc.data();
const today = new Date();
const currentTotalPurchases = data.totalPurchases;
const currentDayInterval = data.dayInterval;
const dateLastPurchasedJavaScriptObject = data.dateLastPurchased
? data.dateLastPurchased.toDate()
: data.dateCreated.toDate();

const daysSinceLastPurchase = getDaysBetweenDates(
today,
dateLastPurchasedJavaScriptObject,
);
const estimate = calculateEstimate(
currentDayInterval,
daysSinceLastPurchase,
currentTotalPurchases,
);

if (checked) {
await updateDoc(itemRef, {
dateLastPurchased: today,
Expand Down
29 changes: 25 additions & 4 deletions src/components/ListItem.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,41 @@ import { updateItem } from '../api';
import { useEffect } from 'react';
import { ONE_DAY_IN_MILLISECONDS } from '../utils/dates';

export function ListItem({ name, listPath, id, isChecked, datePurchased }) {
export function ListItem({
listPath,
name,
id,
isChecked,
dateLastPurchased,
totalPurchases,
dayInterval,
dateCreated,
}) {
const handleOnChange = async (event) => {
let { checked } = event.target;
if (!checked) return;

await updateItem(listPath, id, checked);
await updateItem(listPath, checked, {
id,
dateLastPurchased,
totalPurchases,
dayInterval,
dateCreated,
});
};

useEffect(() => {
const today = new Date().getTime();
const datePurchasedInMillis = datePurchased?.toMillis();
const datePurchasedInMillis = dateLastPurchased?.toMillis();

if (isChecked && today - datePurchasedInMillis >= ONE_DAY_IN_MILLISECONDS) {
updateItem(listPath, id, !isChecked);
updateItem(listPath, !isChecked, {
id,
dateLastPurchased,
totalPurchases,
dayInterval,
dateCreated,
});
}
}, []);

Expand Down
5 changes: 4 additions & 1 deletion src/views/List.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@ export function List({ data, listPath }) {
listPath={listPath}
id={item.id}
isChecked={item.checked}
datePurchased={item.dateLastPurchased}
dateLastPurchased={item.dateLastPurchased}
totalPurchases={item.totalPurchases}
dayInterval={item.dayInterval}
dateCreated={item.dateCreated}
/>
))}
</ul>
Expand Down

0 comments on commit 0d8b397

Please sign in to comment.