Skip to content

Instantly share code, notes, and snippets.

@Qasem-h
Created January 2, 2021 14:08
Show Gist options
  • Save Qasem-h/5f015e9359348580434ad6de3ff04413 to your computer and use it in GitHub Desktop.
Save Qasem-h/5f015e9359348580434ad6de3ff04413 to your computer and use it in GitHub Desktop.
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