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
212 additions
and
49 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,36 @@ | ||
import React from "react"; | ||
import { Box, IconButton, Link } from "@chakra-ui/core"; | ||
import NextLink from "next/link"; | ||
import { useDeletePostMutation, useMeQuery } from "../generated/graphql"; | ||
|
||
interface EditDeletePostButtonsProps { | ||
id: number; | ||
creatorId: number; | ||
} | ||
|
||
export const EditDeletePostButtons: React.FC<EditDeletePostButtonsProps> = ({ | ||
id, | ||
creatorId, | ||
}) => { | ||
const [{ data: meData }] = useMeQuery(); | ||
const [, deletePost] = useDeletePostMutation(); | ||
|
||
if (meData?.me?.id !== creatorId) { | ||
return null; | ||
} | ||
|
||
return ( | ||
<Box> | ||
<NextLink href="/post/edit/[id]" as={`/post/edit/${id}`}> | ||
<IconButton as={Link} mr={4} icon="edit" aria-label="Edit Post" /> | ||
</NextLink> | ||
<IconButton | ||
icon="delete" | ||
aria-label="Delete Post" | ||
onClick={() => { | ||
deletePost({ id }); | ||
}} | ||
/> | ||
</Box> | ||
); | ||
}; |
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,8 @@ | ||
mutation UpdatePost($id: Int!, $title: String!, $text: String!) { | ||
updatePost(id: $id, title: $title, text: $text) { | ||
id | ||
title | ||
text | ||
textSnippet | ||
} | ||
} |
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,76 @@ | ||
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 { | ||
usePostQuery, | ||
useUpdatePostMutation, | ||
} from "../../../generated/graphql"; | ||
import { createUrqlClient } from "../../../utils/createUrqlClient"; | ||
import { useGetIntId } from "../../../utils/useGetIntId"; | ||
|
||
const EditPost = ({}) => { | ||
const router = useRouter(); | ||
const intId = useGetIntId(); | ||
const [{ data, fetching }] = usePostQuery({ | ||
pause: intId === -1, | ||
variables: { | ||
id: intId, | ||
}, | ||
}); | ||
const [, updatePost] = useUpdatePostMutation(); | ||
if (fetching) { | ||
return ( | ||
<Layout> | ||
<div>loading...</div> | ||
</Layout> | ||
); | ||
} | ||
|
||
if (!data?.post) { | ||
return ( | ||
<Layout> | ||
<Box>could not find post</Box> | ||
</Layout> | ||
); | ||
} | ||
|
||
return ( | ||
<Layout variant="small"> | ||
<Formik | ||
initialValues={{ title: data.post.title, text: data.post.text }} | ||
onSubmit={async (values) => { | ||
await updatePost({ id: intId, ...values }); | ||
router.back(); | ||
}} | ||
> | ||
{({ 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" | ||
> | ||
update post | ||
</Button> | ||
</Form> | ||
)} | ||
</Formik> | ||
</Layout> | ||
); | ||
}; | ||
|
||
export default withUrqlClient(createUrqlClient)(EditPost); |
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,9 @@ | ||
import { useRouter } from "next/router"; | ||
|
||
export const useGetIntId = () => { | ||
const router = useRouter(); | ||
const intId = | ||
typeof router.query.id === "string" ? parseInt(router.query.id) : -1; | ||
|
||
return intId; | ||
}; |
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,14 @@ | ||
import { utimes } from "fs"; | ||
import { useRouter } from "next/router"; | ||
import { usePostQuery } from "../generated/graphql"; | ||
import { useGetIntId } from "./useGetIntId"; | ||
|
||
export const useGetPostFromUrl = () => { | ||
const intId = useGetIntId(); | ||
return usePostQuery({ | ||
pause: intId === -1, | ||
variables: { | ||
id: intId, | ||
}, | ||
}); | ||
}; |