forked from cartesapp/cartes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreducers.ts
161 lines (147 loc) · 3.8 KB
/
reducers.ts
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import { defaultTo, omit } from 'Components/utils/utils'
import reduceReducers from 'reduce-reducers'
import { combineReducers, Reducer } from 'redux'
type DottedName = string
function explainedVariable(
state: DottedName | null = null,
action: Action
): DottedName | null {
switch (action.type) {
case 'EXPLAIN_VARIABLE':
return action.variableName || null
default:
return state
}
}
type QuestionsKind =
| "à l'affiche"
| 'non prioritaires'
| 'liste'
| 'liste noire'
export type SimulationConfig = {
situation: Simulation['situation']
bloquant?: Array<DottedName>
questions?: Partial<Record<QuestionsKind, Array<DottedName>>>
branches?: Array<{ nom: string; situation: SimulationConfig['situation'] }>
'unité par défaut': string
}
type Situation = Partial<Record<DottedName, any>>
export type Simulation = {
config: SimulationConfig
url: string
hiddenNotifications: Array<string>
situation: Situation
initialSituation: Situation
foldedSteps: Array<DottedName>
unfoldedStep?: DottedName | null
messages: Object
}
function simulation(stateRaw = {}, action: Action) {
const state = stateRaw || {}
const { objectives } = action
if (!objectives) return state
const objective = objectives[0], //TODO limiting, should be serialized for multiple objective simulations
objectiveSimulation = state[objective] || {},
objectiveSituation = objectiveSimulation.situation || {}
console.log('ACTION', action)
/*
if (action.type === 'SET_MESSAGE_READ') {
return {
...state,
messages: { ...state.messages, [action.message]: true },
}
}
*/
switch (action.type) {
case 'HIDE_NOTIFICATION':
return {
...state,
[objective]: {
...objectiveSimulation,
hiddenNotifications: [
...(objectiveSimulation.hiddenNotifications || []),
action.id,
],
},
}
case 'RESET_SIMULATION':
return {
...state,
[objective]: {
hiddenNotifications: [],
situation: {},
messages: {},
},
}
case 'UPDATE_SITUATION': {
const { fieldName: dottedName, value, objectives } = action
const newSituation =
value === undefined
? omit([dottedName], objectiveSituation)
: {
...objectiveSituation,
[dottedName]: value,
}
return {
...state,
[objective]: { situation: newSituation },
}
}
}
return state
}
function rules(state = null, { type, rules }) {
if (type === 'SET_RULES') {
return rules
} else return state
}
function tutorials(state = {}, { type, id }) {
if (type === 'SKIP_TUTORIAL') {
return { ...state, [id]: 'skip' }
} else if (type === 'RESET_TUTORIALS') {
return {}
} else return state
}
function scenario(state = 'B', action) {
if (action.type === 'SET_SCENARIO') {
return action.scenario
} else return state
}
function batchUpdateSituationReducer(state: RootState, action: Action) {
if (action.type !== 'BATCH_UPDATE_SITUATION') {
return state
}
return Object.entries(action.situation).reduce<RootState | null>(
(newState, [fieldName, value, objectives]) => {
return mainReducer(newState ?? undefined, {
type: 'UPDATE_SITUATION',
fieldName,
value,
objectives,
})
},
state
)
}
function exemple(state = null, action) {
if (action.type !== 'SET_EXEMPLE') return state
return action.exemple
}
const mainReducer = (state: any, action: Action) =>
combineReducers({
explainedVariable,
// We need to access the `rules` in the simulation reducer
simulation: (a: Simulation | null = null, b: Action): Simulation | null =>
simulation(a, b),
previousSimulation: defaultTo(null),
rules,
iframeOptions: defaultTo(null),
tutorials,
scenario,
exemple,
})(state, action)
export default reduceReducers<RootState>(
mainReducer as any,
batchUpdateSituationReducer as Reducer<RootState>
) as Reducer<RootState>
export type RootState = ReturnType<typeof mainReducer>