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

Show error messages for adding empty or duplicate items #28

Merged
merged 4 commits into from
Sep 15, 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
2 changes: 1 addition & 1 deletion src/App.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export function App() {
/>
<Route
path="/manage-list"
element={<ManageList listPath={listPath} user={user} />}
element={<ManageList listPath={listPath} user={user} data={data} />}
/>
</Route>
</Routes>
Expand Down
2 changes: 1 addition & 1 deletion src/components/ListItem.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export function ListItem({ name, listPath, id, isChecked, datePurchased }) {

useEffect(() => {
const today = new Date().getTime();
const datePurchasedInMillis = datePurchased.toMillis();
const datePurchasedInMillis = datePurchased?.toMillis();

if (isChecked && today - datePurchasedInMillis >= ONE_DAY_IN_MILLISECONDS) {
updateItem(listPath, id, !isChecked);
Expand Down
23 changes: 18 additions & 5 deletions src/components/ManageListForms/AddItemForm.jsx
Original file line number Diff line number Diff line change
@@ -1,19 +1,32 @@
import { useState } from 'react';
import { addItem } from '../../api/firebase';
import toast from 'react-hot-toast';

export default function AddItemForm({ listPath }) {
export default function AddItemForm({ listPath, data }) {
const [formData, setFormData] = useState({
itemName: '',
daysUntilNextPurchase: '',
});

const handleSubmit = async (event) => {
event.preventDefault();

const formattedItemName = formData.itemName
.toLowerCase()
.replace(/[^a-z]/g, '');

const match = data.find((item) => item.name === formattedItemName);

try {
event.preventDefault();
await addItem(listPath, formData);
alert(`${formData.itemName} was added to the list successfully`);
if (!match) {
await addItem(listPath, { ...formData, itemName: formattedItemName });
toast.success(`${formattedItemName} was added to your list`);
} else {
toast.error(`${formattedItemName} is already on your list`);
return;
}
} catch (error) {
alert(`There was a problem adding ${formData.itemName} to the list`);
toast.error(`Failed to add ${formattedItemName}`);
}
};

Expand Down
4 changes: 2 additions & 2 deletions src/views/ManageList.jsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import AddItemForm from '../components/ManageListForms/AddItemForm';
import ShareListForm from '../components/ManageListForms/ShareListForm';

export function ManageList({ listPath, user }) {
export function ManageList({ listPath, user, data }) {
return (
<div>
<AddItemForm listPath={listPath} />
<AddItemForm listPath={listPath} data={data} />
<ShareListForm listPath={listPath} user={user} />
</div>
);
Expand Down
Loading