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 CreateShoppingList file to components folder #21

Merged
merged 3 commits into from
Aug 25, 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
30 changes: 30 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"firebase": "^10.12.5",
"react": "^18.3.1",
"react-dom": "^18.3.1",
"react-hot-toast": "^2.4.1",
"react-router-dom": "^6.26.0"
},
"devDependencies": {
Expand Down
4 changes: 3 additions & 1 deletion src/App.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@ export function App() {
<Route path="/" element={<Layout />}>
<Route
index
element={<Home data={lists} setListPath={setListPath} />}
element={
<Home user={user} data={lists} setListPath={setListPath} />
}
/>
<Route path="/list" element={<List data={data} />} />
<Route path="/manage-list" element={<ManageList />} />
Expand Down
2 changes: 2 additions & 0 deletions src/api/firebase.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,8 @@ export async function createList(userId, userEmail, listName) {
updateDoc(userDocumentRef, {
sharedLists: arrayUnion(listDocRef),
});

return listDocRef.path;
}

/**
Expand Down
38 changes: 38 additions & 0 deletions src/components/CreateShoppingList.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { useState } from 'react';
import { createList } from '../api/firebase';
import { useNavigate } from 'react-router-dom';
import toast from 'react-hot-toast';

export default function CreateShoppingList({ user, setListPath }) {
const [listName, setListName] = useState('');
const userId = user?.uid;
const userEmail = user?.email;
const navigate = useNavigate();

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

try {
const listDocRef = await createList(userId, userEmail, listName);
setListPath(listDocRef);
Hudamabkhoot marked this conversation as resolved.
Show resolved Hide resolved
toast.success('List created successfully!');
navigate('/list');
} catch (error) {
toast.error('List creation failed. Please try again');
}
};

return (
<form onSubmit={handleSubmit}>
<label htmlFor="shoppingList">Enter Shopping List name:</label>
<input
id="shoppingList"
type="text"
value={listName}
onChange={(e) => setListName(e.target.value)}
required
/>
<button type="submit">Create Shopping List</button>
</form>
);
}
7 changes: 5 additions & 2 deletions src/views/Home.jsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import { SingleList } from '../components';
import './Home.css';
import CreateShoppingList from '../components/CreateShoppingList';

export function Home({ data, setListPath }) {
export function Home({ user, data, setListPath }) {
//console.log(user)
return (
<div className="Home">
<p>
Hello from the home (<code>/</code>) page!
</p>
<ul>
{data.map((item, index) => (
{data?.map((item, index) => (
<SingleList
key={index}
name={item.name}
Expand All @@ -17,6 +19,7 @@ export function Home({ data, setListPath }) {
/>
))}
</ul>
<CreateShoppingList user={user} setListPath={setListPath} />
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I love that you created a separate component for the form! It really helps with reusability and keeps the code better organized.

</div>
);
}
2 changes: 2 additions & 0 deletions src/views/Layout.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import './Layout.css';
import { useAuth } from '../api';
import { SignInButton, SignOutButton } from '../api/useAuth';
import { NavLink } from 'react-router-dom';
import { Toaster } from 'react-hot-toast';

/**
* TODO: The links defined in this file don't work!
Expand Down Expand Up @@ -46,6 +47,7 @@ export function Layout() {
</NavLink>
</div>
</nav>
<Toaster />
</div>
</>
);
Expand Down
Loading