import update from 'immutability-helper'; import { IReducerAction } from 'model/common.model'; import { RECEIVE_ACCESSION, RECEIVE_ACCESSION_OVERVIEW, APPEND_ACCESSIONS, RECEIVE_ACCESSION_MAPINFO, RECEIVE_ACCESSION_AUDIT_LOG, RECEIVE_TILE_LAYER, APPEND_ACCESSIONS_WITH_SUGGESTIONS, } from 'accessions/constants'; import FilteredPage from 'model/FilteredPage'; import Accession from 'model/accession/Accession'; import AccessionMapInfo from 'model/accession/AccessionMapInfo'; import AccessionOverview from 'model/accession/AccessionOverview'; import AccessionAuditLog from 'model/accession/AccessionAuditLog'; import MapLayer, { AVAILABLE_LAYERS } from 'model/genesys/MapTileLayer'; import ApiCall from 'model/ApiCall'; const INITIAL_STATE: { accession: ApiCall; auditLog: ApiCall, paged: ApiCall>; suggestions: any; overview: ApiCall; mapInfo: ApiCall; mapLayers: MapLayer[] } = { accession: null, auditLog: null, paged: null, suggestions: null, overview: null, mapInfo: null, mapLayers: AVAILABLE_LAYERS, }; function publicAccessions(state = INITIAL_STATE, action: IReducerAction) { switch (action.type) { case RECEIVE_ACCESSION: { const {apiCall} = action.payload; const receivedIndex = state.paged && apiCall.data ? state.paged.data.content.findIndex((item) => item.uuid === apiCall.data.uuid) : -1; if (receivedIndex !== -1) { return update(state, { accession: { $set: apiCall }, auditLog: {$set: null}, paged: { data: { content: { [receivedIndex]: {$set: apiCall.data}, }, }, }, }); } else { return update(state, { accession: { $set: apiCall}, auditLog: {$set: null}, }); } } case RECEIVE_ACCESSION_AUDIT_LOG: { const {apiCall} = action.payload; return update(state, { auditLog: { $set: apiCall }, }); } case RECEIVE_ACCESSION_OVERVIEW: { const { apiCall: { loading, error, timestamp, data } } = action.payload; return update(state, { overview: { $set: { loading, error, timestamp, data: data !== undefined ? data : state.overview && state.overview.data, } }, // paged: { $set: null }, // mapInfo: { $set: null }, }); } case APPEND_ACCESSIONS: { const { apiCall: { loading, error, timestamp, data } } = action.payload; return update(state, { paged: { $set: { loading, error, timestamp, data: FilteredPage.merge(state.paged && state.paged.data, data), } }, // mapInfo: { $set: null }, // overview: { $set: null }, }); } case APPEND_ACCESSIONS_WITH_SUGGESTIONS: { const {apiCall: {loading, error, timestamp, data}} = action.payload; return update(state, { paged: { $set: { loading, error, timestamp, data: FilteredPage.merge(state.paged && state.paged.data, data), }, }, suggestions: {$set: data ? data.suggestions : state.suggestions}, // mapInfo: { $set: null }, // overview: { $set: null }, }); } case RECEIVE_ACCESSION_MAPINFO: { const { apiCall } = action.payload; apiCall.data = apiCall.loading && state.mapInfo ? state.mapInfo.data : apiCall.data; return update(state, { mapInfo: { $set: apiCall }, // paged: {$set: null}, // overview: {$set: null}, }); } case RECEIVE_TILE_LAYER: { const {tileLayer} = action.payload; const indexToUpdate = state.mapLayers.findIndex((layerItem) => layerItem.name === tileLayer.name); return update(state, { mapLayers: { [indexToUpdate]: {$set: tileLayer}, }, }); } default: return state; } } export default publicAccessions;