-
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
Ar mp mark item purchased #26
Changes from all commits
b777258
d73c8ff
7fd6213
4a25199
cea2ce2
499993b
5a7b45b
fc05a32
da3720a
8b105e3
60d5a52
cec248d
8656783
58bb02e
66c44f3
3525236
c2af263
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,56 @@ | ||
import { useState, useEffect } from 'react'; | ||
import './ListItem.css'; | ||
import { updateItem } from '../api'; | ||
import { increment } from 'firebase/firestore'; | ||
|
||
export function ListItem({ name }) { | ||
return <li className="ListItem">{name}</li>; | ||
export function ListItem({ item }) { | ||
const { name, dateLastPurchased } = item; | ||
const [checked, setChecked] = useState(false); | ||
const has24HoursPassed = (dateLastPurchased) => { | ||
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. Nice function naming! I'm always a fan of easily readable code! 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. Agree! |
||
const purchaseDate = dateLastPurchased.toDate(); | ||
const currentTime = new Date().getTime(); // Current time in milliseconds | ||
const ONE_DAY_IN_MILLISECONDS = 86400000; // 24 hours in milliseconds | ||
return currentTime - purchaseDate >= ONE_DAY_IN_MILLISECONDS; | ||
}; | ||
|
||
useEffect(() => { | ||
if (dateLastPurchased) { | ||
const is24HoursPassed = has24HoursPassed(dateLastPurchased); | ||
if (is24HoursPassed) { | ||
setChecked(false); | ||
} else { | ||
setChecked(true); | ||
} | ||
} | ||
}, [dateLastPurchased]); | ||
|
||
const handleChange = () => { | ||
if (!checked) { | ||
setChecked(!checked); | ||
const listPath = localStorage.getItem('tcl-shopping-list-path'); | ||
updateItem(listPath, { | ||
itemName: name, | ||
dateLastPurchased: checked ? new Date() : null, | ||
totalPurchases: increment(1), | ||
}) | ||
.then(() => { | ||
console.log('Item updated successfully.'); | ||
}) | ||
.catch(() => { | ||
console.error('Error updating item: ', error); | ||
}); | ||
} | ||
}; | ||
|
||
return ( | ||
<li className="ListItem"> | ||
<input | ||
type="checkbox" | ||
checked={checked} | ||
onChange={handleChange} | ||
disabled={checked} | ||
/> | ||
{name} | ||
</li> | ||
); | ||
} |
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.
Good find with this
increment
function