-
Notifications
You must be signed in to change notification settings - Fork 1
Description
Summary
In order to advise users about when to purchase things, the app needs to be able to calculate that guess and store it a future date.
The calculateEstimate
function from the @the-collab-lab/shopping-list-utils
module will help us know how many days in the future a purchase should happen. In order to use calculateEstimate
, we need to know how many days have passed since the time the item was last purchased.
Acceptance criteria
- When the user purchases an item, the item’s
dateNextPurchased
property is calculated using thecalculateEstimate
function and saved to the Firestore database-
dateNextPurchased
is saved as a date, not a number
-
- A
getDaysBetweenDates
function is exported fromutils/dates.js
and imported intoapi/firebase.js
- This function takes two JavaScript Dates and return the number of days that have passed between them
Notes
Working with Firestore’s dates
Firestore returns dates as special objects called Timestamps.
You will need to convert the Timestamp to a JavaScript Date in order to work with it and complete this feature. The Timestamp
object has a .toDate()
method – see the Firestore docs.
Calculating the time between JavaScript dates
Comparing and manipulating JavaScript Dates is notoriously tricky. Here's a hint: all Date objects have a getTime()
method that returns a representation of that date in milliseconds (MDN docs).
Calculating the next purchase date
The @the-collab-lab/shopping-list-utils
module should already be installed in your project. This module exports calculateEstimate
.
calculateEstimate
accepts three arguments. They are, in order:
previousEstimate
(a number): The last estimated purchase interval.daysSinceLastPurchase
(a number): The number of days that have elapsed between the last purchased date and now. You will need to figure out how to calculate this argument!totalPurchases
(a number): The total number of purchases made for the item,
Given this information, calculateEstimate
returns a whole number indicating the number of days until the user is likely to make their next purchase.