|
| 1 | +import { BurwellSourceActions, BurwellState } from "./types"; |
| 2 | + |
| 3 | +const burwellDefaultState: BurwellState = { |
| 4 | + isFetching: false, |
| 5 | + msg: "", |
| 6 | + clicks: 0, |
| 7 | + maps: [], |
| 8 | + selectedScale: "all", |
| 9 | + selectedFeatures: [], |
| 10 | + activeFeature: {}, |
| 11 | + menuOpen: false, |
| 12 | + flyTo: true, |
| 13 | +}; |
| 14 | + |
| 15 | +const reducer = ( |
| 16 | + state: BurwellState = burwellDefaultState, |
| 17 | + action: BurwellSourceActions |
| 18 | +) => { |
| 19 | + switch (action.type) { |
| 20 | + case "request-data": |
| 21 | + return Object.assign({}, state, { |
| 22 | + isFetching: true, |
| 23 | + }); |
| 24 | + case "recieve-data": |
| 25 | + // map ids to features to use mapbox effects appwide |
| 26 | + const maps = action.maps.map((d, i) => { |
| 27 | + d.id = i; |
| 28 | + return d; |
| 29 | + }); |
| 30 | + return Object.assign({}, state, { |
| 31 | + isFetching: false, |
| 32 | + maps: maps, |
| 33 | + }); |
| 34 | + case "select-scale": |
| 35 | + return Object.assign({}, state, { |
| 36 | + selectedScale: action.selectedScale, |
| 37 | + }); |
| 38 | + case "select-features": |
| 39 | + return Object.assign({}, state, { |
| 40 | + selectedFeatures: action.selectedFeatures, |
| 41 | + }); |
| 42 | + case "activate-feature": |
| 43 | + return Object.assign({}, state, { |
| 44 | + activeFeature: action.activeFeature, |
| 45 | + }); |
| 46 | + case "toggle-menu": |
| 47 | + return Object.assign({}, state, { |
| 48 | + menuOpen: action.menuOpen, |
| 49 | + }); |
| 50 | + case "toggle-fly-option": |
| 51 | + return Object.assign({}, state, { |
| 52 | + flyTo: action.flyTo, |
| 53 | + }); |
| 54 | + default: |
| 55 | + return state; |
| 56 | + } |
| 57 | +}; |
| 58 | + |
| 59 | +export default reducer; |
0 commit comments