@@ -3,13 +3,103 @@ import { Modal, Button } from '@wordpress/components';
3
3
import { useTranslate } from 'i18n-calypso' ;
4
4
import { useState } from 'react' ;
5
5
import { useDispatch } from 'react-redux' ;
6
+ import { useBlogStickersQuery } from 'calypso/blocks/blog-stickers/use-blog-stickers-query' ;
6
7
import { LoadingBar } from 'calypso/components/loading-bar' ;
7
8
import wp from 'calypso/lib/wp' ;
8
9
import { successNotice , errorNotice } from 'calypso/state/notices/actions' ;
9
10
11
+ type CancelMigrationButtonProps = {
12
+ isMigrationInProgress : boolean ;
13
+ onClick : ( ) => void ;
14
+ buttonType : 'cta' | 'confirm' ;
15
+ variant ?: string ;
16
+ key ?: string ;
17
+ } ;
18
+
19
+ const CancelMigrationButton = ( {
20
+ isMigrationInProgress,
21
+ onClick,
22
+ buttonType,
23
+ ...buttonProps
24
+ } : CancelMigrationButtonProps ) => {
25
+ const translate = useTranslate ( ) ;
26
+
27
+ let cancelMigrationButtonText = translate ( 'Cancel migration' ) ;
28
+
29
+ if ( buttonType === 'cta' && isMigrationInProgress ) {
30
+ cancelMigrationButtonText = translate ( 'Request migration cancellation' ) ;
31
+ }
32
+
33
+ if ( buttonType === 'confirm' && isMigrationInProgress ) {
34
+ cancelMigrationButtonText = translate ( 'Send request' ) ;
35
+ }
36
+
37
+ return (
38
+ < Button
39
+ onClick = { ( e : React . MouseEvent < HTMLButtonElement > ) => {
40
+ e . preventDefault ( ) ;
41
+ onClick ( ) ;
42
+ } }
43
+ { ...buttonProps }
44
+ >
45
+ { cancelMigrationButtonText }
46
+ </ Button >
47
+ ) ;
48
+ } ;
49
+
50
+ const CancelMigrationModal = ( {
51
+ closeModal,
52
+ handleCancelMigration,
53
+ siteId,
54
+ isMigrationInProgress,
55
+ } : {
56
+ closeModal : ( ) => void ;
57
+ handleCancelMigration : ( siteId : number ) => void ;
58
+ siteId : number ;
59
+ isMigrationInProgress : boolean ;
60
+ } ) => {
61
+ const translate = useTranslate ( ) ;
62
+
63
+ const modalTitle = ! isMigrationInProgress
64
+ ? translate ( 'Cancel migration' )
65
+ : translate ( 'Request migration cancellation' ) ;
66
+
67
+ const modalContent = ! isMigrationInProgress
68
+ ? translate (
69
+ "If you cancel now, our Happiness Engineers will be notified that you've chosen not to move your site to WordPress.com, and your current site will remain exactly as it is."
70
+ )
71
+ : translate (
72
+ "Since your migration is already underway, you'll need to send us a cancellation request. If you cancel now, you'll lose all your progress."
73
+ ) ;
74
+
75
+ return (
76
+ < Modal
77
+ className = "migration-started-difm__cancel-dialog"
78
+ title = { modalTitle }
79
+ onRequestClose = { closeModal }
80
+ size = "medium"
81
+ >
82
+ < p > { modalContent } </ p >
83
+ < div className = "migration-started-difm__cancel-dialog-buttons" >
84
+ < Button key = "cancel" variant = "secondary" onClick = { ( ) => closeModal ( ) } >
85
+ { translate ( "Don't cancel migration" ) }
86
+ </ Button >
87
+ < CancelMigrationButton
88
+ key = "send"
89
+ variant = "primary"
90
+ isMigrationInProgress = { isMigrationInProgress }
91
+ onClick = { ( ) => handleCancelMigration ( siteId ) }
92
+ buttonType = "confirm"
93
+ />
94
+ </ div >
95
+ </ Modal >
96
+ ) ;
97
+ } ;
98
+
10
99
const CancelDifmMigrationForm = ( { siteId } : { siteId : number } ) => {
11
100
const translate = useTranslate ( ) ;
12
101
const dispatch = useDispatch ( ) ;
102
+ const { data : stickers = [ ] } = useBlogStickersQuery ( siteId ) ;
13
103
const [ isOpen , setOpen ] = useState ( false ) ;
14
104
const openModal = ( ) => setOpen ( true ) ;
15
105
const closeModal = ( ) => setOpen ( false ) ;
@@ -21,6 +111,8 @@ const CancelDifmMigrationForm = ( { siteId }: { siteId: number } ) => {
21
111
return null ;
22
112
}
23
113
114
+ const isMigrationInProgress = stickers . includes ( 'migration-in-progress' ) ;
115
+
24
116
const handleSendCancellationRequest = async ( siteId : number ) => {
25
117
try {
26
118
return await wp . req . post ( {
@@ -50,9 +142,13 @@ const CancelDifmMigrationForm = ( { siteId }: { siteId: number } ) => {
50
142
return ;
51
143
}
52
144
145
+ const cancellationSuccessMessage = isMigrationInProgress
146
+ ? translate ( 'Migration cancellation request sent.' )
147
+ : translate ( 'Migration cancelled.' ) ;
148
+
53
149
setCancellationStatus ( 'success' ) ;
54
150
dispatch (
55
- successNotice ( translate ( 'Migration cancellation request sent.' ) , {
151
+ successNotice ( cancellationSuccessMessage , {
56
152
duration : 5000 ,
57
153
} )
58
154
) ;
@@ -64,37 +160,20 @@ const CancelDifmMigrationForm = ( { siteId }: { siteId: number } ) => {
64
160
< LoadingBar className = "migration-started-difm__cancel-loading-bar" progress = { 0.5 } />
65
161
) }
66
162
{ cancellationStatus === null && (
67
- < Button
163
+ < CancelMigrationButton
164
+ isMigrationInProgress = { isMigrationInProgress }
165
+ onClick = { openModal }
166
+ buttonType = "cta"
68
167
variant = "link"
69
- onClick = { ( e : React . MouseEvent < HTMLButtonElement > ) => {
70
- e . preventDefault ( ) ;
71
- openModal ( ) ;
72
- } }
73
- >
74
- { translate ( 'Request cancellation' ) }
75
- </ Button >
168
+ />
76
169
) }
77
170
{ isOpen && (
78
- < Modal
79
- className = "migration-started-difm__cancel-dialog"
80
- title = { translate ( 'Request migration cancellation' ) }
81
- onRequestClose = { closeModal }
82
- size = "medium"
83
- >
84
- < p >
85
- { translate (
86
- "Since your migration is already underway, you'll need to send us a cancellation request. If you cancel now, you'll lose all your progress."
87
- ) }
88
- </ p >
89
- < div className = "migration-started-difm__cancel-dialog-buttons" >
90
- < Button key = "cancel" variant = "secondary" onClick = { ( ) => closeModal ( ) } >
91
- { translate ( "Don't cancel migration" ) }
92
- </ Button >
93
- < Button key = "send" variant = "primary" onClick = { ( ) => handleCancelMigration ( siteId ) } >
94
- { translate ( 'Send request' ) }
95
- </ Button >
96
- </ div >
97
- </ Modal >
171
+ < CancelMigrationModal
172
+ closeModal = { closeModal }
173
+ handleCancelMigration = { handleCancelMigration }
174
+ siteId = { siteId }
175
+ isMigrationInProgress = { isMigrationInProgress }
176
+ />
98
177
) }
99
178
</ div >
100
179
) ;
0 commit comments