import { ApolloClient, useApolloClient, useQuery } from '@apollo/client'; import { Button, makeStyles, Theme } from '@material-ui/core'; import gql from 'graphql-tag'; import * as React from 'react'; import { Field, FormRenderProps } from 'react-final-form'; import { useHistory, useParams } from 'react-router'; import { Link } from 'react-router-dom'; import Form from '../../components/Form'; import Input from '../../components/form/TextField'; import { showMessage } from '../../components/Toast'; import { CheckActivationCode, CheckActivationCodeVariables } from '../../graphql'; import { requestJSON } from '../../util/http'; import { required } from '../../util/validation'; import Loading from '../pages/Loading'; import AccountPage from './AccountPage'; const checkActivationCodeQuery = gql` query CheckActivationCode($code: String!) { checkActivationCode(code: $code) } `; const useStyles = makeStyles((theme: Theme) => ({ submit: { width: '100%', marginTop: theme.spacing(4), }, google: { width: '100%', marginTop: theme.spacing(4), background: theme.palette.common.white, }, errorMessage: { marginTop: theme.spacing(6), }, })); const activate = (client: ApolloClient, code: string, callback: any) => async ({ name, password, }: { name: string; password: string; }) => { await requestJSON('/auth/activate', 'post', { name, password, code, }); showMessage('Your account has been activated. Please sign in.'); callback(); }; const Activate = () => { const classes = useStyles(); const history = useHistory(); const { code } = useParams<{ code: string }>(); const client = useApolloClient(); const { data, loading } = useQuery(checkActivationCodeQuery, { variables: { code, }, }); if (loading) { return ; } return ( Signing in? } > {data!.checkActivationCode &&
{data!.checkActivationCode}
} {!data!.checkActivationCode && (
history.push('/signin'))} render={({ handleSubmit, submitting }: FormRenderProps) => ( )} /> )}
); }; export default Activate;