-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathconsent_form.tsx
267 lines (237 loc) · 7.89 KB
/
consent_form.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
import { assert, Weak } from "../../../../helpers";
import {
PropsUITable,
PropsUITableBody,
PropsUITableCell,
PropsUITableHead,
PropsUITableRow,
} from "../../../../types/elements";
import {
PropsUIPromptConsentForm,
PropsUIPromptConsentFormTable,
} from "../../../../types/prompts";
import { Table } from "../elements/table";
import { LabelButton, PrimaryButton } from "../elements/button";
import { BodyLarge, Title4 } from "../elements/text";
import TextBundle from "../../../../text_bundle";
import { Translator } from "../../../../translator";
import { ReactFactoryContext } from "../../factory";
import React, { JSX } from "react";
import _ from "lodash";
type Props = Weak<PropsUIPromptConsentForm> & ReactFactoryContext;
interface TableContext {
title: string;
deletedRowCount: number;
}
export const ConsentForm = (props: Props): JSX.Element => {
const tablesIn = React.useRef<Array<PropsUITable & TableContext>>(
parseTables(props.tables)
);
const metaTables = React.useRef<Array<PropsUITable & TableContext>>(
parseTables(props.metaTables)
);
const tablesOut = React.useRef<Array<PropsUITable & TableContext>>(
tablesIn.current
);
const [waiting, setWaiting] = React.useState<boolean>(false);
const { locale, resolve } = props;
const cancelButton = Translator.translate(cancelButtonLabel, props.locale);
function rowCell(
dataFrame: any,
column: string,
row: number
): PropsUITableCell {
const text = String(dataFrame[column][`${row}`]);
return { __type__: "PropsUITableCell", text: text };
}
function headCell(dataFrame: any, column: string): PropsUITableCell {
return { __type__: "PropsUITableCell", text: column };
}
function columnNames(dataFrame: any): string[] {
return Object.keys(dataFrame);
}
function columnCount(dataFrame: any): number {
return columnNames(dataFrame).length;
}
function rowCount(dataFrame: any): number {
if (columnCount(dataFrame) === 0) {
return 0;
} else {
const firstColumn = dataFrame[columnNames(dataFrame)[0]];
return Object.keys(firstColumn).length - 1;
}
}
function rows(data: any): PropsUITableRow[] {
const result: PropsUITableRow[] = [];
const nRows = rowCount(data);
for (let row = 0; row <= nRows; row++) {
const id = `${row}`;
const cells = columnNames(data).map((column: string) =>
rowCell(data, column, row)
);
result.push({ __type__: "PropsUITableRow", id, cells });
}
return result;
}
function parseTables(
tablesData: PropsUIPromptConsentFormTable[]
): Array<PropsUITable & TableContext> {
console.log("parseTables");
return tablesData.map((table) => parseTable(table));
}
function parseTable(
tableData: PropsUIPromptConsentFormTable
): PropsUITable & TableContext {
const id = tableData.id;
const title = Translator.translate(tableData.title, props.locale);
const deletedRowCount = 0;
const dataFrame = JSON.parse(tableData.data_frame);
const headCells = columnNames(dataFrame).map((column: string) =>
headCell(dataFrame, column)
);
const head: PropsUITableHead = {
__type__: "PropsUITableHead",
cells: headCells,
};
const body: PropsUITableBody = {
__type__: "PropsUITableBody",
rows: rows(dataFrame),
};
return { __type__: "PropsUITable", id, head, body, title, deletedRowCount };
}
function renderTable(
table: Weak<PropsUITable> & TableContext,
readOnly = false
): JSX.Element {
return (
<div key={table.id} className="flex flex-col gap-4 mb-4">
<Title4 text={table.title} margin="" />
<Table
{...table}
readOnly={readOnly}
locale={locale}
onChange={handleTableChange}
/>
</div>
);
}
function handleTableChange(id: string, rows: PropsUITableRow[]): void {
const tablesCopy = tablesOut.current.slice(0);
const index = tablesCopy.findIndex((table) => table.id === id);
if (index > -1) {
const {
title,
head,
body: oldBody,
deletedRowCount: oldDeletedRowCount,
} = tablesCopy[index];
const body: PropsUITableBody = { __type__: "PropsUITableBody", rows };
const deletedRowCount =
oldDeletedRowCount + (oldBody.rows.length - rows.length);
tablesCopy[index] = {
__type__: "PropsUITable",
id,
head,
body,
title,
deletedRowCount,
};
}
tablesOut.current = tablesCopy;
}
function handleDonate(): void {
setWaiting(true);
const value = serializeConsentData();
resolve?.({ __type__: "PayloadJSON", value });
}
function handleCancel(): void {
resolve?.({ __type__: "PayloadFalse", value: false });
}
function serializeConsentData(): string {
const array = serializeTables().concat(serializeMetaData());
return JSON.stringify(array);
}
function serializeMetaData(): any[] {
return serializeMetaTables().concat(serializeDeletedMetaData());
}
function serializeTables(): any[] {
return tablesOut.current.map((table) => serializeTable(table));
}
function serializeMetaTables(): any[] {
return metaTables.current.map((table) => serializeTable(table));
}
function serializeDeletedMetaData(): any {
const rawData = tablesOut.current
.filter(({ deletedRowCount }) => deletedRowCount > 0)
.map(
({ id, deletedRowCount }) =>
`User deleted ${deletedRowCount} rows from table: ${id}`
);
const data = JSON.stringify(rawData);
return { user_omissions: data };
}
function serializeTable({ id, head, body: { rows } }: PropsUITable): any {
const data = rows.map((row) => serializeRow(row, head));
return { [id]: data };
}
function serializeRow(row: PropsUITableRow, head: PropsUITableHead): any {
assert(
row.cells.length === head.cells.length,
`Number of cells in row (${row.cells.length}) should be equals to number of cells in head (${head.cells.length})`
);
const keys = head.cells.map((cell) => cell.text);
const values = row.cells.map((cell) => cell.text);
return _.fromPairs(_.zip(keys, values));
}
return (
<>
<BodyLarge
text={Translator.translate(props.description ?? description, locale)}
/>
<div className="flex flex-col gap-8">
{tablesIn.current.map((table) => renderTable(table))}
<div>
<BodyLarge
margin=""
text={Translator.translate(
props.donateQuestion ?? donateQuestionLabel,
locale
)}
/>
<div className="flex flex-row gap-4 mt-4 mb-4">
<PrimaryButton
label={Translator.translate(
props.donateButton ?? donateButtonLabel,
locale
)}
onClick={handleDonate}
color="bg-success text-white"
spinning={waiting}
/>
<LabelButton
label={cancelButton}
onClick={handleCancel}
color="text-grey1"
/>
</div>
</div>
</div>
</>
);
};
const donateQuestionLabel = new TextBundle()
.add("en", "Do you want to donate the above data?")
.add("nl", "Wilt u de bovenstaande gegevens doneren?");
const donateButtonLabel = new TextBundle()
.add("en", "Yes, donate")
.add("nl", "Ja, doneer");
const cancelButtonLabel = new TextBundle().add("en", "No").add("nl", "Nee");
const description = new TextBundle()
.add(
"en",
"Determine whether you would like to donate the data below. Carefully check the data and adjust when required. With your donation you contribute to the previously described research. Thank you in advance."
)
.add(
"nl",
"Bepaal of u de onderstaande gegevens wilt doneren. Bekijk de gegevens zorgvuldig en pas zo nodig aan. Met uw donatie draagt u bij aan het eerder beschreven onderzoek. Alvast hartelijk dank."
);