This repository has been archived by the owner on Aug 13, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ValidationTestWidget.tsx
269 lines (246 loc) · 8.92 KB
/
ValidationTestWidget.tsx
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
/*---------------------------------------------------------------------------------------------
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/
import { Modal, Button, Input, Table, ProgressRadial } from "@itwin/itwinui-react";
import React, { useCallback, useMemo } from "react";
import ValidationLink from "../../ValidationLink";
import { ValidationRuleTable } from "./ValidationRuleTable";
import { formatRelative } from "date-fns";
import { NewRuleComponent } from "./NewRuleComponent";
import { ResponseFromGetResult, RuleDetails } from "@itwin/property-validation-client";
import "./ui.css";
import { useSetValidationState } from "../../StateProvider";
// Widget for creating and running tests. Also presents modal for creating new rules.
export function ValidationTestWidget() {
// table data containing list of tests and runs.
const [testData, setTestData] = React.useState<any[]>([]);
// table data containing list of rules.
const [ruleList, setRuleList] = React.useState<RuleDetails[]>([]);
// test name when creating new test.
const [testName, setTestName] = React.useState<string>("");
// list of currently selected rules for creating new test.
const [selectedRuleIds, setSelectedRuleIds] = React.useState<string[]>([]);
// state variable for displaying "New Test" modal.
const [showNewTestModal, setShowNewTestModal] = React.useState<boolean>(false);
// state variable for displaying "New Rule" modal.
const [addNewRule, setAddNewRule] = React.useState<boolean>(false);
// app context callback for setting result.
const setResultData = useSetValidationState();
// columns for Test table component.
const columns = useMemo(
() => [
{
Header: "Tests",
columns: [
{
// "displayName" column to show display name of test/run.
id: "displayName",
Header: "",
accessor: "displayName",
},
{
// "action" column to add click actions next to test/run.
id: "action",
Header: "",
accessor: "action",
maxWidth: 75,
},
],
},
],
[]
);
// Component initialization. Fetch latest validation data.
React.useEffect(() => {
fetchValidationTests().catch(console.error);
fetchValidationRules().catch(console.error);
}, []);
// Method to fetch latest test (and run) data...and prepare it for table component.
const fetchValidationTests = async () => {
// fetch list of tests and runs using validation API.
const testData: any[] = await ValidationLink.getTests();
const runs: any[] = await ValidationLink.getRuns();
// sort tests by date.
testData.sort((a: any, b: any) => {
return Date.parse(b.creationDateTime) - Date.parse(a.creationDateTime);
});
// sort runs by date.
runs.sort((a: any, b: any) => {
return Date.parse(b.executedDateTime) - Date.parse(a.executedDateTime);
});
// maintain a list of runs that are not yet "completed".
const pendingRuns = [];
for (const run of runs) {
for (const index in testData) {
if ((run._links.test.href as string).includes(testData[index].id)) {
if (!testData[index].subRows) testData[index].subRows = [];
// add each run under the test it belongs to (in the form of subrow property: to create expandable subrows within table).
testData[index].subRows.push(run);
if (run.status !== "completed")
// if a run is not complete, add to pendingRun list.
pendingRuns.push(testData[index].id);
}
}
// for the "displayName" of the run use execution date and time.
run.displayName = formatRelative(
Date.parse(run.executedDateTime),
Date.now()
);
if (run.status === "completed")
// if run completed, add View button as the "action" column entry (to view its results).
run.action = (
<Button
size="small"
onClick={(_event) => {
updateValidationResults(run.resultId);
}}
>
View
</Button>
);
}
for (const test of testData) {
// If test contains pending run, add ProgressRadial as the "action" column entry to show progress.
if (pendingRuns.includes(test.id))
test.action = <ProgressRadial indeterminate size="small" />;
// Else add run "▶" button as the "action" column entry.
else
test.action = (
<Button
id={test.id}
size="small"
onClick={(_event) => {
// when the button is clicked, run validation test using API.
runValidationTest(test.id);
const button = document.getElementById(test.id);
// hide ▶ button after it's clicked (to avoid multiple runs).
if (button) button.style.display = "none";
}}
>
▶
</Button>
);
}
setTestData(testData);
// if any pendingRuns exist, re-run this method after 5 seconds to get latest data.
if (pendingRuns.length > 0) setTimeout(fetchValidationTests, 5000);
};
// Fetch validation results. Entry-point into ValidationResultsWidget
const updateValidationResults = useCallback(
async (resultId: string) => {
const resultData: ResponseFromGetResult = await ValidationLink.getResult(
resultId
);
// set resultData on app context.
if (setResultData) setResultData(resultData);
},
[setResultData]
);
// Run validation test, and fetch latest data for run status.
const runValidationTest = async (testId: string) => {
await ValidationLink.runTest(testId);
fetchValidationTests();
};
// Fetch validation rules and sort by creation date.
const fetchValidationRules = async () => {
const ruleData: RuleDetails[] = await ValidationLink.getRules();
const rules: RuleDetails[] = ruleData.filter(
(rule: RuleDetails) => rule.functionName === "PropertyValueRange"
);
rules.sort((a: RuleDetails, b: RuleDetails) => {
return Date.parse(b.creationDateTime) - Date.parse(a.creationDateTime);
});
setRuleList(rules);
};
// Create new test.
const createTest = async () => {
const test = await ValidationLink.createTest(
testName,
testName,
selectedRuleIds
);
if (test) {
// fetch latest test data if test is successfully created (to update table component).
fetchValidationTests();
// close out "New Test" modal.
setShowNewTestModal(false);
}
};
// New Rule added. Fetch latest rule data.
const ruleAdded = async () => {
fetchValidationRules();
setAddNewRule(false);
};
// If testData (which includes tests and runs) is ready, display table component.
const table = testData ? (
<Table
columns={columns}
data={testData}
emptyTableContent="No Tests"
isSelectable={false}
density="extra-condensed"
autoResetExpanded={false}
autoResetPage={false}
/>
) : (
"No Tests"
);
let modalContents, modalTitle;
// if addNewRule state variable is true, display contents to create "New Rule" modal.
if (addNewRule) {
modalTitle = "Create New Rule";
modalContents = (
<>
<NewRuleComponent ruleAdded={ruleAdded} />
</>
);
} else {
// else display contents to create "New Test" modal.
modalContents = (
<div className="new-test-modal">
<div style={{ display: "flex", gap: 8 }}>
<Input id="testName" onInput={(event) => {setTestName(event.currentTarget.value)}} placeholder="Test Name" />
</div>
{/* Includes ValidationRuleSelector component to select rule Ids */}
<ValidationRuleTable
ruleList={ruleList}
rulesSelected={(ruleIds: string[]) => setSelectedRuleIds(ruleIds)}
/>
<div style={{ display: "flex", gap: 8, justifyContent: "end" }}>
<Button onClick={() => setAddNewRule(true)}>Add Rule</Button>
<Button styleType="high-visibility" onClick={createTest}>
Create
</Button>
</div>
</div>
);
}
// Render output
return (
<>
{/* "Create Test" button */}
<Button
className="custom-button"
styleType="default"
size="small"
onClick={() => setShowNewTestModal(true)}
>
Create Test
</Button>
{/* Tests/runs table */}
{table}
{/* "New Test" or "New Rule" modal if needed. */}
<Modal
isOpen={showNewTestModal}
title={modalTitle}
style={!addNewRule ? { minWidth: "550px" } : {}}
onClose={() => {
addNewRule ? setAddNewRule(false) : setShowNewTestModal(false);
}}
>
{modalContents}
</Modal>
</>
);
}