Skip to content

Commit

Permalink
Merge pull request #25 from the-collab-lab/hm-qg-updateItemPurchasedS…
Browse files Browse the repository at this point in the history
…tate

Marking shopping list items as purchased
  • Loading branch information
Hudamabkhoot authored Sep 6, 2024
2 parents a20a91a + 44635e0 commit 1ae8026
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 13 deletions.
5 changes: 4 additions & 1 deletion src/App.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,10 @@ export function App() {
<Home user={user} data={lists} setListPath={setListPath} />
}
/>
<Route path="/list" element={<List data={data} />} />
<Route
path="/list"
element={<List data={data} listPath={listPath} />}
/>
<Route
path="/manage-list"
element={<ManageList listPath={listPath} user={user} />}
Expand Down
30 changes: 24 additions & 6 deletions src/api/firebase.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,15 +183,33 @@ export async function addItem(listPath, { itemName, daysUntilNextPurchase }) {
dateNextPurchased: getFutureDate(daysUntilNextPurchase),
name: itemName,
totalPurchases: 0,
checked: false,
});
}

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, id, checked) {
const listCollectionRef = collection(db, listPath, 'items');
const itemRef = doc(listCollectionRef, id);

try {
const itemDoc = await getDoc(itemRef);
const data = itemDoc.data();
const currentTotalPurchases = data.totalPurchases;

if (checked) {
await updateDoc(itemRef, {
dateLastPurchased: new Date(),
totalPurchases: currentTotalPurchases + 1,
checked: checked,
});
} else {
await updateDoc(itemRef, {
checked: checked,
});
}
} catch (error) {
console.error('There was an error updating the item state: ', error);
}
}

export async function deleteItem() {
Expand Down
34 changes: 32 additions & 2 deletions src/components/ListItem.jsx
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>
);
}
2 changes: 1 addition & 1 deletion src/utils/dates.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const ONE_DAY_IN_MILLISECONDS = 86400000;
export const ONE_DAY_IN_MILLISECONDS = 86400000;

/**
* Get a new JavaScript Date that is `offset` days in the future.
Expand Down
1 change: 0 additions & 1 deletion src/views/Home.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import './Home.css';
import CreateShoppingList from '../components/CreateShoppingList';

export function Home({ user, data, setListPath }) {
//console.log(user)
return (
<div className="Home">
<p>
Expand Down
11 changes: 9 additions & 2 deletions src/views/List.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { useEffect, useState } from 'react';
import { ListItem, SearchBar } from '../components';
import { useNavigate } from 'react-router-dom';

export function List({ data }) {
export function List({ data, listPath }) {
const [search, setSearch] = useState('');
const [displayData, setDisplayData] = useState([]);
const navigate = useNavigate();
Expand All @@ -21,7 +21,14 @@ export function List({ data }) {
/>
<ul>
{displayData.map((item) => (
<ListItem key={item.id} name={item.name} />
<ListItem
key={item.id}
name={item.name}
listPath={listPath}
id={item.id}
isChecked={item.checked}
datePurchased={item.dateLastPurchased}
/>
))}
</ul>
{data.length === 0 && (
Expand Down

0 comments on commit 1ae8026

Please sign in to comment.