import { useQuery, useMutation } from '@apollo/react-hooks' import { makeStyles, Grid } from '@material-ui/core' import Paper from '@material-ui/core/Paper' import { Field, Form, Formik } from 'formik' import gql from 'graphql-tag' import React, { useReducer } from 'react' import { useLocation, useHistory } from 'react-router-dom' import * as Yup from 'yup' import { Button } from 'src/components/buttons' import { SecretInput } from 'src/components/inputs/formik' import { H2, Label3, P } from 'src/components/typography' import { ReactComponent as Logo } from 'src/styling/icons/menu/logo.svg' import styles from './shared.styles' const QueryParams = () => new URLSearchParams(useLocation().search) const useStyles = makeStyles(styles) const VALIDATE_REGISTER_LINK = gql` query validateRegisterLink($token: String!) { validateRegisterLink(token: $token) { username role } } ` const REGISTER = gql` mutation register( $token: String! $username: String! $password: String! $role: String! ) { register( token: $token username: $username password: $password role: $role ) } ` const validationSchema = Yup.object().shape({ password: Yup.string() .required('A password is required') .test( 'len', 'Your password must contain more than 8 characters', val => val.length >= 8 ), confirmPassword: Yup.string().oneOf( [Yup.ref('password'), null], 'Passwords must match' ) }) const initialValues = { password: '', confirmPassword: '' } const initialState = { username: null, role: null, result: '' } const reducer = (state, action) => { const { type, payload } = action return { ...state, ...payload, result: type } } const Register = () => { const classes = useStyles() const history = useHistory() const token = QueryParams().get('t') const [state, dispatch] = useReducer(reducer, initialState) const { error: queryError, loading } = useQuery(VALIDATE_REGISTER_LINK, { variables: { token: token }, onCompleted: ({ validateRegisterLink: info }) => { if (!info) { return dispatch({ type: 'failure' }) } dispatch({ type: 'success', payload: { username: info.username, role: info.role } }) }, onError: () => dispatch({ type: 'failure' }) }) const [register, { error: mutationError }] = useMutation(REGISTER, { onCompleted: ({ register: success }) => { if (success) history.push('/wizard', { fromAuthRegister: true }) } }) const getErrorMsg = (formikErrors, formikTouched) => { if (!formikErrors || !formikTouched) return null if (queryError || mutationError) return 'Internal server error' if (formikErrors.password && formikTouched.password) return formikErrors.password if (formikErrors.confirmPassword && formikTouched.confirmPassword) return formikErrors.confirmPassword return null } return (

Lamassu Admin

{!loading && state.result === 'success' && ( { register({ variables: { token: token, username: state.username, password: values.password, role: state.role } }) }}> {({ errors, touched }) => (
{getErrorMsg(errors, touched) && (

{getErrorMsg(errors, touched)}

)}
)}
)} {!loading && state.result === 'failure' && ( <> Link has expired )}
) } export default Register