index.js
168 lines
| 1 | import { registerStore } from '@wordpress/data'; |
| 2 | |
| 3 | const DEFAULT_STATE = { |
| 4 | isFetching: true, |
| 5 | isPreview: false, |
| 6 | tab: 'library', |
| 7 | templates: [], |
| 8 | patterns: [], |
| 9 | library: { |
| 10 | items: [], |
| 11 | currentPage: 0, |
| 12 | totalPages: 0, |
| 13 | }, |
| 14 | preview: { |
| 15 | type: 'templates', |
| 16 | item: {}, |
| 17 | }, |
| 18 | }; |
| 19 | |
| 20 | registerStore( 'tpc/block-editor', { |
| 21 | reducer( state = DEFAULT_STATE, action ) { |
| 22 | if ( 'SET_FETCHING' === action.type ) { |
| 23 | return { |
| 24 | ...state, |
| 25 | isFetching: action.isFetching, |
| 26 | }; |
| 27 | } |
| 28 | |
| 29 | if ( 'TOGGLE_PREVIEW' === action.type ) { |
| 30 | return { |
| 31 | ...state, |
| 32 | isPreview: ! state.isPreview, |
| 33 | }; |
| 34 | } |
| 35 | |
| 36 | if ( 'UPDATE_CURRENT_TAB' === action.type ) { |
| 37 | return { |
| 38 | ...state, |
| 39 | tab: action.tab, |
| 40 | isPreview: false, |
| 41 | }; |
| 42 | } |
| 43 | |
| 44 | if ( 'UPDATE_TEMPLATES' === action.type ) { |
| 45 | return { |
| 46 | ...state, |
| 47 | templates: { |
| 48 | items: action.items, |
| 49 | currentPage: Number( action.currentPage ), |
| 50 | totalPages: Number( action.totalPages ), |
| 51 | }, |
| 52 | }; |
| 53 | } |
| 54 | |
| 55 | if ( 'UPDATE_PATTERNS' === action.type ) { |
| 56 | return { |
| 57 | ...state, |
| 58 | patterns: action.items, |
| 59 | }; |
| 60 | } |
| 61 | |
| 62 | if ( 'UPDATE_LIBRARY' === action.type ) { |
| 63 | return { |
| 64 | ...state, |
| 65 | library: { |
| 66 | items: action.items, |
| 67 | currentPage: Number( action.currentPage ), |
| 68 | totalPages: Number( action.totalPages ), |
| 69 | }, |
| 70 | }; |
| 71 | } |
| 72 | |
| 73 | if ( 'SET_PREVIEW_DATA' === action.type ) { |
| 74 | return { |
| 75 | ...state, |
| 76 | preview: action.preview, |
| 77 | }; |
| 78 | } |
| 79 | |
| 80 | return state; |
| 81 | }, |
| 82 | |
| 83 | selectors: { |
| 84 | isFetching( state ) { |
| 85 | return state.isFetching; |
| 86 | }, |
| 87 | |
| 88 | isPreview( state ) { |
| 89 | return state.isPreview; |
| 90 | }, |
| 91 | |
| 92 | getCurrentTab( state ) { |
| 93 | return state.tab; |
| 94 | }, |
| 95 | |
| 96 | getTemplates( state ) { |
| 97 | return state.templates; |
| 98 | }, |
| 99 | |
| 100 | getPatterns( state ) { |
| 101 | return state.patterns; |
| 102 | }, |
| 103 | |
| 104 | getLibrary( state ) { |
| 105 | return state.library; |
| 106 | }, |
| 107 | |
| 108 | getPreview( state ) { |
| 109 | return state.preview; |
| 110 | }, |
| 111 | }, |
| 112 | |
| 113 | actions: { |
| 114 | setFetching( isFetching ) { |
| 115 | return { |
| 116 | type: 'SET_FETCHING', |
| 117 | isFetching, |
| 118 | }; |
| 119 | }, |
| 120 | |
| 121 | togglePreview( isPreview ) { |
| 122 | return { |
| 123 | type: 'TOGGLE_PREVIEW', |
| 124 | isPreview, |
| 125 | }; |
| 126 | }, |
| 127 | |
| 128 | updateCurrentTab( tab ) { |
| 129 | return { |
| 130 | type: 'UPDATE_CURRENT_TAB', |
| 131 | tab, |
| 132 | }; |
| 133 | }, |
| 134 | |
| 135 | updateTemplates( items, currentPage, totalPages ) { |
| 136 | return { |
| 137 | type: 'UPDATE_TEMPLATES', |
| 138 | items, |
| 139 | currentPage, |
| 140 | totalPages, |
| 141 | }; |
| 142 | }, |
| 143 | |
| 144 | updatePatterns( items ) { |
| 145 | return { |
| 146 | type: 'UPDATE_PATTERNS', |
| 147 | items, |
| 148 | }; |
| 149 | }, |
| 150 | |
| 151 | updateLibrary( items, currentPage, totalPages ) { |
| 152 | return { |
| 153 | type: 'UPDATE_LIBRARY', |
| 154 | items, |
| 155 | currentPage, |
| 156 | totalPages, |
| 157 | }; |
| 158 | }, |
| 159 | |
| 160 | setPreviewData( preview ) { |
| 161 | return { |
| 162 | type: 'SET_PREVIEW_DATA', |
| 163 | preview, |
| 164 | }; |
| 165 | }, |
| 166 | }, |
| 167 | } ); |
| 168 |