generated from the-collab-lab/smart-shopping-list
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* add checkbox to ListItem * updated isPurchased handling * Passed through listPath, added updateDoc to firebase function, debugging list item * passed listPath to ListItem, added useEffect to update isPurchased, changed updateItem function in firebase * Refactored to remove try/catch in updateItem function * used setTimeout to uncheck boxes for items that were purchased more than a day ago * debugged checkbox * Changed to useMemo, updated timeout to use milliseconds in a day * Changed to useMemo, updated timeout to use milliseconds in a day * Decrement in the db when user unchecks before 24hrs is up, moved async purchaseItem function into changeHandler and removed useEffect * Revert "Decrement in the db when user unchecks before 24hrs is up, moved async purchaseItem function into changeHandler and removed useEffect" This reverts commit d5688d8. * Decrement in the db when user unchecks before 24hrs is up, moved async purchaseItem function into changeHandler and removed useEffect * removed unnecessary comments, added in intentional comments * Switched changeHandler to function expression * Added updateTimer function to calculate remaining time, updated useEffeect to use remaining time in setTimeout * Updated setTimeout if statement * Update src/components/ListItem.jsx Co-authored-by: Raynaldo Sutisna <[email protected]> * refactored to prevent timer running when purchasedOneDayAgo is false --------- Co-authored-by: Devina Gillis <[email protected]> Co-authored-by: Devina Gillis <[email protected]> Co-authored-by: Raynaldo Sutisna <[email protected]>
- Loading branch information
1 parent
bb10418
commit 166fd84
Showing
4 changed files
with
79 additions
and
10 deletions.
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
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,5 +1,70 @@ | ||
import './ListItem.css'; | ||
import { updateItem } from '../api/firebase.js'; | ||
import { useState, useEffect, useMemo } from 'react'; | ||
import { ONE_DAY_IN_MILLISECONDS } from '../utils/dates.js'; | ||
|
||
export function ListItem({ name }) { | ||
return <li className="ListItem">{name}</li>; | ||
export function ListItem({ listPath, item }) { | ||
/* Returns a boolean that is passed into isChecked useState | ||
On render, box is checked if purchased less than a day ago */ | ||
|
||
const purchasedOneDayAgo = useMemo(() => { | ||
if (item.dateLastPurchased === null) { | ||
return false; | ||
} | ||
|
||
const timeDiff = Date.now() - item.dateLastPurchased.seconds * 1000; | ||
return timeDiff <= ONE_DAY_IN_MILLISECONDS; | ||
}, [item.dateLastPurchased]); | ||
|
||
const [isChecked, setIsChecked] = useState(purchasedOneDayAgo); | ||
const changeHandler = (e) => { | ||
setIsChecked(!isChecked); | ||
async function purchaseItem() { | ||
try { | ||
await updateItem(listPath, item.id, !isChecked); | ||
} catch (error) { | ||
alert(error.message); | ||
} | ||
} | ||
purchaseItem(); | ||
}; | ||
|
||
//Calculate time remaining if purchase was less than 24 hours ago | ||
const updateTimer = () => { | ||
if (item.dateLastPurchased) { | ||
const timeElapsed = Date.now() - item.dateLastPurchased.seconds * 1000; | ||
if (timeElapsed < ONE_DAY_IN_MILLISECONDS) { | ||
return ONE_DAY_IN_MILLISECONDS - timeElapsed; | ||
} else { | ||
return 0; | ||
} | ||
} | ||
}; | ||
|
||
//sets a timer to uncheck an item 24 hours after it's purchased | ||
useEffect(() => { | ||
if (purchasedOneDayAgo) { | ||
let timeRemaining = updateTimer(); | ||
|
||
const timer = setTimeout(() => { | ||
setIsChecked(false); | ||
}, timeRemaining); | ||
return () => clearTimeout(timer); | ||
} | ||
}, [isChecked, purchasedOneDayAgo]); | ||
|
||
return ( | ||
<li className="ListItem"> | ||
<label htmlFor={item.name}> | ||
{item.name} | ||
<input | ||
type="checkbox" | ||
id={item.name} | ||
name="purchased" | ||
onChange={changeHandler} | ||
checked={isChecked} | ||
/> | ||
</label> | ||
</li> | ||
); | ||
} |
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