diff --git a/app/src/state/actions.tsx b/app/src/state/actions.tsx index ef14284d3..4220c08ef 100644 --- a/app/src/state/actions.tsx +++ b/app/src/state/actions.tsx @@ -100,11 +100,6 @@ export const IAPP_GEOJSON_GET_REQUEST = 'IAPP_GEOJSON_GET_REQUEST'; export const IAPP_GEOJSON_GET_ONLINE = 'IAPP_GEOJSON_GET_ONLINE'; export const IAPP_GEOJSON_GET_SUCCESS = 'IAPP_GEOJSON_GET_SUCCESS'; -export const ACTIVITIES_TABLE_ROWS_GET_REQUEST = 'ACTIVITIES_TABLE_ROWS_GET_REQUEST'; -export const ACTIVITIES_TABLE_ROWS_GET_ONLINE = 'ACTIVITIES_TABLE_ROWS_GET_ONLINE'; -export const ACTIVITIES_TABLE_ROWS_GET_SUCCESS = 'ACTIVITIES_TABLE_ROWS_GET_SUCCESS'; -export const ACTIVITIES_TABLE_ROWS_GET_FAILURE = 'ACTIVITIES_TABLE_ROWS_GET_FAILURE'; - export const MAP_INIT_REQUEST = 'MAP_INIT_REQUEST'; export const MAP_INIT_FOR_RECORDSET = 'MAP_INIT_FOR_RECORDSET'; diff --git a/app/src/state/actions/activity/Activity.ts b/app/src/state/actions/activity/Activity.ts index e46dd3f30..701b46ecb 100644 --- a/app/src/state/actions/activity/Activity.ts +++ b/app/src/state/actions/activity/Activity.ts @@ -19,6 +19,20 @@ export interface ICreateLocal { id: string; data: Record; } +export interface ActivityTableRowRequest { + recordSetID: string | number; + tableFiltersHash: Record; + page: number; + limit: number; +} + +export interface ActivityTableRowGetRequest extends ActivityTableRowRequest { + filterObj: Record; +} +export interface ActivityTableRowsGetSuccess extends ActivityTableRowRequest { + rows: Record[]; +} + class Activity { private static readonly PREFIX = 'Activity'; static readonly Offline = Offline; @@ -59,5 +73,9 @@ class Activity { static readonly getFailure = createAction(`${this.PREFIX}/getFailure`, (arg?: Response) => ({ payload: arg })); + + static readonly getRows = createAction(`${this.PREFIX}/getRows`); + static readonly getRowsRequest = createAction(`${this.PREFIX}/getRowsRequest`); + static readonly getRowsSuccess = createAction(`${this.PREFIX}/getRowsSuccess`); } export default Activity; diff --git a/app/src/state/reducers/map.ts b/app/src/state/reducers/map.ts index ededecf38..4fbf687cc 100644 --- a/app/src/state/reducers/map.ts +++ b/app/src/state/reducers/map.ts @@ -4,8 +4,6 @@ import { ACTIVITIES_GEOJSON_GET_SUCCESS, ACTIVITIES_GET_IDS_FOR_RECORDSET_REQUEST, ACTIVITIES_GET_IDS_FOR_RECORDSET_SUCCESS, - ACTIVITIES_TABLE_ROWS_GET_REQUEST, - ACTIVITIES_TABLE_ROWS_GET_SUCCESS, ACTIVITY_PAGE_MAP_EXTENT_TOGGLE, CSV_LINK_CLICKED, CUSTOM_LAYER_DRAWN, @@ -64,6 +62,7 @@ import MapActions from 'state/actions/map'; import GeoTracking from 'state/actions/geotracking/GeoTracking'; import RecordCache from 'state/actions/cache/RecordCache'; import IappActions from 'state/actions/activity/Iapp'; +import Activity from 'state/actions/activity/Activity'; export enum LeafletWhosEditingEnum { ACTIVITY = 'ACTIVITY', @@ -488,46 +487,35 @@ function createMapReducer(configuration: AppConfig): (MapState, AnyAction) => Ma layerIndex = draftState.layers.findIndex((layer) => layer.recordSetID === setID); } draftState.layers[layerIndex].layerState = {}; - if (action.payload.recordSets[setID].colorScheme) { - draftState.layers[layerIndex].layerState.colorScheme = action.payload.recordSets[setID].colorScheme; - } - if (action.payload.recordSets[setID].color) { - draftState.layers[layerIndex].layerState.color = action.payload.recordSets[setID].color; - } - if (action.payload.recordSets[setID].mapToggle) { - draftState.layers[layerIndex].layerState.mapToggle = action.payload.recordSets[setID].mapToggle; - } - if (action.payload.recordSets[setID].labelToggle) { - draftState.layers[layerIndex].layerState.labelToggle = action.payload.recordSets[setID].labelToggle; - } - if (action.payload.recordSets[setID].drawOrder) { - draftState.layers[layerIndex].layerState.drawOrder = action.payload.recordSets[setID].drawOrder; - } - if (action.payload.recordSets[setID].cacheMetadata) { - draftState.layers[layerIndex].layerState.cacheMetadata = action.payload.recordSets[setID].cacheMetadata; - } + Object.assign(draftState.layers[layerIndex].layerState, action.payload.recordSets[setID]); }); } else if (WhatsHere.map_init_get_poi_ids_fetched.match(action)) { - draftState.whatsHere.IAPPIDs = action.payload ?? []; - draftState.whatsHere.iappRows = []; - draftState.whatsHere.IAPPPage = 0; - draftState.whatsHere.IAPPLimit = 15; + Object.assign(draftState.whatsHere, { + IAPPIDs: action.payload ?? [], + iappRows: [], + IAPPPage: 0, + IAPPLimit: 15 + }); } else if (WhatsHere.map_init_get_activity_ids_fetched.match(action)) { - draftState.whatsHere.ActivityIDs = [...action.payload]; - draftState.whatsHere.activityRows = []; - draftState.whatsHere.ActivityPage = 0; - draftState.whatsHere.ActivityLimit = 15; + Object.assign(draftState.whatsHere, { + ActivityIDs: [...action.payload], + activityRows: [], + ActivityPage: 0, + ActivityLimit: 15 + }); } else if (WhatsHere.map_feature.match(action)) { - draftState.whatsHere.clickedActivity = null; - draftState.whatsHere.clickedActivityDescription = null; - draftState.whatsHere.clickedIAPP = null; - draftState.whatsHere.clickedIAPPDescription = null; - draftState.whatsHere.loadingActivities = true; - draftState.whatsHere.loadingIAPP = true; - draftState.whatsHere.feature = action.payload; - draftState.whatsHere.toggle = state.whatsHere.toggle; - draftState.whatsHere.limit = 5; - draftState.whatsHere.page = 0; + Object.assign(draftState.whatsHere, { + clickedActivity: null, + clickedActivityDescription: null, + clickedIAPP: null, + clickedIAPPDescription: null, + loadingActivities: true, + loadingIAPP: true, + feature: action.payload, + toggle: state.whatsHere.toggle, + limit: 5, + page: 0 + }); } else if (WhatsHere.server_filtered_ids_fetched.match(action)) { draftState.whatsHere.serverActivityIDs = action.payload.activities; draftState.whatsHere.serverIAPPIDs = action.payload.iapp; @@ -548,47 +536,54 @@ function createMapReducer(configuration: AppConfig): (MapState, AnyAction) => Ma draftState.whatsHere.ActivityIDs = Array.from(new Set(activityIds)); draftState.whatsHere.IAPPIDs = Array.from(new Set(iappIds)); } else if (WhatsHere.sort_filter_update.match(action)) { + const { field, direction } = action.payload; if (action.payload.type === RecordSetType.IAPP) { - draftState.whatsHere.IAPPPage = 0; - draftState.whatsHere.IAPPSortField = action.payload.field; - draftState.whatsHere.IAPPSortDirection = action.payload.direction; + Object.assign(draftState.whatsHere, { + IAPPPage: 0, + IAPPSortField: field, + IAPPSortDirection: direction + }); } else { - draftState.whatsHere.ActivityPage = 0; - draftState.whatsHere.ActivitySortField = action.payload.field; - draftState.whatsHere.ActivitySortDirection = action.payload.direction; + Object.assign(draftState.whatsHere, { + ActivityPage: 0, + ActivitySortField: action.payload.field, + ActivitySortDirection: action.payload.direction + }); } } else if (WhatsHere.toggle.match(action)) { if (draftState.whatsHere.toggle) { - draftState.whatsHere.loadingActivities = false; - draftState.whatsHere.loadingIAPP = false; + Object.assign(draftState.whatsHere, { + loadingActivities: false, + loadingIAPP: false + }); } else { - draftState.whatsHere.toggle = !state.whatsHere.toggle; - draftState.whatsHere.feature = null; - draftState.whatsHere.iappRows = []; - draftState.whatsHere.activityRows = []; - draftState.whatsHere.limit = 5; - draftState.whatsHere.page = 0; + Object.assign(draftState.whatsHere, { + toggle: !state.whatsHere.toggle, + feature: null, + iappRows: [], + activityRows: [], + limit: 5, + page: 0 + }); } } else if (WhatsHere.map_set_section.match(action)) { - draftState.whatsHere.section = action.payload; - draftState.whatsHere.page = 0; - draftState.whatsHere.limit = 5; + Object.assign(draftState.whatsHere, { + section: action.payload, + page: 0, + limit: 5 + }); } else if (WhatsHere.set_highlighted_iapp.match(action)) { draftState.userRecordOnHoverRecordRow = { id: action.payload, - geometry: state?.whatsHere?.iappRows.filter((row) => { - return row.site_id === action.payload; - })[0].geometry + geometry: state?.whatsHere?.iappRows.filter((row) => row.site_id === action.payload)[0].geometry }; - draftState.userRecordOnHoverRecordType = RecordSetType.IAPP; - // to delete: - draftState.whatsHere.highlightedType = RecordSetType.IAPP; - draftState.whatsHere.highlightedURLID = action.payload; - draftState.whatsHere.highlightedIAPP = action.payload; - draftState.whatsHere.highlightedACTIVITY = null; - draftState.whatsHere.highlightedGeo = state?.whatsHere?.iappRows.filter((row) => { - return row.site_id === action.payload; - })[0]; + Object.assign(draftState.whatsHere, { + highlightedType: RecordSetType.IAPP, + highlightedURLID: action.payload, + highlightedIAPP: action.payload, + highlightedACTIVITY: null, + highlightedGeo: state?.whatsHere?.iappRows.filter((row) => row.site_id === action.payload)[0] + }); } else if (WhatsHere.set_highlighted_activity.match(action)) { draftState.userRecordOnHoverRecordRow = { id: action.payload.id, @@ -600,13 +595,13 @@ function createMapReducer(configuration: AppConfig): (MapState, AnyAction) => Ma ] }; draftState.userRecordOnHoverRecordType = RecordSetType.Activity; - draftState.whatsHere.highlightedType = RecordSetType.Activity; - draftState.whatsHere.highlightedURLID = action.payload.id; - draftState.whatsHere.highlightedIAPP = null; - draftState.whatsHere.highlightedACTIVITY = action.payload.short_id; - draftState.whatsHere.highlightedGeo = state?.whatsHere?.activityRows.filter((row) => { - return row.short_id === action.payload.short_id; - })[0]; + Object.assign(draftState.whatsHere, { + highlightedType: RecordSetType.Activity, + highlightedURLID: action.payload.id, + highlightedIAPP: null, + highlightedACTIVITY: action.payload.short_id, + highlightedGeo: state?.whatsHere?.activityRows.filter((row) => row.short_id === action.payload.short_id)[0] + }); } else if (WhatsHere.iapp_rows_success.match(action)) { draftState.whatsHere.loadingIAPP = false; draftState.whatsHere.iappRows = [...action.payload]; @@ -648,65 +643,69 @@ function createMapReducer(configuration: AppConfig): (MapState, AnyAction) => Ma draftState.track_me_draw_geo.drawingShape = false; } else if (GeoTracking.resume.match(action)) { draftState.track_me_draw_geo.drawingShape = true; - } else if (IappActions.getRows.match(action)) { + } else if (IappActions.getRows.match(action) || Activity.getRows.match(action)) { const { recordSetID, page, limit, tableFiltersHash } = action.payload; draftState.recordTables[recordSetID] ??= {}; - draftState.recordTables[recordSetID].loading = true; - draftState.recordTables[recordSetID].page = page; - draftState.recordTables[recordSetID].limit = limit; - draftState.recordTables[recordSetID].tableFiltersHash = tableFiltersHash; + Object.assign(draftState.recordTables[recordSetID], { + loading: true, + page: page, + limit: limit, + tableFiltersHash: tableFiltersHash + }); } else if (IappActions.getRowsSuccess.match(action)) { - { - // the hash, page, and limit all need to line up - if ( - draftState.recordTables?.[action.payload.recordSetID]?.tableFiltersHash !== action.payload.tableFiltersHash - ) { - console.warn( - 'hash mismatch', - draftState.recordTables?.[action.payload.recordSetID]?.tableFiltersHash, - action.payload.tableFiltersHash - ); - return; - } - if (Number(draftState.recordTables?.[action.payload.recordSetID]?.limit) !== Number(action.payload.limit)) { - console.warn( - 'limit mismatch', - draftState.recordTables?.[action.payload.recordSetID]?.limit, - action.payload.limit - ); - console.warn( - 'typeof', - typeof draftState.recordTables?.[action.payload.recordSetID]?.limit, - typeof action.payload.limit - ); - return; - } - if (Number(draftState.recordTables?.[action.payload.recordSetID]?.page) !== Number(action.payload.page)) { - console.warn( - 'page mismatch', - draftState.recordTables?.[action.payload.recordSetID]?.page, - action.payload.page - ); - return; - } - if (draftState.recordTables?.[action.payload.recordSetID]) { - draftState.recordTables[action.payload.recordSetID].rows = action.payload.rows; - } else { - draftState.recordTables[action.payload.recordSetID] = {}; - draftState.recordTables[action.payload.recordSetID].rows = action.payload.rows; - } // set defaults - draftState.recordTables[action.payload.recordSetID].loading = false; + // the hash, page, and limit all need to line up + const { recordSetID, tableFiltersHash, limit, page, rows } = action.payload; + const recordTable = draftState.recordTables?.[recordSetID]; + if (recordTable?.tableFiltersHash !== tableFiltersHash) { + console.warn('hash mismatch', draftState.recordTables?.[recordSetID]?.tableFiltersHash, tableFiltersHash); + return; + } + if (Number(recordTable?.limit) !== Number(limit)) { + console.warn('limit mismatch', draftState.recordTables?.[recordSetID]?.limit, limit); + return; + } + if (Number(recordTable?.page) !== Number(action.payload.page)) { + console.warn('page mismatch', draftState.recordTables?.[recordSetID]?.page, page); + return; + } + if (recordTable) { + draftState.recordTables[recordSetID].rows = rows; + } else { + draftState.recordTables[recordSetID] = { rows }; + } // set defaults + draftState.recordTables[recordSetID].loading = false; + } else if (Activity.getRowsSuccess.match(action)) { + // the hash, page, and limit all need to line up + const { recordSetID, tableFiltersHash, limit, page, rows } = action.payload; + const recordTable = draftState.recordTables?.[recordSetID]; + if (recordTable?.tableFiltersHash !== tableFiltersHash) { + console.warn('hash mismatch', recordTable?.tableFiltersHash, tableFiltersHash); return; } + if (Number(recordTable?.limit) !== Number(limit)) { + console.warn('limit mismatch', recordTable?.limit, limit); + return; + } + if (Number(recordTable?.page) !== Number(page)) { + console.warn('page mismatch', recordTable?.page, page); + return; + } + if (draftState.recordTables?.[recordSetID]) { + draftState.recordTables[recordSetID].rows = rows; + } else { + draftState.recordTables[recordSetID] = { rows }; + } // set defaults + draftState.recordTables[action.payload.recordSetID].loading = false; } else { switch (action.type) { case TOGGLE_LAYER_PICKER_OPEN: draftState.layerPickerOpen = !draftState.layerPickerOpen; break; - case TOGGLE_WMS_LAYER: + case TOGGLE_WMS_LAYER: { const index = draftState.simplePickerLayers2.findIndex((layer) => layer.url === action.payload.layer.url); draftState.simplePickerLayers2[index].toggle = !draftState.simplePickerLayers2[index]?.toggle; break; + } case TOGGLE_DRAWN_LAYER: { const index = draftState.clientBoundaries.findIndex((layer) => layer.id === action.payload.layer.id); draftState.clientBoundaries[index].toggle = !draftState.clientBoundaries[index]?.toggle; @@ -716,16 +715,6 @@ function createMapReducer(configuration: AppConfig): (MapState, AnyAction) => Ma draftState.MapMode = draftState.MapMode === 'VECTOR_ENDPOINT' ? 'GEOJSON' : 'VECTOR_ENDPOINT'; break; } - case ACTIVITIES_TABLE_ROWS_GET_REQUEST: { - if (!draftState.recordTables?.[action.payload.recordSetID]) { - draftState.recordTables[action.payload.recordSetID] = {}; - } - draftState.recordTables[action.payload.recordSetID].loading = true; - draftState.recordTables[action.payload.recordSetID].page = action.payload.page; - draftState.recordTables[action.payload.recordSetID].limit = action.payload.limit; - draftState.recordTables[action.payload.recordSetID].tableFiltersHash = action.payload.tableFiltersHash; - break; - } case ACTIVITIES_GEOJSON_GET_SUCCESS: { // Everything should point to this now instead: draftState.activitiesGeoJSONDict = { @@ -833,49 +822,6 @@ function createMapReducer(configuration: AppConfig): (MapState, AnyAction) => Ma draftState.layers[index].tableFiltersHash = action.payload.tableFiltersHash; break; } - case ACTIVITIES_TABLE_ROWS_GET_SUCCESS: { - // the hash, page, and limit all need to line up - if ( - draftState.recordTables?.[action.payload.recordSetID]?.tableFiltersHash !== - action.payload.tableFiltersHash - ) { - console.warn( - 'hash mismatch', - draftState.recordTables?.[action.payload.recordSetID]?.tableFiltersHash, - action.payload.tableFiltersHash - ); - break; - } - if (Number(draftState.recordTables?.[action.payload.recordSetID]?.limit) !== Number(action.payload.limit)) { - console.warn( - 'limit mismatch', - draftState.recordTables?.[action.payload.recordSetID]?.limit, - action.payload.limit - ); - console.warn( - 'typeof', - typeof draftState.recordTables?.[action.payload.recordSetID]?.limit, - typeof action.payload.limit - ); - break; - } - if (Number(draftState.recordTables?.[action.payload.recordSetID]?.page) !== Number(action.payload.page)) { - console.warn( - 'page mismatch', - draftState.recordTables?.[action.payload.recordSetID]?.page, - action.payload.page - ); - break; - } - if (draftState.recordTables?.[action.payload.recordSetID]) { - draftState.recordTables[action.payload.recordSetID].rows = action.payload.rows; - } else { - draftState.recordTables[action.payload.recordSetID] = {}; - draftState.recordTables[action.payload.recordSetID].rows = action.payload.rows; - } // set defaults - draftState.recordTables[action.payload.recordSetID].loading = false; - break; - } case ACTIVITY_PAGE_MAP_EXTENT_TOGGLE: { draftState.activityPageMapExtentToggle = !state.activityPageMapExtentToggle; break; diff --git a/app/src/state/sagas/map.ts b/app/src/state/sagas/map.ts index b85336c88..7eddc58aa 100644 --- a/app/src/state/sagas/map.ts +++ b/app/src/state/sagas/map.ts @@ -8,8 +8,6 @@ import { ACTIVITIES_GEOJSON_REFETCH_ONLINE, ACTIVITIES_GET_IDS_FOR_RECORDSET_ONLINE, ACTIVITIES_GET_IDS_FOR_RECORDSET_REQUEST, - ACTIVITIES_TABLE_ROWS_GET_ONLINE, - ACTIVITIES_TABLE_ROWS_GET_REQUEST, ACTIVITY_UPDATE_GEO_REQUEST, CUSTOM_LAYER_DRAWN, DRAW_CUSTOM_LAYER, @@ -548,25 +546,16 @@ function* handle_URL_CHANGE(action) { const page = mapState.recordTables?.[id]?.page || 0; const limit = mapState.recordTables?.[id]?.limit || 20; + const actionArg = { + recordSetID: id, + tableFiltersHash: recordSetsState.recordSets?.[id]?.tableFiltersHash, + page: page, + limit: limit + }; if (recordSetType === RecordSetType.Activity) { - yield put({ - type: ACTIVITIES_TABLE_ROWS_GET_REQUEST, - payload: { - recordSetID: id, - tableFiltersHash: recordSetsState.recordSets?.[id]?.tableFiltersHash, - page: page, - limit: limit - } - }); + yield put(Activity.getRows(actionArg)); } else if (recordSetType === RecordSetType.IAPP) { - yield put( - IappActions.getRows({ - recordSetID: id, - tableFiltersHash: recordSetsState.recordSets?.[id]?.tableFiltersHash, - page: page, - limit: limit - }) - ); + yield put(IappActions.getRows(actionArg)); } } } @@ -590,17 +579,14 @@ function* handle_UserFilterChange(action: PayloadAction