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

Rt el add new item #21

Merged
merged 8 commits into from
Aug 25, 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
10 changes: 9 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,13 @@
"prettier --cache --write"
],
"*.{html,json,css,scss,md}": "prettier --cache --write"
}
},
"version": "1.0.0",
"description": "This README file explains The Collab Lab’s smart shopping list project and provides instructions for developing it locally.",
"main": "vite.config.js",
"directories": {
"test": "tests"
},
"author": "",
"license": "ISC"
}
5 changes: 4 additions & 1 deletion src/App.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,10 @@ export function App() {
element={<Home data={lists} setListPath={setListPath} />}
/>
<Route path="/list" element={<List data={data} />} />
<Route path="/manage-list" element={<ManageList />} />
<Route
path="/manage-list"
element={<ManageList listPath={listPath} />}
/>
</Route>
</Routes>
</Router>
Expand Down
25 changes: 15 additions & 10 deletions src/api/firebase.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {
addDoc,
arrayUnion,
getDoc,
setDoc,
Expand Down Expand Up @@ -162,18 +163,22 @@ export async function shareList(listPath, currentUserId, recipientEmail) {
* @param {number} itemData.daysUntilNextPurchase The number of days until the user thinks they'll need to buy the item again.
*/
export async function addItem(listPath, { itemName, daysUntilNextPurchase }) {
const listCollectionRef = collection(db, listPath, 'items');
const listCollectionRef = await collection(db, listPath, 'items');
// TODO: Replace this call to console.log with the appropriate
// Firebase function, so this information is sent to your database!
return console.log(listCollectionRef, {
dateCreated: new Date(),
// NOTE: This is null because the item has just been created.
// We'll use updateItem to put a Date here when the item is purchased!
dateLastPurchased: null,
dateNextPurchased: getFutureDate(daysUntilNextPurchase),
name: itemName,
totalPurchases: 0,
});
try {
await addDoc(listCollectionRef, {
dateCreated: new Date(),
// NOTE: This is null because the item has just been created.
// We'll use updateItem to put a Date here when the item is purchased!
dateLastPurchased: null,
dateNextPurchased: getFutureDate(daysUntilNextPurchase),
name: itemName,
totalPurchases: 0,
});
} catch (error) {
console.log(error);
}
}

export async function updateItem() {
Expand Down
80 changes: 76 additions & 4 deletions src/views/ManageList.jsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,79 @@
export function ManageList() {
import { useState } from 'react';
import { addItem } from '../api';

export function ManageList({ listPath }) {
const [formData, setFormData] = useState({
name: '',
nextPurchase: 0,
});
const [message, setMessage] = useState('');

const handleChange = (e) => {
const { name, value } = e.target;
setFormData((prevFormData) => {
return {
...prevFormData,
[name]: value,
};
});
};

const handleSubmit = async (e) => {
e.preventDefault();
const { name, nextPurchase } = formData;

if (!name || !nextPurchase) {
setMessage('Please fill out all fields');
return;
}
try {
await addItem(listPath, {
itemName: name,
daysUntilNextPurchase: nextPurchase,
});
setMessage(`${name} has been successfully added to the list`);
setFormData({
name: '',
nextPurchase: 0,
});
} catch (error) {
console.log('Failed to add the item: ', error);
setMessage('Failed to add the item to the list.');
}
};

return (
<p>
Hello from the <code>/manage-list</code> page!
</p>
<>
<form onSubmit={handleSubmit}>
<label htmlFor="name">Item name</label>
<input
id="name"
type="text"
placeholder="Item"
value={formData.name}
onChange={handleChange}
name="name"
required
/>
<br />

<label htmlFor="nextPurchase">When is your next purchase</label>
<select
name="nextPurchase"
id="nextPurchase"
onChange={handleChange}
value={formData.nextPurchase}
>
<option value="">---</option>
<option value={7}>Soon</option>
<option value={14}>Kind of soon</option>
<option value={30}>Not soon</option>
</select>

<p>{message}</p>

<button>Add Item</button>
</form>
</>
);
}
Loading