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
10 changed files
with
172 additions
and
14 deletions.
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
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,16 @@ | ||
import React from "react"; | ||
import { Wrapper, WrapperVariant } from "./Wrapper"; | ||
import { NavBar } from "./NavBar"; | ||
|
||
interface LayoutProps { | ||
variant?: WrapperVariant; | ||
} | ||
|
||
export const Layout: React.FC<LayoutProps> = ({ children, variant }) => { | ||
return ( | ||
<> | ||
<NavBar /> | ||
<Wrapper variant={variant}>{children}</Wrapper> | ||
</> | ||
); | ||
}; |
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
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,11 @@ | ||
mutation CreatePost($input: PostInput!) { | ||
createPost(input: $input) { | ||
id | ||
createdAt | ||
updatedAt | ||
title | ||
text | ||
points | ||
creatorId | ||
} | ||
} |
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,53 @@ | ||
import { Box, Button } from "@chakra-ui/core"; | ||
import { Form, Formik } from "formik"; | ||
import { withUrqlClient } from "next-urql"; | ||
import { useRouter } from "next/router"; | ||
import React from "react"; | ||
import { InputField } from "../components/InputField"; | ||
import { Layout } from "../components/Layout"; | ||
import { useCreatePostMutation } from "../generated/graphql"; | ||
import { createUrqlClient } from "../utils/createUrqlClient"; | ||
import { useIsAuth } from "../utils/useIsAuth"; | ||
|
||
const CreatePost: React.FC<{}> = ({}) => { | ||
const router = useRouter(); | ||
useIsAuth(); | ||
const [, createPost] = useCreatePostMutation(); | ||
return ( | ||
<Layout variant="small"> | ||
<Formik | ||
initialValues={{ title: "", text: "" }} | ||
onSubmit={async (values) => { | ||
const { error } = await createPost({ input: values }); | ||
if (!error) { | ||
router.push("/"); | ||
} | ||
}} | ||
> | ||
{({ isSubmitting }) => ( | ||
<Form> | ||
<InputField name="title" placeholder="title" label="Title" /> | ||
<Box mt={4}> | ||
<InputField | ||
textarea | ||
name="text" | ||
placeholder="text..." | ||
label="Body" | ||
/> | ||
</Box> | ||
<Button | ||
mt={4} | ||
type="submit" | ||
isLoading={isSubmitting} | ||
variantColor="teal" | ||
> | ||
create post | ||
</Button> | ||
</Form> | ||
)} | ||
</Formik> | ||
</Layout> | ||
); | ||
}; | ||
|
||
export default withUrqlClient(createUrqlClient)(CreatePost); |
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
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,13 @@ | ||
import { useMeQuery } from "../generated/graphql"; | ||
import { useRouter } from "next/router"; | ||
import { useEffect } from "react"; | ||
|
||
export const useIsAuth = () => { | ||
const [{ data, fetching }] = useMeQuery(); | ||
const router = useRouter(); | ||
useEffect(() => { | ||
if (!fetching && !data?.me) { | ||
router.replace("/login"); | ||
} | ||
}, [fetching, data, router]); | ||
}; |