diff --git a/frontend/pages/subscriptions.js b/frontend/pages/subscriptions.js index 5a9cbb5e..fe0724cd 100644 --- a/frontend/pages/subscriptions.js +++ b/frontend/pages/subscriptions.js @@ -15,7 +15,7 @@ import { ModalOverlay, ModalContent, } from "@chakra-ui/react"; -import NewSubscription from "../src/components/NewSubscription"; +import NewSubscription from "../src/components/NewModalSubscripton"; import { AiOutlinePlusCircle } from "react-icons/ai"; const Subscriptions = () => { diff --git a/frontend/src/components/NewModalSubscripton.js b/frontend/src/components/NewModalSubscripton.js new file mode 100644 index 00000000..14b1536c --- /dev/null +++ b/frontend/src/components/NewModalSubscripton.js @@ -0,0 +1,177 @@ +import React, { useState, useEffect } from "react"; +import { useSubscriptions } from "../core/hooks"; +import { + Input, + Stack, + Text, + HStack, + useRadioGroup, + FormControl, + FormErrorMessage, + ModalBody, + ModalCloseButton, + ModalHeader, + Button, + ModalFooter, + Spinner, + IconButton, +} from "@chakra-ui/react"; +import RadioCard from "./RadioCard"; +import { useForm } from "react-hook-form"; +import { GithubPicker } from "react-color"; +import { BiRefresh } from "react-icons/bi"; +import { makeColor } from "../core/utils/makeColor"; +const NewSubscription = ({ isFreeOption, onClose }) => { + const [color, setColor] = useState(makeColor()); + const { typesCache, createSubscription } = useSubscriptions(); + const { handleSubmit, errors, register } = useForm({}); + const [radioState, setRadioState] = useState("ethereum_blockchain"); + let { getRootProps, getRadioProps } = useRadioGroup({ + name: "type", + defaultValue: radioState, + onChange: setRadioState, + }); + + const group = getRootProps(); + + useEffect(() => { + if (createSubscription.isSuccess) { + onClose(); + } + }, [createSubscription.isSuccess, onClose]); + + if (typesCache.isLoading) return ; + + const createSubscriptionWrap = (props) => { + createSubscription.mutate({ + ...props, + color: color, + type: isFreeOption ? "free" : radioState, + }); + }; + + const handleChangeColorComplete = (color) => { + setColor(color.hex); + }; + + return ( +
+ Subscribe to a new address + + + + + + {errors.label && errors.label.message} + + + + + + {errors.address && errors.address.message} + + + + + {isFreeOption + ? `Free subscription is only availible Ethereum blockchain source` + : `On which source?`} + + + + + {typesCache.data.subscriptions.map((type) => { + const radio = getRadioProps({ + value: type.id, + isDisabled: + !type.active || + (isFreeOption && + type.subscription_type !== "ethereum_blockchain"), + }); + return ( + + {type.name} + + ); + })} + + null} + > + + {errors.subscription_type && errors.subscription_type.message} + + + + + + + Label color + {" "} + setColor(makeColor())} + icon={} + /> + null} + w="200px" + > + + + + + + {errors.color && errors.color.message} + + + + + + + + + ); +}; + +export default NewSubscription; diff --git a/frontend/src/components/NewSubscription.js b/frontend/src/components/NewSubscription.js index 14b1536c..be79ac04 100644 --- a/frontend/src/components/NewSubscription.js +++ b/frontend/src/components/NewSubscription.js @@ -1,4 +1,4 @@ -import React, { useState, useEffect } from "react"; +import React, { useState, useEffect, useCallback } from "react"; import { useSubscriptions } from "../core/hooks"; import { Input, @@ -8,23 +8,24 @@ import { useRadioGroup, FormControl, FormErrorMessage, - ModalBody, - ModalCloseButton, - ModalHeader, Button, - ModalFooter, Spinner, IconButton, + ButtonGroup, + Spacer, + Flex, } from "@chakra-ui/react"; import RadioCard from "./RadioCard"; -import { useForm } from "react-hook-form"; -import { GithubPicker } from "react-color"; +// import { useForm } from "react-hook-form"; +import { CirclePicker } from "react-color"; import { BiRefresh } from "react-icons/bi"; import { makeColor } from "../core/utils/makeColor"; -const NewSubscription = ({ isFreeOption, onClose }) => { +import { useForm } from "react-hook-form"; +const _NewSubscription = ({ isFreeOption, onClose, setIsLoading }) => { const [color, setColor] = useState(makeColor()); - const { typesCache, createSubscription } = useSubscriptions(); const { handleSubmit, errors, register } = useForm({}); + const { typesCache, createSubscription } = useSubscriptions(); + // const { handleSubmit, errors, register } = useForm({}); const [radioState, setRadioState] = useState("ethereum_blockchain"); let { getRootProps, getRadioProps } = useRadioGroup({ name: "type", @@ -34,96 +35,106 @@ const NewSubscription = ({ isFreeOption, onClose }) => { const group = getRootProps(); + useEffect(() => { + if (setIsLoading) { + setIsLoading(createSubscription.isLoading); + } + }, [createSubscription.isLoading, setIsLoading]); + useEffect(() => { if (createSubscription.isSuccess) { onClose(); } }, [createSubscription.isSuccess, onClose]); - if (typesCache.isLoading) return ; + const createSubscriptionWrapper = useCallback( + (props) => { + createSubscription.mutate({ + ...props, + color: color, + type: isFreeOption ? 0 : radioState, + }); + }, + [createSubscription, isFreeOption, color, radioState] + ); - const createSubscriptionWrap = (props) => { - createSubscription.mutate({ - ...props, - color: color, - type: isFreeOption ? "free" : radioState, - }); - }; + if (typesCache.isLoading) return ; const handleChangeColorComplete = (color) => { setColor(color.hex); }; - return ( -
- Subscribe to a new address - - - - - - {errors.label && errors.label.message} - - - - - - {errors.address && errors.address.message} - - - - - {isFreeOption - ? `Free subscription is only availible Ethereum blockchain source` - : `On which source?`} - + console.log("check errors:", errors); + if (!errors) return ""; - - - {typesCache.data.subscriptions.map((type) => { - const radio = getRadioProps({ - value: type.id, - isDisabled: - !type.active || - (isFreeOption && - type.subscription_type !== "ethereum_blockchain"), - }); - return ( - - {type.name} - - ); - })} - - null} - > - - {errors.subscription_type && errors.subscription_type.message} - - - - - + return ( + + + + + {errors?.label && errors?.label.message} + + + + + + {errors?.address && errors?.address.message} + + + + {/* + {isFreeOption + ? `Right now you can subscribe only to ethereum blockchain` + : `On which source?`} + */} + + + + {typesCache.data.subscriptions.map((type) => { + const radio = getRadioProps({ + value: type.id, + isDisabled: + !type.active || + (isFreeOption && + type.subscription_type !== "ethereum_blockchain"), + }); + return ( + + {type.name} + + ); + })} + + null} + > + + {errors?.subscription_type && errors?.subscription_type.message} + + + + + + Label color {" "} @@ -147,18 +158,21 @@ const NewSubscription = ({ isFreeOption, onClose }) => { w="200px" > + + + + - + + {errors?.color && errors?.color.message} + + - - {errors.color && errors.color.message} - - - - + + - + ); }; -export default NewSubscription; +export default _NewSubscription;