Skip to content

Commit

Permalink
Merge pull request #24 from the-collab-lab/an-sc-issue-6
Browse files Browse the repository at this point in the history
6. As a user, I want to be able to invite others to an existing shopping list #6
  • Loading branch information
eonflower authored Feb 23, 2024
2 parents 9b54739 + 0c6e7d3 commit d0da703
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/api/firebase.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,21 +137,22 @@ 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 'You are not the owner of this list. Only the owner can share the 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 'User not found, please try again.';
}
// Add the list to the recipient user's sharedLists array.
const listDocumentRef = doc(db, listPath);
const userDocumentRef = doc(db, 'users', recipientEmail);
updateDoc(userDocumentRef, {
sharedLists: arrayUnion(listDocumentRef),
});
return `Yay, ${recipientEmail} was invited to your list!`;
}

/**
Expand Down
47 changes: 47 additions & 0 deletions src/components/InviteForm.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { useState } from 'react';
import { shareList } from '../api';
import { useAuth } from '../api/useAuth.jsx';

const InviteForm = ({ listPath, closeModal }) => {
const [input, setInput] = useState('');
const { user } = useAuth();

const handleSubmit = async (e) => {
e.preventDefault();
try {
const message = await shareList(listPath, user.uid, input);
if (message) {
alert(message);
setInput('');
closeModal();
return;
}
} catch (err) {
console.error(err);
}
};

const handleInputChange = (e) => {
setInput(e.target.value);
};
return (
<>
<form onSubmit={handleSubmit}>
<label htmlFor="email">
Please enter the email of the person you want to invite:
<input
type="email"
name="recipientEmail"
placeholder="[email protected]"
id="email"
value={input}
onChange={handleInputChange}
/>
</label>
<button type="submit">Invite to List</button>
</form>
</>
);
};

export default InviteForm;
20 changes: 20 additions & 0 deletions src/components/Modal.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import React from 'react';

const Modal = ({ isOpen, onClose, children }) => {
return (
<>
{isOpen ? (
<div className="modal-overlay">
<div className="modal-content">
<button className="close-btn" onClick={onClose}>
Close
</button>
{children}
</div>
</div>
) : null}
</>
);
};

export default Modal;
18 changes: 18 additions & 0 deletions src/views/ManageList.jsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,29 @@
import { useState } from 'react';
import AddItem from '../components/AddItem';
import InviteForm from '../components/InviteForm';
import Modal from '../components/Modal';

export function ManageList({ listPath }) {
const [openInviteModal, setOpenInviteModal] = useState(false);

const openModal = () => {
setOpenInviteModal(true);
};

const closeModal = () => {
setOpenInviteModal(false);
};

return (
<>
<p>
Hello from the <code>/manage-list</code> page!
</p>
{/* Button to open modal for inviting user */}
{!openInviteModal ? <button onClick={openModal}>Invite</button> : null}
<Modal isOpen={openInviteModal} onClose={closeModal}>
<InviteForm listPath={listPath} closeModal={closeModal} />
</Modal>
<AddItem listPath={listPath} />
</>
);
Expand Down

0 comments on commit d0da703

Please sign in to comment.