Skip to content

Commit

Permalink
Merge pull request #35 from the-collab-lab/nk-gb-share-list
Browse files Browse the repository at this point in the history
Nk gb share list
  • Loading branch information
kweeuhree authored Sep 3, 2024
2 parents 257e85e + a4e5071 commit a0baabe
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 11 deletions.
13 changes: 9 additions & 4 deletions src/api/firebase.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ 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 '!owner';
}
// Get the document for the recipient user.
const usersCollectionRef = collection(db, 'users');
Expand All @@ -151,9 +151,14 @@ export async function shareList(listPath, currentUserId, recipientEmail) {
// 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),
});
try {
updateDoc(userDocumentRef, {
sharedLists: arrayUnion(listDocumentRef),
});
return 'shared';
} catch {
return;
}
}

/**
Expand Down
60 changes: 53 additions & 7 deletions src/views/ManageList.jsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useCallback, useState } from 'react';
import { useStateWithStorage } from '../utils';
import { addItem } from '../api';
import { addItem, shareList, useAuth } from '../api';

const soonDate = 7;
const kindOfSoonDate = 14;
Expand All @@ -9,16 +9,21 @@ const notSoonDate = 30;
export function ManageList() {
const [daysUntilNextPurchase, setDaysUntilNextPurchase] = useState(null);
const [itemName, setItemName] = useState('');
const [emailData, setEmailData] = useState('');

const [listPath, setListPath] = useStateWithStorage(
'tcl-shopping-list-path',
null,
);
const [listPath] = useStateWithStorage('tcl-shopping-list-path', null);

// const listPath = 't4XIww03JAXm1QWr6UPEebbLRl13/first list';
const { user } = useAuth();
const userId = user?.uid;
const userEmail = user?.email;

const handleTextChange = (event) => {
setItemName(event.target.value);
switch (event.target.id) {
case 'item-name':
return setItemName(event.target.value);
case 'email-input':
return setEmailData(event.target.value);
}
};

const handleChange = (event) => {
Expand Down Expand Up @@ -55,6 +60,32 @@ export function ManageList() {
[itemName, daysUntilNextPurchase, listPath],
);

const shareCurrentList = async () => {
const listShared = await shareList(listPath, userId, emailData);

if (listShared === '!owner') {
alert('You cannot share the list you do not own.');
} else if (listShared === 'shared') {
alert('List was shared with recipient.');
} else {
alert(
"The list was not shared because the recipient's email address does not exist in the system.",
);
}
};

const handleEmailInputSubmit = (event) => {
event.preventDefault();

if (userEmail === emailData) {
alert('You cannot share the list with yourself.');
} else {
shareCurrentList();
}

setEmailData('');
};

return (
<div>
<form onSubmit={handleSubmit}>
Expand Down Expand Up @@ -102,6 +133,21 @@ export function ManageList() {
<br />
<button type="submit">Submit</button>
</form>

{/* invite a user to share list form */}
<form onSubmit={handleEmailInputSubmit}>
<label htmlFor="email-input">Enter Email:</label>
<input
type="email"
id="email-input"
value={emailData}
placeholder="Enter email"
required
onChange={handleTextChange}
/>
<br />
<button type="submit">Invite User</button>
</form>
</div>
);
}

0 comments on commit a0baabe

Please sign in to comment.