import React, { FC, useState, useEffect } from 'react'; import DappbotAPI from '@eximchain/dappbot-api-client'; import { useResource } from 'react-request-hook'; import { ArgPrompt, ErrorBox, Loader, ChevronText } from '../helpers'; import { Payment } from '@eximchain/dappbot-types/spec/methods'; import Responses from '@eximchain/dappbot-types/spec/responses'; import { trackSignup } from '../../services'; export interface CreateAccountProps { API: DappbotAPI setEmail: (email: string) => void } export const CreateAccount: FC = ({ API, setEmail }) => { const [email, setEmailState] = useState(''); const [name, setName] = useState(''); const [organization, setOrganization] = useState(''); const [occupation, setOccupation] = useState(''); const [signupResponse, requestSignup] = useResource(API.payment.signUp.resource); const { data, isLoading, error } = signupResponse; const [localErr, setLocalErr] = useState(null as any); useEffect(function handleResponse() { if (Responses.isSuccessResponse(data)) { setEmail(email); trackSignup(API, email, { name, organization, occupation }); } else { setLocalErr(data); } }, [data, isLoading, error]) if (email === '') return ( What email would you like to use? This will be your username.} withResult={setEmailState} key='emailPrompt' /> ) if (name === '') return ( What is your name?} withResult={(nameVal) => { setName(nameVal); }} key='namePrompt' /> ) if (organization === '') return ( What organization are you working with? If working alone, please enter "Self"., This is optional, but we are curious. ]} withResult={(orgVal) => { // This value needs to not be an empty string in order // to progress within the flow. let chosenVal = orgVal !== '' ? orgVal : 'N/A'; setOrganization(chosenVal); }} key='orgPrompt' /> ) if (occupation === '') return ( What is your occupation?, This is also optional, although we are still curious. ]} withResult={(occupationVal) => { let chosenVal = occupationVal !== '' ? occupationVal : 'N/A'; setOccupation(chosenVal); requestSignup({ email, name, plans: Payment.trialStripePlan() }) }} key='occupationPrompt' /> ) return error || localErr ? ( ) : ( ) } export default CreateAccount;