index.js (9383B)
1 const {apiFetch} = wp; 2 const {registerStore} = wp.data; 3 4 import {initialState, reducer} from './reducer'; 5 import {actions} from './actions'; 6 import cloneDeep from 'lodash/cloneDeep'; 7 import sortBy from 'lodash/sortBy'; 8 import countBy from 'lodash/countBy'; 9 import map from 'lodash/map'; 10 import flattenDeep from 'lodash/flattenDeep'; 11 import uniq from 'lodash/uniq'; 12 import uniqBy from 'lodash/uniqBy'; 13 import {applyCategoryFilter, applySearchFilter, applyHashFilter, applyPriceFilter, applyDependencyFilters, valueOfDependencyFilter, flattenPageData} from './filters' 14 import {getCurrentState, getCollectionChildrenData, loadChallengeStep, NONE_KEY} from './helper'; 15 import {isTemplatePremium} from './dependencyHelper' 16 import {installedBlocksTypes} from './actionHelper'; 17 18 const getOriginalPageData = (state) => { 19 if (state.activeItemType === 'collection' && state.collection.activeCollection !== null) 20 return getCollectionChildrenData(state.library, state.collection.activeCollection); 21 return getCurrentState(state).data; 22 }; 23 24 const getActivePriceFilter = (state) => { 25 return getCurrentState(state).priceFilter; 26 }; 27 const getSearchContext = (state) => { 28 return (state.activeItemType !== 'saved') ? getCurrentState(state).searchContext : null; 29 }; 30 31 const getActiveCategory = (state) => { 32 return state[state.activeItemType].activeCategory; 33 }; 34 35 const getCurrentPage = (state) => { 36 return state[state.activeItemType].currentPage; 37 }; 38 const getActiveItemType = (state) => { 39 return state.activeItemType; 40 }; 41 42 // get relevant page data, apply category, price, search, dependent filters 43 const getPageData = (state, applyDependencyFilter = true) => { 44 let pageData = getOriginalPageData(state); 45 const searchKeyword = getSearchContext(state); 46 let hashFilteredData = []; 47 // Hash filter to take priority 48 if (state.activeItemType !== 'collection' && searchKeyword.length > 5) hashFilteredData = applyHashFilter(pageData, searchKeyword); 49 // Full search for pageData 50 if (pageData && Object.keys(pageData).length > 0) { 51 pageData = applySearchFilter(pageData, searchKeyword); 52 if (applyDependencyFilter) pageData = applyDependencyFilters(pageData, getDependencyFilters(state), getDependencyFilterRule(state)); 53 54 pageData = applyPriceFilter(pageData, getActivePriceFilter(state), getDependencyFilters(state)); 55 if (state.collection.activeCollection === null || state.activeItemType !== 'collection') { 56 pageData = applyCategoryFilter(pageData, getActiveCategory(state)); 57 pageData = sortBy(pageData, getCurrentState(state).sortBy); 58 } 59 return uniqBy([...pageData, ...hashFilteredData], 'ID'); 60 } 61 return null; 62 }; 63 64 const getDependencyFilters = (state) => { 65 return {...getAllDependencFilters(state), ...getCurrentState(state).dependencyFilters}; 66 }; 67 68 const getAllDependencFilters = (state) => { 69 const activeState = state[state.activeItemType || 'section']; 70 return [...activeState.wholePlugins, ...activeState.thirdPartyPlugins].reduce((acc, cur) => { 71 return {...acc, [cur]: {value: false} }; 72 }, undefined) 73 }; 74 75 76 const getDependencyFiltersStatistics = (state) => { 77 const pageData = flattenPageData(getOriginalPageData(state)); 78 const dependentPluginsArray = uniq(flattenDeep(map(pageData, 'dependencies'))); 79 let dependencyFilters = getDependencyFilters(state); 80 Object.keys(dependencyFilters) 81 .forEach((plugin) => { 82 dependencyFilters[plugin] = {...dependencyFilters[plugin], disabled: dependentPluginsArray.indexOf(plugin) === -1} 83 }) 84 dependencyFilters[NONE_KEY] = {value: valueOfDependencyFilter(dependencyFilters[NONE_KEY]), disabled: false}; 85 return dependencyFilters; 86 }; 87 const getDependencyFilterRule = (state) => { 88 return state[state.activeItemType].dependencyFilterRule; 89 }; 90 registerStore('redux-templates/sectionslist', { 91 92 reducer, 93 actions, 94 95 selectors: { 96 fetchLibraryFromAPI(state) { 97 return state.library; 98 }, 99 receive(state) { 100 return state.sections; 101 }, 102 103 getActivePriceFilter, 104 getSearchContext, 105 getDependencyFilters, 106 getDependencyFiltersStatistics, 107 getActiveItemType, 108 getCurrentPage, 109 getActiveCategory, 110 getDependencyFilterRule, 111 getWholePlugins(state) { 112 return (state.activeItemType !== 'saved') ? getCurrentState(state).wholePlugins : null; 113 }, 114 getThirdPartyPlugins(state) { 115 return (state.activeItemType !== 'saved') ? getCurrentState(state).thirdPartyPlugins : null; 116 }, 117 // get categories from currentState, sortBy alphabetically, with the count of pageData within the current category 118 getCategoryData(state) { 119 let categories = []; 120 let pageData = getOriginalPageData(state); 121 if (pageData && Object.keys(pageData).length > 0) { 122 pageData = applySearchFilter(pageData, getSearchContext(state)); 123 pageData = applyDependencyFilters(pageData, getDependencyFilters(state), getDependencyFilterRule(state)); 124 pageData = applyPriceFilter(pageData, getActivePriceFilter(state), getDependencyFilters(state)); 125 } 126 if (state.collection.activeCollection === null || state.activeItemType !== 'collection') { 127 categories = cloneDeep(getCurrentState(state).categories); 128 categories = categories.map(category => { 129 const filteredData = map(pageData[category.slug], 'id'); 130 return {...category, filteredData}; 131 }); 132 } 133 134 categories = sortBy(categories, 'name'); 135 return categories; 136 }, 137 // get relevant page data, apply category, price, search, dependent filters 138 getPageData, 139 140 getStatistics(state) { 141 let pageData = getOriginalPageData(state); 142 let staticsData = {true: 0, false: 0}; 143 if (pageData && Object.keys(pageData).length > 0) { 144 pageData = applySearchFilter(pageData, getSearchContext(state)); 145 pageData = applyDependencyFilters(pageData, getDependencyFilters(state), getDependencyFilterRule(state)); 146 if (state.collection.activeCollection === null || state.activeItemType !== 'collection') pageData = applyCategoryFilter(pageData, getActiveCategory(state)); 147 staticsData = countBy(pageData, (item) => isTemplatePremium(item, getDependencyFilters(state)) === true); 148 } 149 return staticsData; 150 }, 151 getLoading(state) { 152 return state.loading; 153 }, 154 getColumns(state) { 155 return state.columns; 156 }, 157 getSortBy(state) { 158 return getCurrentState(state).sortBy; 159 }, 160 getActiveCollection(state) { 161 return state.collection.activeCollection; 162 }, 163 getActiveCollectionData(state) { 164 if (state.library && state.library.collections && state.collection) 165 return state.library.collections[state.collection.activeCollection]; 166 return null; 167 }, 168 getSaved(state) { 169 return state.saved; 170 }, 171 getErrorMessages(state) { 172 return state.errorMessages; 173 }, 174 getInstalledDependencies(state) { 175 return state.installedDependencies; 176 }, 177 getTourOpen(state) { 178 return state.tour.isOpen; 179 }, 180 getTourActiveButtonGroup(state) { 181 return state.tour.activeButtonGroup; 182 }, 183 getTourPreviewVisible(state) { 184 return state.tour.isPreviewVisible; 185 }, 186 getImportingTemplate(state) { 187 return state.importingTemplate; 188 }, 189 getChallengeStep(state) { 190 return loadChallengeStep(); 191 }, 192 getChallengeOpen(state) { 193 return state.challenge.isOpen; 194 }, 195 getChallengeTooltipRect(state) { 196 return state.challenge.tooltipRect; 197 }, 198 getChallengeFinalStatus(state) { 199 return state.challenge.finalStatus; 200 }, 201 getChallengePassed(state) { 202 return state.challenge.passed; 203 }, 204 getChallengeListExpanded(state) { 205 return state.challenge.listExpanded; 206 }, 207 getActivateDialogDisplay(state) { 208 return state.activateDialog; 209 }, 210 getImportToAppend(state) { 211 return state.isImportToAppend; 212 } 213 }, 214 215 controls: { 216 FETCH_LIBRARY_FROM_API(action) { 217 return apiFetch({path: action.path, method: 'POST', data: {registered_blocks: installedBlocksTypes()}}); 218 }, 219 FETCH_SAVED_FROM_API(action) { 220 return apiFetch({path: action.path, method: 'POST', data: {registered_blocks: installedBlocksTypes()}}); 221 } 222 }, 223 224 resolvers: { 225 * fetchLibraryFromAPI(state) { 226 try { 227 const receiveSectionResult = yield actions.fetchLibraryFromAPI('redux/v1/templates/library'); 228 return actions.setLibrary(receiveSectionResult.data); 229 } catch (error) { 230 return actions.appendErrorMessage(error.code + ' ' + error.message) 231 } 232 } 233 }, 234 235 initialState 236 });