Skip to content

Commit

Permalink
add a function that returns a valid date last purchased or a valid da…
Browse files Browse the repository at this point in the history
…te created; propose a function that will calculate days form passed in date to today; pass the latter into firebase
  • Loading branch information
kweeuhree committed Sep 19, 2024
1 parent 3ac0feb commit 57119a5
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/api/firebase.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
} from 'firebase/firestore';
import { useEffect, useState } from 'react';
import { db } from './config';
import { addDaysFromToday, getDaysBetweenDates } from '../utils';
import { addDaysFromToday, getDaysFromDate } from '../utils';

/**
* A custom hook that subscribes to the user's shopping lists in our Firestore
Expand Down
32 changes: 25 additions & 7 deletions src/utils/dates.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,30 @@ export const calculateDateNextPurchased = (currentDate, item) => {
* @param {Date} laterDate The ending date.
* @returns {number} The number of days between the two dates.
*/
function getDaysBetweenDates(earlierDate, laterDate) {
export function getDaysBetweenDates(earlierDate, laterDate) {
return Math.floor(
(laterDate.getTime() - earlierDate.getTime()) / ONE_DAY_IN_MILLISECONDS,
);
}

// takes in a Date
// calculates days since date that was passed and today
export const getDaysFromDate = (pastDate) => {
const today = new Date();
const daysToToday = getDaysBetweenDates(pastDate, today);
return daysToToday;
};

// takes in Timestamps, returns a Date
export const getDateLastPurchasedOrDateCreated = (
dateLastPurchased,
dateCreated,
) => {
const lastPurchaseDate = dateLastPurchased?.toDate();
// console.log(lastPurchaseDate ?? dateCreated.toDate(), 'returning this from long name func');
return lastPurchaseDate ?? dateCreated.toDate();
};

/**
* Calculate the purchase intervals between current, next, and last purchase dates.
* @param {Date} currentDate The current date.
Expand All @@ -70,20 +88,20 @@ function calculatePurchaseIntervals(
dateNextPurchased,
dateLastPurchased,
) {
const lastPurchaseDate = dateLastPurchased?.toDate();

const lastEstimatedIntervalStartDate =
lastPurchaseDate ?? dateCreated.toDate();
const lastEstimatedIntervalStartDate = getDateLastPurchasedOrDateCreated(
dateLastPurchased,
dateCreated,
);

const lastEstimatedInterval = getDaysBetweenDates(
lastEstimatedIntervalStartDate,
dateNextPurchased.toDate(),
);

const daysSinceLastPurchase =
lastPurchaseDate === undefined
dateLastPurchased?.toDate() === undefined
? 0
: getDaysBetweenDates(lastPurchaseDate, currentDate);
: getDaysBetweenDates(dateLastPurchased.toDate(), currentDate);

return { lastEstimatedInterval, daysSinceLastPurchase };
}
Expand Down

0 comments on commit 57119a5

Please sign in to comment.