forked from alyssazhan/ML4GIS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
historyHandler.js
60 lines (54 loc) · 1.51 KB
/
historyHandler.js
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
import type { MainLayoutState, Action } from "../MainLayout/types"
import { setIn, updateIn, asMutable, without } from "seamless-immutable"
import moment from "moment"
const typesToSaveWithHistory = {
BEGIN_BOX_TRANSFORM: "Transform/Move Box",
BEGIN_MOVE_POINT: "Move Point",
DELETE_REGION: "Delete Region",
BEGIN_CIRCLE_TRANSFORM: "Transform/Move Circle",
}
export const saveToHistory = (state: MainLayoutState, name: string) =>
updateIn(state, ["history"], (h) =>
[
{
time: moment().toDate(),
state: without(state, "history"),
name,
},
].concat((h || []).slice(0, 9))
)
export default (reducer) => {
return (state: MainLayoutState, action: Action) => {
const prevState = state
const nextState = reducer(state, action)
if (action.type === "RESTORE_HISTORY") {
if (state.history.length > 0) {
return setIn(
nextState.history[0].state,
["history"],
nextState.history.slice(1)
)
}
} else {
if (
prevState !== nextState &&
Object.keys(typesToSaveWithHistory).includes(action.type)
) {
return setIn(
nextState,
["history"],
[
{
time: moment().toDate(),
state: without(prevState, "history"),
name: typesToSaveWithHistory[action.type] || action.type,
},
]
.concat(nextState.history || [])
.slice(0, 9)
)
}
}
return nextState
}
}