Skip to content

Commit d173937

Browse files
committed
Improvements on reducer logic
1 parent 5204af2 commit d173937

File tree

3 files changed

+45
-39
lines changed

3 files changed

+45
-39
lines changed

packages/web/.eslintrc.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
"argsIgnorePattern": "^_"
2626
}
2727
],
28-
"@typescript-eslint/no-explicit-any": "off",
28+
"no-console": "error",
2929
"jest/no-deprecated-functions": "off"
3030
}
3131
}

packages/web/src/app/reducer/index.ts

Lines changed: 41 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
import { useReducerAsync } from "use-reducer-async";
1+
import { AsyncActionHandlers, useReducerAsync } from "use-reducer-async";
22
import logReducer from "./log.ts";
33
import { calculateStats } from "../map/utils.ts";
44
import tArea from "@turf/area";
55
import tBboxPolygon from "@turf/bbox-polygon";
66
import { getFgbData } from "../utils/get-fgb-data.ts";
77
import { Map } from "maplibre-gl";
8+
import { Reducer } from "preact/hooks";
89

910
const availableTimestamps = [
1011
`2024-05-19T05:00:00Z`,
@@ -34,13 +35,24 @@ export type AppReducer<State, Action> = (state: State, action: Action) => State;
3435
/* eslint-enable no-unused-vars */
3536

3637
export interface AppState {
37-
map: any;
38+
map?: Map;
3839
mapStatus: MapStatus;
39-
geojson?: any;
40-
currentTimestamp?: Date;
41-
currentTimestampGeojson?: any;
40+
mapData: GeoJSON.FeatureCollection;
41+
currentTimestamp: Date;
42+
timestamps: string[];
4243
}
4344

45+
const appInitialState: AppState = {
46+
map: undefined,
47+
mapStatus: MapStatus.IDLE,
48+
mapData: {
49+
type: "FeatureCollection",
50+
features: [],
51+
},
52+
currentTimestamp: new Date(availableTimestamps[2]),
53+
timestamps: [...availableTimestamps],
54+
};
55+
4456
export type AppAction =
4557
| {
4658
type: AppActionTypes.SET_MAP_REF;
@@ -54,31 +66,16 @@ export type AppAction =
5466
currentTimestamp: Date;
5567
};
5668
}
57-
| {
58-
type: AppActionTypes.UPDATE_VIEW;
59-
}
6069
| {
6170
type: AppActionTypes.UPDATE_VIEW_START;
6271
}
6372
| {
6473
type: AppActionTypes.UPDATE_VIEW_SUCCESS;
6574
data: {
66-
geojson: any;
67-
timestamps: string[];
75+
mapData: GeoJSON.FeatureCollection;
6876
};
6977
};
7078

71-
export const appInitialState = {
72-
map: undefined,
73-
mapStatus: MapStatus.IDLE,
74-
geojson: {
75-
type: "FeatureCollection",
76-
features: [],
77-
},
78-
currentTimestamp: new Date(availableTimestamps[2]),
79-
timestamps: [...availableTimestamps],
80-
};
81-
8279
function appReducer(state: AppState, action: AppAction) {
8380
switch (action.type) {
8481
case AppActionTypes.SET_MAP_REF:
@@ -93,10 +90,14 @@ function appReducer(state: AppState, action: AppAction) {
9390
mapStatus: MapStatus.LOADING,
9491
};
9592
case AppActionTypes.UPDATE_VIEW_SUCCESS: {
96-
const { geojson } = action.data;
93+
if (!state.map) {
94+
return { ...state };
95+
}
96+
97+
const { mapData } = action.data;
9798
const { currentTimestamp } = state;
9899

99-
const stats = calculateStats(geojson);
100+
const stats = calculateStats(mapData);
100101

101102
const bounds = state.map.getBounds().toArray();
102103
const [[minX, minY], [maxX, maxY]] = bounds;
@@ -108,7 +109,7 @@ function appReducer(state: AppState, action: AppAction) {
108109
...state,
109110
formattedArea,
110111
stats,
111-
geojson,
112+
mapData,
112113
currentTimestamp,
113114
currentTimestampGeojson: currentTimestamp,
114115
mapStatus: MapStatus.READY,
@@ -117,7 +118,7 @@ function appReducer(state: AppState, action: AppAction) {
117118
case AppActionTypes.SET_CURRENT_TIMESTAMP: {
118119
const { currentTimestamp } = action.data;
119120

120-
const currentTimestampGeojson = state.geojson;
121+
const currentTimestampGeojson = state.mapData;
121122
const stats = calculateStats(currentTimestampGeojson);
122123
return {
123124
...state,
@@ -132,38 +133,43 @@ function appReducer(state: AppState, action: AppAction) {
132133
}
133134
}
134135

135-
const asyncActionHandlers: any = {
136+
type AsyncAction = {
137+
type: AppActionTypes.UPDATE_VIEW;
138+
};
139+
140+
const asyncActionHandlers: AsyncActionHandlers<
141+
Reducer<AppState, AppAction>,
142+
AsyncAction
143+
> = {
136144
[AppActionTypes.UPDATE_VIEW]:
137-
({ dispatch, getState }: any) =>
145+
({ dispatch, getState }) =>
138146
async () => {
139147
try {
140148
const { map, mapStatus, currentTimestamp } = getState();
141149

142-
// Only update the view if the map is ready
143-
if (mapStatus !== MapStatus.READY) {
150+
if (!map || mapStatus !== MapStatus.READY) {
144151
return;
145152
}
146153

147154
dispatch({
148155
type: AppActionTypes.UPDATE_VIEW_START,
149156
});
150157

151-
const { geojson, timestamps } = await getFgbData({
158+
const mapData = await getFgbData({
152159
map,
153160
timestamp: currentTimestamp,
154161
});
155162

156-
map.getSource("data").setData(geojson);
163+
map.getSource("data").setData(mapData);
157164

158165
dispatch({
159166
type: AppActionTypes.UPDATE_VIEW_SUCCESS,
160-
data: { geojson, timestamps },
167+
data: { mapData },
161168
});
162169
} catch (error) {
170+
// eslint-disable-next-line no-console
163171
console.log(error);
164-
alert(
165-
"Unexpected error while loading the map, please see console log.",
166-
);
172+
alert("Unexpected error while loading the map.");
167173
}
168174
},
169175
};

packages/web/src/app/utils/get-fgb-data.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ export async function getFgbData({
88
}: {
99
map: Map;
1010
timestamp: Date;
11-
}) {
12-
const geojson = {
11+
}): Promise<GeoJSON.FeatureCollection> {
12+
const geojson: GeoJSON.FeatureCollection = {
1313
type: "FeatureCollection",
1414
features: [] as GeoJSONFeature[],
1515
};
@@ -40,5 +40,5 @@ export async function getFgbData({
4040
}
4141
}
4242

43-
return { geojson };
43+
return geojson;
4444
}

0 commit comments

Comments
 (0)