Skip to content

Commit

Permalink
completed logic to get interval
Browse files Browse the repository at this point in the history
  • Loading branch information
marshjaja committed Sep 11, 2024
1 parent 86e6712 commit a2b3500
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 30 deletions.
38 changes: 20 additions & 18 deletions src/api/firebase.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@ import {
onSnapshot,
updateDoc,
addDoc,
Timestamp,
} from 'firebase/firestore';
import { useEffect, useState } from 'react';
import { db } from './config';
import { getFutureDate } from '../utils';
import toast from 'react-hot-toast';
import { calculateEstimate } from '@the-collab-lab/shopping-list-utils';
import { ONE_DAY_IN_MILLISECONDS } from '../utils/dates';
import { getDaysBetweenDates } from '../utils/dates';
/**
* 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 @@ -190,38 +191,39 @@ export async function addItem(listPath, { itemName, daysUntilNextPurchase }) {
});
}

export async function updateItem(listPath, id, checked, dayInterval) {
export async function updateItem(listPath, id, checked) {
const listCollectionRef = collection(db, listPath, 'items');
const itemRef = doc(listCollectionRef, id);

try {
const itemDoc = await getDoc(itemRef);
const data = itemDoc.data();
const today = new Date();
const currentTotalPurchases = data.totalPurchases;
const currentDayInterval = data.dayInterval;
const dateLastPurchasedInMillis = data.dateLastPurchased
? data.dateLastPurchased.toMillis()
: data.dateCreated.toMillis();
const daysSinceLastPurchase = Date.now() - dateLastPurchasedInMillis;
console.log('DaysSinceLastPurchase ', daysSinceLastPurchase);

//conditional assignment, if x < 1 -> 1, else drop decimals return whole number of days
console.log(
'DaysSinceLastPurchase sb ms',
daysSinceLastPurchase / ONE_DAY_IN_MILLISECONDS,
const dateLastPurchasedJavaScriptObject = data.dateLastPurchased
? data.dateLastPurchased.toDate()
: today;

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

if (checked) {
await updateDoc(itemRef, {
dateLastPurchased: new Date(),
dateLastPurchased: Timestamp.fromDate(new Date()),
totalPurchases: currentTotalPurchases + 1,
checked: checked,
dayInterval: daysSinceLastPurchase,
dateNextPurchased: calculateEstimate(
currentDayInterval,
daysSinceLastPurchase,
currentTotalPurchases,
), //format Timestamps -> September 4, 2024 at 11:11:11 AM UTC+2
dateNextPurchased: Timestamp.fromMillis(
today.setDate(today.getDate() + estimate),
),
});
} else {
await updateDoc(itemRef, {
Expand Down
15 changes: 4 additions & 11 deletions src/components/ListItem.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,20 @@ import { updateItem } from '../api';
import { useEffect } from 'react';
import { ONE_DAY_IN_MILLISECONDS } from '../utils/dates';

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

await updateItem(listPath, id, checked, dayInterval);
await updateItem(listPath, id, checked);
};

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

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

Expand Down
8 changes: 8 additions & 0 deletions src/utils/dates.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,11 @@ export const ONE_DAY_IN_MILLISECONDS = 86400000;
export function getFutureDate(offset) {
return new Date(Date.now() + offset * ONE_DAY_IN_MILLISECONDS);
}
export function getDaysBetweenDates(startDate, endDate) {
const differenceInMillis = startDate.getTime() - endDate.getTime();
const daysSinceLastPurchase =
Math.round(differenceInMillis / ONE_DAY_IN_MILLISECONDS) === 0
? 1
: Math.round(differenceInMillis / ONE_DAY_IN_MILLISECONDS);
return daysSinceLastPurchase;
}
1 change: 0 additions & 1 deletion src/views/List.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ export function List({ data, listPath }) {
id={item.id}
isChecked={item.checked}
datePurchased={item.dateLastPurchased}
dayInterval={item.dayInterval}
/>
))}
</ul>
Expand Down

0 comments on commit a2b3500

Please sign in to comment.