From fa992a71104c6676d347fa79b2fd6a6b8dad7ac1 Mon Sep 17 00:00:00 2001 From: Kelly Dwan Date: Wed, 17 Jun 2020 19:54:22 -0400 Subject: [PATCH] Set the endpoint for the block by using the self link property --- packages/block-directory/src/store/actions.js | 8 ++--- .../block-directory/src/store/test/actions.js | 29 ++++++++++++------- 2 files changed, 22 insertions(+), 15 deletions(-) diff --git a/packages/block-directory/src/store/actions.js b/packages/block-directory/src/store/actions.js index bc09c9d1baa84d..a2da6b8d9794e7 100644 --- a/packages/block-directory/src/store/actions.js +++ b/packages/block-directory/src/store/actions.js @@ -74,7 +74,8 @@ export function* installBlockType( block ) { }, method: 'POST', } ); - yield addInstalledBlockType( { ...block, plugin: response.plugin } ); + const endpoint = response?._links?.self[ 0 ]?.href; + yield addInstalledBlockType( { ...block, endpoint } ); yield loadAssets( assets ); const registeredBlocks = yield select( 'core/blocks', 'getBlockTypes' ); @@ -95,17 +96,16 @@ export function* installBlockType( block ) { * @param {Object} block The blockType object. */ export function* uninstallBlockType( block ) { - const plugin = block.plugin.replace( '.php', '' ); try { yield apiFetch( { - path: `__experimental/plugins/${ plugin }`, + url: block.endpoint, data: { status: 'inactive', }, method: 'PUT', } ); yield apiFetch( { - path: `__experimental/plugins/${ plugin }`, + url: block.endpoint, method: 'DELETE', } ); yield removeInstalledBlockType( block ); diff --git a/packages/block-directory/src/store/test/actions.js b/packages/block-directory/src/store/test/actions.js index c40aa876f07cb0..a301a5ab283088 100644 --- a/packages/block-directory/src/store/test/actions.js +++ b/packages/block-directory/src/store/test/actions.js @@ -4,17 +4,24 @@ import { installBlockType, uninstallBlockType } from '../actions'; describe( 'actions', () => { + const endpoint = '/wp-json/__experimental/plugins/block/block'; const item = { id: 'block/block', name: 'Test Block', assets: [ 'script.js' ], }; - const plugin = { plugin: 'block/block.php', status: 'active', name: 'Test Block', version: '1.0.0', + _links: { + self: [ + { + href: endpoint, + }, + ], + }, }; describe( 'installBlockType', () => { @@ -41,10 +48,10 @@ describe( 'actions', () => { }, } ); - const itemWithPlugin = { ...item, plugin: plugin.plugin }; + const itemWithEndpoint = { ...item, endpoint }; expect( generator.next( plugin ).value ).toEqual( { type: 'ADD_INSTALLED_BLOCK_TYPE', - item: itemWithPlugin, + item: itemWithEndpoint, } ); expect( generator.next().value ).toEqual( { @@ -144,16 +151,16 @@ describe( 'actions', () => { } ); describe( 'uninstallBlockType', () => { - const itemWithPlugin = { ...item, plugin: plugin.plugin }; + const itemWithEndpoint = { ...item, endpoint }; it( 'should uninstall a block successfully', () => { - const generator = uninstallBlockType( itemWithPlugin ); + const generator = uninstallBlockType( itemWithEndpoint ); // First the deactivation step expect( generator.next().value ).toMatchObject( { type: 'API_FETCH', request: { - path: '__experimental/plugins/block/block', + url: endpoint, method: 'PUT', }, } ); @@ -162,14 +169,14 @@ describe( 'actions', () => { expect( generator.next().value ).toMatchObject( { type: 'API_FETCH', request: { - path: '__experimental/plugins/block/block', + url: endpoint, method: 'DELETE', }, } ); expect( generator.next().value ).toEqual( { type: 'REMOVE_INSTALLED_BLOCK_TYPE', - item: itemWithPlugin, + item: itemWithEndpoint, } ); expect( generator.next() ).toEqual( { @@ -179,12 +186,12 @@ describe( 'actions', () => { } ); it( "should set a global notice if the plugin can't be deleted", () => { - const generator = uninstallBlockType( itemWithPlugin ); + const generator = uninstallBlockType( itemWithEndpoint ); expect( generator.next().value ).toMatchObject( { type: 'API_FETCH', request: { - path: '__experimental/plugins/block/block', + url: endpoint, method: 'PUT', }, } ); @@ -192,7 +199,7 @@ describe( 'actions', () => { expect( generator.next().value ).toMatchObject( { type: 'API_FETCH', request: { - path: '__experimental/plugins/block/block', + url: endpoint, method: 'DELETE', }, } );