From a7992403ae0ca05fdf26e6323747635100a8bb2a Mon Sep 17 00:00:00 2001 From: emmacodes Date: Fri, 30 Aug 2024 22:26:23 -0700 Subject: [PATCH 1/3] added invite friend form and connect with shareList func --- src/App.jsx | 2 +- src/api/firebase.js | 6 ++++-- src/views/ManageList.jsx | 34 ++++++++++++++++++++++++++++++++-- 3 files changed, 37 insertions(+), 5 deletions(-) diff --git a/src/App.jsx b/src/App.jsx index bc52f1a..a9d0fe2 100644 --- a/src/App.jsx +++ b/src/App.jsx @@ -49,7 +49,7 @@ export function App() { element={} /> } /> - } /> + } /> diff --git a/src/api/firebase.js b/src/api/firebase.js index b6157fa..0c507b9 100644 --- a/src/api/firebase.js +++ b/src/api/firebase.js @@ -139,14 +139,14 @@ export async function createList(userId, userEmail, listName) { export async function shareList(listPath, currentUserId, recipientEmail) { // Check if current user is owner. if (!listPath.includes(currentUserId)) { - return; + return { response: "You don't have access to this shopping list." }; } // Get the document for the recipient user. const usersCollectionRef = collection(db, 'users'); const recipientDoc = await getDoc(doc(usersCollectionRef, recipientEmail)); // If the recipient user doesn't exist, we can't share the list. if (!recipientDoc.exists()) { - return; + return { response: 'User does not existed' }; } // Add the list to the recipient user's sharedLists array. const listDocumentRef = doc(db, listPath); @@ -154,6 +154,8 @@ export async function shareList(listPath, currentUserId, recipientEmail) { updateDoc(userDocumentRef, { sharedLists: arrayUnion(listDocumentRef), }); + + return { response: 'The shopping list has been shared!' }; } /** diff --git a/src/views/ManageList.jsx b/src/views/ManageList.jsx index a2f3963..9abccda 100644 --- a/src/views/ManageList.jsx +++ b/src/views/ManageList.jsx @@ -1,12 +1,13 @@ import { useState } from 'react'; -import { addItem } from '../api'; +import { addItem, shareList } from '../api'; -export function ManageList() { +export function ManageList({ userId }) { const [formData, setFormData] = useState({ name: '', frequency: '', }); + const [email, setEmail] = useState(''); function handleChange(e) { e.preventDefault(); setFormData((prev) => ({ @@ -15,6 +16,11 @@ export function ManageList() { })); } + function handleEmailChange(e) { + e.preventDefault(); + setEmail(e.target.value); + } + function handleSubmit(e) { e.preventDefault(); if (formData.name.trim() === '') { @@ -46,6 +52,16 @@ export function ManageList() { }); } + async function handleEmailSubmit(e) { + e.preventDefault(); + const listPath = localStorage.getItem('tcl-shopping-list-path'); + try { + const result = await shareList(listPath, userId, email); + window.alert(result.response); + setEmail(''); + } catch (error) {} + } + return ( <>

@@ -85,6 +101,20 @@ export function ManageList() { + +

+ + + + +
); From e143256d9db43d32358687a04fcb94bf93714633 Mon Sep 17 00:00:00 2001 From: MarcosPerez16 Date: Sun, 1 Sep 2024 00:50:31 -0700 Subject: [PATCH 2/3] fixed typo in firebase.js file in shareList function. --- src/api/firebase.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/api/firebase.js b/src/api/firebase.js index 0c507b9..5506eb8 100644 --- a/src/api/firebase.js +++ b/src/api/firebase.js @@ -146,7 +146,7 @@ export async function shareList(listPath, currentUserId, recipientEmail) { const recipientDoc = await getDoc(doc(usersCollectionRef, recipientEmail)); // If the recipient user doesn't exist, we can't share the list. if (!recipientDoc.exists()) { - return { response: 'User does not existed' }; + return { response: 'User does not exist' }; } // Add the list to the recipient user's sharedLists array. const listDocumentRef = doc(db, listPath); From 2212f274198fb5ddc42819ba010c04398f800ed9 Mon Sep 17 00:00:00 2001 From: MarcosPerez16 Date: Sun, 1 Sep 2024 03:30:27 -0700 Subject: [PATCH 3/3] Enhanced `shareList` function with specific response messages using template literals for better clarity. --- src/api/firebase.js | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/src/api/firebase.js b/src/api/firebase.js index 5506eb8..24bec28 100644 --- a/src/api/firebase.js +++ b/src/api/firebase.js @@ -139,23 +139,28 @@ export async function createList(userId, userEmail, listName) { export async function shareList(listPath, currentUserId, recipientEmail) { // Check if current user is owner. if (!listPath.includes(currentUserId)) { - return { response: "You don't have access to this shopping list." }; + return { + response: `You don't have access to the shopping list "${listPath.split('/').pop()}".`, + }; } + // Get the document for the recipient user. const usersCollectionRef = collection(db, 'users'); const recipientDoc = await getDoc(doc(usersCollectionRef, recipientEmail)); // If the recipient user doesn't exist, we can't share the list. if (!recipientDoc.exists()) { - return { response: 'User does not exist' }; + return { response: `User with email "${recipientEmail}" does not exist.` }; } // Add the list to the recipient user's sharedLists array. const listDocumentRef = doc(db, listPath); const userDocumentRef = doc(db, 'users', recipientEmail); - updateDoc(userDocumentRef, { + await updateDoc(userDocumentRef, { sharedLists: arrayUnion(listDocumentRef), }); - return { response: 'The shopping list has been shared!' }; + return { + response: `The shopping list "${listPath.split('/').pop()}" has been shared!`, + }; } /**