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
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
b777258
checkbox up and running
MarcosPerez16 Sep 5, 2024
d73c8ff
Adding functionality to checkbox input, not currently functional
MarcosPerez16 Sep 5, 2024
7fd6213
working on backend connection to firebase
arandel1 Sep 6, 2024
4a25199
feature: checkbox remains checked when user refreshes browser, dateLa…
arandel1 Sep 6, 2024
cea2ce2
totalPurchases field increments by +1 when user clicks/checks checkbox
arandel1 Sep 6, 2024
499993b
calculating 24 hours function in progress -- just need to get the che…
arandel1 Sep 6, 2024
5a7b45b
item is automatically unchecked after manually changing dateLastPurch…
MarcosPerez16 Sep 6, 2024
fc05a32
user can check an item and the totalPurchase field increments plus 1 …
arandel1 Sep 7, 2024
da3720a
cleaned up code: commented out console.logs, removed any expired, com…
arandel1 Sep 7, 2024
8b105e3
Destructured item props for better readability. Deleted unnecessary c…
MarcosPerez16 Sep 7, 2024
60d5a52
changed milliseconds calculation constant, left some commented code w…
arandel1 Sep 7, 2024
cec248d
user can check a box, dateLastPurchased updates, and checkbox will un…
arandel1 Sep 9, 2024
8656783
bug fix: removed the redundant isChecked field, state of the checkbox…
arandel1 Sep 10, 2024
58bb02e
cleaned up lines and removed logs
arandel1 Sep 10, 2024
66c44f3
accidentally added notes for working issue 11
arandel1 Sep 10, 2024
3525236
removed commented out notes
arandel1 Sep 10, 2024
c2af263
Merge branch 'main' into ar-mp-mark-item-purchased
mindyzwan Sep 10, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 13 additions & 6 deletions src/api/firebase.js
Original file line number Diff line number Diff line change
Expand Up @@ -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

} from 'firebase/firestore';
import { useEffect, useState } from 'react';
import { db } from './config';
Expand Down Expand Up @@ -183,12 +184,18 @@ export async function addItem(listPath, { itemName, daysUntilNextPurchase }) {
});
}

export async function updateItem() {
/**
* TODO: Fill this out so that it uses the correct Firestore function
* to update an existing item. You'll need to figure out what arguments
* this function must accept!
*/
export async function updateItem(listPath, { itemName }) {
if (!listPath || listPath.trim() === '') {
console.error('Error: Invalid listPath');
return;
}
const updateItemListCollectionRef = collection(db, listPath, 'items');
const updateItemListDocRef = doc(updateItemListCollectionRef, itemName);
const updateData = {
dateLastPurchased: new Date(),
totalPurchases: increment(1),
};
return updateDoc(updateItemListDocRef, updateData);
}

export async function deleteItem() {
Expand Down
55 changes: 53 additions & 2 deletions src/components/ListItem.jsx
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) => {
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!

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>
);
}
4 changes: 2 additions & 2 deletions src/views/List.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ export function List({ data, userId }) {

<ul>
{filteredList &&
filteredList.map((list) => {
return <ListItem key={list.id} name={list.name} />;
filteredList.map((item) => {
return <ListItem key={item.id} item={item} />;
})}
</ul>
</>
Expand Down
Loading