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

Marking shopping list items as purchased #25

Merged
merged 5 commits into from
Sep 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
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
@@ -1,7 +1,7 @@
import { useEffect, useState } from 'react';
import { ListItem, SearchBar } from '../components';

export function List({ data }) {
export function List({ data, listPath }) {
const [search, setSearch] = useState('');
const [displayData, setDisplayData] = useState([]);

Expand All @@ -19,7 +19,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>
</>
Expand Down
Loading