Skip to content

Commit

Permalink
feat(#10): Refactored setMessage for addItem with failed and success …
Browse files Browse the repository at this point in the history
…messaging
  • Loading branch information
shuveksha-tuladhar committed Sep 9, 2024
1 parent 658cf85 commit cd1a4a3
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions src/views/ManageList.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,21 @@ export function ManageList({ listPath, user }) {
const [message, setMessage] = useState('');
const [recipientEmail, setRecipientEmail] = useState('');

const messages = {
added: 'Your item was successfully added!',
failed:
"Your item wasn't added! There was an error saving the item. Please try again.",
empty: 'Please enter an item to add to your list.',
duplicate: 'Item already exists!',
};

const handleSubmit = async (event) => {
event.preventDefault();
try {
await addItem(listPath, { itemName, daysUntilNextPurchase });
setMessage('Item was successfully saved to the database.');
setMessage('added');
} catch (error) {
setMessage('There was an error saving the item to the database.');
setMessage('failed');
}
setItemName('');
setDaysUntilNextPurchase(7);
Expand Down Expand Up @@ -43,7 +51,7 @@ export function ManageList({ listPath, user }) {
id="itemName"
value={itemName}
onChange={(e) => setItemName(e.target.value)}
required
// required
/>
<fieldset>
<legend>How soon will you need to buy this item again?</legend>
Expand Down Expand Up @@ -79,7 +87,7 @@ export function ManageList({ listPath, user }) {
<button type="submit">Add Item</button>
</form>
<br></br>
{message && <p>{message}</p>}
{message && <p>{messages[message] || ''}</p>}

<div>
<form onSubmit={handleShare}>
Expand Down

0 comments on commit cd1a4a3

Please sign in to comment.