Skip to content

Instantly share code, notes, and snippets.

@harrybanda
Created September 22, 2021 12:36
Show Gist options
  • Save harrybanda/df83a102eeb68479652ee7abf0b3356c to your computer and use it in GitHub Desktop.
Save harrybanda/df83a102eeb68479652ee7abf0b3356c to your computer and use it in GitHub Desktop.
import ForgeUI, { ModalDialog, Form, TextField } from "@forge/ui";
import { storage } from "@forge/api";
import uuid from "uuid-random";
export const BookDialog = ({
isOpenModal,
setOpenModal,
isUpdating,
selectedBook,
}) => {
const onSubmit = async (formData) => {
let bookId;
if (isUpdating) {
bookId = selectedBook.key;
} else {
bookId = "book_" + uuid();
}
await storage.set(bookId, formData);
setOpenModal(false);
};
return (
isOpenModal && (
<ModalDialog
header={isUpdating ? "Update Book" : "Add Book"}
onClose={() => setOpenModal(false)}
>
<Form onSubmit={onSubmit}>
<TextField
name="bookname"
label="Book Name"
defaultValue={isUpdating ? selectedBook.value.bookname : ""}
/>
<TextField
name="author"
label="Author"
defaultValue={isUpdating ? selectedBook.value.author : ""}
/>
<TextField
name="quantity"
label="Quantity"
defaultValue={isUpdating ? selectedBook.value.quantity : ""}
/>
<TextField
name="price"
label="Price"
defaultValue={isUpdating ? selectedBook.value.price : ""}
/>
</Form>
</ModalDialog>
)
);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment