Skip to content

Commit

Permalink
Merge pull request #22 from the-collab-lab/mp-sm-create-new-shopping-…
Browse files Browse the repository at this point in the history
…list

create new shopping list
  • Loading branch information
sar-mko authored Aug 25, 2024
2 parents d4bdc8e + baec87d commit 819d758
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 8 deletions.
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
56 changes: 50 additions & 6 deletions src/views/Home.jsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,37 @@
import './Home.css';
import { SingleList } from '../components';
import { useState } from 'react';
import { createList, useAuth } from '../api';
import { useNavigate } from 'react-router-dom';

export function Home({ data, setListPath }) {
const [listName, setListName] = useState('');
const [error, setError] = useState('');
const { user } = useAuth();
const userId = user?.uid;
const userEmail = user?.email;
const navigate = useNavigate();

async function handleSubmit(e) {
e.preventDefault();

try {
if (!listName || !listName.trim()) {
throw new Error('Empty field, please enter a valid name');
}
const result = await createList(userId, userEmail, listName);
setListPath(result);
navigate('/list');

setTimeout(() => {
alert('List saved to database');
}, 100);
} catch (err) {
setError(err.message);
console.log(error);
}
}

return (
<div className="Home">
<p>
Expand All @@ -10,15 +41,28 @@ export function Home({ data, setListPath }) {
{data &&
data.map((list) => {
return (
<SingleList
key={crypto.randomUUID()}
name={list.name}
setListPath={setListPath}
path={list.path}
/>
<>
<SingleList
key={crypto.randomUUID()}
name={list.name}
setListPath={setListPath}
path={list.path}
/>
</>
);
})}
</ul>
<form action="" onSubmit={handleSubmit}>
<label htmlFor="listName">Enter List Name:</label>
<input
type="text"
id="listName"
value={listName}
onChange={(e) => setListName(e.target.value)}
/>
<button>Submit</button>
<p>{error}</p>
</form>
</div>
);
}
3 changes: 1 addition & 2 deletions src/views/Layout.jsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Outlet } from 'react-router-dom';
import { NavLink } from 'react-router-dom';
import { Outlet, NavLink } from 'react-router-dom';

import './Layout.css';
import { useAuth, SignInButton, SignOutButton } from '../api';
Expand Down

0 comments on commit 819d758

Please sign in to comment.