@@ -22,23 +22,17 @@ import { calculateEstimate } from '@the-collab-lab/shopping-list-utils';
2222 * @returns
2323 */
2424export function useShoppingLists ( userId , userEmail ) {
25- // Start with an empty array for our data.
2625 const initialState = [ ] ;
2726 const [ data , setData ] = useState ( initialState ) ;
2827
2928 useEffect ( ( ) => {
30- // If we don't have a userId or userEmail (the user isn't signed in),
31- // we can't get the user's lists.
3229 if ( ! userId || ! userEmail ) return ;
33-
34- // When we get a userEmail, we use it to subscribe to real-time updates
3530 const userDocRef = doc ( db , 'users' , userEmail ) ;
3631
3732 onSnapshot ( userDocRef , ( docSnap ) => {
3833 if ( docSnap . exists ( ) ) {
3934 const listRefs = docSnap . data ( ) . sharedLists ;
4035 const newData = listRefs . map ( ( listRef ) => {
41- // We keep the list's id and path so we can use them later.
4236 return { name : listRef . id , path : listRef . path } ;
4337 } ) ;
4438 setData ( newData ) ;
@@ -56,36 +50,25 @@ export function useShoppingLists(userId, userEmail) {
5650 * @see https://firebase.google.com/docs/firestore/query-data/listen
5751 */
5852export function useShoppingListData ( listPath ) {
59- // Start with an empty array for our data.
6053 /** @type {import('firebase/firestore').DocumentData[] } */
6154 const initialState = [ ] ;
6255 const [ data , setData ] = useState ( initialState ) ;
6356
6457 useEffect ( ( ) => {
6558 if ( ! listPath ) return ;
6659
67- // When we get a listPath, we use it to subscribe to real-time updates
68- // from Firestore.
6960 return onSnapshot ( collection ( db , listPath , 'items' ) , ( snapshot ) => {
70- // The snapshot is a real-time update. We iterate over the documents in it
71- // to get the data.
7261 const nextData = snapshot . docs . map ( ( docSnapshot ) => {
73- // Extract the document's data from the snapshot.
7462 const item = docSnapshot . data ( ) ;
7563
76- // The document's id is not in the data,
77- // but it is very useful, so we add it to the data ourselves.
7864 item . id = docSnapshot . id ;
7965
8066 return item ;
8167 } ) ;
8268
83- // Update our React state with the new data.
8469 setData ( nextData ) ;
8570 } ) ;
8671 } , [ listPath ] ) ;
87-
88- // Return the data so it can be used by our React components.
8972 return data ;
9073}
9174
@@ -94,16 +77,11 @@ export function useShoppingListData(listPath) {
9477 * @param {Object } user The user object from Firebase Auth.
9578 */
9679export async function addUserToDatabase ( user ) {
97- // Check if the user already exists in the database.
9880 const userDoc = await getDoc ( doc ( db , 'users' , user . email ) ) ;
99- // If the user already exists, we don't need to do anything.
81+
10082 if ( userDoc . exists ( ) ) {
10183 return ;
10284 } else {
103- // If the user doesn't exist, add them to the database.
104- // We'll use the user's email as the document id
105- // because it's more likely that the user will know their email
106- // than their uid.
10785 await setDoc ( doc ( db , 'users' , user . email ) , {
10886 email : user . email ,
10987 name : user . displayName ,
@@ -138,18 +116,16 @@ export async function createList(userId, userEmail, listName) {
138116 * @param {string } recipientEmail The email of the user to share the list with.
139117 */
140118export async function shareList ( listPath , currentUserId , recipientEmail ) {
141- // Check if current user is owner.
142119 if ( ! listPath . includes ( currentUserId ) ) {
143120 throw new Error ( 'You do not have permission to share this list' ) ;
144121 }
145- // Get the document for the recipient user.
122+
146123 const usersCollectionRef = collection ( db , 'users' ) ;
147124 const recipientDoc = await getDoc ( doc ( usersCollectionRef , recipientEmail ) ) ;
148- // If the recipient user doesn't exist, we can't share the list.
125+
149126 if ( ! recipientDoc . exists ( ) ) {
150127 throw new Error ( 'The user with the provided email does not exist' ) ;
151128 }
152- // Add the list to the recipient user's sharedLists array.
153129 const listDocumentRef = doc ( db , listPath ) ;
154130 const userDocumentRef = doc ( db , 'users' , recipientEmail ) ;
155131 await updateDoc ( userDocumentRef , {
@@ -167,13 +143,10 @@ export async function shareList(listPath, currentUserId, recipientEmail) {
167143 */
168144export async function addItem ( listPath , { itemName, daysUntilNextPurchase } ) {
169145 const listCollectionRef = await collection ( db , listPath , 'items' ) ;
170- // TODO: Replace this call to console.log with the appropriate
171- // Firebase function, so this information is sent to your database!
146+
172147 try {
173148 await addDoc ( listCollectionRef , {
174149 dateCreated : new Date ( ) ,
175- // NOTE: This is null because the item has just been created.
176- // We'll use updateItem to put a Date here when the item is purchased!
177150 dateLastPurchased : null ,
178151 dateNextPurchased : getFutureDate ( daysUntilNextPurchase ) ,
179152 name : itemName ,
@@ -185,14 +158,7 @@ export async function addItem(listPath, { itemName, daysUntilNextPurchase }) {
185158}
186159
187160export async function updateItem ( listPath , itemId , { totalPurchases } ) {
188- /**
189- * TODO: Fill this out so that it uses the correct Firestore function
190- * to update an existing item. You'll need to figure out what arguments
191- * this function must accept!
192- */
193-
194161 try {
195- // Get a reference to the specific item document in Firestore
196162 const itemRef = doc ( db , listPath , 'items' , itemId ) ;
197163 const docSnap = await getDoc ( itemRef ) ;
198164 const data = docSnap . data ( ) ;
@@ -221,11 +187,6 @@ export async function updateItem(listPath, itemId, { totalPurchases }) {
221187}
222188
223189export async function deleteItem ( listPath , itemId ) {
224- /**
225- * TODO: Fill this out so that it uses the correct Firestore function
226- * to delete an existing item. You'll need to figure out what arguments
227- * this function must accept!
228- */
229190 try {
230191 const itemRef = doc ( db , listPath , 'items' , itemId ) ;
231192 await deleteDoc ( itemRef ) ;
@@ -239,12 +200,17 @@ export async function deleteItem(listPath, itemId) {
239200 * Sorts the items in the list according to category using urgencyIndex and inactiveIndex to determine what category an item belongs.
240201 * @param {string } data The path to the data object to sort.
241202 */
203+
242204export async function comparePurchaseUrgency ( data ) {
243205 const now = new Date ( ) ;
206+ const inactivityPeriod = 60 ;
207+ const dayOfPurchase = 0 ;
208+ const soon = 7 ;
209+ const kindOfSoon = 30 ;
244210
245211 data . map ( ( item ) => {
246212 const urgencyIndex = Math . ceil (
247- getDaysBetweenDates ( now , item . dateNextPurchased . toDate ( ) ) , //takes the difference between the current date and the date the item is next purchased
213+ getDaysBetweenDates ( now , item . dateNextPurchased . toDate ( ) ) ,
248214 ) ;
249215
250216 const lastPurchase = item . dateLastPurchased
@@ -255,26 +221,20 @@ export async function comparePurchaseUrgency(data) {
255221 item . inactiveIndex = inactiveItem ;
256222 item . urgencyIndex = urgencyIndex ;
257223
258- if ( inactiveItem > 60 ) {
224+ if ( inactiveItem > inactivityPeriod ) {
259225 item . category = 'inactive' ;
260- } else if ( urgencyIndex < 0 ) {
226+ } else if ( urgencyIndex < dayOfPurchase ) {
261227 item . category = 'Overdue' ;
262- } else if ( urgencyIndex <= 7 ) {
228+ } else if ( urgencyIndex <= soon ) {
263229 item . category = 'soon' ;
264- } else if ( urgencyIndex < 30 ) {
230+ } else if ( urgencyIndex < kindOfSoon ) {
265231 item . category = 'kind of soon' ;
266232 } else {
267233 item . category = 'Not soon' ;
268234 }
269235 return item ;
270236 } ) ;
271- /**
272- * Function to implement custom sort based on inacrtivity, then on urgencyIndex and name
273- * 1- if urgency of a is inactive and b is not inactive, sort b ontop the top of a
274- * 2- if urgency of a is not inactive and b is inactive, sort a ontop of b
275- * 3- if urgency is the same, sort based on UrgencyIndex
276- * 4- if urgencyIndex is the same, sort based on name
277- */
237+
278238 data . sort ( ( a , b ) => {
279239 if ( a . category === 'inactive' && b . category !== 'inactive' ) {
280240 return 1 ;
0 commit comments