-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathnode.reducer.ts
More file actions
198 lines (188 loc) · 6.48 KB
/
node.reducer.ts
File metadata and controls
198 lines (188 loc) · 6.48 KB
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/*
* Copyright (c) 2025 Ping Identity Corporation. All rights reserved.
*
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
*/
/**
* Import the required utilities from Redux Toolkit
*/
import { createAction, createReducer } from '@reduxjs/toolkit';
/**
* Import the collector utilities
*/
import {
returnActionCollector,
returnFlowCollector,
returnPasswordCollector,
returnSingleValueCollector,
returnIdpCollector,
returnSubmitCollector,
returnTextCollector,
returnSingleSelectCollector,
returnMultiSelectCollector,
returnReadOnlyCollector,
} from './collector.utils.js';
import type { DaVinciField } from './davinci.types.js';
import {
ActionCollector,
MultiSelectCollector,
SingleSelectCollector,
FlowCollector,
PasswordCollector,
SingleValueCollector,
IdpCollector,
SubmitCollector,
TextCollector,
ReadOnlyCollector,
ValidatedTextCollector,
} from './collector.types.js';
/**
* @const nextCollectorValues - Action for setting the next collector values
* @see https://redux-toolkit.js.org/api/createAction
*
* This is for internal "collector" setup for handling the state of the current node
*/
export const nextCollectorValues = createAction<{
fields: DaVinciField[];
formData: { value: Record<string, unknown> };
}>('node/next');
export const updateCollectorValues = createAction<{
id: string;
value: string | string[];
index?: number;
}>('node/update');
/**
* @const initialCollectorValues - Initial state for the collector values
*/
const initialCollectorValues: (
| FlowCollector
| PasswordCollector
| TextCollector
| IdpCollector
| SubmitCollector
| ActionCollector<'ActionCollector'>
| SingleValueCollector<'SingleValueCollector'>
| SingleSelectCollector
| MultiSelectCollector
| ReadOnlyCollector
| ValidatedTextCollector
)[] = [];
/**
* @const nodeCollectorReducer - Reducer for handling the collector values
* @see https://redux-toolkit.js.org/api/createReducer
*/
export const nodeCollectorReducer = createReducer(initialCollectorValues, (builder) => {
builder
/**
* Using the `nextCollectorValues` const (e.g. `'node/next'`) to add the case
* 'node/next' is essentially derived `createSlice` below. `node.next()` is
* transformed to `'node/next'` for the action type.
*/
.addCase(nextCollectorValues, (_, action) => {
const fields = action.payload.fields;
// Map the fields to the initial state with the schema of Generic Collector
const collectors = Array.isArray(fields)
? fields.map((field: DaVinciField, idx: number) => {
/**
* Some collectors may not have the same properties as others;
* LABEL field types are one of them, so let's catch them first.
*/
if (field.type === 'LABEL') {
return returnReadOnlyCollector(field, idx);
}
// *Some* collectors may have default or existing data to display
const data =
action.payload.formData &&
action.payload.formData.value &&
action.payload.formData.value[field.key];
// Match specific collectors
switch (field.type) {
case 'CHECKBOX':
case 'COMBOBOX': {
// Intentional fall-through
const strArr = data as string[];
return returnMultiSelectCollector(field, idx, strArr);
}
case 'DROPDOWN':
case 'RADIO': {
// Intentional fall-through
const str = data as string;
return returnSingleSelectCollector(field, idx, str);
}
case 'FLOW_BUTTON':
case 'FLOW_LINK': {
// Intentional fall-through
// No data to send
return returnFlowCollector(field, idx);
}
case 'PASSWORD':
case 'PASSWORD_VERIFY': {
// No data to send
return returnPasswordCollector(field, idx);
}
case 'TEXT': {
const str = data as string;
return returnTextCollector(field, idx, str);
}
case 'SOCIAL_LOGIN_BUTTON': {
// No data to send
return returnIdpCollector(field, idx);
}
case 'SUBMIT_BUTTON': {
// No data to send
return returnSubmitCollector(field, idx);
}
default:
// Default is handled below
}
// Generic Collectors
if (field.type.includes('BUTTON') || field.type.includes('LINK')) {
// No data to send
return returnActionCollector(field, idx, 'ActionCollector');
}
const str = data as string;
return returnSingleValueCollector(field, idx, 'SingleValueCollector', str);
})
: [];
return collectors || [];
})
/**
* Using the `updateCollectorValues` const (e.g. `'node/update'`) to add the case
* 'node/next' is essentially derived `createSlice` below. `node.next()` is
* transformed to `'node/next'` for the action type.
*/
.addCase(updateCollectorValues, (state, action) => {
const collector = state.find((collector) => collector.id === action.payload.id);
if (!collector) {
throw new Error('No collector found to update');
}
if (collector.category === 'ActionCollector') {
throw new Error('ActionCollectors are read-only');
}
if (collector.category === 'NoValueCollector') {
throw new Error('NoValueCollectors, like ReadOnlyCollectors, are read-only');
}
if (action.payload.value === undefined) {
throw new Error('Value argument cannot be undefined');
}
if (
collector.category === 'SingleValueCollector' ||
collector.category === 'ValidatedSingleValueCollector'
) {
if (Array.isArray(action.payload.value)) {
throw new Error('SingleValueCollector does not accept an array');
}
collector.input.value = action.payload.value;
return;
}
if (collector.category === 'MultiValueCollector') {
if (Array.isArray(action.payload.value)) {
collector.input.value = [...action.payload.value];
} else {
collector.input.value.push(action.payload.value);
}
return;
}
});
});