1
- import { useReducerAsync } from "use-reducer-async" ;
1
+ import { AsyncActionHandlers , useReducerAsync } from "use-reducer-async" ;
2
2
import logReducer from "./log.ts" ;
3
3
import { calculateStats } from "../map/utils.ts" ;
4
4
import tArea from "@turf/area" ;
5
5
import tBboxPolygon from "@turf/bbox-polygon" ;
6
6
import { getFgbData } from "../utils/get-fgb-data.ts" ;
7
7
import { Map } from "maplibre-gl" ;
8
+ import { Reducer } from "preact/hooks" ;
8
9
9
10
const availableTimestamps = [
10
11
`2024-05-19T05:00:00Z` ,
@@ -34,13 +35,24 @@ export type AppReducer<State, Action> = (state: State, action: Action) => State;
34
35
/* eslint-enable no-unused-vars */
35
36
36
37
export interface AppState {
37
- map : any ;
38
+ map ?: Map ;
38
39
mapStatus : MapStatus ;
39
- geojson ?: any ;
40
- currentTimestamp ? : Date ;
41
- currentTimestampGeojson ?: any ;
40
+ mapData : GeoJSON . FeatureCollection ;
41
+ currentTimestamp : Date ;
42
+ timestamps : string [ ] ;
42
43
}
43
44
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
+
44
56
export type AppAction =
45
57
| {
46
58
type : AppActionTypes . SET_MAP_REF ;
@@ -54,31 +66,16 @@ export type AppAction =
54
66
currentTimestamp : Date ;
55
67
} ;
56
68
}
57
- | {
58
- type : AppActionTypes . UPDATE_VIEW ;
59
- }
60
69
| {
61
70
type : AppActionTypes . UPDATE_VIEW_START ;
62
71
}
63
72
| {
64
73
type : AppActionTypes . UPDATE_VIEW_SUCCESS ;
65
74
data : {
66
- geojson : any ;
67
- timestamps : string [ ] ;
75
+ mapData : GeoJSON . FeatureCollection ;
68
76
} ;
69
77
} ;
70
78
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
-
82
79
function appReducer ( state : AppState , action : AppAction ) {
83
80
switch ( action . type ) {
84
81
case AppActionTypes . SET_MAP_REF :
@@ -93,10 +90,14 @@ function appReducer(state: AppState, action: AppAction) {
93
90
mapStatus : MapStatus . LOADING ,
94
91
} ;
95
92
case AppActionTypes . UPDATE_VIEW_SUCCESS : {
96
- const { geojson } = action . data ;
93
+ if ( ! state . map ) {
94
+ return { ...state } ;
95
+ }
96
+
97
+ const { mapData } = action . data ;
97
98
const { currentTimestamp } = state ;
98
99
99
- const stats = calculateStats ( geojson ) ;
100
+ const stats = calculateStats ( mapData ) ;
100
101
101
102
const bounds = state . map . getBounds ( ) . toArray ( ) ;
102
103
const [ [ minX , minY ] , [ maxX , maxY ] ] = bounds ;
@@ -108,7 +109,7 @@ function appReducer(state: AppState, action: AppAction) {
108
109
...state ,
109
110
formattedArea,
110
111
stats,
111
- geojson ,
112
+ mapData ,
112
113
currentTimestamp,
113
114
currentTimestampGeojson : currentTimestamp ,
114
115
mapStatus : MapStatus . READY ,
@@ -117,7 +118,7 @@ function appReducer(state: AppState, action: AppAction) {
117
118
case AppActionTypes . SET_CURRENT_TIMESTAMP : {
118
119
const { currentTimestamp } = action . data ;
119
120
120
- const currentTimestampGeojson = state . geojson ;
121
+ const currentTimestampGeojson = state . mapData ;
121
122
const stats = calculateStats ( currentTimestampGeojson ) ;
122
123
return {
123
124
...state ,
@@ -132,38 +133,43 @@ function appReducer(state: AppState, action: AppAction) {
132
133
}
133
134
}
134
135
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
+ > = {
136
144
[ AppActionTypes . UPDATE_VIEW ] :
137
- ( { dispatch, getState } : any ) =>
145
+ ( { dispatch, getState } ) =>
138
146
async ( ) => {
139
147
try {
140
148
const { map, mapStatus, currentTimestamp } = getState ( ) ;
141
149
142
- // Only update the view if the map is ready
143
- if ( mapStatus !== MapStatus . READY ) {
150
+ if ( ! map || mapStatus !== MapStatus . READY ) {
144
151
return ;
145
152
}
146
153
147
154
dispatch ( {
148
155
type : AppActionTypes . UPDATE_VIEW_START ,
149
156
} ) ;
150
157
151
- const { geojson , timestamps } = await getFgbData ( {
158
+ const mapData = await getFgbData ( {
152
159
map,
153
160
timestamp : currentTimestamp ,
154
161
} ) ;
155
162
156
- map . getSource ( "data" ) . setData ( geojson ) ;
163
+ map . getSource ( "data" ) . setData ( mapData ) ;
157
164
158
165
dispatch ( {
159
166
type : AppActionTypes . UPDATE_VIEW_SUCCESS ,
160
- data : { geojson , timestamps } ,
167
+ data : { mapData } ,
161
168
} ) ;
162
169
} catch ( error ) {
170
+ // eslint-disable-next-line no-console
163
171
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." ) ;
167
173
}
168
174
} ,
169
175
} ;
0 commit comments