Skip to content

Commit

Permalink
Migrate any stored data
Browse files Browse the repository at this point in the history
  • Loading branch information
talldan committed May 13, 2022
1 parent 62eb2ed commit 7f7b5a4
Show file tree
Hide file tree
Showing 5 changed files with 147 additions and 2 deletions.
5 changes: 3 additions & 2 deletions packages/preferences-persistence/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
*/
import create from './create';
import convertLegacyLocalStorageData from './migrations/legacy-local-storage-data';
import convertPreferencesPackageData from './migrations/preferences-package-data';

export { create };

Expand Down Expand Up @@ -34,9 +35,9 @@ export function __unstableCreatePersistenceLayer( serverData, userId ) {

let preloadedData;
if ( serverData && serverModified >= localModified ) {
preloadedData = serverData;
preloadedData = convertPreferencesPackageData( serverData );
} else if ( localData ) {
preloadedData = localData;
preloadedData = convertPreferencesPackageData( localData );
} else {
// Check if there is data in the legacy format from the old persistence system.
preloadedData = convertLegacyLocalStorageData( userId );
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
export default function convertComplementaryAreas( state ) {
return Object.keys( state ).reduce( ( stateAccumulator, scope ) => {
const scopeData = state[ scope ];

// If a complementary area is truthy, convert it to the `isComplementaryAreaVisible` boolean.
if ( scopeData?.complementaryArea ) {
const updatedScopeData = { ...scopeData };
delete updatedScopeData.complementaryArea;
updatedScopeData.isComplementaryAreaVisible = true;
stateAccumulator[ scope ] = updatedScopeData;
return stateAccumulator;
}

return stateAccumulator;
}, state );
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/**
* Internal dependencies
*/
import convertComplementaryAreas from './convert-complementary-areas';

export default function convertPreferencesPackageData( data ) {
return convertComplementaryAreas( data );
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/**
* Internal dependencies
*/
import convertComplementaryAreas from '../convert-complementary-areas';

describe( 'convertComplementaryAreas', () => {
it( 'converts the `complementaryArea` property in each scope to an `isComplementaryAreaVisible` boolean', () => {
const input = {
'core/customize-widgets': {
complementaryArea: 'edit-post/block',
},
'core/edit-site': {
complementaryArea: 'edit-site/template',
},
'core/edit-post': {
complementaryArea: 'edit-post/block',
},
'core/edit-widgets': {},
};

const expectedOutput = {
'core/customize-widgets': {
isComplementaryAreaVisible: true,
},
'core/edit-site': {
isComplementaryAreaVisible: true,
},
'core/edit-post': {
isComplementaryAreaVisible: true,
},
'core/edit-widgets': {},
};

expect( convertComplementaryAreas( input ) ).toEqual( expectedOutput );
} );
} );
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/**
* Internal dependencies
*/
import convertPreferencesPackageData from '../';

const input = {
'core/customize-widgets': {
welcomeGuide: false,
fixedToolbar: true,
},
'core/edit-widgets': {
welcomeGuide: false,
fixedToolbar: true,
showBlockBreadcrumbs: false,
complementaryArea: 'edit-widgets/block-areas',
},
'core/edit-post': {
welcomeGuide: false,
fixedToolbar: true,
fullscreenMode: false,
hiddenBlockTypes: [ 'core/audio', 'core/cover' ],
editorMode: 'visual',
preferredStyleVariations: {
'core/quote': 'large',
},
inactivePanels: [],
openPanels: [ 'post-status' ],
pinnedItems: {
'my-sidebar-plugin/title-sidebar': false,
},
},
'core/edit-site': {
welcomeGuide: false,
welcomeGuideStyles: false,
fixedToolbar: true,
complementaryArea: 'edit-site/global-styles',
},
};

describe( 'convertPreferencesPackageData', () => {
it( 'converts data to the expected format', () => {
expect( convertPreferencesPackageData( input ) )
.toMatchInlineSnapshot( `
Object {
"core/customize-widgets": Object {
"fixedToolbar": true,
"welcomeGuide": false,
},
"core/edit-post": Object {
"editorMode": "visual",
"fixedToolbar": true,
"fullscreenMode": false,
"hiddenBlockTypes": Array [
"core/audio",
"core/cover",
],
"inactivePanels": Array [],
"openPanels": Array [
"post-status",
],
"pinnedItems": Object {
"my-sidebar-plugin/title-sidebar": false,
},
"preferredStyleVariations": Object {
"core/quote": "large",
},
"welcomeGuide": false,
},
"core/edit-site": Object {
"fixedToolbar": true,
"isComplementaryAreaVisible": true,
"welcomeGuide": false,
"welcomeGuideStyles": false,
},
"core/edit-widgets": Object {
"fixedToolbar": true,
"isComplementaryAreaVisible": true,
"showBlockBreadcrumbs": false,
"welcomeGuide": false,
},
}
` );
} );
} );

0 comments on commit 7f7b5a4

Please sign in to comment.