Skip to content

Commit 5c53a67

Browse files
committed
Add PostDetail deleteMutation
1 parent 1fa0f96 commit 5c53a67

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

frontend/src/components/post/PostDetail.tsx

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ interface PublishMutationResponse {
2020
publish: PostType
2121
}
2222

23+
interface DeleteMutationResponse {
24+
deletePost: PostType
25+
}
26+
2327
interface MatchParam {
2428
id: string
2529
}
@@ -38,6 +42,8 @@ const PostDetail: React.FC = () => {
3842

3943
const publishMutation = useMutation<PublishMutationResponse>(PUBLISH_MUTATION)
4044

45+
const deleteMutation = useMutation<DeleteMutationResponse>(DELETE_MUTATION)
46+
4147
const handlePublish = () => {
4248
if (!postQuery.data) return
4349

@@ -54,6 +60,26 @@ const PostDetail: React.FC = () => {
5460
})
5561
}
5662

63+
const handleDelete = () => {
64+
if (!postQuery.data) return
65+
66+
deleteMutation({
67+
variables: {
68+
where: { id: postQuery.data.post.id },
69+
},
70+
})
71+
.then(() => {
72+
client
73+
.resetStore()
74+
.then(() =>
75+
history.push(postQuery.data.post.published ? '/' : '/drafts'),
76+
)
77+
})
78+
.catch((e) => {
79+
setError(e.message)
80+
})
81+
}
82+
5783
console.log('PostDetail: ', postQuery)
5884

5985
if (!meQuery || !postQuery || !postQuery.data) return <div>ERROR</div>
@@ -79,6 +105,7 @@ const PostDetail: React.FC = () => {
79105
{isOwner && !postQuery.data.post.published && (
80106
<Button onClick={handlePublish}>Publish</Button>
81107
)}
108+
{isOwner && <Button onClick={handleDelete}>Delete</Button>}
82109
{error}
83110
</div>
84111
)
@@ -93,6 +120,14 @@ const PUBLISH_MUTATION = gql`
93120
}
94121
`
95122

123+
const DELETE_MUTATION = gql`
124+
mutation DeletePost($where: PostWhereUniqueInput!) {
125+
deletePost(where: $where) {
126+
id
127+
}
128+
}
129+
`
130+
96131
const POST_QUERY = gql`
97132
query Post($where: PostWhereUniqueInput!) {
98133
post(where: $where) {

frontend/src/routes/RoutesAuth.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ interface MeQueryResponse {
2424
data: {
2525
me: UserType
2626
}
27+
error: {
28+
message: string
29+
}
30+
loading: boolean
2731
}
2832

2933
const RoutesAuth: React.FC = () => {

0 commit comments

Comments
 (0)