PluginProbe ʕ •ᴥ•ʔ
GenerateBlocks / 2.0.0
GenerateBlocks v2.0.0
trunk 1.0 1.0.1 1.0.2 1.1.0 1.1.1 1.1.2 1.2.0 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 1.3.5 1.4.0 1.4.1 1.4.2 1.4.3 1.4.4 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.6.0 1.7.0 1.7.1 1.7.2 1.7.3 1.8.0 1.8.1 1.8.2 1.8.3 1.9.0 1.9.1 2.0.0 2.0.1 2.0.2 2.1.0 2.1.1 2.1.2 2.2.0 2.2.1 2.3.0
generateblocks / src / hooks / useQueryReducer.js
generateblocks / src / hooks Last commit date
index.js 1 year ago useAuthors.js 3 years ago useBlockStyles.js 1 year ago useDebounceState.js 4 years ago useDeviceAttributes.js 2 years ago useDeviceType.js 1 year ago useInnerBlocksCount.js 4 years ago useQueryReducer.js 1 year ago useRecordsReducer.js 3 years ago useSelectedBlockElements.js 2 years ago useStyleIndicator.js 2 years ago useTaxonomies.js 1 year ago useTaxonomyRecords.js 3 years ago
useQueryReducer.js
30 lines
1 import { useReducer } from '@wordpress/element';
2
3 const ADD_QUERY_PARAMETER = 'add_query_parameter';
4 const REMOVE_QUERY_PARAMETER = 'remove_query_parameter';
5
6 const queryReducer = function( queryState, action ) {
7 switch ( action.type ) {
8 case ADD_QUERY_PARAMETER:
9 return Object.assign( {}, queryState, action.payload );
10
11 case REMOVE_QUERY_PARAMETER:
12 const { [ action.payload ]: removedKey, ...newQueryState } = queryState; // eslint-disable-line no-unused-vars
13 return newQueryState;
14
15 default:
16 throw new Error( `queryReducer does not support action type "${ action.type }".` );
17 }
18 };
19
20 export default ( initialQueryState = {} ) => {
21 const [ state, dispatch ] = useReducer( queryReducer, initialQueryState );
22
23 return {
24 queryState: state,
25 setParameter: ( key, value ) => ( dispatch( { type: ADD_QUERY_PARAMETER, payload: { [ key ]: value } } ) ),
26 insertParameters: ( payload ) => ( dispatch( { type: ADD_QUERY_PARAMETER, payload } ) ),
27 removeParameter: ( payload ) => ( dispatch( { type: REMOVE_QUERY_PARAMETER, payload } ) ),
28 };
29 };
30