generated from the-collab-lab/smart-shopping-list
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #39 from the-collab-lab/el-improve-list-nav
Improve user navigation in the list and between lists
- Loading branch information
Showing
6 changed files
with
165 additions
and
148 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
import { useState } from 'react'; | ||
import { addItem } from '../api'; | ||
|
||
export function AddItem({ data, listPath }) { | ||
const [formNewItem, setFormNewItem] = useState({ | ||
name: '', | ||
nextPurchase: 0, | ||
}); | ||
const [messageItem, setMessageItem] = useState(''); | ||
|
||
const handleNewItemChange = (e) => { | ||
const { name, value } = e.target; | ||
setFormNewItem((prevForm) => { | ||
return { | ||
...prevForm, | ||
[name]: value, | ||
}; | ||
}); | ||
}; | ||
|
||
const handleNewItemSubmit = async (e) => { | ||
e.preventDefault(); | ||
const { name, nextPurchase } = formNewItem; | ||
|
||
if (!name || !nextPurchase) { | ||
setMessageItem('Please fill out all fields'); | ||
return; | ||
} | ||
try { | ||
const normalizedName = (name) => { | ||
return name | ||
.toLowerCase() | ||
.replace(/[^\w\s]|_/g, '') | ||
.replace(/\s+/g, ''); | ||
}; | ||
|
||
const itemExists = data.some( | ||
(item) => normalizedName(item.name) === normalizedName(name), | ||
); | ||
|
||
if (itemExists) { | ||
alert(`${normalizedName(name)} is already in the list`); | ||
return; | ||
} | ||
|
||
await addItem(listPath, { | ||
itemName: name, | ||
daysUntilNextPurchase: nextPurchase, | ||
}); | ||
alert(`${name} has been successfully added to the list`); | ||
setFormNewItem({ | ||
name: '', | ||
nextPurchase: 0, | ||
}); | ||
} catch (error) { | ||
console.log('Failed to add the item: ', error); | ||
alert(`Failed to add ${name} to the list. Please try again!`); | ||
} | ||
}; | ||
|
||
return ( | ||
<> | ||
<form onSubmit={handleNewItemSubmit}> | ||
<label htmlFor="name">Item name</label> | ||
<input | ||
id="name" | ||
type="text" | ||
placeholder="Item" | ||
value={formNewItem.name} | ||
onChange={handleNewItemChange} | ||
name="name" | ||
required | ||
/> | ||
|
||
<label htmlFor="nextPurchase">When is your next purchase</label> | ||
<select | ||
name="nextPurchase" | ||
id="nextPurchase" | ||
onChange={handleNewItemChange} | ||
value={formNewItem.nextPurchase} | ||
required | ||
> | ||
<option value="">---</option> | ||
<option value={7}>Soon</option> | ||
<option value={14}>Kind of soon</option> | ||
<option value={30}>Not soon</option> | ||
</select> | ||
|
||
<button>Add Item</button> | ||
|
||
<p>{messageItem}</p> | ||
</form> | ||
</> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { useState } from 'react'; | ||
import { useNavigate } from 'react-router-dom'; | ||
import { createList } from '../api/firebase'; | ||
|
||
export function AddList({ setListPath, userId, userEmail }) { | ||
const [listName, setListName] = useState(''); | ||
|
||
const navigate = useNavigate(); | ||
|
||
const handleCreateListButton = async (e) => { | ||
e.preventDefault(); | ||
|
||
try { | ||
await createList(userId, userEmail, listName); | ||
alert(`${listName} list was successfully created.`); | ||
|
||
const createListPath = `${userId}/${listName}}`; | ||
setListPath(createListPath); | ||
navigate('/list'); | ||
} catch (error) { | ||
console.error('error creating a list', error); | ||
alert('Failed to create the list. Please try again!'); | ||
} | ||
}; | ||
|
||
return ( | ||
<> | ||
<form onSubmit={handleCreateListButton}> | ||
<label htmlFor="listName">List Name:</label> | ||
<input | ||
type="text" | ||
id="listName" | ||
value={listName} | ||
onChange={(e) => setListName(e.target.value)} | ||
placeholder="Enter the name of your new list" | ||
required | ||
/> | ||
<button type="submit" className="button"> | ||
Create list | ||
</button> | ||
</form> | ||
</> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.