-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdevansh-2
More file actions
241 lines (211 loc) · 8.06 KB
/
Copy pathdevansh-2
File metadata and controls
241 lines (211 loc) · 8.06 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
import React, { useEffect, useState } from 'react';
import PropTypes from 'prop-types';
import SyntaxHighlighter from 'react-syntax-highlighter';
import { atomOneDark } from 'react-syntax-highlighter/dist/esm/styles/hljs';
import Base64 from 'base-64';
import ParseFile from '../../../util/FileParser';
import { Input } from '../Input';
import { ValidateDataSet, VALIDATION_TYPE_REQUIRED } from '../../../util/Validator';
import { RemoveFromArray } from '../../../util/util';
function FileUpload(props) {
const inherits = props.inherits;
const varient = props.varient;
const show_file_content = props.show_file_content ? props.show_file_content : "true"
const setErrorInCaseUploadFails=props.setErrorInCaseUploadFails
const noMandatory = props.noMandatory
const noLabel = props.noLabel
const with_command_args = props.with_command_args
const fullWidthFileName = props.fullWidthFileName
const style = props.style || {}
const [state, setState] = useState(normalizeState(props.state));
const hitCallbackFn = props.hitCallbackFn ? props.hitCallbackFn : () => { };
useEffect(() => {
if (props.state)
setState(new_state => ({
...new_state,
...normalizeState(props.state)
}))
}, [props.state])
function onChangeHandler(e) {
ParseFile(e, onParseComplete,setErrorInCaseUploadFails);
hitCallbackFn();
}
const onParseComplete = (file_data) => {
let files = varient == "single" ? [] : state.form_data.data.files;
files.push(file_data);
updateFilesInState(files);
}
const removeEnvManifest = (i) => {
let files = state.form_data.data.files;
files.splice(i, 1);
updateFilesInState(files);
}
function updateFilesInState(files) {
setState(new_state => ({
...new_state,
content: files.length > 0 ? new_state.content : "",
form_data: {
...new_state.form_data,
data: {
files: files,
},
error: {
files: ""
}
}
}));
if (inherits.onFileChange) {
inherits.onFileChange(files);
}
}
const onFileSelect = (i) => {
setState(new_state => ({
...new_state,
content: new_state.form_data.data.files[i].content
}));
}
// Overloading
inherits.validateForm = () => {
if (!with_command_args) {
// RemoveFromArray(state.form_data.default_validations.cmd_agrs,VALIDATION_TYPE_REQUIRED)
}
if (noMandatory) {
if (state.form_data.default_validations && state.form_data.default_validations.files) {
RemoveFromArray(state.form_data.default_validations.files, VALIDATION_TYPE_REQUIRED);
}
}
const result = ValidateDataSet(state.form_data.data, state.form_data.default_validations);
if (!result.valid) {
setState(new_state => ({
...new_state,
form_data: {
...new_state.form_data,
error: result.error
}
}));
}
return result;
}
inherits.getData = () => {
return state.form_data.data;
}
inherits.getState = () => {
return state;
}
const onKeyValueChangeHandler = (k, v) => {
setState(new_state => ({
...new_state,
form_data: {
...new_state.form_data,
data: {
...new_state.form_data.data,
[k]: v
},
error: { ...state.form_data.error, [k]: null }
}
}));
}
return (
<div className="box-file-upload pd-10" style={style}>
<Input
type={varient == "single" ? "file" : varient == "show_filename_only" ? "file-upload-name" : "multi-file-upload"}
name={"files"}
mandatorySign={noMandatory ? false : true}
label={noLabel ? null : varient == "single" ? <>Please upload the file</> :
varient == "show_filename_only" ? "Please provide shell script for ticket validation" :
<>Please upload the files <span className="hint-text">(You can upload multiple files)</span></>}
onChangeHandler={onChangeHandler}
data={state.form_data.data}
onFileSelect={onFileSelect}
onFileRemove={removeEnvManifest}
error={state.form_data.error}
fullWidthFileName={fullWidthFileName}
fileSupportText={props.fileSupportText}
newStyle={props.newStyle} />
{
with_command_args &&
<Input
type="tagged-input"
name="cmd_agrs"
placeholder="Command Args"
label="Command Args "
subHeading="(Enter command args and press Enter)"
data={state.form_data.data}
error={state.form_data.error}
onChangeHandler={onKeyValueChangeHandler}
/>
}
{show_file_content === "true" && varient === "show_filename_only" ?
<div style={{
backgroundColor: 'var(--color-content-primary)', height: '100%',
borderRadius: 'var(--radius-0) var(--radius-0) var(--radius-8) var(--radius-0)', position: 'relative'
}}>
{
varient == "show_filename_only" ?
<></> :
state.content ?
<SyntaxHighlighter id="viewFullScreen" language="yaml" style={atomOneDark} >
{Base64.decode(state.content)}
</SyntaxHighlighter>
:
<>
{
state.form_data.data.files ?
state.form_data.data.files[0] ?
<>
{state.form_data.data.files[0].content ?
<SyntaxHighlighter id="viewFullScreen" language="yaml" style={atomOneDark} >
{state.form_data.data.files[0].content ? Base64.decode(state.form_data.data.files[0].content) : ""}
</SyntaxHighlighter> : null
}
</>
:
null
:
null
}
</>
}
</div>
: null}
</div>
);
}
FileUpload.propTypes = {
...PropTypes.objectOf(PropTypes.any),
}
export default FileUpload;
// Utils
function normalizeState(s) {
if (!s) return getDefaultState();
if (s.form_data) return s;
// s is raw data from getData() — wrap it in the full state shape
const defaults = getDefaultState();
return {
...defaults,
form_data: {
...defaults.form_data,
data: { ...defaults.form_data.data, ...s },
},
};
}
function getDefaultState() {
return {
content: "",
form_data: {
data: {
files: [],
cmd_agrs: []
},
error: {
files: ""
},
default_validations: {
files: [VALIDATION_TYPE_REQUIRED],
}
}
}
}
export function FileUploadDefaultState() {
return getDefaultState();
}