1
- import { Button , Icon , Message , Modal } from 'semantic-ui-react' ;
2
- import { JSON_SCHEMA_URL } from '../../../services/api' ;
1
+ import { Button , Icon , Message , Modal } from 'semantic-ui-react' ;
2
+ import { JSON_SCHEMA_URL } from '../../../services/api' ;
3
3
import { validate as jsonSchemaValidate } from '../../../services/jsonSchemaValidator' ;
4
4
import Qmra from '../../../core/model/qmra/Qmra' ;
5
- import React , { ChangeEvent , useState } from 'react' ;
5
+ import React , { ChangeEvent , useState } from 'react' ;
6
6
7
7
interface IProps {
8
8
onChange : ( response : Qmra ) => void ;
9
9
qmra : Qmra ;
10
10
}
11
11
12
- const JsonUpload = ( { onChange, qmra } : IProps ) => {
12
+ const JsonUpload = ( { onChange, qmra} : IProps ) => {
13
13
const [ data , setData ] = useState < any > ( ) ;
14
14
const [ errors , setErrors ] = useState < string [ ] > ( [ ] ) ;
15
15
const [ isLoading , setIsLoading ] = useState < boolean > ( false ) ;
@@ -22,17 +22,23 @@ const JsonUpload = ({ onChange, qmra }: IProps) => {
22
22
}
23
23
} ;
24
24
25
+ const checkFile = ( data : any ) => Array . isArray ( data . inflow ) && Array . isArray ( data . exposure ) &&
26
+ Array . isArray ( data . doseresponse ) && Array . isArray ( data . health ) && data . treatment &&
27
+ Array . isArray ( data . treatment . processes ) && Array . isArray ( data . treatment . schemes ) ;
28
+
25
29
const handleConfirm = ( ) => {
26
30
let q = Qmra . fromDefaults ( ) ;
27
- if ( 'config' in data ) {
28
- q = qmra . fromPayload ( data ) ;
31
+
32
+ if ( 'config' in data && checkFile ( data . config ) ) {
33
+ q = qmra . fromPayload ( data . config ) ;
29
34
}
30
- if ( 'data' in data ) {
35
+ if ( 'data' in data && checkFile ( data . data ) ) {
31
36
q = Qmra . fromObject ( data ) ;
32
37
}
33
- if ( 'inflow' in data ) {
38
+ if ( 'inflow' in data && checkFile ( data ) ) {
34
39
q = qmra . fromPayload ( data ) ;
35
40
}
41
+
36
42
setShowModal ( false ) ;
37
43
onChange ( q ) ;
38
44
} ;
@@ -54,27 +60,30 @@ const JsonUpload = ({ onChange, qmra }: IProps) => {
54
60
55
61
if ( ! checkPassed ) {
56
62
setData ( undefined ) ;
63
+ setShowModal ( true ) ;
57
64
e . push ( 'Invalid JSON' ) ;
65
+ setErrors ( e ) ;
58
66
}
59
67
60
68
const d = JSON . parse ( text ) ;
61
69
62
- // TODO: JSON SCHEME VALIDATION
63
-
64
70
jsonSchemaValidate (
65
71
d ,
66
72
JSON_SCHEMA_URL + '/qmra/qmra.payload.json'
67
73
) . then ( ( r ) => {
68
- console . log ( r ) ;
74
+ if ( r [ 0 ] ) {
75
+ setErrors ( [ ] ) ;
76
+ setIsLoading ( false ) ;
77
+ setShowModal ( true ) ;
78
+ setData ( d ) ;
79
+ } else {
80
+ if ( Array . isArray ( r [ 1 ] ) && 'message' in r [ 1 ] [ 0 ] ) {
81
+ setErrors ( r [ 1 ] . map ( ( e ) => e . message ) ) ;
82
+ }
83
+ setIsLoading ( false ) ;
84
+ setShowModal ( true ) ;
85
+ }
69
86
} ) ;
70
-
71
- if ( checkPassed && e . length === 0 ) {
72
- setData ( d ) ;
73
- }
74
-
75
- setErrors ( e ) ;
76
- setIsLoading ( false ) ;
77
- setShowModal ( true ) ;
78
87
} ;
79
88
80
89
const handleUploadFile = ( e : ChangeEvent < HTMLInputElement > ) => {
@@ -97,7 +106,7 @@ const JsonUpload = ({ onChange, qmra }: IProps) => {
97
106
labelPosition = "left"
98
107
loading = { isLoading }
99
108
/>
100
- < input hidden = { true } type = "file" id = "inputField" onChange = { handleUploadFile } />
109
+ < input hidden = { true } type = "file" id = "inputField" onChange = { handleUploadFile } />
101
110
< Modal onClose = { handleTriggerModal } open = { showModal } size = "small" >
102
111
< Modal . Header > Upload Json</ Modal . Header >
103
112
< Modal . Content >
@@ -111,17 +120,17 @@ const JsonUpload = ({ onChange, qmra }: IProps) => {
111
120
{ errors . length > 0 && (
112
121
< Modal . Actions >
113
122
< Button color = "grey" onClick = { handleTriggerModal } >
114
- < Icon name = "remove" /> Close
123
+ < Icon name = "remove" /> Close
115
124
</ Button >
116
125
</ Modal . Actions >
117
126
) }
118
127
{ errors . length < 1 && (
119
128
< Modal . Actions >
120
129
< Button color = "red" onClick = { handleTriggerModal } >
121
- < Icon name = "remove" /> No
130
+ < Icon name = "remove" /> No
122
131
</ Button >
123
132
< Button color = "green" onClick = { handleConfirm } >
124
- < Icon name = "checkmark" /> Yes
133
+ < Icon name = "checkmark" /> Yes
125
134
</ Button >
126
135
</ Modal . Actions >
127
136
) }
0 commit comments