/
map.tsx
147 lines (130 loc) · 3.71 KB
/
map.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import * as dotProp from "dot-prop-immutable"
import { IAnalyticsMeta } from "../../shared/analytics/GoogleAnalytics"
import { ITable, IGeomInfo } from "./ealgis";
import { IMap, ILayer } from "./maps";
// Actions
const TOGGLE_DEBUG_MODE = "ealgis/map/TOGGLE_DEBUG_MODE"
const SAVE_POSITION = "ealgis/map/SAVE_POSITION"
const RESTORE_DEFAULT_POSITION = "ealgis/map/RESTORE_DEFAULT_POSITION"
const LOAD_HIGHLIGHTED_FEATURES = "ealgis/map/LOAD_HIGHLIGHTED_FEATURES"
const RECEIVE_GOOGLE_PLACES_RESULT = "ealgis/map/RECEIVE_GOOGLE_PLACES_RESULT"
const initialState: IModule = {
debug: false,
position: {},
highlightedFeatures: [],
}
// Reducer
export default function reducer(state = initialState, action: IAction) {
switch (action.type) {
case TOGGLE_DEBUG_MODE:
return dotProp.toggle(state, "debug")
case SAVE_POSITION:
case RESTORE_DEFAULT_POSITION:
return dotProp.set(state, `position`, action.position)
case LOAD_HIGHLIGHTED_FEATURES:
return dotProp.set(state, "highlightedFeatures", action.featurGids)
default:
return state
}
}
// Action Creators
export function toggleDebugMode(): IAction {
return {
type: TOGGLE_DEBUG_MODE,
}
}
export function savePosition(position: IPosition): IAction {
return {
type: SAVE_POSITION,
position,
}
}
export function restoreDefaultPosition(position: IPosition): IAction {
return {
type: RESTORE_DEFAULT_POSITION,
position,
meta: {
analytics: {
category: "Map",
},
},
}
}
export function setHighlightedFeatures(featurGids: Array<number>): IAction {
return {
type: LOAD_HIGHLIGHTED_FEATURES,
featurGids,
}
}
export function receiveGooglePlacesResult(): IAction {
return {
type: RECEIVE_GOOGLE_PLACES_RESULT,
meta: {
analytics: {
category: "Map",
},
},
}
}
// Models
export interface IModule {
debug: boolean
position: IPosition
highlightedFeatures: Array<number>
}
export interface IAction {
type: string
position?: IPosition
featurGids?: Array<number>
meta?: {
analytics: IAnalyticsMeta
}
}
export interface IPosition {
center?: Array<number>
zoom?: number
resolution?: number
extent?: Array<number>
allowUpdate?: boolean
}
// Side effects, only as applicable
// e.g. thunks, epics, et cetera
export function restoreDefaultMapPosition(position: IPosition) {
return (dispatch: any) => {
dispatch(restoreDefaultPosition(Object.assign(position, { allowUpdate: true })))
}
}
export function moveToPosition(position: IPosition) {
return (dispatch: any) => {
dispatch(savePosition(Object.assign(position, { allowUpdate: true })))
}
}
export function moveToGooglePlacesResult(extent: Array<number>) {
return (dispatch: any) => {
dispatch(receiveGooglePlacesResult())
const position: IPosition = {
extent: extent,
zoom: 18,
allowUpdate: true,
}
dispatch(savePosition(position))
}
}
// Utilities
export function getMapTablesDescriptions(tables: Array<ITable>) {
return tables
.map((table: ITable, idx: number) => {
return `${table.metadata_json.type} (${table.metadata_json.kind})`
})
.join(", ")
}
export function getMapGeometryDescriptions(map: IMap, geominfo: IGeomInfo) {
return new Set(
map.json.layers.map((layer: ILayer, idx: number) => {
const ginfo = geominfo[`${layer.schema}.${layer.geometry}`]
return ginfo.description
})
)
.toJSON()
.join(", ")
}