Skip to content

Commit

Permalink
Merge pull request #20 from the-collab-lab/dc-tc-new-shopping-list
Browse files Browse the repository at this point in the history
creating a new shopping list
  • Loading branch information
didemydn authored Aug 24, 2024
2 parents c765bd4 + 927c189 commit 5197a57
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 2 deletions.
9 changes: 8 additions & 1 deletion src/App.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,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 path="/manage-list" element={<ManageList />} />
Expand Down
10 changes: 10 additions & 0 deletions src/views/Home.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
form {
display: flex;
flex-direction: column;
width: 50%;
}
#listName,
.button {
padding: 4% 4%;
margin: 2% 2%;
}
45 changes: 44 additions & 1 deletion src/views/Home.jsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,35 @@
import './Home.css';
import { SingleList } from '../components';
import { useState } from 'react';
import { useNavigate } from 'react-router-dom';
import { createList } from '../api/firebase';

export function Home({ data, setListPath, userId, userEmail }) {
const [listName, setListName] = useState('');
const [message, setMessage] = useState('');
const navigate = useNavigate(); //to call redirected to the List view

const handleCreateListButton = async (e) => {
e.preventDefault();
if (!listName) {
setMessage('Enter a list name');
return;
}

try {
await createList(userId, userEmail, listName);
setMessage('New list successfully created');

const createListPath = `${userId}/${listName}}`;
setListPath(createListPath);
navigate('/list'); //redirect to the list view
} catch (error) {
//Logging and error messages if list is not created
console.error('error creating a list', error);
setMessage('Failed to create list. Please try again!');
}
};

export function Home({ data, setListPath }) {
return (
<div className="Home">
<p>
Expand All @@ -17,6 +45,21 @@ export function Home({ data, setListPath }) {
/>
))}
</ul>

<form onSubmit={handleCreateListButton}>
<label htmlFor="listName">List Name:</label>
<input
type="text"
id="listName"
value={listName}
onChange={(e) => setListName(e.target.value)}
placeholder="Enter the name of your new list"
/>
<button type="submit" className="button">
Create list
</button>
{message}
</form>
</div>
);
}

0 comments on commit 5197a57

Please sign in to comment.