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

Add checks to new item input #30

Merged
merged 3 commits into from
Mar 10, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
4 changes: 3 additions & 1 deletion src/App.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,9 @@ export function App() {
/>
<Route
path="/manage-list"
element={<ManageList listPath={listPath} userId={userId} />}
element={
<ManageList listPath={listPath} userId={userId} data={data} />
}
/>
</Route>
</Routes>
Expand Down
27 changes: 26 additions & 1 deletion src/views/ManageList.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { useState } from 'react';
import { addItem } from '../api';
import { shareList } from '../api/firebase.js';

export function ManageList({ listPath, userId }) {
export function ManageList({ listPath, userId, data }) {
const initialItemFormState = {
itemName: '',
daysUntilNextPurchase: 0,
Expand All @@ -17,9 +17,34 @@ export function ManageList({ listPath, userId }) {
setShareEmail(target.value);
};

const currentList = data.map((item) => {
return item.name
.toLowerCase()
.replace(/[!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~/\s]/g, '');
andiedoescode marked this conversation as resolved.
Show resolved Hide resolved
});

const handleItemSubmit = async (event) => {
newItem.daysUntilNextPurchase = Number(newItem.daysUntilNextPurchase);
event.preventDefault();

//edge case: what if someone decides to put only puncutation as item name
if (newItem.itemName) {
const itemName = newItem.itemName
.toLowerCase()
.replace(/[!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~/\s]/g, '');

if (currentList.includes(itemName)) {
alert('This item already exists in the list.');
return;
}
}

if (!newItem.itemName) {
alert('Please enter an item name');
return;
}
andiedoescode marked this conversation as resolved.
Show resolved Hide resolved

//Compare new item name to current list in database
try {
await addItem(listPath, newItem);
alert('Item successfully added!');
Expand Down
Loading