Skip to content

Commit

Permalink
Merge pull request #23 from the-collab-lab/fz-ma-setup-new-shopping-list
Browse files Browse the repository at this point in the history
Issue#4: Set up new shopping list
  • Loading branch information
zahrafalak committed Aug 25, 2024
2 parents 8aa6847 + 51d58fb commit fc92917
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 18 deletions.
9 changes: 8 additions & 1 deletion src/App.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,14 @@ export function App() {
<Route path="/" element={<Layout />}>
<Route
index
element={<Home data={lists} setListPath={setListPath} />}
element={
<Home
data={lists}
setListPath={setListPath}
userId={userId}
userEmail={userEmail}
/>
}
/>
<Route path="/list" element={<List data={data} />} />
<Route
Expand Down
61 changes: 61 additions & 0 deletions src/components/CreateList.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import { useState } from 'react';
import { createList } from '../api';
import { useNavigate } from 'react-router-dom';
import toast, { Toaster } from 'react-hot-toast';

export function CreateList({ userId, userEmail, setListPath }) {
const [inputValue, setInputValue] = useState('');
const navigate = useNavigate();

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

// Ensure inputValue has a length > 0 after trimming
if (!inputValue.trim()) {
toast.error('List name cannot be empty.');
return;
}

try {
await createList(userId, userEmail, inputValue);
const path = `${userId}/${inputValue}`;
setListPath(path);
setInputValue('');
toast.success('Success: Your New List is Created!');
// Delay for toast notification before redirecting
setTimeout(() => {
navigate('/list');
}, 1500); // 1.5 seconds delay
} catch (error) {
console.error('Error creating list:', error);
toast.error('Failed to create the list. Please try again.');
}
};

const handleChange = (e) => {
setInputValue(e.target.value);
};
return (
<>
<li>
<form onSubmit={handleSubmit}>
<h3>Create New Shopping List</h3>
<label htmlFor="newListName">Name Your List</label>
<br />
<input
type="text"
value={inputValue}
onChange={handleChange}
name="newListName"
id="newListName"
aria-label="Shopping List Name"
aria-required="true" // Indicates that this field is required
/>
<br />
<button aria-label="Create new shopping list">Create List</button>
</form>
<Toaster />
</li>
</>
);
}
46 changes: 29 additions & 17 deletions src/views/Home.jsx
Original file line number Diff line number Diff line change
@@ -1,24 +1,36 @@
import "./Home.css";
import { SingleList } from "../components";
import './Home.css';
import { SingleList } from '../components';
import { CreateList } from '../components/CreateList';

export function Home({ data, setListPath }) {

export function Home({ data, setListPath, userId, userEmail }) {
const hasList = data.length !== 0;

return (
<div className="Home">
<p>
Hello from the home (<code>/</code>) page!
</p>
<ul>
{hasList &&
data.map((list, index) => (
<SingleList
key={index}
name={list.name}
path={list.path}
<>
<div className="Home">
<p>
Hello from the home (<code>/</code>) page!
</p>
<ul>
{hasList &&
data.map((list, index) => (
<SingleList
key={index}
name={list.name}
path={list.path}
setListPath={setListPath}
/>
))}
{
<CreateList
userId={userId}
userEmail={userEmail}
setListPath={setListPath}
/>
))}
</ul>
</div>
}
</ul>
</div>
</>
);
}

0 comments on commit fc92917

Please sign in to comment.