-
Notifications
You must be signed in to change notification settings - Fork 1
/
EditCard.js
350 lines (330 loc) · 10.7 KB
/
EditCard.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
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
import React from "react";
import PropTypes from "prop-types";
import {
Card,
Typography,
IconButton,
MenuItem,
FormControl,
Select,
Button,
Tooltip,
} from "@material-ui/core";
import SendIcon from "@material-ui/icons/Send";
import MapIcon from "@material-ui/icons/Map";
import LinkIcon from "@material-ui/icons/Link";
import LinkOffIcon from "@material-ui/icons/LinkOff";
import RequiredNonRequiredSelector from "./RequiredNonRequiredSelector";
import ConstantDialog from "./ConstantDialog";
import SendButtonTooltip from "./SendButtonTooltip";
import api from "../services/api";
import config from "../../config.json";
import { stylesObj } from "../styling/stylesObj";
const getValueMapButtonStyle = (questionnaireItem, mapItem) => {
if (!(questionnaireItem.header || "").length) {
return stylesObj.editCardSelectorButtonDisabled;
}
const tempChoiceMap = mapItem.choiceMap || {};
if (Object.entries(tempChoiceMap).length) {
return stylesObj.editCardSelectorButtonComplete;
}
return stylesObj.editCardSelectorButtonIncomplete;
};
const formatMenuItems = (currentMap) =>
Object.keys(currentMap.headers).map((k) => (
<MenuItem value={k}>{k}</MenuItem>
));
const formatSelect = (header, key, map, associationFunction) => (
<FormControl style={stylesObj.editCardSelector}>
<Select
value={header || ""}
onChange={associationFunction}
name={key}
data_cy={`${key}_selectItem`}
>
{formatMenuItems(map)}
</Select>
</FormControl>
);
const formatQuestions = (
mapCheck,
map,
associationFunction,
valueMapFunction,
constantChange,
setConstantDialogOpen,
itemVisibility
) =>
Object.keys(mapCheck.flatQuestionnaire).map((k) => {
// let mappedToConstant = !!((Object.keys(mapCheck.flatQuestionnaire[k].constant) || '').length);
console.log(mapCheck.flatQuestionnaire)
const mappedToConstant = !!Object.keys(
mapCheck.flatQuestionnaire[k].constant || {}
).length;
const mappedToHeader = !!(mapCheck.flatQuestionnaire[k].header || "")
.length;
const mappedItem = mappedToConstant || mappedToHeader;
return (
<>
{(itemVisibility === "all" ||
mapCheck.flatQuestionnaire[k].required) && (
<div key={`question-${k}`} style={stylesObj.editCardSelectorPadding}>
<Typography
wrap="noWrap"
style={
mappedItem
? stylesObj.completeQuestion
: stylesObj.incompleteQuestion
}
>
<Tooltip arrow title={
<React.Fragment>
<p><b>Name:</b> {mapCheck.flatQuestionnaire[k].text}</p>
<p><b>Value Type:</b> {mapCheck.flatQuestionnaire[k].valueType}</p>
<p><b>Required:</b> {mapCheck.flatQuestionnaire[k].required ? "True" : "False"}</p>
<p><b>Mapped:</b> {mapCheck.flatQuestionnaire[k].header}</p>
</React.Fragment>
}>
<strong>
{mapCheck.flatQuestionnaire[k].required && "* "}
{mapCheck.flatQuestionnaire[k].text}
</strong>
</Tooltip>
{!mappedToConstant && (
<Tooltip title="Replace this item with a constant value">
<IconButton
onClick={() => {
let tempValueMap = [];
if (
mapCheck.flatQuestionnaire[k].valueType === "choice"
) {
tempValueMap =
mapCheck.flatQuestionnaire[k].answerValueSet;
}
setConstantDialogOpen(
mapCheck.flatQuestionnaire[k].text,
k,
tempValueMap,
mapCheck.flatQuestionnaire[k].valueType,
mapCheck.flatQuestionnaire[k].path
);
}}
>
<LinkIcon />
</IconButton>
</Tooltip>
)}
</Typography>
{!mappedToConstant && (
<div>
{formatSelect(
mapCheck.flatQuestionnaire[k].header,
k,
map,
associationFunction
)}
<br />
{mapCheck.flatQuestionnaire[k].valueType === "choice" && (
<Button
variant="contained"
style={getValueMapButtonStyle(
mapCheck.flatQuestionnaire[k],
map.headers[mapCheck.flatQuestionnaire[k].header] || {}
)}
onClick={() => {
valueMapFunction(mapCheck.flatQuestionnaire[k].header, k);
}}
disabled={
!(mapCheck.flatQuestionnaire[k].header || "").length
}
>
Map values
<MapIcon style={stylesObj.editCardSelectorButtonIcon} />
</Button>
)}
</div>
)}
{mappedToConstant && (
<Typography wrap="noWrap">
<span>
{`Constant value: ${mapCheck.flatQuestionnaire[k].constant.display}`}
</span>
<Tooltip title="Remove link to constant value and map to header">
<IconButton
onClick={() => {
constantChange(k, "delete");
}}
>
<LinkOffIcon />
</IconButton>
</Tooltip>
</Typography>
)}
</div>
)}
</>
);
});
class EditCard extends React.Component {
constructor(props) {
super(props);
this.state = {
buttonDelay: false,
constantDialogOpen: false,
constantHeader: "",
qID: "",
valueType: "",
headerPath: [],
itemVisibility: "all",
};
this.setConstantDialogOpen = this.setConstantDialogOpen.bind(this);
this.handleVisibilityChange = this.handleVisibilityChange.bind(this);
this.sendMap = this.sendMap.bind(this);
}
handleVisibilityChange(text) {
this.setState({ itemVisibility: text });
}
setConstantDialogOpen(ch, qID, vm, vt, path) {
const { constantDialogOpen } = this.state;
const tempConstantHeader = typeof ch === "string" ? ch : "";
const tempQID = typeof qID === "string" ? qID : "";
const tempValueMap = Array.isArray(vm) ? vm : [];
const tempValueType = typeof vt === "string" ? vt : "";
const tempPath = Array.isArray(path) ? path : [];
this.setState({
constantDialogOpen: !constantDialogOpen,
constantHeader: tempConstantHeader,
qID: tempQID,
valueMap: tempValueMap,
valueType: tempValueType,
headerPath: tempPath,
});
}
sendMap() {
// disable for testing
const { map } = this.props;
api.sendMap(map).then((resp) => {
console.log(resp);
});
this.setState({
buttonDelay: true,
submittedMap: JSON.parse(JSON.stringify(map)),
});
setTimeout(() => {
this.setState({ buttonDelay: false });
}, 10000);
}
render() {
const {
constantChange,
mapCheck,
map,
mapValidity,
onAssociation,
onValueMap,
unmappedHeaders,
} = this.props;
const {
buttonDelay,
constantDialogOpen,
constantHeader,
qID,
valueMap,
valueType,
headerPath,
itemVisibility,
submittedMap,
} = this.state;
const mapUnchanged = JSON.stringify(submittedMap) === JSON.stringify(map);
const buttonDisabled =
(Object.keys(unmappedHeaders).length > 0 && map.fileType !== "json") ||
!mapValidity ||
buttonDelay ||
mapUnchanged;
const buttonUploadStyling = buttonDisabled
? stylesObj.editCardUploadButtonDisabled
: stylesObj.editCardUploadButtonEnabled;
return (
<Card style={stylesObj.editCard}>
<div style={stylesObj.themePadding}>
<Typography variant="h6" style={stylesObj.marginQuarterBottom}>
<strong>Map Source Headers to Target Questions</strong>
</Typography>
<Typography variant="body1" style={stylesObj.marginQuarterBottom}>
<strong>* </strong>
Denotes Required Item
</Typography>
<RequiredNonRequiredSelector
itemVisibility={itemVisibility}
handleVisibilityChange={this.handleVisibilityChange}
/>
<div style={stylesObj.themePaddingQuarter}>
{mapCheck && map && (
<div>
{formatQuestions(
mapCheck,
map.map,
onAssociation,
onValueMap,
constantChange,
this.setConstantDialogOpen,
itemVisibility
)
}
</div>
)}
</div>
</div>
<Tooltip
title={
!buttonDisabled ? (
""
) : (
<SendButtonTooltip
mapUnchanged={mapUnchanged}
tempDelay={buttonDelay}
unmappedHeaders={unmappedHeaders}
flatQuestionnaire={mapCheck.flatQuestionnaire}
/>
)
}
>
<div style={stylesObj.editCardUploadButtonDiv}>
<Button
variant="contained"
style={buttonUploadStyling}
onClick={this.sendMap}
disabled={buttonDisabled}
>
Submit to
{config.externalMappingLocation}
<SendIcon style={stylesObj.marginQuarter} />
</Button>
</div>
</Tooltip>
{constantDialogOpen && (
<ConstantDialog
open={constantDialogOpen}
closeConstantMapDialog={this.setConstantDialogOpen}
constantHeader={constantHeader}
qID={qID}
setConstant={constantChange}
valueArray={valueMap}
valueType={valueType}
path={headerPath}
/>
)}
</Card>
);
}
}
EditCard.propTypes = {
constantChange: PropTypes.func.isRequired,
mapCheck: PropTypes.objectOf(PropTypes.object).isRequired,
map: PropTypes.objectOf(PropTypes.object).isRequired,
mapValidity: PropTypes.bool.isRequired,
onAssociation: PropTypes.func.isRequired,
onValueMap: PropTypes.func.isRequired,
unmappedHeaders: PropTypes.objectOf(PropTypes.object).isRequired,
};
export default EditCard;