@orchestrator-ui/orchestrator-ui-components 3.6.0 → 3.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@orchestrator-ui/orchestrator-ui-components",
3
- "version": "3.6.0",
3
+ "version": "3.7.0",
4
4
  "license": "Apache-2.0",
5
5
  "description": "Library of UI Components used to display the workflow orchestrator frontend",
6
6
  "author": {
@@ -9,6 +9,7 @@ import {
9
9
  CustomerField,
10
10
  DateField,
11
11
  DividerField,
12
+ FileUploadField,
12
13
  ImsNodeIdField,
13
14
  ImsPortIdField,
14
15
  IpNetworkField,
@@ -85,6 +86,8 @@ export function autoFieldFunction(
85
86
  return AcceptField;
86
87
  case 'ipvanynetwork': // Deprecated
87
88
  return IpNetworkField;
89
+ case 'file': // Deprecated
90
+ return FileUploadField;
88
91
  }
89
92
  break;
90
93
  }
@@ -0,0 +1,151 @@
1
+ /*
2
+ * Copyright 2024 SURF.
3
+ * Licensed under the Apache License, Version 2.0 (the "License");
4
+ * you may not use this file except in compliance with the License.
5
+ * You may obtain a copy of the License at
6
+ * http://www.apache.org/licenses/LICENSE-2.0
7
+ *
8
+ * Unless required by applicable law or agreed to in writing, software
9
+ * distributed under the License is distributed on an "AS IS" BASIS,
10
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11
+ * See the License for the specific language governing permissions and
12
+ * limitations under the License.
13
+ *
14
+ */
15
+ import React, { useState } from 'react';
16
+
17
+ import { useTranslations } from 'next-intl';
18
+ import { connectField, filterDOMProps } from 'uniforms';
19
+
20
+ import { EuiFilePicker, EuiFormRow, EuiText } from '@elastic/eui';
21
+
22
+ import { getCommonFormFieldStyles } from '@/components/WfoForms/formFields/commonStyles';
23
+ import { useOrchestratorTheme, useWithOrchestratorTheme } from '@/hooks';
24
+ import { useUploadFileMutation } from '@/rtk/endpoints/fileUpload';
25
+ import { FieldProps } from '@/types';
26
+
27
+ export type FileUploadProps = FieldProps<string>;
28
+
29
+ function FileUpload({
30
+ id,
31
+ label,
32
+ description,
33
+ onChange,
34
+ error,
35
+ showInlineError,
36
+ errorMessage,
37
+ ...props
38
+ }: FileUploadProps) {
39
+ const t = useTranslations('pydanticForms.widgets.fileUpload');
40
+ const { theme } = useOrchestratorTheme();
41
+ const [uploadFile, { isLoading, reset }] = useUploadFileMutation();
42
+ const [hasInValidFiletypes, setHasInValidFiletypes] = useState(false);
43
+ const [hasError, setHasError] = useState(false);
44
+ const [hasFileToBig, setHasFileToBig] = useState(false);
45
+
46
+ const { formRowStyle } = useWithOrchestratorTheme(getCommonFormFieldStyles);
47
+
48
+ const {
49
+ allowed_file_types: allowedMimeTypes,
50
+ url,
51
+ filesize_limit: fileSizeLimit,
52
+ } = props.field;
53
+
54
+ const cimUrl = url.replace('/cim', '');
55
+
56
+ const resetErrors = () => {
57
+ setHasInValidFiletypes(false);
58
+ setHasError(false);
59
+ setHasFileToBig(false);
60
+ };
61
+
62
+ const getErrorText = (): string => {
63
+ if (hasInValidFiletypes) {
64
+ return t('invalidFiletype');
65
+ }
66
+ if (hasError) {
67
+ return t('errorUploading');
68
+ }
69
+ if (hasFileToBig) {
70
+ return t('fileToBig', { fileSizeLimit });
71
+ }
72
+ return '';
73
+ };
74
+
75
+ const handleUploadedFiles = (files: FileList | null) => {
76
+ const file = files?.item(0) || null;
77
+
78
+ if (!file) return;
79
+
80
+ const { type, size } = file;
81
+
82
+ if (allowedMimeTypes && !allowedMimeTypes.includes(type)) {
83
+ resetErrors();
84
+ setHasInValidFiletypes(true);
85
+ return;
86
+ } else if (size > fileSizeLimit) {
87
+ resetErrors();
88
+ setHasFileToBig(true);
89
+ return;
90
+ } else {
91
+ uploadFile({ url: cimUrl, file })
92
+ .then((response) => {
93
+ if (response.error) {
94
+ resetErrors();
95
+ setHasError(true);
96
+ return;
97
+ } else {
98
+ onChange(response.data.file_id);
99
+ resetErrors();
100
+ }
101
+ })
102
+ .catch((error) => {
103
+ resetErrors();
104
+ setHasError(true);
105
+ console.error(error);
106
+ return;
107
+ });
108
+ reset();
109
+ }
110
+ };
111
+
112
+ return (
113
+ <section {...filterDOMProps(props)}>
114
+ <EuiFormRow
115
+ css={formRowStyle}
116
+ label={label}
117
+ labelAppend={<EuiText size="m">{description}</EuiText>}
118
+ error={showInlineError ? errorMessage : false}
119
+ isInvalid={error}
120
+ id={id}
121
+ fullWidth
122
+ >
123
+ <>
124
+ <EuiFilePicker
125
+ onChange={handleUploadedFiles}
126
+ isInvalid={
127
+ hasError || hasInValidFiletypes || hasFileToBig
128
+ }
129
+ display="large"
130
+ isLoading={isLoading}
131
+ initialPromptText={t('initialPromptText')}
132
+ multiple={false}
133
+ accept={allowedMimeTypes}
134
+ />
135
+ <div
136
+ css={{
137
+ color: theme.colors.danger,
138
+ marginTop: theme.size.base,
139
+ marginLeft: theme.size.xs,
140
+ fontWeight: theme.font.weight.semiBold,
141
+ }}
142
+ >
143
+ {getErrorText()}
144
+ </div>
145
+ </>
146
+ </EuiFormRow>
147
+ </section>
148
+ );
149
+ }
150
+
151
+ export const FileUploadField = connectField(FileUpload, { kind: 'leaf' });
@@ -9,5 +9,6 @@ export * from './IpPrefixTableFieldStyling';
9
9
  export * from './SplitPrefixStyling';
10
10
  export * from './TimestampField';
11
11
  export * from './VlanField';
12
+ export * from './FileUploadField';
12
13
  export * from './types';
13
14
  export * from './utils';
@@ -24,3 +24,4 @@ export * from './SubscriptionField';
24
24
  export * from './SummaryField';
25
25
  export * from './CustomerField';
26
26
  export * from './ConnectedSelectField';
27
+ export * from './deprecated/FileUploadField';
@@ -1 +1 @@
1
- export const ORCHESTRATOR_UI_LIBRARY_VERSION = '3.6.0';
1
+ export const ORCHESTRATOR_UI_LIBRARY_VERSION = '3.7.0';
@@ -133,6 +133,12 @@
133
133
  "selectNode": "Select node",
134
134
  "selectPort": "Select port",
135
135
  "selectNodeFirst": "Select a node first"
136
+ },
137
+ "fileUpload": {
138
+ "invalidFiletype": "Invalid filetype!",
139
+ "errorUploading": "Error uploading file!",
140
+ "fileToBig": "File to large. Maximum file size: {fileSizeLimit}",
141
+ "initialPromptText": "Select or drag and drop a file"
136
142
  }
137
143
  }
138
144
  },
@@ -132,6 +132,12 @@
132
132
  "selectNode": "Selecteer node",
133
133
  "selectPort": "Selecteer poort",
134
134
  "selectNodeFirst": "Selecteer eerst een node"
135
+ },
136
+ "fileUpload": {
137
+ "invalidFiletype": "Ongeldig bestandstype!",
138
+ "errorUploading": "Fout bij het upoaden van het bestand!",
139
+ "fileToBig": "Bestand te groot. Maximum bestand: {fileSizeLimit}",
140
+ "initialPromptText": "Selecteer een bestand of sleep het hierheen"
135
141
  }
136
142
  }
137
143
  },
@@ -0,0 +1,33 @@
1
+ import { BaseQueryTypes, orchestratorApi } from '@/rtk';
2
+
3
+ export interface FileUploadPayload {
4
+ url: string;
5
+ file: File;
6
+ }
7
+
8
+ interface FileUploadReturnValue {
9
+ file_id: string;
10
+ file_name: string;
11
+ }
12
+
13
+ const fileUploadApi = orchestratorApi.injectEndpoints({
14
+ endpoints: (build) => ({
15
+ uploadFile: build.mutation<FileUploadReturnValue, FileUploadPayload>({
16
+ query: ({ url, file }) => {
17
+ const formData = new FormData();
18
+ formData.append('file', file);
19
+ return {
20
+ url,
21
+ method: 'POST',
22
+ body: formData,
23
+ };
24
+ },
25
+ extraOptions: {
26
+ baseQueryType: BaseQueryTypes.fetch,
27
+ apiName: 'cim',
28
+ },
29
+ }),
30
+ }),
31
+ });
32
+
33
+ export const { useUploadFileMutation } = fileUploadApi;