Created
January 2, 2021 14:08
-
-
Save Qasem-h/5f015e9359348580434ad6de3ff04413 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React from "react"; | |
import { Formik, Form } from "formik"; | |
import { Box, Button } from "@chakra-ui/core"; | |
import { Wrapper } from "../components/Wrapper"; | |
import { InputField } from "../components/InputField"; | |
import { toErrorMap } from "../utils/toErrorMap"; | |
import { useRouter } from "next/router"; | |
import { useLoginMutation } from "../generated/graphql"; | |
import { createUrqlClient } from "../utils/createUrqlClient"; | |
import { withUrqlClient } from "next-urql"; | |
const Login: React.FC<{}> = ({}) => { | |
const router = useRouter(); | |
const [, login] = useLoginMutation(); | |
const initialValues = { usernameOrEmail: "", password: "" }; | |
const doLogin = async (values, { setErrors }) => { | |
const response = await login(values); | |
if (response.data?.login.errors) { | |
setErrors(toErrorMap(response.data.login.errors)); | |
} else if (response.data?.login.user) { | |
// worked | |
router.push("/"); | |
} | |
}; | |
return ( | |
<Wrapper variant="small"> | |
<Formik onSubmit={doLogin}> | |
{({ isSubmitting }) => ( | |
<Form> | |
<InputField | |
name="usernameOrEmail" | |
placeholder="Username or Email" | |
label="Username Or Email" | |
/> | |
<Box mt={4}> | |
<InputField | |
name="password" | |
placeholder="password" | |
label="Password" | |
type="password" | |
/> | |
</Box> | |
<Button | |
mt={4} | |
type="submit" | |
isLoading={isSubmitting} | |
variantColor="teal" | |
> | |
Login | |
</Button> | |
</Form> | |
)} | |
</Formik> | |
</Wrapper> | |
); | |
}; | |
export default withUrqlClient(createUrqlClient)(Login); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment