2021-05-20 12:22:07 +10:00
|
|
|
import React, { useState } from "react";
|
2020-10-01 22:32:21 +10:00
|
|
|
import { Button, Flex, Label } from "theme-ui";
|
|
|
|
|
|
|
|
|
|
import Modal from "../components/Modal";
|
|
|
|
|
import TokenSettings from "../components/token/TokenSettings";
|
2020-10-17 10:45:59 +11:00
|
|
|
import TokenPreview from "../components/token/TokenPreview";
|
2021-02-08 16:53:56 +11:00
|
|
|
import LoadingOverlay from "../components/LoadingOverlay";
|
2020-10-01 22:32:21 +10:00
|
|
|
|
|
|
|
|
import { isEmpty } from "../helpers/shared";
|
2021-02-04 15:06:34 +11:00
|
|
|
|
|
|
|
|
import useResponsiveLayout from "../hooks/useResponsiveLayout";
|
2020-10-01 22:32:21 +10:00
|
|
|
|
2021-05-20 12:22:07 +10:00
|
|
|
function EditTokenModal({ isOpen, onDone, token, onUpdateToken }) {
|
2020-10-01 22:32:21 +10:00
|
|
|
function handleClose() {
|
2020-10-07 16:42:40 +11:00
|
|
|
setTokenSettingChanges({});
|
2020-10-01 22:32:21 +10:00
|
|
|
onDone();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function handleSave() {
|
|
|
|
|
await applyTokenChanges();
|
|
|
|
|
onDone();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const [tokenSettingChanges, setTokenSettingChanges] = useState({});
|
|
|
|
|
|
|
|
|
|
function handleTokenSettingsChange(key, value) {
|
|
|
|
|
setTokenSettingChanges((prevChanges) => ({ ...prevChanges, [key]: value }));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function applyTokenChanges() {
|
|
|
|
|
if (token && !isEmpty(tokenSettingChanges)) {
|
|
|
|
|
// Ensure size value is positive
|
|
|
|
|
let verifiedChanges = { ...tokenSettingChanges };
|
|
|
|
|
if ("defaultSize" in verifiedChanges) {
|
|
|
|
|
verifiedChanges.defaultSize = verifiedChanges.defaultSize || 1;
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-20 12:22:07 +10:00
|
|
|
await onUpdateToken(token.id, verifiedChanges);
|
2020-10-01 22:32:21 +10:00
|
|
|
setTokenSettingChanges({});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const selectedTokenWithChanges = {
|
|
|
|
|
...token,
|
|
|
|
|
...tokenSettingChanges,
|
|
|
|
|
};
|
|
|
|
|
|
2021-01-03 14:53:06 +11:00
|
|
|
const layout = useResponsiveLayout();
|
|
|
|
|
|
2020-10-01 22:32:21 +10:00
|
|
|
return (
|
|
|
|
|
<Modal
|
|
|
|
|
isOpen={isOpen}
|
|
|
|
|
onRequestClose={handleClose}
|
2020-10-09 16:18:52 +11:00
|
|
|
style={{
|
2021-01-03 14:53:06 +11:00
|
|
|
maxWidth: layout.modalSize,
|
2020-10-09 16:18:52 +11:00
|
|
|
width: "calc(100% - 16px)",
|
|
|
|
|
}}
|
2020-10-01 22:32:21 +10:00
|
|
|
>
|
|
|
|
|
<Flex
|
|
|
|
|
sx={{
|
|
|
|
|
flexDirection: "column",
|
|
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
<Label pt={2} pb={1}>
|
|
|
|
|
Edit token
|
|
|
|
|
</Label>
|
2021-05-20 12:22:07 +10:00
|
|
|
{!token ? (
|
2021-02-08 16:53:56 +11:00
|
|
|
<Flex
|
|
|
|
|
sx={{
|
|
|
|
|
width: "100%",
|
|
|
|
|
height: layout.screenSize === "large" ? "500px" : "300px",
|
|
|
|
|
position: "relative",
|
|
|
|
|
}}
|
|
|
|
|
bg="muted"
|
|
|
|
|
>
|
|
|
|
|
<LoadingOverlay />
|
|
|
|
|
</Flex>
|
|
|
|
|
) : (
|
|
|
|
|
<TokenPreview token={selectedTokenWithChanges} />
|
|
|
|
|
)}
|
2020-10-01 22:32:21 +10:00
|
|
|
<TokenSettings
|
|
|
|
|
token={selectedTokenWithChanges}
|
|
|
|
|
onSettingsChange={handleTokenSettingsChange}
|
|
|
|
|
/>
|
|
|
|
|
<Button onClick={handleSave}>Save</Button>
|
|
|
|
|
</Flex>
|
|
|
|
|
</Modal>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default EditTokenModal;
|