generated from the-collab-lab/smart-shopping-list
-
Notifications
You must be signed in to change notification settings - Fork 3
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 #21 from the-collab-lab/vi_ce_form_list
Create a new list
- Loading branch information
Showing
6 changed files
with
90 additions
and
6 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import { useState } from 'react'; | ||
import { createList } from '../api'; | ||
import { useNavigate } from 'react-router-dom'; | ||
|
||
const ListForm = (props) => { | ||
const { setMessage, setListPath, userId, userEmail } = props; | ||
const [newList, setNewList] = useState(''); | ||
const navigate = useNavigate(); | ||
|
||
const handleSubmit = async (event) => { | ||
event.preventDefault(); | ||
if (newList.trim().length === 0) { | ||
setMessage('Please type a list name :)'); | ||
setNewList(''); | ||
return; | ||
} | ||
|
||
try { | ||
const response = await createList(userId, userEmail, newList); | ||
if (response) { | ||
setListPath( | ||
`${response._key.path.segments[0]}/${response._key.path.segments[1]}`, | ||
); | ||
navigate( | ||
`/list/${response._key.path.segments[0]}/${response._key.path.segments[1]}`, | ||
); | ||
} | ||
} catch (error) { | ||
if (error) { | ||
setMessage('There was an error creating the list'); | ||
} | ||
} | ||
setNewList(''); | ||
}; | ||
|
||
const handleInputChange = (event) => { | ||
const data = event.target.value; | ||
setNewList(data); | ||
setMessage(null); | ||
}; | ||
const handleKeyPressed = (event) => { | ||
if (event.key === 'Enter') { | ||
handleSubmit(); | ||
} | ||
}; | ||
|
||
return ( | ||
<form onSubmit={handleSubmit}> | ||
<label htmlFor="new list name">Name new list</label> | ||
<input | ||
type="text" | ||
id="new list name" | ||
value={newList} | ||
onChange={(event) => handleInputChange(event)} | ||
onClick={(event) => handleKeyPressed(event)} | ||
/> | ||
<button type="submit">Create list</button> | ||
</form> | ||
); | ||
}; | ||
|
||
export default ListForm; |
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