-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Expand file tree
/
Copy pathresolvers.js
More file actions
35 lines (30 loc) · 794 Bytes
/
resolvers.js
File metadata and controls
35 lines (30 loc) · 794 Bytes
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
/**
* External dependencies
*/
import { camelCase } from 'change-case';
import { mapKeys } from 'lodash';
/**
* WordPress dependencies
*/
import apiFetch from '@wordpress/api-fetch';
/**
* Internal dependencies
*/
import { fetchDownloadableBlocks, receiveDownloadableBlocks } from './actions';
export const getDownloadableBlocks =
( filterValue ) =>
async ( { dispatch } ) => {
if ( ! filterValue ) {
return;
}
try {
dispatch( fetchDownloadableBlocks( filterValue ) );
const results = await apiFetch( {
path: `wp/v2/block-directory/search?term=${ filterValue }`,
} );
const blocks = results.map( ( result ) =>
mapKeys( result, ( value, key ) => camelCase( key ) )
);
dispatch( receiveDownloadableBlocks( blocks, filterValue ) );
} catch {}
};