forked from benawad/lireddit
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
192 additions
and
1 deletion.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import React from "react"; | ||
import { Box, Link, Flex, Button } from "@chakra-ui/core"; | ||
import NextLink from "next/link"; | ||
import { useMeQuery } from "../generated/graphql"; | ||
|
||
interface NavBarProps {} | ||
|
||
export const NavBar: React.FC<NavBarProps> = ({}) => { | ||
const [{ data, fetching }] = useMeQuery(); | ||
let body = null; | ||
|
||
// data is loading | ||
if (fetching) { | ||
// user not logged in | ||
} else if (!data?.me) { | ||
body = ( | ||
<> | ||
<NextLink href="/login"> | ||
<Link mr={2}>login</Link> | ||
</NextLink> | ||
<NextLink href="/register"> | ||
<Link>register</Link> | ||
</NextLink> | ||
</> | ||
); | ||
// user is logged in | ||
} else { | ||
body = ( | ||
<Flex> | ||
<Box mr={2}>{data.me.username}</Box> | ||
<Button variant="link">logout</Button> | ||
</Flex> | ||
); | ||
} | ||
|
||
return ( | ||
<Flex bg="tomato" p={4}> | ||
<Box ml={"auto"}>{body}</Box> | ||
</Flex> | ||
); | ||
}; |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
mutation Login($options: UsernamePasswordInput!) { | ||
login(options: $options) { | ||
errors { | ||
field | ||
message | ||
} | ||
user { | ||
id | ||
username | ||
} | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
query Me { | ||
me { | ||
id | ||
username | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,10 @@ | ||
const Index = () => <div>hello world</div>; | ||
import { NavBar } from "../components/NavBar"; | ||
|
||
const Index = () => ( | ||
<> | ||
<NavBar /> | ||
<div>hello world</div> | ||
</> | ||
); | ||
|
||
export default Index; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
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 { useLoginMutation } from "../generated/graphql"; | ||
import { toErrorMap } from "../utils/toErrorMap"; | ||
import { useRouter } from "next/router"; | ||
|
||
const Login: React.FC<{}> = ({}) => { | ||
const router = useRouter(); | ||
const [, login] = useLoginMutation(); | ||
return ( | ||
<Wrapper variant="small"> | ||
<Formik | ||
initialValues={{ username: "", password: "" }} | ||
onSubmit={async (values, { setErrors }) => { | ||
const response = await login({ options: values }); | ||
if (response.data?.login.errors) { | ||
setErrors(toErrorMap(response.data.login.errors)); | ||
} else if (response.data?.login.user) { | ||
// worked | ||
router.push("/"); | ||
} | ||
}} | ||
> | ||
{({ isSubmitting }) => ( | ||
<Form> | ||
<InputField | ||
name="username" | ||
placeholder="username" | ||
label="Username" | ||
/> | ||
<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 Login; |
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