Skip to content

Commit

Permalink
Merge pull request #29 from the-collab-lab/ce-vi-alert-empty-item
Browse files Browse the repository at this point in the history
Add check to input field on the manage-list page
  • Loading branch information
vivitt authored Mar 8, 2024
2 parents bdd0ac7 + 6c39298 commit 3e1607b
Show file tree
Hide file tree
Showing 9 changed files with 107 additions and 49 deletions.
1 change: 1 addition & 0 deletions src/App.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ export function App() {
path="/manage-list"
element={
<ManageList
data={data}
listPath={listPath}
userId={userId}
userEmail={userEmail}
Expand Down
Empty file added src/components/ErrorMessage.css
Empty file.
7 changes: 7 additions & 0 deletions src/components/ErrorMessage.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import './ErrorMessage.css';

const ErrorMessage = (props) => {
return <div className="errorMessage">{<p>{props.errorMessage}</p>}</div>;
};

export default ErrorMessage;
4 changes: 3 additions & 1 deletion src/components/ListForm.jsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { useState } from 'react';
import { createList } from '../api';
import { useNavigate } from 'react-router-dom';
import { inputHasValue } from '../utils/inputValidation';

const ListForm = (props) => {
const { setMessage, setListPath, userId, userEmail } = props;
Expand All @@ -9,7 +10,8 @@ const ListForm = (props) => {

const handleSubmit = async (event) => {
event.preventDefault();
if (newList.trim().length === 0) {

if (!inputHasValue(newList)) {
setMessage('Please type a list name :)');
setNewList('');
return;
Expand Down
14 changes: 14 additions & 0 deletions src/utils/inputValidation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
export const inputHasValue = (value) => {
return value.trim().length === 0 ? false : true;
};

export const inputHasOnlyNUmbers = (string) => {
return !!string.match(/^\d+$/);
};

export const inputHasRepeatedValue = (string, value) => {
const regex = /[^a-z]/g;
const stringOne = string.toLowerCase().trim().replace(regex, '');
const stringTwo = value.toLowerCase().trim().replace(regex, '');
return stringOne === stringTwo ? true : false;
};
3 changes: 3 additions & 0 deletions src/views/Home.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.Home__form {
height: 8rem;
}
19 changes: 11 additions & 8 deletions src/views/Home.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,25 @@ import './Home.css';
import { SingleList } from '../components';
import { useState } from 'react';
import ListForm from '../components/ListForm';
import ErrorMessage from '../components/ErrorMessage';

export function Home({ data, setListPath, userId, userEmail }) {
const [message, setMessage] = useState(null);
const [message, setMessage] = useState('');

return (
<div className="Home">
<p>
Hello from the home (<code>/</code>) page!
</p>
<ListForm
setMessage={setMessage}
setListPath={setListPath}
userId={userId}
userEmail={userEmail}
/>
<span> {message ? <p> {message} </p> : <></>} </span>
<div className="Home__form">
<ListForm
setMessage={setMessage}
setListPath={setListPath}
userId={userId}
userEmail={userEmail}
/>
{message !== '' && <ErrorMessage errorMessage={message} />}
</div>
<ul>
{data.map((list, i) => (
<SingleList
Expand Down
3 changes: 3 additions & 0 deletions src/views/ManageList.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.ManageList__form {
height: 8rem;
}
105 changes: 65 additions & 40 deletions src/views/ManageList.jsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
import { useState } from 'react';
import { addItem } from '../api/firebase';
import { shareList } from '../api/firebase';
import './ManageList.css';
import ErrorMessage from '../components/ErrorMessage';
import {
inputHasValue,
inputHasOnlyNUmbers,
inputHasRepeatedValue,
} from '../utils/inputValidation';

export function ManageList({ listPath, userId, userEmail }) {
const [errMessage, setErrMessage] = useState('');
const inputHasValue = (value) => {
return value.trim().length === 0 ? false : true;
};
export function ManageList({ data, listPath, userId, userEmail }) {
const [addItemErrMessage, setAddItemErrMessage] = useState('');
const [shareListErrMessage, setShareListErrMessage] = useState('');

async function handleSubmit(e) {
e.preventDefault();
Expand All @@ -17,6 +22,18 @@ export function ManageList({ listPath, userId, userEmail }) {
let itemName = formData.get('item');
let time = formData.get('time');

if (!inputHasValue(itemName) || inputHasOnlyNUmbers(itemName)) {
setAddItemErrMessage('Please enter an item');
form.reset();
return;
}

if (data.some((item) => inputHasRepeatedValue(item.name, itemName))) {
setAddItemErrMessage('This item is already in your list');
form.reset();
return;
}

let daysUntilNextPurchase;
if (time === 'soon') {
daysUntilNextPurchase = 7;
Expand All @@ -26,10 +43,6 @@ export function ManageList({ listPath, userId, userEmail }) {
daysUntilNextPurchase = 30;
}

if (!inputHasValue(itemName)) {
form.reset();
return;
}
let response = await addItem(listPath, { itemName, daysUntilNextPurchase });

if (response) {
Expand All @@ -49,13 +62,13 @@ export function ManageList({ listPath, userId, userEmail }) {
let email = mailFormData.get('email');

if (!inputHasValue(email)) {
setErrMessage('Share the list by entering a valid user email');
setShareListErrMessage('Share the list by entering a valid user email');
mailForm.reset();
return;
}

if (email === userEmail) {
setErrMessage(
setShareListErrMessage(
'To share the list, enter the email of a user that is not you',
);
mailForm.reset();
Expand All @@ -72,37 +85,49 @@ export function ManageList({ listPath, userId, userEmail }) {
}

return (
<>
<div className="ManageList">
<p>
Hello from the <code>/manage-list</code> page!
</p>
<form method="post" onSubmit={handleSubmit}>
<label>
Add item
<input type="text" name="item"></input>
</label>
<label htmlFor="time-select">When do I need it?</label>
<select name="time" id="time-select">
<option value="soon">Soon (within 7 days)</option>
<option value="soonIsh">Soon-ish (in 14 days)</option>
<option value="notSoon">Not soon (in 30 days)</option>
</select>
<button type="submit">Submit</button>
</form>
<hr></hr>
<form method="post" onSubmit={sendInvite}>
<label htmlFor="email">
Share List with another user
<input
type="email"
name="email"
id="email"
onChange={() => setErrMessage('')}
></input>
</label>
<button type="submit">Submit</button>
</form>
{errMessage !== '' ? <p>{errMessage}</p> : null}
</>
<div className="ManageList__form">
<form method="post" onSubmit={handleSubmit}>
<label>
Add item
<input
type="text"
name="item"
onChange={() => setAddItemErrMessage('')}
></input>
</label>
<label htmlFor="time-select">When do I need it?</label>
<select name="time" id="time-select">
<option value="soon">Soon (within 7 days)</option>
<option value="soonIsh">Soon-ish (in 14 days)</option>
<option value="notSoon">Not soon (in 30 days)</option>
</select>
<button type="submit">Submit</button>
</form>
{addItemErrMessage !== '' && (
<ErrorMessage errorMessage={addItemErrMessage} />
)}
</div>
<div className="ManageList__form">
<form method="post" onSubmit={sendInvite}>
<label htmlFor="email">
Share List with another user
<input
type="email"
name="email"
id="email"
onChange={() => setShareListErrMessage('')}
></input>
</label>
<button type="submit">Submit</button>
</form>
{shareListErrMessage !== '' && (
<ErrorMessage errorMessage={shareListErrMessage} />
)}
</div>
</div>
);
}

0 comments on commit 3e1607b

Please sign in to comment.