actions.js
4 years ago
controls.js
5 years ago
index.js
5 years ago
reducer.js
2 years ago
resolvers.js
4 years ago
selectors.js
4 years ago
reducer.js
283 lines
| 1 | import { combineReducers, dispatch } from '@wordpress/data'; |
| 2 | import apiFetch from '@wordpress/api-fetch'; |
| 3 | import { pick } from 'lodash'; |
| 4 | |
| 5 | /** |
| 6 | * Presets reducer |
| 7 | * |
| 8 | * @param {array} state |
| 9 | * @param {object} action |
| 10 | */ |
| 11 | const presetReducer = (state = [], action) => { |
| 12 | switch (action.type) { |
| 13 | case "SET_PRESET": |
| 14 | return action.value; |
| 15 | case "ADD_PRESET": |
| 16 | return [...state, ...[action.value]]; |
| 17 | case "UPDATE_PRESET": |
| 18 | return state.map((item, index) => { |
| 19 | if (item.id !== action.value?.id) { |
| 20 | return item; |
| 21 | } |
| 22 | return { |
| 23 | ...item, |
| 24 | ...action.value, |
| 25 | }; |
| 26 | }); |
| 27 | case "REMOVE_PRESET": |
| 28 | return state.filter((item) => { |
| 29 | return item !== action.value; |
| 30 | }); |
| 31 | } |
| 32 | return state; |
| 33 | }; |
| 34 | |
| 35 | /** |
| 36 | * Presets reducer |
| 37 | * |
| 38 | * @param {array} state |
| 39 | * @param {object} action |
| 40 | */ |
| 41 | const audioPresetReducer = (state = [], action) => { |
| 42 | switch (action.type) { |
| 43 | case "SET_AUDIO_PRESET": |
| 44 | return action.value; |
| 45 | case "SET_AUDIO_PRESET": |
| 46 | return action.value; |
| 47 | case "ADD_AUDIO_PRESET": |
| 48 | return [...state, ...[action.value]]; |
| 49 | case "UPDATE_AUDIO_PRESET": |
| 50 | return state.map((item, index) => { |
| 51 | if (item.id !== action.value?.id) { |
| 52 | return item; |
| 53 | } |
| 54 | return { |
| 55 | ...item, |
| 56 | ...action.value, |
| 57 | }; |
| 58 | }); |
| 59 | case "REMOVE_AUDIO_PRESET": |
| 60 | return state.filter((item) => { |
| 61 | return item !== action.value; |
| 62 | }); |
| 63 | } |
| 64 | return state; |
| 65 | }; |
| 66 | |
| 67 | /** |
| 68 | * For preset loading |
| 69 | * |
| 70 | * @param {boolean} state |
| 71 | * @param {object} action |
| 72 | */ |
| 73 | const presetLoadingReducer = (state = true, action) => { |
| 74 | switch (action.type) { |
| 75 | case "SET_PRESET_LOADING": |
| 76 | return action.value; |
| 77 | } |
| 78 | }; |
| 79 | |
| 80 | /** |
| 81 | * For preset loading |
| 82 | * |
| 83 | * @param {boolean} state |
| 84 | * @param {object} action |
| 85 | */ |
| 86 | const videosLoadingReducer = (state = true, action) => { |
| 87 | switch (action.type) { |
| 88 | case "SET_VIDEOS_LOADING": |
| 89 | return action.value; |
| 90 | } |
| 91 | }; |
| 92 | |
| 93 | const proModalReducer = (state = false, action) => { |
| 94 | switch (action.type) { |
| 95 | case "SET_PRO_MODAL": |
| 96 | return action.value; |
| 97 | } |
| 98 | }; |
| 99 | |
| 100 | const addVideo = (videos, video) => { |
| 101 | // check for existing item |
| 102 | const index = videos.find((e) => e.id === video.id); |
| 103 | if (index) { |
| 104 | return videos; |
| 105 | } |
| 106 | return [...videos, ...[video]]; |
| 107 | }; |
| 108 | |
| 109 | const videosReducer = ( |
| 110 | state = { |
| 111 | total: 0, |
| 112 | total_pages: 0, |
| 113 | videos: [], |
| 114 | hasResolved: false, |
| 115 | isError: false, |
| 116 | }, |
| 117 | action |
| 118 | ) => { |
| 119 | switch (action.type) { |
| 120 | case "SET_VIDEOS": |
| 121 | return action.value; |
| 122 | case "UPDATE_VIDEOS": |
| 123 | return { ...state, ...action.value }; |
| 124 | case "APPEND_VIDEOS": |
| 125 | let draft = state; |
| 126 | (action.value || []).forEach((video) => { |
| 127 | draft.videos = addVideo(draft.videos, video); |
| 128 | }); |
| 129 | return draft; |
| 130 | case "ADD_VIDEO": |
| 131 | return { ...state, videos: addVideo(state.videos, action.value) }; |
| 132 | case "UPDATE_VIDEO": |
| 133 | return { |
| 134 | ...state, |
| 135 | videos: state.videos.map((item, index) => { |
| 136 | if (item.id !== action.value?.id) { |
| 137 | return item; |
| 138 | } |
| 139 | return { |
| 140 | ...item, |
| 141 | ...action.value, |
| 142 | }; |
| 143 | }), |
| 144 | }; |
| 145 | case "REMOVE_VIDEO": |
| 146 | return { |
| 147 | ...state, |
| 148 | videos: state.videos.filter((item) => { |
| 149 | return item !== action.value; |
| 150 | }), |
| 151 | }; |
| 152 | } |
| 153 | return state; |
| 154 | }; |
| 155 | |
| 156 | /** |
| 157 | * Branding options are global and stored in settings |
| 158 | * @param {object} state |
| 159 | * @param {object} action |
| 160 | */ |
| 161 | const brandingReducer = (state = {}, action) => { |
| 162 | switch (action.type) { |
| 163 | case "SET_BRANDING": |
| 164 | return action.value; |
| 165 | case "UPDATE_BRANDING": |
| 166 | return { |
| 167 | ...state, |
| 168 | [action.name]: action.value, |
| 169 | }; |
| 170 | } |
| 171 | return state; |
| 172 | }; |
| 173 | |
| 174 | /** |
| 175 | * Youtube are global and stored in settings |
| 176 | * @param {object} state |
| 177 | * @param {object} action |
| 178 | */ |
| 179 | const youtubeReducer = (state = {}, action) => { |
| 180 | switch (action.type) { |
| 181 | case "SET_YOUTUBE": |
| 182 | return action.value; |
| 183 | case "UPDATE_YOUTUBE": |
| 184 | return { |
| 185 | ...state, |
| 186 | [action.name]: action.value, |
| 187 | }; |
| 188 | } |
| 189 | return state; |
| 190 | }; |
| 191 | |
| 192 | /** |
| 193 | * General are global and stored in settings |
| 194 | * @param {object} state |
| 195 | * @param {object} action |
| 196 | */ |
| 197 | const presetSettingsReducer = (state = {}, action) => { |
| 198 | switch (action.type) { |
| 199 | case "SET_PRESET_SETTINGS": |
| 200 | return action.value; |
| 201 | } |
| 202 | return state; |
| 203 | }; |
| 204 | const audioPresetSettingsReducer = (state = {}, action) => { |
| 205 | switch (action.type) { |
| 206 | case "SET_PRESET_AUDIO_SETTINGS": |
| 207 | return action.value; |
| 208 | } |
| 209 | return state; |
| 210 | }; |
| 211 | /** |
| 212 | * For fetching the options |
| 213 | * |
| 214 | * @param {object} state |
| 215 | * @param {object} action |
| 216 | */ |
| 217 | const optionsApi = (state, action) => { |
| 218 | switch (action.type) { |
| 219 | /** |
| 220 | * Fetch our options |
| 221 | */ |
| 222 | case "FETCH_OPTIONS": |
| 223 | return apiFetch({ |
| 224 | path: "/presto-player/v1/settings/", |
| 225 | }).then((settings) => { |
| 226 | dispatch("presto-player/player").setBranding( |
| 227 | settings.presto_player_branding |
| 228 | ); |
| 229 | dispatch("presto-player/player").setYoutube( |
| 230 | settings.presto_player_youtube |
| 231 | ); |
| 232 | dispatch("presto-player/player").setPresetSettings( |
| 233 | settings.presto_player_presets |
| 234 | ); |
| 235 | dispatch("presto-player/player").setPresetAudioSettings( |
| 236 | settings.presto_player_audio_presets |
| 237 | ); |
| 238 | }); |
| 239 | |
| 240 | /** |
| 241 | * Persist options to db |
| 242 | */ |
| 243 | case "SAVE_OPTIONS": |
| 244 | const data = { |
| 245 | presto_player_branding: pick(action?.branding, [ |
| 246 | "logo", |
| 247 | "color", |
| 248 | "logo_width", |
| 249 | "player_css", |
| 250 | ]), |
| 251 | }; |
| 252 | |
| 253 | // remove blanks |
| 254 | Object.keys(data).forEach( |
| 255 | (key) => |
| 256 | (data[key] == null || !Object.keys(data?.[key] || {}).length) && |
| 257 | delete data[key] |
| 258 | ); |
| 259 | |
| 260 | apiFetch({ |
| 261 | path: "/presto-player/v1/settings", |
| 262 | method: "POST", |
| 263 | data, |
| 264 | }); |
| 265 | |
| 266 | return data; |
| 267 | } |
| 268 | }; |
| 269 | |
| 270 | export default combineReducers({ |
| 271 | presetReducer, |
| 272 | audioPresetReducer, |
| 273 | presetLoadingReducer, |
| 274 | videosLoadingReducer, |
| 275 | videosReducer, |
| 276 | proModalReducer, |
| 277 | brandingReducer, |
| 278 | youtubeReducer, |
| 279 | presetSettingsReducer, |
| 280 | optionsApi, |
| 281 | audioPresetSettingsReducer |
| 282 | }); |
| 283 |