generated from the-collab-lab/smart-shopping-list
-
Notifications
You must be signed in to change notification settings - Fork 2
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 #30 from the-collab-lab/nk-db-add-new-item-to-list
Issue #5 | Add Item to Shopping List
- Loading branch information
Showing
2 changed files
with
106 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,108 @@ | ||
import { useCallback, useState } from 'react'; | ||
import { useStateWithStorage } from '../utils'; | ||
import { addItem } from '../api'; | ||
|
||
const soonDate = 7; | ||
const kindOfSoonDate = 14; | ||
const notSoonDate = 30; | ||
|
||
export function ManageList() { | ||
const [daysUntilNextPurchase, setDaysUntilNextPurchase] = useState(null); | ||
const [itemName, setItemName] = useState(''); | ||
|
||
const [listPath, setListPath] = useStateWithStorage( | ||
'tcl-shopping-list-path', | ||
null, | ||
); | ||
|
||
// const listPath = 't4XIww03JAXm1QWr6UPEebbLRl13/first list'; | ||
|
||
const handleTextChange = (event) => { | ||
setItemName(event.target.value); | ||
}; | ||
|
||
const handleChange = (event) => { | ||
const numberOfDays = parseInt(event.target.value); | ||
setDaysUntilNextPurchase(numberOfDays); | ||
}; | ||
|
||
const handleSubmit = useCallback( | ||
async (event) => { | ||
event.preventDefault(); | ||
if (itemName === '') { | ||
alert('Please add an item name.'); | ||
return; | ||
} | ||
if (!daysUntilNextPurchase) { | ||
alert('Please select an option for date'); | ||
return; | ||
} | ||
|
||
try { | ||
await addItem(listPath, { | ||
itemName, | ||
daysUntilNextPurchase, | ||
}); | ||
alert( | ||
'Item was added to the database!', | ||
itemName, | ||
daysUntilNextPurchase, | ||
); | ||
} catch (error) { | ||
alert(`Item was not added to the database, Error: ${error.message}`); | ||
} | ||
}, | ||
[itemName, daysUntilNextPurchase, listPath], | ||
); | ||
|
||
return ( | ||
<p> | ||
Hello from the <code>/manage-list</code> page! | ||
</p> | ||
<div> | ||
<form onSubmit={handleSubmit}> | ||
<label htmlFor="item-name"> | ||
Item Name: | ||
<br /> | ||
<input | ||
type="text" | ||
defaultValue="" | ||
id="item-name" | ||
onChange={handleTextChange} | ||
value={itemName} | ||
/> | ||
</label> | ||
|
||
<br /> | ||
<label> | ||
<input | ||
type="radio" | ||
value={soonDate} | ||
checked={daysUntilNextPurchase === soonDate} | ||
onChange={handleChange} | ||
/> | ||
Soon | ||
</label> | ||
<br /> | ||
<label> | ||
<input | ||
type="radio" | ||
value={kindOfSoonDate} | ||
checked={daysUntilNextPurchase === kindOfSoonDate} | ||
onChange={handleChange} | ||
/> | ||
Kind of soon | ||
</label> | ||
<br /> | ||
<label> | ||
<input | ||
type="radio" | ||
value={notSoonDate} | ||
checked={daysUntilNextPurchase === notSoonDate} | ||
onChange={handleChange} | ||
/> | ||
Not soon | ||
</label> | ||
<br /> | ||
<button type="submit">Submit</button> | ||
</form> | ||
</div> | ||
); | ||
} |