Skip to content
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

Merged
merged 17 commits into from
Sep 10, 2024
Merged

Ar mp mark item purchased #26

merged 17 commits into from
Sep 10, 2024

Conversation

arandel1
Copy link
Collaborator

@arandel1 arandel1 commented Sep 7, 2024

Description

The app now keeps track of how many times a user purchases an item:

  • We imported MUI to feature clean, simple checkboxes
  • When the user checks the checkbox next to an item, Firebase counts it as a purchase, and the totalPurchase field increments by 1
  • When the user checks the checkbox next to an item, Firebase updates the dateLastPurchased field to the time the checkbox was checked.
  • After the user checks an item off (checks the checkbox), the box will stay checked for 24 hours, then uncheck itself after 24 hours. Firebase uses the dateLastPurchased timestamp to calculate 24 hours after.

Related Issue

closes 9

Acceptance Criteria

  • The ListItem component renders a checkbox with a semantic .
  • Checking off the item in the UI also updates the dateLastPurchased and totalPurchases properties on the corresponding Firestore document
  • The item is shown as checked for 24 hours after the purchase is made (i.e. we assume the user does not need to buy the item again for at least 1 day). After 24 hours, the item unchecks itself so the user can buy it again.
  • The updateItem function in firebase.js has been filled out, and sends updates to the firestore database when an item is checked

Type of Changes

feature enhancement

Updates

Before

  • No checkboxes
Screen Shot 2024-09-06 at 7 24 37 PM

After

  • User can use checkboxes to mark when they've purchased that item.
  • You can see before and after avocado is selected: dateLastPurchased updates, and totalPurchases increments up by 1.
Screen Shot 2024-09-06 at 7 18 19 PM Screen Shot 2024-09-06 at 7 22 00 PM Screen Shot 2024-09-06 at 7 18 39 PM

Testing Steps / QA Criteria

  • Pull down from branch
  • npm i to ensure MUI is installed properly
  • open Firestore Database in a separate tab
  • Navigate to your unique user code and a list of your choice, and the items inside that list
  • Select an item (in my example, I selected avocado)
  • check the checkbox next to that item
  • See dateLastPurchased field update to the time you checked the checkbox (or, the current time)
  • See the totalPurchases increase by 1.

To watch the checkbox uncheck itself in real time:

  • Have browser and Firestore tabs open next to each other
  • Manually edit the dateLastPurchased field by clicking the pencil next to the field: Edit the field to the day + one minute before the current time (You can mess around with this!)

@arandel1 arandel1 requested review from EmmaBin and sar-mko and removed request for EmmaBin September 7, 2024 00:34
Copy link

github-actions bot commented Sep 7, 2024

Visit the preview URL for this PR (updated for commit c2af263):

https://tcl-76-smart-shopping-list--pr26-ar-mp-mark-item-purc-g415r0va.web.app

(expires Tue, 17 Sep 2024 17:32:34 GMT)

🔥 via Firebase Hosting GitHub Action 🌎

Sign: 512b1a88be8ae05fd3e727b99332819df760271d

@arandel1 arandel1 requested review from EmmaBin, sar-mko, fullybaked, nathanejbrown and mindyzwan and removed request for sar-mko September 7, 2024 00:34
Copy link
Collaborator

@sar-mko sar-mko left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Really cool implementation :) Was able to check and uncheck items and see properties in the database, and saw the update on click.

Though when testing the 24 hours feature (In firebase, I changed dateLastPurchased to the previous day and a minute after), the page updated after a refresh, but I noticed isChecked in the database stayed equal to true. It could be how I tested it, so I'm curious if anyone also had this happen. It does update in LocalStorage as well!

if (is24HoursPassed) {
// console.log('Unchecking item because 24 hours have passed');
setChecked(false);
localStorage.setItem(`checked-${item.name}`, JSON.stringify(false));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nitpick ( non-blocking ): Maybe passing the checked state into since its updating anyway.

Copy link
Collaborator

@mentalcaries mentalcaries left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Really cool implementation so far.

What I have noticed though is that I am unable to uncheck the item and recheck it right after. The only issue with that is that the item count gets incremented every single time (so even if it was an accidental check, it still counts as one).

image


// Handle checkbox change (when user clicks on it)
const handleChange = () => {
const newCheckedState = !checked;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

question: should the state of the item being checked depend only on the checked state in the app/firestore or should that also be dependent on the date?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Possible solution: totalPurchases increments by 1 when the item unchecks itself.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@arandel1 from the AC (and at least for now), the box once checked, should probably stay checked for 24 hours, as in the user won't be able to uncheck it.

Perhaps we can create another ticket for the case of accidentally checking off an item.

const has24HoursPassed = (dateLastPurchased) => {
const purchaseTime = dateLastPurchased.getTime(); // Time in milliseconds
const currentTime = new Date().getTime(); // Current time in milliseconds
const oneDayInMs = 24 * 60 * 60 * 1000; // 24 hours in milliseconds
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit (non-blocking): as this is a constant, consider renaming in uppercase snake case. Note: this constant exists in dates.js

@@ -6,6 +6,7 @@ import {
doc,
onSnapshot,
updateDoc,
increment,
Copy link
Collaborator

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

});

// Function to check if 24 hours have passed
const has24HoursPassed = (dateLastPurchased) => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice function naming! I'm always a fan of easily readable code!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree!

Copy link
Collaborator

@mentalcaries mentalcaries left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good so far, but a couple of bugs to fix

…here I think the update increment function should go.
Copy link
Collaborator

@EmmaBin EmmaBin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work on this challenging ticket! I appreciate how readable the variable names are. After testing, it met all the criteria. Just have one small nitpick, like Devin mentioned, the totalPurchase updates every time I check and uncheck, even if it's incidental. I'm curious to learn about what is a better way to capture only valid selections—perhaps adding a confirm button after each item?

…check after 24 hours, and dateLastPurchased turns null. Need to remove local storage piece.
… relies on dateLastPurchased and not local storage, user can check a checkbox and dateLastPurchased updates with a timestamp and totalPurchase increments by 1 and the checkbox remains checked and unclickable so the user cannot update the checkbox etc.
* this function must accept!
*/
// User can update items in their list by checking the checkbox associated with an item, and the totalPurchase field increments by 1 in Firebase.
// The checkbox unchecks 24 hours after the box is checked.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nitpick ( non-blocking ): Technically, this second sentence isn't relevant to this piece of code.

Comment on lines 7 to 13
// Destructure item props
const { name, dateLastPurchased } = item;

// Keeping [checked, setChecked] state, but removing any calls to local storage
const [checked, setChecked] = useState(false);

// Function to check if 24 hours have passed: Changed variable tagging.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nitpick ( non-blocking ): These comments are more notes to yourself than something that will likely be helpful in the long run.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed notes

Comment on lines 13 to 17

// getDaysBetweenDates: We need to write a function that calculates current day - dateLastPurchased to calculate the number of days between the current day and the date last purchased

// if dateLastPurchased = null, then dateNextPurchased = null
// dateNextPurchased = days until you need this item again + dateLastPurchased
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nitpick ( non-blocking ): These also look like they may have been notes left over :)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed notes

package.json Outdated
Comment on lines 10 to 12
"@emotion/react": "^11.13.3",
"@emotion/styled": "^11.13.0",
"@mui/material": "^6.0.2",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note (non-blocking): Normally, I'd say to be sure to remove packages you decide not to use with npm uninstall, but I know we're using the same package in the other branch so it's all good 👍

Copy link
Member

@mindyzwan mindyzwan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great! Applauding y'alls tenacity on this one! 👏 Woohoo!

@arandel1 arandel1 merged commit 3a83404 into main Sep 10, 2024
2 checks passed
@arandel1 arandel1 deleted the ar-mp-mark-item-purchased branch September 10, 2024 17:33
@sar-mko sar-mko mentioned this pull request Oct 4, 2024
3 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants