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

Adding mark item as purchased #38

Merged
merged 8 commits into from
Mar 2, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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() {
index
element={<Home data={lists} setListPath={setListPath} exact />}
/>
<Route path="/list" element={<List data={data} />} />
<Route
path="/list"
element={<List data={data} listPath={listPath} />}
/>
<Route
path="/manage-list"
element={<ManageList listPath={listPath} />}
Expand Down
10 changes: 9 additions & 1 deletion src/api/firebase.js
Original file line number Diff line number Diff line change
Expand Up @@ -179,12 +179,20 @@ export async function addItem(listPath, { itemName, daysUntilNextPurchase }) {
});
}

export async function updateItem() {
export async function updateItem(listPath, { itemId, totalPurchases }) {
/**
* 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!
*/

const itemDoc = doc(db, listPath, 'items', itemId);

updateDoc(itemDoc, {
dateLastPurchased: new Date(),
totalPurchases: totalPurchases + 1,
});
return 'Item purchased!';
}

export async function deleteItem() {
Expand Down
40 changes: 38 additions & 2 deletions src/components/ListItem.jsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,41 @@
import React, { useState, useEffect } from 'react';
import './ListItem.css';
import { updateItem } from '../api/firebase';

export function ListItem({ name }) {
return <li className="ListItem">{name}</li>;
export function ListItem({ item, listPath }) {
const { id, totalPurchases, name, dateLastPurchased } = item;
const lessThan24HoursSincePurchase =
dateLastPurchased &&
(new Date() - dateLastPurchased.toDate()) / 3600000 < 24;
jtkabenni marked this conversation as resolved.
Show resolved Hide resolved

const [isChecked, setIsChecked] = useState(
lessThan24HoursSincePurchase || false,
adidalal marked this conversation as resolved.
Show resolved Hide resolved
);

async function handleCheckboxCheck() {
setIsChecked(!isChecked);
if (!isChecked) {
try {
await updateItem(listPath, {
jtkabenni marked this conversation as resolved.
Show resolved Hide resolved
itemId: id,
totalPurchases: totalPurchases,
});
} catch (error) {
console.error(error);
}
}
}

return (
<div>
<input
type="checkbox"
id="item"
name="item"
checked={isChecked}
onChange={handleCheckboxCheck}
/>
<label htmlFor="item">{name}</label>
</div>
);
}
4 changes: 2 additions & 2 deletions src/views/List.jsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { useState } from 'react';
import { ListItem } from '../components';

export function List({ data }) {
export function List({ data, listPath }) {
const [input, setInput] = useState('');

const filteredItems = data.filter((item) => {
Expand Down Expand Up @@ -38,7 +38,7 @@ export function List({ data }) {
</p>
<ul>
{filteredItems.map((item) => (
<ListItem key={item.id} name={item.name} />
<ListItem key={item.id} item={item} listPath={listPath} />
))}
</ul>
</>
Expand Down
Loading