| 1 |
/** |
| 2 |
* Internal dependencies. |
| 3 |
*/ |
| 4 |
import { matchType } from './config'; |
| 5 |
import { clearFacetsFromArgs } from './functions'; |
| 6 |
|
| 7 |
/** |
| 8 |
* Initial state. |
| 9 |
*/ |
| 10 |
export const initialState = { |
| 11 |
aggregations: {}, |
| 12 |
args: { |
| 13 |
highlight: '', |
| 14 |
offset: 0, |
| 15 |
orderby: 'relevance', |
| 16 |
order: 'desc', |
| 17 |
per_page: 6, |
| 18 |
relation: matchType === 'all' ? 'and' : 'or', |
| 19 |
search: '', |
| 20 |
}, |
| 21 |
isLoading: false, |
| 22 |
isOpen: false, |
| 23 |
isSidebarOpen: false, |
| 24 |
isPoppingState: false, |
| 25 |
searchResults: [], |
| 26 |
searchedTerm: '', |
| 27 |
totalResults: 0, |
| 28 |
}; |
| 29 |
|
| 30 |
/** |
| 31 |
* Reducer function for handling state changes. |
| 32 |
* |
| 33 |
* @param {object} state The current state. |
| 34 |
* @param {object} action Action data. |
| 35 |
* @param {string} action.type The action name. |
| 36 |
* @param {object} action.payload New state data from the action. |
| 37 |
* @returns {object} Updated state. |
| 38 |
*/ |
| 39 |
export const reducer = (state, { type, payload }) => { |
| 40 |
const newState = { ...state, isPoppingState: false }; |
| 41 |
|
| 42 |
switch (type) { |
| 43 |
case 'APPLY_ARGS': { |
| 44 |
newState.args = { ...newState.args, ...payload, offset: 0 }; |
| 45 |
newState.isOpen = true; |
| 46 |
break; |
| 47 |
} |
| 48 |
case 'CLEAR_FACETS': { |
| 49 |
newState.args = clearFacetsFromArgs(newState.args); |
| 50 |
break; |
| 51 |
} |
| 52 |
case 'NEW_SEARCH_TERM': { |
| 53 |
newState.args = clearFacetsFromArgs(newState.args); |
| 54 |
newState.args.offset = 0; |
| 55 |
newState.args.search = payload; |
| 56 |
|
| 57 |
break; |
| 58 |
} |
| 59 |
case 'NEW_SEARCH_RESULTS': { |
| 60 |
const { |
| 61 |
hits: { hits, total }, |
| 62 |
aggregations, |
| 63 |
} = payload; |
| 64 |
|
| 65 |
/** |
| 66 |
* Total number of items. |
| 67 |
*/ |
| 68 |
const totalNumber = typeof total === 'number' ? total : total.value; |
| 69 |
|
| 70 |
newState.aggregations = aggregations; |
| 71 |
newState.searchResults = hits; |
| 72 |
newState.searchedTerm = newState.args.search; |
| 73 |
newState.totalResults = totalNumber; |
| 74 |
|
| 75 |
break; |
| 76 |
} |
| 77 |
case 'NEXT_PAGE': { |
| 78 |
newState.args.offset += newState.args.per_page; |
| 79 |
break; |
| 80 |
} |
| 81 |
case 'PREVIOUS_PAGE': { |
| 82 |
newState.args.offset = Math.max(newState.args.offset - newState.args.per_page, 0); |
| 83 |
break; |
| 84 |
} |
| 85 |
case 'START_LOADING': { |
| 86 |
newState.isLoading = true; |
| 87 |
break; |
| 88 |
} |
| 89 |
case 'FINISH_LOADING': { |
| 90 |
newState.isLoading = false; |
| 91 |
break; |
| 92 |
} |
| 93 |
case 'TOGGLE_SIDEBAR': { |
| 94 |
newState.isSidebarOpen = !state.isSidebarOpen; |
| 95 |
break; |
| 96 |
} |
| 97 |
case 'CLOSE_MODAL': { |
| 98 |
newState.args = clearFacetsFromArgs(newState.args); |
| 99 |
newState.isOpen = false; |
| 100 |
break; |
| 101 |
} |
| 102 |
case 'POP_STATE': { |
| 103 |
const { isOpen, ...args } = payload; |
| 104 |
|
| 105 |
newState.args = args; |
| 106 |
newState.isOpen = isOpen; |
| 107 |
newState.isPoppingState = true; |
| 108 |
|
| 109 |
break; |
| 110 |
} |
| 111 |
default: |
| 112 |
break; |
| 113 |
} |
| 114 |
|
| 115 |
return newState; |
| 116 |
}; |
| 117 |
|