generated from the-collab-lab/smart-shopping-list
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #25 from the-collab-lab/hm-qg-updateItemPurchasedS…
…tate Marking shopping list items as purchased
- Loading branch information
Showing
6 changed files
with
70 additions
and
13 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
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,35 @@ | ||
import './ListItem.css'; | ||
import { updateItem } from '../api'; | ||
import { useEffect } from 'react'; | ||
import { ONE_DAY_IN_MILLISECONDS } from '../utils/dates'; | ||
|
||
export function ListItem({ name }) { | ||
return <li className="ListItem">{name}</li>; | ||
export function ListItem({ name, listPath, id, isChecked, datePurchased }) { | ||
const handleOnChange = async (event) => { | ||
let { checked } = event.target; | ||
if (!checked) return; | ||
|
||
await updateItem(listPath, id, checked); | ||
}; | ||
|
||
useEffect(() => { | ||
const today = new Date().getTime(); | ||
const datePurchasedInMillis = datePurchased.toMillis(); | ||
|
||
if (isChecked && today - datePurchasedInMillis >= ONE_DAY_IN_MILLISECONDS) { | ||
updateItem(listPath, id, !isChecked); | ||
} | ||
}, []); | ||
|
||
return ( | ||
<li className="ListItem"> | ||
<input | ||
type="checkbox" | ||
id={id} | ||
onChange={handleOnChange} | ||
checked={isChecked} | ||
disabled={isChecked} | ||
/> | ||
<label htmlFor={`${id}`}>{name}</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
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