@@ -7,7 +7,7 @@ import apiFetch from '@wordpress/api-fetch';
7
7
import { __ , sprintf } from '@wordpress/i18n' ;
8
8
import { useEffect , useState } from '@wordpress/element' ;
9
9
import { useDispatch , useSelect } from '@wordpress/data' ;
10
- import { Button } from '@wordpress/components' ;
10
+ import { Button , __experimentalConfirmDialog as ConfirmDialog } from '@wordpress/components' ;
11
11
import { globe } from '@wordpress/icons' ;
12
12
import { registerPlugin } from '@wordpress/plugins' ;
13
13
@@ -28,6 +28,7 @@ function IncomingPost() {
28
28
const [ isUnLinkedToggling , setIsUnLinkedToggling ] = useState ( false ) ;
29
29
const [ isUnLinked , setIsUnLinked ] = useState ( false ) ;
30
30
const [ hasInitializedSidebar , setHasInitializedSidebar ] = useState ( false ) ;
31
+ const [ showConfirmDialog , setShowConfirmDialog ] = useState ( false ) ;
31
32
32
33
const { postId, areMetaBoxesInitialized } = useSelect ( select => {
33
34
const {
@@ -79,7 +80,7 @@ function IncomingPost() {
79
80
} , [ isUnLinked ] ) ;
80
81
81
82
const toggleUnlinkedState = async ( unlinked ) => {
82
- apiFetch ( {
83
+ return apiFetch ( {
83
84
path : `newspack-network/v1/content-distribution/unlink/${ postId } ` ,
84
85
method : 'POST' ,
85
86
data : {
@@ -103,6 +104,7 @@ function IncomingPost() {
103
104
toggleUnlinkedState ( unlinked )
104
105
. then ( ( ) => {
105
106
setIsUnLinkedToggling ( false ) ;
107
+ setIsUnLinked ( false ) ;
106
108
window . location . reload ( ) ;
107
109
} ) ; // Reload to get the origin post content.
108
110
} ) ;
@@ -121,38 +123,56 @@ function IncomingPost() {
121
123
} ;
122
124
123
125
return (
124
- < ContentDistributionPanel
125
- header = {
126
- isUnLinked ? __ (
127
- 'This post has been unlinked from the origin post. Edits to the origin post will not update this version.' ,
128
- 'newspack-network'
129
- )
130
- : __ (
131
- 'This post is linked to the origin post. Edits to the origin post will update this version.' ,
132
- 'newspack-network'
133
- )
134
- }
135
- buttons = {
136
- < >
137
- < Button
138
- variant = "secondary"
139
- target = "_blank"
140
- href = { originalUrl }
141
- >
142
- { __ ( 'View origin post' , 'newspack-network' ) }
143
- </ Button >
144
- < Button
145
- variant = { isUnLinked ? 'primary' : 'secondary' }
146
- isDestructive = { ! isUnLinked }
147
- disabled = { isUnLinkedToggling }
148
- onClick = { ( ) => {
149
- toggleUnlinkedClicked ( ! isUnLinked ) ;
150
- } }
151
- >
152
- { isUnLinkedToggling ? ( isUnLinked ? __ ( 'Relinking...' , 'newspack-network' ) : __ ( 'Unlinking...' , 'newspack-network' ) ) : ( ! isUnLinked ? __ ( 'Unlink from origin post' , 'newspack-network' ) : __ ( 'Relink to origin post' , 'newspack-network' ) ) }
153
- </ Button >
154
- </ >
155
- } />
126
+ < >
127
+ < ContentDistributionPanel
128
+ header = {
129
+ isUnLinked ? __ (
130
+ 'This post has been unlinked from its origin. Edits to the origin post will not update this version.' ,
131
+ 'newspack-network'
132
+ )
133
+ : __ (
134
+ 'This post is linked to its origin. Edits to the origin post will update this version.' ,
135
+ 'newspack-network'
136
+ )
137
+ }
138
+ buttons = {
139
+ < >
140
+ < Button
141
+ variant = "secondary"
142
+ target = "_blank"
143
+ href = { originalUrl }
144
+ >
145
+ { __ ( 'View origin post' , 'newspack-network' ) }
146
+ </ Button >
147
+ < Button
148
+ variant = { isUnLinked ? 'primary' : 'secondary' }
149
+ isDestructive = { ! isUnLinked }
150
+ disabled = { isUnLinkedToggling }
151
+ onClick = { ( ) => {
152
+ setShowConfirmDialog ( true ) ;
153
+ } }
154
+ >
155
+ { isUnLinkedToggling ? ( isUnLinked ? __ ( 'Relinking...' , 'newspack-network' ) : __ ( 'Unlinking...' , 'newspack-network' ) ) : ( ! isUnLinked ? __ ( 'Unlink from origin post' , 'newspack-network' ) : __ ( 'Relink to origin post' , 'newspack-network' ) ) }
156
+ </ Button >
157
+ </ >
158
+ }
159
+ />
160
+ < ConfirmDialog
161
+ isOpen = { showConfirmDialog }
162
+ onConfirm = { ( ) => {
163
+ toggleUnlinkedClicked ( ! isUnLinked ) ;
164
+ setShowConfirmDialog ( false ) ;
165
+ } }
166
+ onCancel = { ( ) => setShowConfirmDialog ( false ) }
167
+ confirmButtonText = { isUnLinked ? __ ( 'Relink' , 'newspack-network' ) : __ ( 'Unlink' , 'newspack-network' ) }
168
+ size = "small"
169
+ >
170
+ { isUnLinked ?
171
+ __ ( 'Are you sure you want to relink this post to its origin? Any changes you\'ve made will be lost.' , 'newspack-network' ) :
172
+ __ ( 'Are you sure you want to unlink this post from its origin?' , 'newspack-network' )
173
+ }
174
+ </ ConfirmDialog >
175
+ </ >
156
176
) ;
157
177
}
158
178
0 commit comments