forked from buerokratt/Service-Module
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.tsx
More file actions
572 lines (517 loc) · 21.7 KB
/
index.tsx
File metadata and controls
572 lines (517 loc) · 21.7 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
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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
import * as Tabs from '@radix-ui/react-tabs';
import { Edge, getConnectedEdges, getIncomers, getOutgoers, Node } from '@xyflow/react';
import React, { useEffect, useMemo, useState } from 'react';
import { DndProvider } from 'react-dnd';
import { HTML5Backend } from 'react-dnd-html5-backend';
import { useTranslation } from 'react-i18next';
import useServiceStore, { extractMapValues, getEndpointBody } from 'store/new-services.store';
import useServiceListStore from 'store/services.store';
import useToastStore from 'store/toasts.store';
import { DynamicChoices } from 'types/dynamic-choices';
import { EndpointData } from 'types/endpoint';
import { MultiChoiceQuestionButton } from 'types/multi-choice-question';
import { NodeDataProps } from 'types/service-flow';
import { getValueByPath } from 'utils/object-util';
import { isTemplate, removeTrailingUnderscores, stringToTemplate, templateToString } from 'utils/string-util';
import { Button, Track } from '..';
import Popup from '../Popup';
import ApiContent from './ApiContent';
import AssignContent from './AssignContent';
import ConditionBuilderContent from './ConditionBuilderContent';
import ConditionContent from './ConditionContent';
import DefaultMessageContent from './DefaultMessageContent';
import DynamicChoicesContent from './DynamicChoicesContent';
import EndConversationContent from './EndConversationContent';
import FileGenerateContent from './FileGenerateContent';
import FileSignContent from './FileSignContent';
import JsonRequestContent from './JsonRequestContent';
import MultiChoiceQuestionContent from './MultiChoiceQuestionContent';
import OpenWebPageContent from './OpenWebPageContent';
import OpenWebPageTestContent from './OpenWebPageTestContent';
import RasaRulesContent from './RasaRulesContent';
import TextfieldContent from './TextfieldContent';
import TextfieldTestContent from './TextfieldTestContent';
import { servicesRequestsExplain } from '../../resources/api-constants';
import api from '../../services/api-dev';
import { StepType } from '../../types';
import { getInitialGroup, GroupOrRule } from './RuleBuilder/types';
import './styles.scss';
const FlowElementsPopup: React.FC = () => {
const { t } = useTranslation();
const [selectedTab, setSelectedTab] = useState<string | null>(null);
const [isSaveEnabled, setIsSaveEnabled] = useState(true);
const { isJsonRequestVisible, jsonRequestContent, setJsonRequestVisible, setJsonRequestContent } = useServiceStore();
const node = useServiceStore((state) => state.selectedNode);
const selectedService = useServiceListStore((state) => state.selectedService);
const instance = useServiceStore.getState().reactFlowInstance;
const isUserDefinedNode = node?.data?.stepType === StepType.UserDefined;
const serviceName = useServiceStore((state) => removeTrailingUnderscores(state.serviceNameDashed()));
const rules = useServiceStore((state) => state.rules);
const assignElements = useServiceStore((state) => state.assignElements);
const endpointsVariables = useServiceStore((state) => state.endpointsResponseVariables);
const stepType = node?.data.stepType;
const defaultMultiChoiceQuestionButtons = useMemo(
() => [
{
id: '1',
title: 'Jah',
payload: `#service, /${selectedService?.type ?? 'POST'}/services/active/${serviceName}_mcq_${
node?.data.label[node?.data.label.length - 1]
}_0`,
},
{
id: '2',
title: 'Ei',
payload: `#service, /${selectedService?.type ?? 'POST'}/services/active/${serviceName}_mcq_${
node?.data.label[node?.data.label.length - 1]
}_1`,
},
],
[selectedService?.type, serviceName, node?.data.label],
);
const defaultDynamicChoices: DynamicChoices = useMemo(
() => ({
list: '',
serviceName: '',
key: '',
payloadKeys: '',
}),
[],
);
// StepType.Textfield
const [textfieldMessage, setTextfieldMessage] = useState<string | null>(null);
const [textfieldMessagePlaceholders, setTextfieldMessagePlaceholders] = useState<{ [key: string]: string }>({});
// StepType.OpenWebpage
const [webpageName, setWebpageName] = useState<string | null>(null);
const [webpageUrl, setWebpageUrl] = useState<string | null>(null);
// StepType.FileGenerate
const [fileName, setFileName] = useState<string | null>(null);
const [fileContent, setFileContent] = useState<string | null>(null);
// StepType.FileSign
const [signOption, setSignOption] = useState<{ label: string; value: string } | null>(node?.data.signOption ?? null);
// StepType.MultiChoiceQuestion
const [multiChoiceQuestionQuestion, setMultiChoiceQuestionQuestion] = useState<string>(
node?.data.multiChoiceQuestion?.question ?? '',
);
const [multiChoiceQuestionButtons, setMultiChoiceQuestionButtons] = useState<MultiChoiceQuestionButton[]>(
node?.data.multiChoiceQuestion?.buttons ?? defaultMultiChoiceQuestionButtons,
);
const [dynamicChoices, setDynamicChoices] = useState<DynamicChoices>(
node?.data.dynamicChoices ?? defaultDynamicChoices,
);
const [nodeEndpoint, setNodeEndpoint] = useState<EndpointData | undefined>(node?.data.endpoint);
const [title, setTitle] = useState(node?.data.label ?? '');
const [titleError, setTitleError] = useState<string | undefined>(undefined);
useEffect(() => {
if (!node) return;
setTitle(node.data.label ?? '');
switch (stepType) {
case StepType.Input:
case StepType.Condition:
if (node.data?.rules && Array.isArray(node.data.rules.children)) {
useServiceStore.getState().changeRulesNode(node.data.rules.children);
} else {
useServiceStore.getState().changeRulesNode([]);
}
break;
case StepType.Assign:
if (node.data?.assignElements) {
useServiceStore.getState().changeAssignNode(node.data.assignElements);
} else {
useServiceStore.getState().changeAssignNode([]);
}
break;
case StepType.MultiChoiceQuestion:
setMultiChoiceQuestionQuestion(node.data?.multiChoiceQuestion?.question ?? '');
setMultiChoiceQuestionButtons(node.data?.multiChoiceQuestion?.buttons ?? defaultMultiChoiceQuestionButtons);
break;
case StepType.DynamicChoices:
setDynamicChoices(node.data?.dynamicChoices ?? defaultDynamicChoices);
break;
default:
break;
}
}, [defaultDynamicChoices, defaultMultiChoiceQuestionButtons, node, stepType]);
if (!node) return <></>;
const isReadonly = node.data.readonly;
const onClose = () => {
setSelectedTab(null);
setJsonRequestVisible(false);
setJsonRequestContent(null);
setTextfieldMessage(null);
setWebpageName(null);
setWebpageUrl(null);
setFileName(null);
setFileContent(null);
setTextfieldMessagePlaceholders({});
setMultiChoiceQuestionQuestion('');
setMultiChoiceQuestionButtons(defaultMultiChoiceQuestionButtons);
setIsSaveEnabled(true);
setDynamicChoices(defaultDynamicChoices);
useServiceStore.getState().resetSelectedNode();
useServiceStore.getState().resetRules();
useServiceStore.getState().resetAssign();
};
const handleSaveClick = () => {
const updatedNode: Node<NodeDataProps> = {
...node,
data: {
...node.data,
label: title,
message: textfieldMessage ?? node.data?.message,
link: webpageUrl ?? node.data?.link,
linkText: webpageName ?? node.data?.linkText,
fileName: fileName ?? node.data?.fileName,
fileContent: fileContent ?? node.data?.fileContent,
signOption: signOption ?? node.data?.signOption,
multiChoiceQuestion:
node.data.stepType === StepType.MultiChoiceQuestion
? {
question: multiChoiceQuestionQuestion,
buttons: multiChoiceQuestionButtons,
}
: undefined,
dynamicChoices: node.data.stepType === StepType.DynamicChoices ? dynamicChoices : undefined,
endpoint: nodeEndpoint ?? node.data?.endpoint,
testingPassed: undefined,
},
};
if (stepType === StepType.Input || stepType === StepType.Condition) {
prepareRulesForSaving(updatedNode);
}
if (stepType === StepType.MultiChoiceQuestion) {
saveMultiChoicePopup(node, updatedNode);
}
if (stepType === StepType.UserDefined) {
prepareEndpointsForSaving(updatedNode);
}
if (stepType === StepType.Assign) {
prepareAssignForSaving(updatedNode);
}
useServiceStore.getState().handlePopupSave(updatedNode);
onClose();
};
const prepareAssignForSaving = (updatedNode: Node<NodeDataProps>) => {
const flatEndpointVariables = endpointsVariables.map((endpoint) => endpoint.chips).flat();
assignElements.forEach((element) => {
const key = removeTrailingUnderscores(element.key);
element.key = key;
// Convert simple values such as "some input" to simple string
if (!isTemplate(element.value)) {
element.value = stringToTemplate('"' + element.value + '"');
return;
}
const fullPath = templateToString(element.value);
const endpointVariable = flatEndpointVariables.find((variable) => fullPath.startsWith(String(variable.value)));
if (!endpointVariable) {
// Element is not an object so no data for ObjectTree
return;
}
const value = String(endpointVariable.value);
const remainingPath = fullPath.substring(
fullPath[value.length] === '['
? // Uses array notation, e.g. endpointVariable[1].something; needed for backwards compatibility
value.length
: // Uses object notation, e.g. endpointVariable.1.something
value.length + 1,
);
element.data = remainingPath ? getValueByPath(endpointVariable.data, remainingPath) : endpointVariable.data;
});
updatedNode.data.assignElements = assignElements;
};
const prepareEndpointsForSaving = (updatedNode: Node<NodeDataProps>) => {
filterOutEndpointsTrailingUnderscores(updatedNode);
const newLabel = updatedNode.data.label?.toString().split(' ');
if (updatedNode.data.endpoint?.name) {
newLabel[0] = updatedNode.data.endpoint?.name ?? node.data.label?.toString().split(' ')[0];
const nodeWithSameLabel = instance
?.getNodes()
.find((n) => n.data.label === newLabel.join(' ') && n.id !== updatedNode.id);
if (nodeWithSameLabel) {
useToastStore.getState().error({
title: t('newService.toast.elementNameAlreadyExists'),
message: t('newService.toast.elementNameAlreadyExistsMessage'),
});
return;
}
updatedNode.data.label = newLabel.join(' ');
}
useServiceStore.getState().loadEndpointsResponseVariables();
};
const filterOutEndpointsTrailingUnderscores = (updatedNode: Node<NodeDataProps>) => {
if (updatedNode.data?.endpoint?.definitions) {
for (const definition of updatedNode.data.endpoint.definitions) {
for (const section of ['body', 'headers', 'params'] as const) {
if (definition[section]?.variables) {
for (const v of definition[section].variables) {
v.name = removeTrailingUnderscores(v.name);
}
}
}
}
}
};
const prepareRulesForSaving = (updatedNode: Node<NodeDataProps>) => {
const rulesArray = Array.isArray(rules) ? rules : [];
for (const item of rulesArray) {
const processRuleField = (obj: GroupOrRule) => {
if ('field' in obj) obj.field = removeTrailingUnderscores(obj.field);
else {
for (const child of obj.children) {
processRuleField(child);
}
}
};
processRuleField(item);
}
updatedNode.data.rules = node.data.rules
? { ...node.data.rules, children: rulesArray }
: { ...getInitialGroup(), children: rulesArray };
};
const handleJsonRequestClick = async () => {
if (isJsonRequestVisible) {
setJsonRequestVisible(false);
return;
}
try {
const endpoint = node.data.endpoint?.definitions[0];
if (!endpoint) return;
const response = await api.post(servicesRequestsExplain(), {
requests: [
{
url: endpoint.url,
method: endpoint.methodType,
headers: extractMapValues(endpoint.headers),
body: getEndpointBody(endpoint),
params: extractMapValues(endpoint.params),
},
],
});
setJsonRequestContent(response.data.response);
setJsonRequestVisible(true);
} catch (error) {
console.error('Error: ', error);
}
};
const getJsonRequestButtonTitle = () => {
if (!isUserDefinedNode || selectedTab === t('serviceFlow.tabs.test')) return '';
if (isJsonRequestVisible) return t('serviceFlow.popup.hideJsonRequest');
return t('serviceFlow.popup.showJsonRequest');
};
const saveMultiChoicePopup = (originalNode: Node<NodeDataProps>, updatedNode: Node<NodeDataProps>) => {
if (!instance) return;
const currentButtons = originalNode.data.multiChoiceQuestion?.buttons ?? defaultMultiChoiceQuestionButtons;
const newButtons = updatedNode.data.multiChoiceQuestion?.buttons ?? [];
const edges = instance.getEdges();
const nodes = instance.getNodes();
const connectedEdges = getConnectedEdges([originalNode], edges);
const currentButtonMap = new Map(currentButtons.map((btn) => [btn.id, btn]));
const newButtonMap = new Map(newButtons.map((btn) => [btn.id, btn]));
const addedButtons = newButtons.filter((btn) => !currentButtonMap.has(btn.id));
const removedButtons = currentButtons.filter((btn) => !newButtonMap.has(btn.id));
const renamedButtons = currentButtons
.filter((btn) => newButtonMap.has(btn.id))
.filter((btn) => newButtonMap.get(btn.id)!.title !== btn.title)
.map((btn) => ({
oldTitle: btn.title,
newTitle: newButtonMap.get(btn.id)!.title,
id: btn.id,
}));
const updatedEdges = edges.map((edge) => {
if (!edge.label || !connectedEdges.some((ce) => ce.id === edge.id)) return edge;
const rename = renamedButtons.find((r) => r.oldTitle === edge.label);
if (rename) {
return { ...edge, label: rename.newTitle };
}
return edge;
});
const edgesToRemove = connectedEdges.filter((edge) => {
if (!edge.label || edge.source !== originalNode.id) return false;
return removedButtons.some((btn) => btn.title === edge.label);
});
const filteredEdges = updatedEdges.filter((e) => !edgesToRemove.some((edgeToRemove) => edgeToRemove.id === e.id));
const existingButtonTitles = new Set(
filteredEdges
.filter((edge) => edge.source === originalNode.id)
.map((edge) => edge.label)
.filter(Boolean),
);
const buttonsNeedingEdges = addedButtons.filter((btn) => !existingButtonTitles.has(btn.title));
const newEdges = buttonsNeedingEdges.map((button) => {
const newEdge: Edge = {
id: `${originalNode.id}->${button.id}`,
source: originalNode.id,
target: `ghost-${button.id}`,
type: 'step',
animated: true,
deletable: false,
label: button.title,
};
return newEdge;
});
const newGhostNodes = newEdges.map((edge) => ({
id: edge.target,
type: 'ghost',
position: {
x: originalNode.position.x + 200,
y: originalNode.position.y,
},
data: { type: 'ghost' },
className: 'ghost',
selectable: false,
draggable: false,
}));
let finalNodes = [...nodes.filter((n) => n.id !== updatedNode.id), updatedNode, ...newGhostNodes];
let finalEdges = [...filteredEdges, ...newEdges];
finalNodes = finalNodes.filter(
(node) =>
node.type !== 'ghost' ||
getIncomers(node, finalNodes, finalEdges).length > 0 ||
getOutgoers(node, finalNodes, finalEdges).length > 0,
);
instance.setNodes(finalNodes);
instance.setEdges(finalEdges);
};
return (
<Popup
style={{ maxWidth: 700 }}
title={title}
onClose={onClose}
footer={
<Track direction="horizontal" gap={16} justify="between" style={{ width: '100%' }}>
<Button appearance="text" onClick={handleJsonRequestClick}>
{getJsonRequestButtonTitle()}
</Button>
<Track gap={16}>
{!isReadonly && (
<Button appearance="secondary" onClick={onClose}>
{t('global.cancel')}
</Button>
)}
<Button disabled={!isSaveEnabled} onClick={handleSaveClick}>
{t(isReadonly ? 'global.close' : 'global.save')}
</Button>
</Track>
</Track>
}
titleEditable
onTitleChange={(newTitle) => {
const nodeWithSameLabel = instance?.getNodes().find((n) => n.data.label === newTitle && n.id !== node.id);
setTitleError(nodeWithSameLabel ? t('newService.toast.elementNameAlreadyExists').toString() : undefined);
}}
onTitleSave={setTitle}
onTitleEditCancel={() => {
setTitleError(undefined);
setTitle(node.data.label ?? '');
}}
titleError={titleError}
>
<Track direction="vertical" align="stretch" gap={16} className="flow-body-reverse-margin">
<Tabs.Root
className="vertical-tabs__column"
orientation="horizontal"
value={selectedTab ?? t('serviceFlow.tabs.setup')!}
onValueChange={setSelectedTab}
>
<Tabs.List>
<Tabs.Trigger className="vertical-tabs__trigger" value={t('serviceFlow.tabs.setup')}>
{t('serviceFlow.tabs.setup')}
</Tabs.Trigger>
{!isReadonly && (
<Tabs.Trigger className="vertical-tabs__trigger" value={t('serviceFlow.tabs.test')}>
{t('serviceFlow.tabs.test')}
</Tabs.Trigger>
)}
</Tabs.List>
<Tabs.Content value={t('serviceFlow.tabs.setup')} className="vertical-tabs__body">
{stepType === StepType.Textfield && (
<TextfieldContent
defaultMessage={node.data.message ?? textfieldMessage ?? undefined}
node={node}
onChange={(message, placeholders) => {
setTextfieldMessage(message);
setTextfieldMessagePlaceholders(placeholders);
}}
/>
)}
{stepType === StepType.OpenWebpage && (
<OpenWebPageContent
onWebpageNameChange={setWebpageName}
onWebpageUrlChange={setWebpageUrl}
defaultWebpageUrl={node.data.link ?? webpageUrl ?? undefined}
defaultWebpageName={node.data.linkText ?? webpageName ?? undefined}
/>
)}
{stepType === StepType.Input && (
<DndProvider backend={HTML5Backend}>
<ConditionBuilderContent />
</DndProvider>
)}
{stepType === StepType.FileGenerate && (
<DndProvider backend={HTML5Backend}>
<FileGenerateContent
onFileNameChange={setFileName}
onFileContentChange={setFileContent}
defaultFileName={node?.data?.fileName ?? fileName ?? undefined}
defaultFileContent={node?.data?.fileContent ?? fileContent ?? undefined}
/>
</DndProvider>
)}
{stepType === StepType.FinishingStepRedirect && (
<DefaultMessageContent message={t('serviceFlow.popup.redirectToCustomerSupport')} />
)}
{stepType === StepType.Auth && <DefaultMessageContent message={t('serviceFlow.popup.loginWithTARA')} />}
{stepType === StepType.FileSign && (
<FileSignContent onOptionChange={setSignOption} signOption={signOption} />
)}
{stepType === StepType.FinishingStepEnd && <EndConversationContent />}
{stepType === StepType.RasaRules && <RasaRulesContent />}
{stepType === StepType.Assign && <AssignContent node={node} />}
{stepType === StepType.Condition && <ConditionContent node={node} />}
{stepType === StepType.DynamicChoices && (
<DynamicChoicesContent
node={node}
dynamicChoices={dynamicChoices}
onDynamicChoicesChange={setDynamicChoices}
/>
)}
{stepType === StepType.UserDefined && (
<ApiContent
node={node}
endpoint={node.data.endpoint}
onEndpointChange={(endpoint) => {
if (!endpoint) return;
setNodeEndpoint(endpoint);
}}
/>
)}
{stepType === StepType.MultiChoiceQuestion && (
<MultiChoiceQuestionContent
question={multiChoiceQuestionQuestion}
buttons={multiChoiceQuestionButtons}
setQuestion={setMultiChoiceQuestionQuestion}
setButtons={setMultiChoiceQuestionButtons}
setIsSaveEnabled={setIsSaveEnabled}
/>
)}
<JsonRequestContent isVisible={isJsonRequestVisible} jsonContent={jsonRequestContent} />
</Tabs.Content>
{!isReadonly && (
<Tabs.Content value={t('serviceFlow.tabs.test')} className="vertical-tabs__body">
{stepType === StepType.Textfield && (
<TextfieldTestContent
placeholders={textfieldMessagePlaceholders}
message={textfieldMessage ?? node.data.message}
/>
)}
{stepType === StepType.OpenWebpage && (
<OpenWebPageTestContent websiteUrl={webpageUrl} websiteName={webpageName} />
)}
</Tabs.Content>
)}
</Tabs.Root>
</Track>
</Popup>
);
};
export default FlowElementsPopup;