generated from the-collab-lab/smart-shopping-list-deprecated
-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Np mj sort items soon integers #66
Draft
MonicaDJohnson
wants to merge
13
commits into
master
Choose a base branch
from
np-mj-sort-items-soon-integers
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 9 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
3485388
make PR
ajiles91 2e2ac6e
added estimates.js
ajiles91 ff7293e
added comment to additem.js to test branch pull
MonicaDJohnson 350f846
Merge branch 'aj-mj-add-number-of-purchases' of https://github.com/th…
MonicaDJohnson 74fcb83
working on counter
MonicaDJohnson 2a03d00
counter set up for numberOfPurchases variable
ajiles91 ae3f81f
Merge pull request #65 from the-collab-lab/aj-mj-add-number-of-purchases
ajiles91 f7a7099
implemented adding calculated estimate to items in database with fake…
ajiles91 3969747
added prevDate variable to try to calculate time interval for calcula…
ajiles91 1bf2df9
Merge branch 'master' into np-mj-sort-items-soon-integers
e9a3eea
Fix remaining holdovers from branching
2e2f328
attempt to filter to <= 7
mikeramz86 7e53584
trying to sort by nextExpectedPurchased
ajiles91 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/** | ||
* Calculate a weighted estimate for the interval until the next purchase | ||
* Current purchase a tiny bit less weight than all previous purchases | ||
* @param {Number} lastEstimate The last stored purchase interval estimate | ||
* @param {Number} latestInterval The interval between the most recent and previous purchases | ||
* @param {Number} numberOfPurchases Total number of purchases for the item | ||
*/ | ||
const calculateEstimate = (lastEstimate, latestInterval, numberOfPurchases) => { | ||
if (isNaN(lastEstimate)) { | ||
lastEstimate = 14; | ||
} | ||
|
||
// fake interval to see if variable is added to database - need to store prev and current buy day to calculate difference | ||
if (isNaN(latestInterval)) { | ||
latestInterval = 10; | ||
} | ||
// FIXME algorithm doesn't work when there's only 1 purchase in the database | ||
let previousFactor = lastEstimate * numberOfPurchases; | ||
let latestFactor = latestInterval * (numberOfPurchases - 1); | ||
let totalDivisor = numberOfPurchases * 2 - 1; | ||
return (previousFactor + latestFactor) / totalDivisor; | ||
}; | ||
|
||
export default calculateEstimate; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,64 @@ | ||
import React, { useContext } from 'react'; | ||
import NavTabs from '../components/NavTabs'; | ||
import Loading from '../components/Loading'; | ||
// import ErrorMessage from '../components/ErrorMessage'; | ||
import useListToken, { getCurrentToken } from '../useListToken'; | ||
import { FirestoreCollection } from 'react-firestore'; | ||
import { ListContext } from '../listContext'; | ||
import useListToken from '../useListToken'; | ||
import { FirestoreCollection } from 'react-firestore'; | ||
import Loading from '../components/Loading'; | ||
import ErrorMessage from '../components/ErrorMessage'; | ||
import HomePageButton from '../components/HomePageButton'; | ||
import calculateEstimate from '../lib/estimates.js'; | ||
import latestInterval from '../lib/estimates.js'; | ||
import dayjs from 'dayjs'; | ||
import './List.css'; | ||
|
||
const today = dayjs(); | ||
|
||
function isLessThan24hrs(datePurchased) { | ||
let purchaseDateCalc = dayjs(datePurchased); | ||
return today.diff(purchaseDateCalc, 'hour') <= 24; | ||
} | ||
|
||
const List = props => { | ||
const { shoppingList, setShoppingList, addDatePurchased } = useContext( | ||
ListContext, | ||
); | ||
const { token } = useListToken; | ||
const today = dayjs(); | ||
const { | ||
shoppingList, | ||
setShoppingList, | ||
addDatePurchased, | ||
addCalculatedEstimate, | ||
} = useContext(ListContext); | ||
const { token } = useListToken(); | ||
|
||
function isLessThan24hrs(datePurchased) { | ||
let purchaseDateCalc = dayjs(datePurchased); | ||
return today.diff(purchaseDateCalc, 'hour') <= 24; | ||
} | ||
//when an item has been created but not yet purchased. | ||
//we are checking if the last date it was purchased is less than 24hrs using isLessThan24hrs function | ||
function isChecked(lastDatePurchased) { | ||
return !!lastDatePurchased && isLessThan24hrs(lastDatePurchased); | ||
} | ||
|
||
// let count = 1; | ||
//we are adding the item.id as well as the date purchased when clicking on the checkbox | ||
function handlePurchasedChange(item) { | ||
const datePurchased = item.lastDatePurchased ? null : Date.now(); | ||
addDatePurchased(item, datePurchased); | ||
const numberOfPurchases = item.numberOfPurchases | ||
? item.numberOfPurchases + 1 | ||
: 1; | ||
console.log('Item before:', item); | ||
addDatePurchased(item, datePurchased, numberOfPurchases); | ||
|
||
let lastEstimate = item.nextExpectedPurchase | ||
? item.nextExpectedPurchase | ||
: 14; | ||
|
||
let prevDate = item.lastDatePurchased ? item.lastDatePurchased : null; | ||
|
||
console.log('prevDate:', prevDate); | ||
console.log('currentDate:', datePurchased); | ||
console.log('lastEstimate:', lastEstimate); | ||
|
||
const calculatedEstimate = calculateEstimate( | ||
lastEstimate, | ||
latestInterval, | ||
numberOfPurchases, | ||
); | ||
console.log('the calculateEstimate ran:', calculatedEstimate); | ||
addCalculatedEstimate(item, calculatedEstimate); | ||
console.log('Item after:', item); | ||
} | ||
|
||
return ( | ||
|
@@ -38,19 +69,20 @@ const List = props => { | |
// Sort the data | ||
sort="name" | ||
// Only fetch the items associated with the token saved in localStorage | ||
filter={['token', '==', token || getCurrentToken() || 'no token set']} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Careful here @MonicaDJohnson. This is a breaking change when the user doesn't already have a token saved in |
||
filter={['token', '==', token]} | ||
// isLoading = is a Boolean that represents the loading status for the firebase query. true until an initial payload from Firestore is received. | ||
// data = an Array containing all of the documents in the collection. Each item will contain an id along with the other data contained in the document. | ||
render={({ isLoading, data }) => { | ||
// if (!isLoading && data.length === 0) { | ||
// return <ErrorMessage />; | ||
// } | ||
if (!isLoading && data.length === 0) { | ||
return <ErrorMessage />; | ||
} | ||
|
||
if (!isLoading) { | ||
setShoppingList(data); | ||
} | ||
|
||
return isLoading ? ( | ||
// TODO: Make a display list function is listContext.js | ||
<Loading /> | ||
) : ( | ||
<ul className="shopping-list"> | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What else might you pass here besides
name
? Are the items always in alphabetical order—why?