From 34853881265d21b9cd3daf1997c11716426904ee Mon Sep 17 00:00:00 2001 From: Amber Date: Tue, 28 Jan 2020 20:51:10 -0500 Subject: [PATCH 01/10] make PR From 2e2ac6eb401e2409d410e3509bb8b860b8cae973 Mon Sep 17 00:00:00 2001 From: Amber Date: Tue, 28 Jan 2020 21:06:11 -0500 Subject: [PATCH 02/10] added estimates.js --- src/lib/estimates.js | 20 ++++++++++++++++++++ src/listContext.js | 1 + 2 files changed, 21 insertions(+) create mode 100644 src/lib/estimates.js diff --git a/src/lib/estimates.js b/src/lib/estimates.js new file mode 100644 index 0000000..006bdb2 --- /dev/null +++ b/src/lib/estimates.js @@ -0,0 +1,20 @@ +/** + * 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; + } + + // 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; diff --git a/src/listContext.js b/src/listContext.js index 890d427..ed28119 100644 --- a/src/listContext.js +++ b/src/listContext.js @@ -2,6 +2,7 @@ import React, { useState } from 'react'; import normalizeName from './lib/normalizeName'; import { withFirestore } from 'react-firestore'; import useListToken, { generateToken, getCurrentToken } from './useListToken'; +import calculateEstimate from './lib/estimates.js'; const ListContext = React.createContext(); // const dummyList = ['eggs', 'tomatoes', 'pink', 'purple']; From ff7293e7249acf8b0af1bc88ca05a65200c7cb93 Mon Sep 17 00:00:00 2001 From: MonicaDJohnson <39500978+MonicaDJohnson@users.noreply.github.com> Date: Tue, 28 Jan 2020 21:24:17 -0500 Subject: [PATCH 03/10] added comment to additem.js to test branch pull --- src/pages/AddItem.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/pages/AddItem.js b/src/pages/AddItem.js index c98c524..a81233e 100644 --- a/src/pages/AddItem.js +++ b/src/pages/AddItem.js @@ -101,3 +101,5 @@ const AddItem = () => { }; export default AddItem; + +//MJ: testing to make sure that I pulled the branch correctly. From 74fcb83f1de768c45397f0ae0089bafba3813462 Mon Sep 17 00:00:00 2001 From: MonicaDJohnson <39500978+MonicaDJohnson@users.noreply.github.com> Date: Wed, 29 Jan 2020 21:11:17 -0500 Subject: [PATCH 04/10] working on counter --- src/listContext.js | 6 ++++-- src/pages/List.js | 41 ++++++++++++++++++++++++----------------- 2 files changed, 28 insertions(+), 19 deletions(-) diff --git a/src/listContext.js b/src/listContext.js index ed28119..494069a 100644 --- a/src/listContext.js +++ b/src/listContext.js @@ -76,8 +76,10 @@ const ListContextProvider = props => { }; //we use .set to add the time the item was purchased to an already existing document we search for the item.id - const addDatePurchased = (item, lastDatePurchased) => { - itemsRef.doc(item.id).set({ ...item, lastDatePurchased }); + const addDatePurchased = (item, lastDatePurchased, numberofPurchases) => { + itemsRef + .doc(item.id) + .set({ ...item, lastDatePurchased, numberofPurchases }); }; return ( diff --git a/src/pages/List.js b/src/pages/List.js index 1ac2689..da530e2 100644 --- a/src/pages/List.js +++ b/src/pages/List.js @@ -1,33 +1,39 @@ 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 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 { 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 === 0 || null ? item.numberOfPurchases++ : count; + console.log(item); + addDatePurchased(item, datePurchased, numberOfPurchases); } return ( @@ -38,19 +44,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']} + 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 ; - // } + if (!isLoading && data.length === 0) { + return ; + } if (!isLoading) { setShoppingList(data); } return isLoading ? ( + // TODO: Make a display list function is listContext.js ) : (
    From 2a03d0014cd125a1ecef2b396bfc88db5fca7b6c Mon Sep 17 00:00:00 2001 From: Amber Date: Sun, 2 Feb 2020 01:07:26 -0500 Subject: [PATCH 05/10] counter set up for numberOfPurchases variable --- src/listContext.js | 4 ++-- src/pages/List.js | 7 ++++--- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/src/listContext.js b/src/listContext.js index 494069a..27569f6 100644 --- a/src/listContext.js +++ b/src/listContext.js @@ -76,10 +76,10 @@ const ListContextProvider = props => { }; //we use .set to add the time the item was purchased to an already existing document we search for the item.id - const addDatePurchased = (item, lastDatePurchased, numberofPurchases) => { + const addDatePurchased = (item, lastDatePurchased, numberOfPurchases) => { itemsRef .doc(item.id) - .set({ ...item, lastDatePurchased, numberofPurchases }); + .set({ ...item, lastDatePurchased, numberOfPurchases }); }; return ( diff --git a/src/pages/List.js b/src/pages/List.js index da530e2..1899331 100644 --- a/src/pages/List.js +++ b/src/pages/List.js @@ -26,12 +26,13 @@ const List = props => { function isChecked(lastDatePurchased) { return !!lastDatePurchased && isLessThan24hrs(lastDatePurchased); } - let count = 1; + // 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(); - const numberOfPurchases = - item.numberOfPurchases === 0 || null ? item.numberOfPurchases++ : count; + const numberOfPurchases = item.numberOfPurchases + ? item.numberOfPurchases + 1 + : 1; console.log(item); addDatePurchased(item, datePurchased, numberOfPurchases); } From f7a709940322ee359e6cd221223657b51dea161a Mon Sep 17 00:00:00 2001 From: Amber Date: Sun, 2 Feb 2020 03:30:36 -0500 Subject: [PATCH 06/10] implemented adding calculated estimate to items in database with fake latestInterval variable --- src/lib/estimates.js | 4 ++++ src/listContext.js | 6 +++++- src/pages/List.js | 27 +++++++++++++++++++++++---- 3 files changed, 32 insertions(+), 5 deletions(-) diff --git a/src/lib/estimates.js b/src/lib/estimates.js index 006bdb2..7cf6ec1 100644 --- a/src/lib/estimates.js +++ b/src/lib/estimates.js @@ -10,6 +10,10 @@ const calculateEstimate = (lastEstimate, latestInterval, numberOfPurchases) => { 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); diff --git a/src/listContext.js b/src/listContext.js index 27569f6..950b621 100644 --- a/src/listContext.js +++ b/src/listContext.js @@ -2,7 +2,6 @@ import React, { useState } from 'react'; import normalizeName from './lib/normalizeName'; import { withFirestore } from 'react-firestore'; import useListToken, { generateToken, getCurrentToken } from './useListToken'; -import calculateEstimate from './lib/estimates.js'; const ListContext = React.createContext(); // const dummyList = ['eggs', 'tomatoes', 'pink', 'purple']; @@ -82,12 +81,17 @@ const ListContextProvider = props => { .set({ ...item, lastDatePurchased, numberOfPurchases }); }; + const addCalculatedEstimate = (item, calculatedEstimate) => { + itemsRef.doc(item.id).set({ ...item, calculatedEstimate }); + }; + return ( { - const { shoppingList, setShoppingList, addDatePurchased } = useContext( - ListContext, - ); + const { + shoppingList, + setShoppingList, + addDatePurchased, + addCalculatedEstimate, + } = useContext(ListContext); const { token } = useListToken(); //we are checking if the last date it was purchased is less than 24hrs using isLessThan24hrs function @@ -33,8 +38,22 @@ const List = props => { const numberOfPurchases = item.numberOfPurchases ? item.numberOfPurchases + 1 : 1; - console.log(item); + console.log('Item before:', item); addDatePurchased(item, datePurchased, numberOfPurchases); + + let lastEstimate = item.nextExpectedPurchase + ? item.nextExpectedPurchase + : 14; + console.log('lastEstimate:', lastEstimate); + console.log('numberOfPurchases:', numberOfPurchases); + const calculatedEstimate = calculateEstimate( + lastEstimate, + latestInterval, + numberOfPurchases, + ); + console.log('the calculateEstimate ran:', calculatedEstimate); + addCalculatedEstimate(item, calculatedEstimate); + console.log('Item after:', item); } return ( From 3969747d9eddd28ae72f4a9a324fe5285a3c3dad Mon Sep 17 00:00:00 2001 From: Amber Date: Sun, 2 Feb 2020 04:01:51 -0500 Subject: [PATCH 07/10] added prevDate variable to try to calculate time interval for calculateEstimate function --- src/pages/List.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/pages/List.js b/src/pages/List.js index 3fc7ab9..212dcaf 100644 --- a/src/pages/List.js +++ b/src/pages/List.js @@ -44,8 +44,13 @@ const List = props => { 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); - console.log('numberOfPurchases:', numberOfPurchases); + const calculatedEstimate = calculateEstimate( lastEstimate, latestInterval, From e9a3eea0ed70071e48ee7f2f71f4977f7925b687 Mon Sep 17 00:00:00 2001 From: Steve Gardner Date: Fri, 7 Feb 2020 15:20:12 +1100 Subject: [PATCH 08/10] Fix remaining holdovers from branching --- src/pages/AddItem.js | 2 -- src/pages/List.js | 2 +- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/src/pages/AddItem.js b/src/pages/AddItem.js index 5233f32..2371e58 100644 --- a/src/pages/AddItem.js +++ b/src/pages/AddItem.js @@ -87,5 +87,3 @@ const AddItem = () => { }; export default AddItem; - -//MJ: testing to make sure that I pulled the branch correctly. diff --git a/src/pages/List.js b/src/pages/List.js index 9b85d7b..7febbf4 100644 --- a/src/pages/List.js +++ b/src/pages/List.js @@ -53,7 +53,7 @@ const List = props => { // Sort the data sort="name" // Only fetch the items associated with the token saved in localStorage - filter={['token', '==', token]} + filter={['token', '==', token || getCurrentToken() || 'no token set']} // 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 }) => { From 2e2f3281234569dcd3caab603e09c64f09cd2d7e Mon Sep 17 00:00:00 2001 From: Mike Date: Fri, 7 Feb 2020 23:23:18 -0800 Subject: [PATCH 09/10] attempt to filter to <= 7 --- src/AppRouter.js | 2 + src/pages/ListSorted.js | 113 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 115 insertions(+) create mode 100644 src/pages/ListSorted.js diff --git a/src/AppRouter.js b/src/AppRouter.js index 660a1e8..6778ff6 100644 --- a/src/AppRouter.js +++ b/src/AppRouter.js @@ -3,6 +3,7 @@ import { BrowserRouter, Route, Switch } from 'react-router-dom'; import AddItem from './pages/AddItem'; import List from './pages/List'; import HomePage from './pages/HomePage'; +import ListSort from './pages/ListSorted'; export default function AppRouter() { return ( @@ -11,6 +12,7 @@ export default function AppRouter() { + > ); diff --git a/src/pages/ListSorted.js b/src/pages/ListSorted.js new file mode 100644 index 0000000..4624068 --- /dev/null +++ b/src/pages/ListSorted.js @@ -0,0 +1,113 @@ +import dayjs from 'dayjs'; +import React, { useContext, useState } from 'react'; +import { FirestoreCollection } from 'react-firestore'; + +import { ListContext } from '../listContext'; +import useListToken, { getCurrentToken } from '../useListToken'; +import NavTabs from '../components/NavTabs'; +import Loading from '../components/Loading'; +import normalizeName from '../lib/normalizeName'; + +function isLessThan24hrs(datePurchased) { + return dayjs().diff(dayjs(datePurchased), 'hours') <= 24; +} + +//we are checking if the last date it was purchased is less than 24hrs using isLessThan24hrs function +function isChecked(lastDatePurchased) { + return !!lastDatePurchased && isLessThan24hrs(lastDatePurchased); +} + +const ListSort = props => { + const now = new Date(); + // const today = dayjs(now); + + const [filteredInput, setFilteredInput] = useState(''); + const { shoppingList, setShoppingList, purchaseItem } = useContext( + ListContext, + ); + const { token } = useListToken(); + + function handlePurchasedChange(item) { + // We don't want to uncheck ourselves. We should have a separate ticket for handling a mis-check + // What would we set datePurchased to on an uncheck? Can't be null if we've purchased or our suggestions + // are goofed. Maybe we live with that, or we could keep the most recent lastDatePurchased in case + // of a mistake. For this ticket, let's keep it simple. + if (!isChecked(item.lastDatePurchased)) { + purchaseItem(item, Date.now()); + } + } + + function handleFilterChange(event) { + setFilteredInput(event.target.value); + } + + //5. way to clear out the filter + + function filterListInput(name) { + return normalizeName(name).includes(normalizeName(filteredInput)); + } + + return ( + <> + X + +
      + {shoppingList + .filter(item => filterListInput(item.name)) + .map((item, index) => ( +
    • + +
    • + ))} +
    + + ); + }} + /> + + + ); +}; +export default ListSort; From 7e53584ff85a34be9cfa1816b944df428561016e Mon Sep 17 00:00:00 2001 From: Amber Date: Sun, 9 Feb 2020 17:38:56 -0500 Subject: [PATCH 10/10] trying to sort by nextExpectedPurchased --- src/listContext.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/listContext.js b/src/listContext.js index d01edc8..a4b5d7b 100644 --- a/src/listContext.js +++ b/src/listContext.js @@ -22,7 +22,9 @@ const ListContextProvider = props => { // fetch the latest shopping list from the database and save to state const fetchList = token => { let query = itemsRef - .orderBy('name') + // .orderBy('name') + // .where('nextExpectedPurchase', "===", ) + .orderBy('nextExpectedPurchase') .where('token', '==', token || 'token not set'); const tempArray = []; query