newSubscription can be not modal (dplicate code)

pull/116/head
Tim Pechersky 2021-08-18 14:30:42 +02:00
rodzic bceb66f900
commit eb8f5be087
3 zmienionych plików z 291 dodań i 99 usunięć

Wyświetl plik

@ -15,7 +15,7 @@ import {
ModalOverlay, ModalOverlay,
ModalContent, ModalContent,
} from "@chakra-ui/react"; } from "@chakra-ui/react";
import NewSubscription from "../src/components/NewSubscription"; import NewSubscription from "../src/components/NewModalSubscripton";
import { AiOutlinePlusCircle } from "react-icons/ai"; import { AiOutlinePlusCircle } from "react-icons/ai";
const Subscriptions = () => { const Subscriptions = () => {

Wyświetl plik

@ -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 <Spinner />;
const createSubscriptionWrap = (props) => {
createSubscription.mutate({
...props,
color: color,
type: isFreeOption ? "free" : radioState,
});
};
const handleChangeColorComplete = (color) => {
setColor(color.hex);
};
return (
<form onSubmit={handleSubmit(createSubscriptionWrap)}>
<ModalHeader>Subscribe to a new address</ModalHeader>
<ModalCloseButton />
<ModalBody>
<FormControl isInvalid={errors.label}>
<Input
my={2}
type="text"
autoComplete="off"
placeholder="Enter label"
name="label"
ref={register({ required: "label is required!" })}
></Input>
<FormErrorMessage color="unsafe.400" pl="1">
{errors.label && errors.label.message}
</FormErrorMessage>
</FormControl>
<FormControl isInvalid={errors.address}>
<Input
type="text"
autoComplete="off"
my={2}
placeholder="Enter address"
name="address"
ref={register({ required: "address is required!" })}
></Input>
<FormErrorMessage color="unsafe.400" pl="1">
{errors.address && errors.address.message}
</FormErrorMessage>
</FormControl>
<Stack my={16} direction="column">
<Text fontWeight="600">
{isFreeOption
? `Free subscription is only availible Ethereum blockchain source`
: `On which source?`}
</Text>
<FormControl isInvalid={errors.subscription_type}>
<HStack {...group} alignItems="stretch">
{typesCache.data.subscriptions.map((type) => {
const radio = getRadioProps({
value: type.id,
isDisabled:
!type.active ||
(isFreeOption &&
type.subscription_type !== "ethereum_blockchain"),
});
return (
<RadioCard key={`subscription_type_${type.id}`} {...radio}>
{type.name}
</RadioCard>
);
})}
</HStack>
<Input
type="hidden"
placeholder="subscription_type"
name="subscription_type"
ref={register({ required: "select type" })}
value={radioState}
onChange={() => null}
></Input>
<FormErrorMessage color="unsafe.400" pl="1">
{errors.subscription_type && errors.subscription_type.message}
</FormErrorMessage>
</FormControl>
</Stack>
<FormControl isInvalid={errors.color}>
<Stack direction="row" pb={2}>
<Text fontWeight="600" alignSelf="center">
Label color
</Text>{" "}
<IconButton
size="md"
// colorScheme="primary"
color={"white.100"}
_hover={{ bgColor: { color } }}
bgColor={color}
variant="outline"
onClick={() => setColor(makeColor())}
icon={<BiRefresh />}
/>
<Input
type="input"
placeholder="color"
name="color"
ref={register({ required: "color is required!" })}
value={color}
onChange={() => null}
w="200px"
></Input>
</Stack>
<GithubPicker
// color={this.state.background}
onChangeComplete={handleChangeColorComplete}
/>
<FormErrorMessage color="unsafe.400" pl="1">
{errors.color && errors.color.message}
</FormErrorMessage>
</FormControl>
</ModalBody>
<ModalFooter>
<Button
type="submit"
colorScheme="suggested"
isLoading={createSubscription.isLoading}
>
Confirm
</Button>
<Button colorScheme="gray" onClick={onClose}>
Cancel
</Button>
</ModalFooter>
</form>
);
};
export default NewSubscription;

Wyświetl plik

@ -1,4 +1,4 @@
import React, { useState, useEffect } from "react"; import React, { useState, useEffect, useCallback } from "react";
import { useSubscriptions } from "../core/hooks"; import { useSubscriptions } from "../core/hooks";
import { import {
Input, Input,
@ -8,23 +8,24 @@ import {
useRadioGroup, useRadioGroup,
FormControl, FormControl,
FormErrorMessage, FormErrorMessage,
ModalBody,
ModalCloseButton,
ModalHeader,
Button, Button,
ModalFooter,
Spinner, Spinner,
IconButton, IconButton,
ButtonGroup,
Spacer,
Flex,
} from "@chakra-ui/react"; } from "@chakra-ui/react";
import RadioCard from "./RadioCard"; import RadioCard from "./RadioCard";
import { useForm } from "react-hook-form"; // import { useForm } from "react-hook-form";
import { GithubPicker } from "react-color"; import { CirclePicker } from "react-color";
import { BiRefresh } from "react-icons/bi"; import { BiRefresh } from "react-icons/bi";
import { makeColor } from "../core/utils/makeColor"; 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 [color, setColor] = useState(makeColor());
const { typesCache, createSubscription } = useSubscriptions();
const { handleSubmit, errors, register } = useForm({}); const { handleSubmit, errors, register } = useForm({});
const { typesCache, createSubscription } = useSubscriptions();
// const { handleSubmit, errors, register } = useForm({});
const [radioState, setRadioState] = useState("ethereum_blockchain"); const [radioState, setRadioState] = useState("ethereum_blockchain");
let { getRootProps, getRadioProps } = useRadioGroup({ let { getRootProps, getRadioProps } = useRadioGroup({
name: "type", name: "type",
@ -34,65 +35,74 @@ const NewSubscription = ({ isFreeOption, onClose }) => {
const group = getRootProps(); const group = getRootProps();
useEffect(() => {
if (setIsLoading) {
setIsLoading(createSubscription.isLoading);
}
}, [createSubscription.isLoading, setIsLoading]);
useEffect(() => { useEffect(() => {
if (createSubscription.isSuccess) { if (createSubscription.isSuccess) {
onClose(); onClose();
} }
}, [createSubscription.isSuccess, onClose]); }, [createSubscription.isSuccess, onClose]);
if (typesCache.isLoading) return <Spinner />; const createSubscriptionWrapper = useCallback(
(props) => {
const createSubscriptionWrap = (props) => {
createSubscription.mutate({ createSubscription.mutate({
...props, ...props,
color: color, color: color,
type: isFreeOption ? "free" : radioState, type: isFreeOption ? 0 : radioState,
}); });
}; },
[createSubscription, isFreeOption, color, radioState]
);
if (typesCache.isLoading) return <Spinner />;
const handleChangeColorComplete = (color) => { const handleChangeColorComplete = (color) => {
setColor(color.hex); setColor(color.hex);
}; };
console.log("check errors:", errors);
if (!errors) return "";
return ( return (
<form onSubmit={handleSubmit(createSubscriptionWrap)}> <form onSubmit={handleSubmit(createSubscriptionWrapper)}>
<ModalHeader>Subscribe to a new address</ModalHeader> <FormControl isInvalid={errors?.label}>
<ModalCloseButton />
<ModalBody>
<FormControl isInvalid={errors.label}>
<Input <Input
my={2} my={2}
type="text" type="text"
autoComplete="off" autoComplete="off"
placeholder="Enter label" placeholder="Meaningful name of your subscription"
name="label" name="label"
ref={register({ required: "label is required!" })} ref={register({ required: "label is required!" })}
></Input> ></Input>
<FormErrorMessage color="unsafe.400" pl="1"> <FormErrorMessage color="unsafe.400" pl="1">
{errors.label && errors.label.message} {errors?.label && errors?.label.message}
</FormErrorMessage> </FormErrorMessage>
</FormControl> </FormControl>
<FormControl isInvalid={errors.address}> <FormControl isInvalid={errors?.address}>
<Input <Input
type="text" type="text"
autoComplete="off" autoComplete="off"
my={2} my={2}
placeholder="Enter address" placeholder="Address to subscribe to"
name="address" name="address"
ref={register({ required: "address is required!" })} ref={register({ required: "address is required!" })}
></Input> ></Input>
<FormErrorMessage color="unsafe.400" pl="1"> <FormErrorMessage color="unsafe.400" pl="1">
{errors.address && errors.address.message} {errors?.address && errors?.address.message}
</FormErrorMessage> </FormErrorMessage>
</FormControl> </FormControl>
<Stack my={16} direction="column"> <Stack my={4} direction="column">
<Text fontWeight="600"> {/* <Text fontWeight="600">
{isFreeOption {isFreeOption
? `Free subscription is only availible Ethereum blockchain source` ? `Right now you can subscribe only to ethereum blockchain`
: `On which source?`} : `On which source?`}
</Text> </Text> */}
<FormControl isInvalid={errors.subscription_type}> <FormControl isInvalid={errors?.subscription_type}>
<HStack {...group} alignItems="stretch"> <HStack {...group} alignItems="stretch">
{typesCache.data.subscriptions.map((type) => { {typesCache.data.subscriptions.map((type) => {
const radio = getRadioProps({ const radio = getRadioProps({
@ -118,12 +128,13 @@ const NewSubscription = ({ isFreeOption, onClose }) => {
onChange={() => null} onChange={() => null}
></Input> ></Input>
<FormErrorMessage color="unsafe.400" pl="1"> <FormErrorMessage color="unsafe.400" pl="1">
{errors.subscription_type && errors.subscription_type.message} {errors?.subscription_type && errors?.subscription_type.message}
</FormErrorMessage> </FormErrorMessage>
</FormControl> </FormControl>
</Stack> </Stack>
<FormControl isInvalid={errors.color}> <FormControl isInvalid={errors?.color}>
<Stack direction="row" pb={2}> <Flex direction="row" pb={2} flexWrap="wrap">
<Stack pt={2} direction="row" h="min-content">
<Text fontWeight="600" alignSelf="center"> <Text fontWeight="600" alignSelf="center">
Label color Label color
</Text>{" "} </Text>{" "}
@ -147,18 +158,21 @@ const NewSubscription = ({ isFreeOption, onClose }) => {
w="200px" w="200px"
></Input> ></Input>
</Stack> </Stack>
<Flex p={2}>
<GithubPicker <CirclePicker
// color={this.state.background}
onChangeComplete={handleChangeColorComplete} onChangeComplete={handleChangeColorComplete}
circleSpacing={1}
circleSize={24}
/> />
</Flex>
</Flex>
<FormErrorMessage color="unsafe.400" pl="1"> <FormErrorMessage color="unsafe.400" pl="1">
{errors.color && errors.color.message} {errors?.color && errors?.color.message}
</FormErrorMessage> </FormErrorMessage>
</FormControl> </FormControl>
</ModalBody>
<ModalFooter> <ButtonGroup direction="row" justifyContent="space-evenly">
<Button <Button
type="submit" type="submit"
colorScheme="suggested" colorScheme="suggested"
@ -166,12 +180,13 @@ const NewSubscription = ({ isFreeOption, onClose }) => {
> >
Confirm Confirm
</Button> </Button>
<Spacer />
<Button colorScheme="gray" onClick={onClose}> <Button colorScheme="gray" onClick={onClose}>
Cancel Cancel
</Button> </Button>
</ModalFooter> </ButtonGroup>
</form> </form>
); );
}; };
export default NewSubscription; export default _NewSubscription;