Skip to content

Commit

Permalink
feat: refactor Share List into a separate component
Browse files Browse the repository at this point in the history
  • Loading branch information
shuveksha-tuladhar committed Oct 5, 2024
1 parent a8a9d82 commit 0728cb3
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 36 deletions.
41 changes: 5 additions & 36 deletions src/views/ManageList.jsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import { useState, useMemo } from 'react';
import { addItem, shareList } from '../api/firebase';
import { addItem } from '../api/firebase';
import { FaPlusSquare } from 'react-icons/fa';
import { IconButton } from '../components/IconButton';
import { FaEnvelope } from 'react-icons/fa6';
import { Share } from './Share';

export function ManageList({ listPath, user, data }) {
const currentUserId = user?.uid;
const [itemName, setItemName] = useState('');
const [daysUntilNextPurchase, setDaysUntilNextPurchase] = useState(7);
const [message, setMessage] = useState('');
const [recipientEmail, setRecipientEmail] = useState('');

const [isModalDialogOpen, setIsModalDialogOpen] = useState(false);

const messages = {
added: 'Your item was successfully added!',
Expand Down Expand Up @@ -59,18 +60,6 @@ export function ManageList({ listPath, user, data }) {
}
};

const handleShare = (event) => {
event.preventDefault();
shareList(listPath, currentUserId, recipientEmail)
.then((result) => {
alert(result);
setRecipientEmail('');
})
.catch((error) => {
alert(error);
});
};

return (
<div>
<h1>Manage Your Shopping List for {extractedListName}</h1>
Expand Down Expand Up @@ -129,27 +118,7 @@ export function ManageList({ listPath, user, data }) {
{messages[message] || ''}
</p>
)}

<div>
<form onSubmit={handleShare}>
<label htmlFor="recipientEmail"> Recipient Email: </label>
<input
type="email"
id="recipientEmail"
value={recipientEmail}
onChange={(e) => setRecipientEmail(e.target.value)}
required
/>
<IconButton
aria-label="Share with an email"
as="button"
className="share-email"
label="Share"
IconComponent={FaEnvelope}
type="submit"
/>
</form>
</div>
<Share />
</div>
);
}
43 changes: 43 additions & 0 deletions src/views/Share.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { useState } from 'react';
import { shareList } from '../api/firebase';
import { IconButton } from '../components/IconButton';
import { FaEnvelope } from 'react-icons/fa6';

export function Share() {
const [recipientEmail, setRecipientEmail] = useState('');

const handleShare = (event) => {
event.preventDefault();
shareList(listPath, currentUserId, recipientEmail)
.then((result) => {
alert(result);
setRecipientEmail('');
})
.catch((error) => {
alert(error);
});
};

return (
<div>
<form onSubmit={handleShare}>
<label htmlFor="recipientEmail"> Recipient Email: </label>
<input
type="email"
id="recipientEmail"
value={recipientEmail}
onChange={(e) => setRecipientEmail(e.target.value)}
required
/>
<IconButton
aria-label="Share with an email"
as="button"
className="share-email"
label="Share"
IconComponent={FaEnvelope}
type="submit"
/>
</form>
</div>
);
}

0 comments on commit 0728cb3

Please sign in to comment.