@@ -3,11 +3,13 @@ import React, { Component, Fragment } from 'react';
3
3
import PostHead from 'components/post/PostHead' ;
4
4
import PostContent from 'components/post/PostContent' ;
5
5
import PostTags from 'components/post/PostTags' ;
6
- import { PostsActions } from 'store/actionCreators' ;
6
+ import { PostsActions , CommonActions } from 'store/actionCreators' ;
7
7
import { connect } from 'react-redux' ;
8
8
import type { State } from 'store' ;
9
9
import type { PostData , TocItem } from 'store/modules/posts' ;
10
10
import PostToc from 'components/post/PostToc' ;
11
+ import QuestionModal from 'components/common/QuestionModal/QuestionModal' ;
12
+ import { withRouter , type ContextRouter , type Location } from 'react-router-dom' ;
11
13
12
14
type Props = {
13
15
username : ?string ,
@@ -17,7 +19,9 @@ type Props = {
17
19
activeHeading : ?string ,
18
20
likeInProcess : boolean ,
19
21
currentUsername : ?string ,
20
- } ;
22
+ askRemove : boolean ,
23
+ routerHistory : Location [ ] ,
24
+ } & ContextRouter ;
21
25
22
26
class PostViewer extends Component < Props > {
23
27
initialize = async ( ) => {
@@ -52,19 +56,40 @@ class PostViewer extends Component<Props> {
52
56
}
53
57
} ;
54
58
59
+ onToggleAskRemove = ( ) => {
60
+ PostsActions . toggleAskRemove ( ) ;
61
+ } ;
62
+
63
+ onConfirmRemove = async ( ) => {
64
+ const { post, history, routerHistory } = this . props ;
65
+ PostsActions . toggleAskRemove ( ) ;
66
+ if ( ! post ) return ;
67
+ try {
68
+ CommonActions . removePost ( post . id ) ;
69
+ } catch ( e ) {
70
+ console . log ( e ) ;
71
+ }
72
+ if ( routerHistory . length === 0 ) {
73
+ history . push ( '/' ) ;
74
+ return ;
75
+ }
76
+ history . goBack ( ) ;
77
+ } ;
78
+
55
79
componentDidMount ( ) {
56
80
this . initialize ( ) ;
57
81
}
58
82
59
83
render ( ) {
60
- const { post, toc, activeHeading, username, currentUsername } = this . props ;
84
+ const { post, toc, activeHeading, username, currentUsername, askRemove } = this . props ;
61
85
const { onSetToc, onActivateHeading } = this ;
62
86
if ( ! post ) return null ;
63
87
64
88
return (
65
89
< Fragment >
66
90
< PostToc toc = { toc } activeHeading = { activeHeading } />
67
91
< PostHead
92
+ id = { post . id }
68
93
title = { post . title }
69
94
tags = { post . tags }
70
95
categories = { post . categories }
@@ -73,6 +98,7 @@ class PostViewer extends Component<Props> {
73
98
liked = { post . liked }
74
99
onToggleLike = { this . onToggleLike }
75
100
ownPost = { currentUsername === username }
101
+ onAskRemove = { this . onToggleAskRemove }
76
102
/>
77
103
< PostContent
78
104
thumbnail = { post . thumbnail }
@@ -81,18 +107,28 @@ class PostViewer extends Component<Props> {
81
107
onActivateHeading = { onActivateHeading }
82
108
/>
83
109
< PostTags tags = { post . tags } />
110
+ < QuestionModal
111
+ open = { askRemove }
112
+ title = "포스트 삭제"
113
+ description = "이 포스트를 정말로 삭제하시겠습니까?"
114
+ confirmText = "삭제"
115
+ onConfirm = { this . onConfirmRemove }
116
+ onCancel = { this . onToggleAskRemove }
117
+ />
84
118
</ Fragment >
85
119
) ;
86
120
}
87
121
}
88
122
89
123
export default connect (
90
- ( { posts, pender, user } : State ) => ( {
124
+ ( { posts, pender, user, common } : State ) => ( {
91
125
currentUsername : user . user && user . user . username ,
92
126
post : posts . post ,
93
127
toc : posts . toc ,
94
128
activeHeading : posts . activeHeading ,
95
129
likeInProcess : pender . pending [ 'posts/LIKE' ] || pender . pending [ 'posts/UNLIKE' ] ,
130
+ askRemove : posts . askRemove ,
131
+ routerHistory : common . router . history ,
96
132
} ) ,
97
133
( ) => ( { } ) ,
98
- ) ( PostViewer ) ;
134
+ ) ( withRouter ( PostViewer ) ) ;
0 commit comments