@@ -20,6 +20,10 @@ interface PublishMutationResponse {
20
20
publish : PostType
21
21
}
22
22
23
+ interface DeleteMutationResponse {
24
+ deletePost : PostType
25
+ }
26
+
23
27
interface MatchParam {
24
28
id : string
25
29
}
@@ -38,6 +42,8 @@ const PostDetail: React.FC = () => {
38
42
39
43
const publishMutation = useMutation < PublishMutationResponse > ( PUBLISH_MUTATION )
40
44
45
+ const deleteMutation = useMutation < DeleteMutationResponse > ( DELETE_MUTATION )
46
+
41
47
const handlePublish = ( ) => {
42
48
if ( ! postQuery . data ) return
43
49
@@ -54,6 +60,26 @@ const PostDetail: React.FC = () => {
54
60
} )
55
61
}
56
62
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
+
57
83
console . log ( 'PostDetail: ' , postQuery )
58
84
59
85
if ( ! meQuery || ! postQuery || ! postQuery . data ) return < div > ERROR</ div >
@@ -79,6 +105,7 @@ const PostDetail: React.FC = () => {
79
105
{ isOwner && ! postQuery . data . post . published && (
80
106
< Button onClick = { handlePublish } > Publish</ Button >
81
107
) }
108
+ { isOwner && < Button onClick = { handleDelete } > Delete</ Button > }
82
109
{ error }
83
110
</ div >
84
111
)
@@ -93,6 +120,14 @@ const PUBLISH_MUTATION = gql`
93
120
}
94
121
`
95
122
123
+ const DELETE_MUTATION = gql `
124
+ mutation DeletePost($where: PostWhereUniqueInput!) {
125
+ deletePost(where: $where) {
126
+ id
127
+ }
128
+ }
129
+ `
130
+
96
131
const POST_QUERY = gql `
97
132
query Post($where: PostWhereUniqueInput!) {
98
133
post(where: $where) {
0 commit comments