-
Notifications
You must be signed in to change notification settings - Fork 1k
/
Copy pathIngestionCsvEdition.tsx
372 lines (360 loc) · 12.7 KB
/
IngestionCsvEdition.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
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
import { graphql, useFragment, useMutation } from 'react-relay';
import React, { FunctionComponent, useState } from 'react';
import { Option } from '@components/common/form/ReferenceField';
import * as Yup from 'yup';
import { FormikConfig } from 'formik/dist/types';
import { ExternalReferencesValues } from '@components/common/form/ExternalReferencesField';
import { Field, Form, Formik } from 'formik';
import MenuItem from '@mui/material/MenuItem';
import Box from '@mui/material/Box';
import Alert from '@mui/material/Alert';
import CreatorField from '@components/common/form/CreatorField';
import CommitMessage from '@components/common/form/CommitMessage';
import { IngestionCsvEditionFragment_ingestionCsv$key } from '@components/data/ingestionCsv/__generated__/IngestionCsvEditionFragment_ingestionCsv.graphql';
import CsvMapperField, { csvMapperQuery } from '@components/common/form/CsvMapperField';
import Button from '@mui/material/Button';
import IngestionCsvMapperTestDialog from '@components/data/ingestionCsv/IngestionCsvMapperTestDialog';
import makeStyles from '@mui/styles/makeStyles';
import { CsvMapperFieldSearchQuery } from '@components/common/form/__generated__/CsvMapperFieldSearchQuery.graphql';
import { convertMapper, convertUser } from '../../../../utils/edition';
import { useFormatter } from '../../../../components/i18n';
import { useSchemaEditionValidation } from '../../../../utils/hooks/useEntitySettings';
import { adaptFieldValue } from '../../../../utils/String';
import TextField from '../../../../components/TextField';
import { fieldSpacingContainerStyle } from '../../../../utils/field';
import SelectField from '../../../../components/SelectField';
import DateTimePickerField from '../../../../components/DateTimePickerField';
import type { Theme } from '../../../../components/Theme';
import useQueryLoading from '../../../../utils/hooks/useQueryLoading';
import Loader, { LoaderVariant } from '../../../../components/Loader';
const useStyles = makeStyles<Theme>((theme) => ({
buttons: {
marginTop: 20,
textAlign: 'right',
},
button: {
marginLeft: theme.spacing(2),
},
}));
export const ingestionCsvEditionPatch = graphql`
mutation IngestionCsvEditionPatchMutation($id: ID!, $input: [EditInput!]!) {
ingestionCsvFieldPatch(id: $id, input: $input) {
...IngestionCsvEditionFragment_ingestionCsv
}
}
`;
const ingestionCsvEditionFragment = graphql`
fragment IngestionCsvEditionFragment_ingestionCsv on IngestionCsv {
id
name
description
uri
authentication_type
authentication_value
ingestion_running
current_state_date
csvMapper {
id
name
}
user {
id
entity_type
name
}
}
`;
interface IngestionCsvEditionProps {
ingestionCsv: IngestionCsvEditionFragment_ingestionCsv$key;
handleClose: () => void;
enableReferences?: boolean
}
interface IngestionCsvEditionForm {
message?: string | null
references?: ExternalReferencesValues
name: string,
description?: string | null,
uri: string,
authentication_type: string,
authentication_value?: string | null,
current_state_date: Date | null
ingestion_running?: boolean | null,
csv_mapper_id: string | Option,
user_id: string | Option
}
const IngestionCsvEdition: FunctionComponent<IngestionCsvEditionProps> = ({
ingestionCsv,
handleClose,
enableReferences = false,
}) => {
const { t_i18n } = useFormatter();
const classes = useStyles();
const [open, setOpen] = useState(false);
const ingestionCsvData = useFragment(ingestionCsvEditionFragment, ingestionCsv);
const basicShape = {
name: Yup.string().required(t_i18n('This field is required')),
description: Yup.string().nullable(),
uri: Yup.string().required(t_i18n('This field is required')),
authentication_type: Yup.string().required(t_i18n('This field is required')),
authentication_value: Yup.string().nullable(),
current_state_date: Yup.date()
.typeError(t_i18n('The value must be a datetime (yyyy-MM-dd hh:mm (a|p)m)'))
.nullable(),
user_id: Yup.mixed().nullable(),
username: Yup.string().nullable(),
password: Yup.string().nullable(),
cert: Yup.string().nullable(),
key: Yup.string().nullable(),
ca: Yup.string().nullable(),
csv_mapper_id: Yup.mixed().required(t_i18n('This field is required')),
};
const ingestionCsvValidator = useSchemaEditionValidation('IngestionCsv', basicShape);
const [commitUpdate] = useMutation(ingestionCsvEditionPatch);
const onSubmit: FormikConfig<IngestionCsvEditionForm>['onSubmit'] = (values, { setSubmitting }) => {
const { message, references, ...otherValues } = values;
const commitMessage = message ?? '';
const commitReferences = (references ?? []).map(({ value }) => value);
const inputValues = Object.entries({
...otherValues,
}).map(([key, value]) => ({ key, value: adaptFieldValue(value) }));
commitUpdate({
variables: {
id: ingestionCsvData.id,
input: inputValues,
commitMessage: commitMessage && commitMessage.length > 0 ? commitMessage : null,
references: commitReferences,
},
onCompleted: () => {
setSubmitting(false);
handleClose();
},
});
};
const handleSubmitField = (name: string, value: Option | string | string[] | number | number[] | null) => {
let finalValue = value as string;
if (name === 'csv_mapper_id' || name === 'user_id') {
finalValue = (value as Option).value;
}
ingestionCsvValidator
.validateAt(name, { [name]: value })
.then(() => {
commitUpdate({
variables: {
id: ingestionCsvData.id,
input: { key: name, value: finalValue || '' },
},
});
})
.catch(() => false);
};
const initialValues = {
name: ingestionCsvData.name,
description: ingestionCsvData.description,
uri: ingestionCsvData.uri,
authentication_type: ingestionCsvData.authentication_type,
authentication_value: ingestionCsvData.authentication_value,
current_state_date: ingestionCsvData.current_state_date,
ingestion_running: ingestionCsvData.ingestion_running,
csv_mapper_id: convertMapper(ingestionCsvData, 'csvMapper'),
user_id: convertUser(ingestionCsvData, 'user'),
references: undefined,
};
const queryRef = useQueryLoading<CsvMapperFieldSearchQuery>(csvMapperQuery);
return (
<Formik<IngestionCsvEditionForm>
enableReinitialize={true}
initialValues={initialValues}
validationSchema={ingestionCsvValidator}
onSubmit={onSubmit}
>
{({
values,
submitForm,
isSubmitting,
setFieldValue,
isValid,
dirty,
}) => (
<Form style={{ margin: '20px 0 20px 0' }}>
<Field
component={TextField}
variant="standard"
name="name"
label={t_i18n('Name')}
fullWidth={true}
onSubmit={handleSubmitField}
/>
<Field
component={TextField}
variant="standard"
name="description"
label={t_i18n('Description')}
fullWidth={true}
style={fieldSpacingContainerStyle}
onSubmit={handleSubmitField}
/>
<Field
component={TextField}
variant="standard"
name="uri"
label={t_i18n('CSV URL')}
fullWidth={true}
onSubmit={handleSubmitField}
style={fieldSpacingContainerStyle}
/>
<Field
component={DateTimePickerField}
name="current_state_date"
textFieldProps={{
label: t_i18n(
'Import from date (empty = all CSV feed possible items)',
),
variant: 'standard',
fullWidth: true,
style: { marginTop: 20 },
}}
/>
{
queryRef && (
<React.Suspense fallback={<Loader variant={LoaderVariant.inElement} />}>
<CsvMapperField
name="csv_mapper_id"
isOptionEqualToValue={(option: Option, { value }: Option) => option.value === value}
onChange={handleSubmitField}
queryRef={queryRef}
/>
</React.Suspense>
)
}
<Field
component={SelectField}
variant="standard"
name="authentication_type"
label={t_i18n('Authentication type')}
onSubmit={handleSubmitField}
fullWidth={true}
containerstyle={{
width: '100%',
marginTop: 20,
}}
>
<MenuItem value="none">{t_i18n('None')}</MenuItem>
<MenuItem value="basic">{t_i18n('Basic user / password')}</MenuItem>
<MenuItem value="bearer">{t_i18n('Bearer token')}</MenuItem>
<MenuItem value="certificate">
{t_i18n('Client certificate')}
</MenuItem>
</Field>
{values.authentication_type === 'basic' && (
<>
<Field
component={TextField}
variant="standard"
name="username"
label={t_i18n('Username')}
onSubmit={handleSubmitField}
fullWidth={true}
style={fieldSpacingContainerStyle}
/>
<Field
component={TextField}
variant="standard"
name="password"
label={t_i18n('Password')}
onSubmit={handleSubmitField}
fullWidth={true}
style={fieldSpacingContainerStyle}
/>
</>
)}
{values.authentication_type === 'bearer' && (
<Field
component={TextField}
variant="standard"
name="authentication_value"
label={t_i18n('Token')}
onSubmit={handleSubmitField}
fullWidth={true}
style={fieldSpacingContainerStyle}
/>
)}
{values.authentication_type === 'certificate' && (
<>
<Field
component={TextField}
variant="standard"
name="cert"
label={t_i18n('Certificate (base64)')}
onSubmit={handleSubmitField}
fullWidth={true}
style={fieldSpacingContainerStyle}
/>
<Field
component={TextField}
variant="standard"
name="key"
label={t_i18n('Key (base64)')}
onSubmit={handleSubmitField}
fullWidth={true}
style={fieldSpacingContainerStyle}
/>
<Field
component={TextField}
variant="standard"
name="ca"
label={t_i18n('CA certificate (base64)')}
onSubmit={handleSubmitField}
fullWidth={true}
style={fieldSpacingContainerStyle}
/>
</>
)}
<CreatorField
name="user_id"
label={t_i18n('User responsible for data creation (empty = System)')}
isOptionEqualToValue={(option: Option, value: string) => option.value === value}
onChange={handleSubmitField}
containerStyle={fieldSpacingContainerStyle}
/>
{enableReferences && (
<CommitMessage
submitForm={submitForm}
disabled={isSubmitting || !isValid || !dirty}
setFieldValue={setFieldValue}
open={false}
values={values.references}
id={ingestionCsvData.id}
/>
)}
<Box sx={{ width: '100%', marginTop: 5 }}>
<Alert
severity="info"
variant="outlined"
style={{ padding: '0px 10px 0px 10px' }}
>
{t_i18n('Please, verify the validity of the selected CSV mapper for the given URL.')}<br/>
{t_i18n('Only successful tests allow the ingestion edition.')}
</Alert>
</Box>
<div className={classes.buttons}>
<Button
variant="contained"
color="secondary"
onClick={() => setOpen(true)}
classes={{ root: classes.button }}
disabled={!(values.uri && values.csv_mapper_id)}
>
{t_i18n('Verify')}
</Button>
</div>
<IngestionCsvMapperTestDialog
open={open}
onClose={() => setOpen(false)}
values={values}
/>
</Form>
)}
</Formik>
);
};
export default IngestionCsvEdition;