Skip to content

Commit ac6c258

Browse files
authored
Merge pull request #24 from the-collab-lab/em-mp-invite-others-to-shopping-list
added invite friend form and connect with shareList func
2 parents 3a88def + 2212f27 commit ac6c258

File tree

3 files changed

+43
-6
lines changed

3 files changed

+43
-6
lines changed

src/App.jsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ export function App() {
4949
element={<Home data={lists} setListPath={setListPath} />}
5050
/>
5151
<Route path="/list" element={<List data={data} />} />
52-
<Route path="/manage-list" element={<ManageList />} />
52+
<Route path="/manage-list" element={<ManageList userId={userId} />} />
5353
</Route>
5454
</Routes>
5555
</Router>

src/api/firebase.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -139,21 +139,28 @@ export async function createList(userId, userEmail, listName) {
139139
export async function shareList(listPath, currentUserId, recipientEmail) {
140140
// Check if current user is owner.
141141
if (!listPath.includes(currentUserId)) {
142-
return;
142+
return {
143+
response: `You don't have access to the shopping list "${listPath.split('/').pop()}".`,
144+
};
143145
}
146+
144147
// Get the document for the recipient user.
145148
const usersCollectionRef = collection(db, 'users');
146149
const recipientDoc = await getDoc(doc(usersCollectionRef, recipientEmail));
147150
// If the recipient user doesn't exist, we can't share the list.
148151
if (!recipientDoc.exists()) {
149-
return;
152+
return { response: `User with email "${recipientEmail}" does not exist.` };
150153
}
151154
// Add the list to the recipient user's sharedLists array.
152155
const listDocumentRef = doc(db, listPath);
153156
const userDocumentRef = doc(db, 'users', recipientEmail);
154-
updateDoc(userDocumentRef, {
157+
await updateDoc(userDocumentRef, {
155158
sharedLists: arrayUnion(listDocumentRef),
156159
});
160+
161+
return {
162+
response: `The shopping list "${listPath.split('/').pop()}" has been shared!`,
163+
};
157164
}
158165

159166
/**

src/views/ManageList.jsx

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import { useState } from 'react';
2-
import { addItem } from '../api';
2+
import { addItem, shareList } from '../api';
33

4-
export function ManageList() {
4+
export function ManageList({ userId }) {
55
const [formData, setFormData] = useState({
66
name: '',
77
frequency: '',
88
});
99

10+
const [email, setEmail] = useState('');
1011
function handleChange(e) {
1112
e.preventDefault();
1213
setFormData((prev) => ({
@@ -15,6 +16,11 @@ export function ManageList() {
1516
}));
1617
}
1718

19+
function handleEmailChange(e) {
20+
e.preventDefault();
21+
setEmail(e.target.value);
22+
}
23+
1824
function handleSubmit(e) {
1925
e.preventDefault();
2026
if (formData.name.trim() === '') {
@@ -46,6 +52,16 @@ export function ManageList() {
4652
});
4753
}
4854

55+
async function handleEmailSubmit(e) {
56+
e.preventDefault();
57+
const listPath = localStorage.getItem('tcl-shopping-list-path');
58+
try {
59+
const result = await shareList(listPath, userId, email);
60+
window.alert(result.response);
61+
setEmail('');
62+
} catch (error) {}
63+
}
64+
4965
return (
5066
<>
5167
<p>
@@ -85,6 +101,20 @@ export function ManageList() {
85101

86102
<button type="submit">Submit</button>
87103
</form>
104+
105+
<form onSubmit={handleEmailSubmit}>
106+
<label htmlFor="invite-email">Invite user by email:</label>
107+
<input
108+
id="invite-email"
109+
type="email"
110+
name="email"
111+
value={email}
112+
onChange={handleEmailChange}
113+
required
114+
></input>
115+
116+
<button type="submit">Invite my friend</button>
117+
</form>
88118
</div>
89119
</>
90120
);

0 commit comments

Comments
 (0)