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 / blocks / query-loop / hooks / useQueryReducer.js
generateblocks / src / blocks / query-loop / hooks Last commit date
useQueryReducer.js 4 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