reducer.js (14342B)
1 import {parseSectionData, parsePageData, parseCollectionData, getInstalledDependencies, NONE_KEY} from './helper'; 2 import {getDefaultDependencies} from './helper'; 3 import {loadChallengeStep, saveChallengeStep, setWithExpiry, getWithExpiry} from './helper'; 4 const EXIPRY_TIME = 5 * 24 * 3600 * 1000; 5 export const initialState = { 6 loading: false, 7 activeItemType: getWithExpiry('itemType', 'section'), 8 library: null, 9 columns: getWithExpiry('column', ''), 10 errorMessages: [], 11 section: { 12 categories: [], 13 data: {}, 14 priceFilter: getWithExpiry('section_price', ''), 15 activeCategory: getWithExpiry('section_category', ''), 16 dependencyFilters: {}, 17 dependencyFilterRule: getWithExpiry('section_filterRule', true), 18 searchContext: '', 19 wholePlugins: [], 20 thirdPartyPlugins: [], 21 sortBy: getWithExpiry('section_sort', 'name'), 22 currentPage: getWithExpiry('section_page', 0) 23 }, 24 page: { 25 categories: [], 26 data: {}, 27 priceFilter: getWithExpiry('page_price', ''), 28 activeCategory: getWithExpiry('page_category', ''), 29 dependencyFilters: {}, 30 dependencyFilterRule: getWithExpiry('page_filterRule', true), 31 searchContext: '', 32 wholePlugins: [], 33 thirdPartyPlugins: [], 34 sortBy: getWithExpiry('page_sort', 'name'), 35 currentPage: getWithExpiry('page_page', 0) 36 }, 37 collection: { 38 categories: [], 39 data: {}, 40 priceFilter: getWithExpiry('collection_price', ''), 41 activeCategory: getWithExpiry('collection_category', 'name'), 42 dependencyFilters: {}, 43 dependencyFilterRule: false, 44 searchContext: '', 45 wholePlugins: [], 46 thirdPartyPlugins: [], 47 activeCollection: null, 48 sortBy: getWithExpiry('collection_sort', 'name'), 49 currentPage: getWithExpiry('collection_page', 0) 50 }, 51 installedDependencies: false, // used when deciding should or not reload page after importing the template 52 isImportToAppend: true, // append to or replace the current page content for importing 53 tour: { 54 isOpen: false, 55 activeButtonGroup: null, 56 isPreviewVisible: false 57 }, 58 challenge: { 59 isOpen: false, 60 currentStep: loadChallengeStep(), 61 tooltipRect: {}, 62 finalStatus: '', 63 passed: getWithExpiry('reduxChallengePassed', false), 64 listExpanded: true 65 }, 66 plugins: {}, 67 importingTemplate: null, 68 activateDialog: false 69 }; 70 71 export const reducer = ( state = initialState, action ) => { 72 73 switch ( action.type ) { 74 case 'SET_LIBRARY': 75 if (!action.library.dependencies) return state; 76 redux_templates.supported_plugins = action.library.plugins; 77 const dependencies = getDefaultDependencies(Object.keys(action.library.dependencies)); 78 const parsedSection = parseSectionData(action.library.sections); 79 const parsedPage = parsePageData(action.library.pages); 80 const parsedCollection = parseCollectionData(action.library); 81 redux_templates.stats = { 82 'dependencies': Object.keys(action.library.dependencies).length, 83 'pages': Object.keys(action.library.pages).length, 84 'sections': Object.keys(action.library.sections).length, 85 'collections': Object.keys(action.library.collections).length, 86 } 87 return { 88 ...state, 89 loading: false, 90 library: action.library, 91 section: { 92 ...state.section, 93 ...parsedSection, 94 dependencyFilters: getWithExpiry('section_plugin') ? getWithExpiry('section_plugin') : dependencies 95 }, 96 page: { 97 ...state.page, 98 ...parsedPage, 99 dependencyFilters: getWithExpiry('page_plugin') ? getWithExpiry('page_plugin') : dependencies 100 }, 101 collection: { 102 ...state.collection, 103 ...parsedCollection, 104 dependencyFilters: getWithExpiry('collection_plugin') ? getWithExpiry('collection_plugin') : dependencies 105 } 106 }; 107 case 'SET_ACTIVE_CATEGORY': 108 setWithExpiry(state.activeItemType + '_category', action.activeCategory, EXIPRY_TIME); 109 setWithExpiry(state.activeItemType + '_page', 0, EXIPRY_TIME); 110 return { 111 ...state, 112 [state.activeItemType]: { 113 ...state[state.activeItemType], 114 currentPage: 0, 115 activeCategory: action.activeCategory 116 } 117 }; 118 case 'SET_SEARCH_CONTEXT': 119 setWithExpiry(state.activeItemType + '_search', action.searchContext, EXIPRY_TIME); 120 setWithExpiry(state.activeItemType + '_page', 0, EXIPRY_TIME); 121 return { 122 ...state, 123 [state.activeItemType]: { 124 ...state[state.activeItemType], 125 currentPage: 0, 126 searchContext: action.searchContext 127 } 128 }; 129 case 'SET_ACTIVE_PRICE_FILTER': 130 setWithExpiry(state.activeItemType + '_price', action.activePriceFilter, EXIPRY_TIME); 131 setWithExpiry(state.activeItemType + '_page', 0, EXIPRY_TIME); 132 return { 133 ...state, 134 [state.activeItemType]: { 135 ...state[state.activeItemType], 136 currentPage: 0, 137 priceFilter: action.activePriceFilter 138 } 139 }; 140 case 'SET_ACTIVE_ITEM_TYPE': 141 setWithExpiry('itemType', action.activeItemType, EXIPRY_TIME); 142 return { 143 ...state, 144 activeItemType: action.activeItemType 145 }; 146 case 'SET_DEPENDENCY_FILTERS': 147 setWithExpiry(state.activeItemType + '_plugin', action.dependencyFilters, EXIPRY_TIME); 148 setWithExpiry(state.activeItemType + '_page', 0, EXIPRY_TIME); 149 return { 150 ...state, 151 [state.activeItemType]: { 152 ...state[state.activeItemType], 153 currentPage: 0, 154 dependencyFilters: action.dependencyFilters 155 } 156 } 157 case 'SET_SORT_BY': 158 setWithExpiry(state.activeItemType + '_sort', action.sortBy, EXIPRY_TIME); 159 setWithExpiry(state.activeItemType + '_page', 0, EXIPRY_TIME); 160 return { 161 ...state, 162 [state.activeItemType]: { 163 ...state[state.activeItemType], 164 currentPage: 0, 165 sortBy: action.sortBy 166 } 167 }; 168 case 'SET_CURRENT_PAGE': 169 setWithExpiry(state.activeItemType + '_page', action.currentPage, EXIPRY_TIME); 170 return { 171 ...state, 172 [state.activeItemType]: { 173 ...state[state.activeItemType], 174 currentPage: action.currentPage 175 } 176 }; 177 case 'SET_ACTIVE_COLLECTION': 178 return { 179 ...state, 180 collection: { 181 ...state.collection, 182 activeCollection: action.activeCollection 183 } 184 }; 185 case 'SET_LOADING': 186 return { 187 ...state, 188 loading: action.loading 189 } 190 case 'SET_COLUMNS': 191 setWithExpiry('column', action.columns, EXIPRY_TIME); 192 return { 193 ...state, 194 columns: action.columns 195 } 196 case 'APPEND_ERROR_MESSAGE': 197 return { 198 ...state, 199 errorMessages: state.errorMessages.concat([action.errorMessage]) 200 } 201 case 'DISCARD_ALL_ERROR_MESSAGES': 202 return { 203 ...state, 204 errorMessages: [] 205 } 206 case 'SET_INSTALLED_DEPENDENCIES': 207 return { 208 ...state, 209 installedDependencies: action.installedDependencies 210 } 211 case 'SET_TOUR_OPEN': 212 return { 213 ...state, 214 tour: { 215 ...state.tour, 216 isOpen: action.isTourOpen 217 } 218 }; 219 case 'SET_TOUR_ACTIVE_BUTTON_GROUP': 220 return { 221 ...state, 222 tour: { 223 ...state.tour, 224 activeButtonGroup: action.data 225 } 226 }; 227 case 'SET_PREVIEW_VISIBLE': 228 return { 229 ...state, 230 tour: { 231 ...state.tour, 232 isPreviewVisible: action.isVisible 233 } 234 }; 235 case 'SET_IMPORTING_TEMPLATE': 236 return { 237 ...state, 238 importingTemplate: action.importingTemplate 239 } 240 case 'SET_CHALLENGE_STEP': 241 saveChallengeStep(action.data); 242 return { 243 ...state, 244 challenge: { 245 ...state.challenge, 246 currentStep: action.data 247 } 248 } 249 case 'SET_CHALLENGE_OPEN': 250 return { 251 ...state, 252 challenge: { 253 ...state.challenge, 254 isOpen: action.data 255 } 256 } 257 case 'SET_CHALLENGE_TOOLTIP_RECT': 258 return { 259 ...state, 260 challenge: { 261 ...state.challenge, 262 tooltipRect: action.data 263 } 264 } 265 case 'SET_CHALLENGE_FINAL_STATUS': 266 return { 267 ...state, 268 challenge: { 269 ...state.challenge, 270 finalStatus: action.data 271 } 272 } 273 case 'SET_CHALLENGE_PASSED': 274 setWithExpiry('reduxChallengePassed', action.data, EXIPRY_TIME); 275 return { 276 ...state, 277 challenge: { 278 ...state.challenge, 279 passed: action.data 280 } 281 } 282 case 'SET_CHALLENGE_LIST_EXPANDED': 283 return { 284 ...state, 285 challenge: { 286 ...state.challenge, 287 listExpanded: action.data 288 } 289 } 290 case 'SET_ACTIVATE_DIALOG_DISPLAY': 291 return { 292 ...state, 293 activateDialog: action.data 294 } 295 case 'SET_IMPORT_TO_APPEND': 296 return { 297 ...state, 298 isImportToAppend: action.data 299 } 300 case 'SET_DEPENDENCY_FILTER_RULE': 301 setWithExpiry(state.activeItemType + '_filterRule', action.data, EXIPRY_TIME); 302 return { 303 ...state, 304 [state.activeItemType]: { 305 ...state[state.activeItemType], 306 dependencyFilterRule: action.data 307 } 308 } 309 // Dependency Shortcut click handler: All, None, Installed and Reset 310 case 'SELECT_DEPENDENCIES': 311 const types = ['section', 'page', 'collection']; 312 let atomHandler; 313 switch(action.data) { 314 case 'all': 315 case 'none': 316 const newValue = action.data === 'all'; 317 atomHandler = (plugins) => plugins 318 .filter(plugin => [ NONE_KEY, 'gutenberghub.com', 'shareablock.com' ].includes(plugin) === false ) 319 .reduce( 320 (acc, key) => { 321 return { ...acc, [key]: { value: newValue, disabled: false } } 322 }, 323 { 324 [NONE_KEY]: {value: true, disabled: false}, 325 'gutenberghub.com': {value: true, disabled: false}, 326 'shareablock.com': {value: true, disabled: false} 327 } 328 ) 329 break; 330 case 'installed': 331 atomHandler = (plugins) => getInstalledDependencies(plugins); 332 break; 333 default: 334 atomHandler = (plugins) => getDefaultDependencies(plugins); 335 break; 336 } 337 const filtered = types.reduce( (acc, cur) => { 338 // save to the local storage as well 339 setWithExpiry(cur + '_plugin', {...state[cur].dependencyFilters, ...atomHandler(state[cur].wholePlugins)}, EXIPRY_TIME); 340 return { 341 ...acc, 342 [cur]: { 343 ...state[cur], 344 searchContext: '', 345 dependencyFilterRule: cur !== 'collection', // We must always use false for collection to get template kits to work. 346 dependencyFilters: {...state[cur].dependencyFilters, ...atomHandler(state[cur].wholePlugins)} 347 } 348 } 349 }, {}); 350 return { 351 ...state, 352 ...filtered 353 }; 354 case 'CLEAR_SEARCH': 355 return { 356 ...state, 357 section: { 358 ...state.section, 359 searchContext: '' 360 }, 361 page: { 362 ...state.page, 363 searchContext: '' 364 }, 365 collection: { 366 ...state.collection, 367 searchContext: '' 368 } 369 } 370 case 'CLEAR_STATE': 371 return { 372 ...state, 373 section: { 374 ...state.section, 375 priceFilter: '', 376 activeCategory: '', 377 searchContext: '', 378 }, 379 page: { 380 ...state.page, 381 priceFilter: '', 382 activeCategory: '', 383 searchContext: '', 384 }, 385 collection: { 386 ...state.collection, 387 priceFilter: '', 388 activeCategory: '', 389 searchContext: '', 390 } 391 } 392 } 393 394 return state; 395 };