migrate-blocks.js
4.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
// @ts-check
/* global blockLabMigration */
/**
* External dependencies
*/
import * as React from 'react';
/**
* WordPress dependencies
*/
import { speak } from '@wordpress/a11y';
import apiFetch from '@wordpress/api-fetch';
import { Spinner } from '@wordpress/components';
import { useState } from '@wordpress/element';
import { __ } from '@wordpress/i18n';
/**
* Internal dependencies
*/
import { Step, StepContent, StepFooter, StepIcon } from '../';
/**
* @typedef {Object} MigrateBlocksProps The component props.
* @property {Function} goToNext Goes to the next step.
* @property {boolean} isStepActive Whether this step is active.
* @property {boolean} isStepComplete Whether this step is complete.
* @property {number} stepIndex The step index of this step.
*/
/**
* The step that migrates the blocks.
*
* @param {MigrateBlocksProps} Props The component props.
* @return {React.ReactElement} The component to prompt to migrate the post content.
*/
const MigrateBlocks = ( { isStepActive, isStepComplete, stepIndex } ) => {
const [ currentBlockMigrationStep, setCurrentBlockMigrationStep ] = useState( 0 );
const [ isInProgress, setIsInProgress ] = useState( false );
const [ isError, setIsError ] = useState( false );
const [ errorMessage, setErrorMessage ] = useState( '' );
const [ isSuccess, setIsSuccess ] = useState( false );
const migrationLabels = [
__( 'Migrating your blocks…', 'block-lab' ),
__( 'Migrating your post content…', 'block-lab' ),
];
/**
* Migrates the custom post type, then chains post content migration to the callback.
*/
const migrateCpt = async () => {
await apiFetch( {
path: '/block-lab/migrate-post-type',
method: 'POST',
} ).then( async () => {
setCurrentBlockMigrationStep( 1 );
await migratePostContent();
} ).catch( ( result ) => {
if ( result.hasOwnProperty( 'message' ) ) {
setErrorMessage( result.message );
}
speak( __( 'The migration failed in the CPT migration', 'block-lab' ) );
setIsError( true );
setIsInProgress( false );
} );
};
/**
* Migrates the post content.
*/
const migratePostContent = async () => {
// Used for a 504 Gateway Timeout Error, but could also be for other errors.
const timeoutErrorCode = 'invalid_json';
await apiFetch( {
path: '/block-lab/migrate-post-content',
method: 'POST',
} ).then( () => {
speak( __( 'The migration was successful!', 'block-lab' ) );
setIsSuccess( true );
} ).catch( async ( result ) => {
if ( result.hasOwnProperty( 'code' ) && timeoutErrorCode === result.code ) {
await migratePostContent();
return;
} else if ( result.hasOwnProperty( 'message' ) ) {
setErrorMessage( result.message );
}
speak( __( 'The migration failed in the post content migration', 'block-lab' ) );
setIsError( true );
} );
};
/**
* Handles all of the migration for this step.
*/
const migrate = async () => {
speak( __( 'The migration is now in progress', 'block-lab' ) );
setErrorMessage( '' );
setIsInProgress( true );
// The post content migration is chained to the callback in then().
await migrateCpt();
setIsInProgress( false );
};
return (
<Step isActive={ isStepActive } isComplete={ isStepComplete }>
<StepIcon
index={ stepIndex }
isComplete={ isStepComplete }
/>
<StepContent
heading={ __( 'Migrate Your Blocks', 'block-lab' ) }
isStepActive={ isStepActive }
>
{ ! isSuccess && <p>{ __( "Okay! Everything is ready. Let's do this. While the migration is underway, don't leave this page.", 'block-lab' ) }</p> }
{ !! errorMessage && (
<div className="bl-migration__error">
<p>{ __( 'The following error ocurred:', 'block-lab' ) }</p>
<p>{ errorMessage }</p>
</div>
) }
{ isInProgress && (
<>
<Spinner />
<p>{ migrationLabels[ currentBlockMigrationStep ] }</p>
</>
) }
{ ! isInProgress && ! isSuccess && (
<button
className="btn"
onClick={ migrate }
>
{ isError ? __( 'Try Again', 'block-lab' ) : __( 'Migrate Now', 'block-lab' ) }
</button>
) }
{ isSuccess && (
<>
<p>
<span role="img" aria-label={ __( 'party emoji', 'block-lab' ) }>🎉</span>
{ __( 'The migration completed successfully! Time to say goodbye to Block Lab (it’s been fun!) and step into the FUTURE', 'block-lab' ) }
<span className="message-future">{ __( 'FUTURE', 'block-lab' ) }</span>
<sub>{ __( 'FUTURE', 'block-lab' ) }</sub>.
</p>
<StepFooter>
{ /* @ts-ignore */ }
<a href={ blockLabMigration.gcbUrl } className="btn">
{ __( 'Go To Genesis Custom Blocks', 'block-lab' ) }
</a>
</StepFooter>
</>
) }
</StepContent>
</Step>
);
};
export default MigrateBlocks;