diff options
| author | Kevin Hoerr <kjhoerr@noreply.cybr.es> | 2022-08-14 21:35:45 +0000 |
|---|---|---|
| committer | Kevin Hoerr <kjhoerr@noreply.cybr.es> | 2022-08-14 21:35:45 +0000 |
| commit | c04674fa74c2e43535181431aef5d891f8839619 (patch) | |
| tree | 2f7ce3b61590b39254bc22ac96272b7533ed96d2 /src/components | |
| parent | 461b1fa053bcc86d06156574ab59fa7000dbf69e (diff) | |
| download | pantry-c04674fa74c2e43535181431aef5d891f8839619.tar.gz pantry-c04674fa74c2e43535181431aef5d891f8839619.tar.bz2 pantry-c04674fa74c2e43535181431aef5d891f8839619.zip | |
Merge planner code (#3)
Reviewed-on: https://git.submelon.dev/kjhoerr/pantry/pulls/3
Diffstat (limited to 'src/components')
| -rw-r--r-- | src/components/add-item.tsx | 101 |
1 files changed, 101 insertions, 0 deletions
diff --git a/src/components/add-item.tsx b/src/components/add-item.tsx new file mode 100644 index 0000000..9c4a998 --- /dev/null +++ b/src/components/add-item.tsx @@ -0,0 +1,101 @@ +import { ChangeEvent, useMemo, useState } from "react"; +import { Button, Form, InputOnChangeData, Segment } from "semantic-ui-react"; +import { PantryItem } from "../model"; + +const defaultPantryItem = () => + ({ + name: "", + description: "", + quantity: 1, + quantityUnitType: "oz", + } as PantryItem); + +interface AddItemProps { + addItem: (item: PantryItem) => Promise<void>; +} + +const AddItem = ({ addItem }: AddItemProps) => { + const [additionItem, setAdditionItem] = useState<PantryItem | undefined>(); + const [additionItemLoading, setAdditionItemLoading] = useState(false); + + const handleItemChange = ( + _: ChangeEvent<HTMLInputElement>, + { name, value }: InputOnChangeData + ) => + setAdditionItem((item) => ({ + ...(item !== undefined ? item : defaultPantryItem()), + [name]: value, + })); + + const newItem = useMemo( + () => additionItem ?? defaultPantryItem(), + [additionItem] + ); + + return ( + <> + <Segment attached="top" hidden={additionItem !== undefined}> + <Button primary onClick={() => setAdditionItem(defaultPantryItem())}> + Add Item + </Button> + </Segment> + <Segment attached="top" hidden={additionItem === undefined}> + <Form + loading={additionItemLoading} + onSubmit={() => { + setAdditionItemLoading(true); + addItem(newItem).then(() => { + setAdditionItem(undefined); + setAdditionItemLoading(false); + }); + }} + > + <Form.Group widths="equal"> + <Form.Input + name="name" + label="Item Name" + placeholder="Item name" + value={newItem.name} + onChange={handleItemChange} + /> + <Form.Input + fluid + name="description" + label="Item Description" + placeholder="Item description" + value={newItem.description} + onChange={handleItemChange} + /> + <Form.Input + fluid + name="quantity" + label="Item Quantity" + placeholder="Item quantity" + value={newItem.quantity} + onChange={handleItemChange} + /> + <Form.Input + name="quantityUnitType" + label="Quantity Type" + placeholder="Quantity type" + value={newItem.quantityUnitType} + onChange={handleItemChange} + /> + </Form.Group> + <Form.Group> + <Form.Button primary content="Submit Item" type="submit" /> + <Form.Button + content="Cancel" + onClick={(e) => { + e.preventDefault(); + setAdditionItem(undefined); + }} + /> + </Form.Group> + </Form> + </Segment> + </> + ); +}; + +export default AddItem; |
