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