@addsign/moje-agenda-shared-lib 2.0.62 → 2.0.63

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.
@@ -1,222 +1,211 @@
1
- import React, { useState, useEffect, useCallback } from "react";
2
- import { useDropzone } from "react-dropzone";
3
- import { handleErrors, useFederationContext } from "../../main";
4
- import { MdDeleteOutline, MdInsertDriveFile } from "react-icons/md";
5
- import { AxiosError } from "axios";
6
-
7
- export interface FileData {
8
- id: number;
9
- mimeType: string;
10
- size: number;
11
- filename: string;
12
- createdByEmpId: string;
13
- created: string;
14
- }
15
-
16
- interface FileInputProps {
17
- name: string;
18
- label?: string;
19
- initialFile?: FileData;
20
- onFileChanged: (e: any) => void;
21
- required?: boolean;
22
- description?: string;
23
- disabled?: boolean;
24
- errors?: { [key: string]: { message: string } };
25
- attachmentName?: string;
26
- attachmentType?: string;
27
- }
28
-
29
- const MAX_FILE_SIZE = 1024 * 1024; // 1MB
30
-
31
- const FileInput: React.FC<FileInputProps> = ({
32
- initialFile,
33
- onFileChanged,
34
- label,
35
- name,
36
- required,
37
- description,
38
- disabled,
39
- errors = {},
40
- attachmentName,
41
- attachmentType,
42
- }) => {
43
- const [fileData, setFileData] = useState<FileData | null>(
44
- initialFile || null
45
- );
46
- const [isFocused, setIsFocused] = useState(false);
47
-
48
- const federationContext = useFederationContext();
49
- useEffect(() => {
50
- if (initialFile) {
51
- setFileData(initialFile);
52
- }
53
- }, [initialFile]);
54
-
55
- const onDrop = useCallback(
56
- async (acceptedFiles: File[]) => {
57
- if (acceptedFiles.length > 0) {
58
- const file = acceptedFiles[0];
59
-
60
- if (file.size > MAX_FILE_SIZE) {
61
- // Handle the case when the file size is exceeded
62
- federationContext.emitter.emit("message", {
63
- title: "Velikost souboru byla překročena",
64
- message: `Maximální povolená velikost je ${MAX_FILE_SIZE / (1024 * 1024)} MB.`,
65
- classes: "bg-danger ",
66
- timeout: 0,
67
- type: "error",
68
- });
69
- return;
70
- }
71
-
72
- const formData = new FormData();
73
-
74
- // Add the JSON data object
75
- const dataObject = {
76
- ...(attachmentName && { attachmentName }),
77
- ...(attachmentType && { attachmentType }),
78
- };
79
-
80
- formData.append(
81
- "data",
82
- new Blob([JSON.stringify(dataObject)], { type: "application/json" })
83
- );
84
- formData.append("file", file);
85
-
86
- try {
87
- const response = await federationContext.apiClient.post<FileData>(
88
- "/files/upload",
89
- formData,
90
- {
91
- headers: {
92
- "Content-Type": "multipart/form-data",
93
- },
94
- }
95
- );
96
- setFileData(response.data);
97
- onFileChanged({
98
- target: { name, value: response.data.id.toString() },
99
- });
100
- setIsFocused(false);
101
- } catch (error) {
102
- handleErrors(error as AxiosError, federationContext.emitter);
103
- console.error("There was an error!", error);
104
- }
105
- }
106
- },
107
- [
108
- federationContext.apiClient,
109
- federationContext.emitter,
110
- onFileChanged,
111
- name,
112
- attachmentName,
113
- attachmentType,
114
- ]
115
- );
116
-
117
- const { getRootProps, getInputProps, isDragActive } = useDropzone({
118
- onDrop,
119
- disabled,
120
- multiple: false,
121
- });
122
-
123
- const handleRemove = () => {
124
- setFileData(null);
125
- onFileChanged({ target: { name, value: null } });
126
- };
127
-
128
- return (
129
- <div className="w-full min-h-30 flex-col justify-start items-start gap-1.5 inline-flex sharedLibrary">
130
- <div className="self-stretch flex-col justify-start items-start gap-1.5 flex">
131
- {label && (
132
- <label
133
- className="text-slate-700 text-sm leading-tight font-medium"
134
- htmlFor={name}
135
- >
136
- {label} {required ? "*" : ""}
137
- </label>
138
- )}
139
- <div
140
- className={
141
- `self-stretch px-2 py-2 rounded-lg justify-start items-center gap-2 inline-flex outline-none border` +
142
- ` ${
143
- isFocused && !errors[name]?.message
144
- ? "outline-4 outline-indigo-200 outline-offset-0 border-indigo-300"
145
- : ""
146
- }` +
147
- ` ${
148
- isFocused && errors[name]?.message
149
- ? "outline-4 outline-red-200 outline-offset-0 border-none"
150
- : ""
151
- } ` +
152
- ` ${!isFocused && errors[name]?.message ? "border-red-200" : ""} ` +
153
- ` ${disabled ? "bg-gray-100" : "bg-transparent"}`
154
- }
155
- onFocus={() => setIsFocused(true)}
156
- onBlur={() => setIsFocused(false)}
157
- >
158
- <div className="flex relative grow shrink basis-0 min-h-5 lg:min-h-[32px] justify-start items-stretch gap-2 max-w-full ">
159
- {!fileData ? (
160
- <div
161
- {...getRootProps()}
162
- className={`w-full p-4 border-dashed border-2 rounded-lg text-center ${
163
- isDragActive
164
- ? "border-indigo-300 bg-indigo-50"
165
- : "border-gray-300"
166
- }`}
167
- >
168
- <input {...getInputProps()} id={name} />
169
- <p className="text-gray-500">
170
- {isDragActive
171
- ? "Sem přetáhněte soubor"
172
- : "Klikněte pro nahrání, nebo nahrajte přetažením souboru"}
173
- </p>
174
- </div>
175
- ) : (
176
- <div className="w-full flex items-center justify-between">
177
- <div className=" flex">
178
- <MdInsertDriveFile style={{ fontSize: "2rem" }} />
179
- <a
180
- href={`/api/files/download/${fileData.id}`}
181
- className="pl-2 text-left underline text-primary"
182
- target="_blank"
183
- >
184
- {fileData.filename}
185
- </a>
186
- </div>
187
- {!disabled && (
188
- <div
189
- onClick={handleRemove}
190
- className="text-gray-600 cursor-pointer hover:text-primary hover:bg-gray-200 rounded-full ml-4"
191
- >
192
- <MdDeleteOutline
193
- style={{ fontSize: "1.5rem", margin: "15px" }}
194
- />
195
- </div>
196
- )}
197
- </div>
198
- )}
199
- </div>
200
- </div>
201
- </div>
202
- {description && (
203
- <div
204
- className="HintText self-stretch text-slate-600 text-sm font-normal leading-tight"
205
- id={name + ":description"}
206
- >
207
- {description}
208
- </div>
209
- )}
210
- {errors[name] && (
211
- <div
212
- className="HintText self-stretch text-red-600 text-sm font-normal leading-tight"
213
- id={name + ":error"}
214
- >
215
- {errors[name]?.message}
216
- </div>
217
- )}
218
- </div>
219
- );
220
- };
221
-
222
- export default FileInput;
1
+ import React, { useState, useEffect, useCallback } from "react";
2
+ import { useDropzone } from "react-dropzone";
3
+ import { handleErrors, useFederationContext } from "../../main";
4
+ import { MdDeleteOutline, MdInsertDriveFile } from "react-icons/md";
5
+ import { AxiosError } from "axios";
6
+ import { cn } from "../../utils/utils";
7
+
8
+ export interface FileData {
9
+ id: number;
10
+ mimeType: string;
11
+ size: number;
12
+ filename: string;
13
+ createdByEmpId: string;
14
+ created: string;
15
+ }
16
+
17
+ export interface FileInputProps {
18
+ name: string;
19
+ label?: string;
20
+ initialFile?: FileData;
21
+ onFileChanged: (e: any) => void;
22
+ required?: boolean;
23
+ description?: string;
24
+ disabled?: boolean;
25
+ attachmentName?: string;
26
+ attachmentType?: string;
27
+ hasError?: boolean;
28
+ }
29
+
30
+ const MAX_FILE_SIZE = 1024 * 1024; // 1MB
31
+
32
+ const FileInput: React.FC<FileInputProps> = ({
33
+ initialFile,
34
+ onFileChanged,
35
+ label,
36
+ name,
37
+ required,
38
+ description,
39
+ disabled,
40
+ attachmentName,
41
+ attachmentType,
42
+ hasError,
43
+ }) => {
44
+ const [fileData, setFileData] = useState<FileData | null>(
45
+ initialFile || null
46
+ );
47
+ const [isFocused, setIsFocused] = useState(false);
48
+
49
+ const federationContext = useFederationContext();
50
+ useEffect(() => {
51
+ if (initialFile) {
52
+ setFileData(initialFile);
53
+ }
54
+ }, [initialFile]);
55
+
56
+ const onDrop = useCallback(
57
+ async (acceptedFiles: File[]) => {
58
+ if (acceptedFiles.length > 0) {
59
+ const file = acceptedFiles[0];
60
+
61
+ if (file.size > MAX_FILE_SIZE) {
62
+ // Handle the case when the file size is exceeded
63
+ federationContext.emitter.emit("message", {
64
+ title: "Velikost souboru byla překročena",
65
+ message: `Maximální povolená velikost je ${MAX_FILE_SIZE / (1024 * 1024)} MB.`,
66
+ classes: "bg-danger ",
67
+ timeout: 0,
68
+ type: "error",
69
+ });
70
+ return;
71
+ }
72
+
73
+ const formData = new FormData();
74
+
75
+ // Add the JSON data object
76
+ const dataObject = {
77
+ ...(attachmentName && { attachmentName }),
78
+ ...(attachmentType && { attachmentType }),
79
+ };
80
+
81
+ formData.append(
82
+ "data",
83
+ new Blob([JSON.stringify(dataObject)], { type: "application/json" })
84
+ );
85
+ formData.append("file", file);
86
+
87
+ try {
88
+ const response = await federationContext.apiClient.post<FileData>(
89
+ "/files/upload",
90
+ formData,
91
+ {
92
+ headers: {
93
+ "Content-Type": "multipart/form-data",
94
+ },
95
+ }
96
+ );
97
+ setFileData(response.data);
98
+ onFileChanged({
99
+ target: { name, value: response.data.id.toString() },
100
+ });
101
+ setIsFocused(false);
102
+ } catch (error) {
103
+ handleErrors(error as AxiosError, federationContext.emitter);
104
+ console.error("There was an error!", error);
105
+ }
106
+ }
107
+ },
108
+ [
109
+ federationContext.apiClient,
110
+ federationContext.emitter,
111
+ onFileChanged,
112
+ name,
113
+ attachmentName,
114
+ attachmentType,
115
+ ]
116
+ );
117
+
118
+ const { getRootProps, getInputProps, isDragActive } = useDropzone({
119
+ onDrop,
120
+ disabled,
121
+ multiple: false,
122
+ });
123
+
124
+ const handleRemove = () => {
125
+ setFileData(null);
126
+ onFileChanged({ target: { name, value: null } });
127
+ };
128
+
129
+ return (
130
+ <div className="w-full min-h-30 flex-col justify-start items-start gap-1.5 inline-flex sharedLibrary">
131
+ <div className="self-stretch flex-col justify-start items-start gap-1.5 flex">
132
+ {label && (
133
+ <label
134
+ className="text-slate-700 text-sm leading-tight font-medium"
135
+ htmlFor={name}
136
+ >
137
+ {label} {required ? "*" : ""}
138
+ </label>
139
+ )}
140
+ <div
141
+ className={cn(
142
+ `self-stretch px-2 py-2 rounded-lg justify-start items-center gap-2 inline-flex outline-none border`,
143
+ isFocused &&
144
+ !hasError &&
145
+ "outline-4 outline-indigo-200 outline-offset-0 border-indigo-300",
146
+
147
+ hasError &&
148
+ "outline-4 outline-red-200 outline-offset-0 border-none",
149
+ !isFocused && hasError && "border-red-200 ",
150
+ disabled ? "bg-gray-100" : "bg-transparent"
151
+ )}
152
+ onFocus={() => setIsFocused(true)}
153
+ onBlur={() => setIsFocused(false)}
154
+ >
155
+ <div className="flex relative grow shrink basis-0 min-h-5 lg:min-h-[32px] justify-start items-stretch gap-2 max-w-full ">
156
+ {!fileData ? (
157
+ <div
158
+ {...getRootProps()}
159
+ className={`w-full p-4 border-dashed border-2 rounded-lg text-center ${
160
+ isDragActive
161
+ ? "border-indigo-300 bg-indigo-50"
162
+ : "border-gray-300"
163
+ }`}
164
+ >
165
+ <input {...getInputProps()} id={name} />
166
+ <p className="text-gray-500">
167
+ {isDragActive
168
+ ? "Sem přetáhněte soubor"
169
+ : "Klikněte pro nahrání, nebo nahrajte přetažením souboru"}
170
+ </p>
171
+ </div>
172
+ ) : (
173
+ <div className="w-full flex items-center justify-between">
174
+ <div className=" flex">
175
+ <MdInsertDriveFile style={{ fontSize: "2rem" }} />
176
+ <a
177
+ href={`/api/files/download/${fileData.id}`}
178
+ className="pl-2 text-left underline text-primary"
179
+ target="_blank"
180
+ >
181
+ {fileData.filename}
182
+ </a>
183
+ </div>
184
+ {!disabled && (
185
+ <div
186
+ onClick={handleRemove}
187
+ className="text-gray-600 cursor-pointer hover:text-primary hover:bg-gray-200 rounded-full ml-4"
188
+ >
189
+ <MdDeleteOutline
190
+ style={{ fontSize: "1.5rem", margin: "15px" }}
191
+ />
192
+ </div>
193
+ )}
194
+ </div>
195
+ )}
196
+ </div>
197
+ </div>
198
+ </div>
199
+ {description && (
200
+ <div
201
+ className="HintText self-stretch text-slate-600 text-sm font-normal leading-tight"
202
+ id={name + ":description"}
203
+ >
204
+ {description}
205
+ </div>
206
+ )}
207
+ </div>
208
+ );
209
+ };
210
+
211
+ export default FileInput;
@@ -4,6 +4,7 @@ import { MdDeleteOutline, MdInsertDriveFile } from "react-icons/md";
4
4
  import { IAttachment } from "../../types";
5
5
  import { handleErrors, useFederationContext } from "../../main";
6
6
  import { AxiosError } from "axios";
7
+ import { cn } from "../../utils/utils";
7
8
 
8
9
  interface FileInputMultipleProps {
9
10
  name: string;
@@ -13,10 +14,10 @@ interface FileInputMultipleProps {
13
14
  required?: boolean;
14
15
  description?: string;
15
16
  disabled?: boolean;
16
- errors?: { [key: string]: { message: string } };
17
17
  initialFilesReadOnly?: boolean;
18
18
  attachmentName?: string;
19
19
  attachmentType?: string;
20
+ hasError?: boolean;
20
21
  }
21
22
 
22
23
  interface IAttachmentReadOnly extends IAttachment {
@@ -33,10 +34,10 @@ const FileInputMultiple: React.FC<FileInputMultipleProps> = ({
33
34
  required,
34
35
  description,
35
36
  disabled,
36
- errors = {},
37
37
  initialFilesReadOnly = true,
38
38
  attachmentName,
39
39
  attachmentType,
40
+ hasError,
40
41
  }) => {
41
42
  const [fileDataList, setFileDataList] = useState<IAttachmentReadOnly[]>(
42
43
  initialFiles
@@ -163,10 +164,12 @@ const FileInputMultiple: React.FC<FileInputMultipleProps> = ({
163
164
  </label>
164
165
  )}
165
166
  <div
166
- className={
167
- `self-stretch px-3 py-2 rounded-lg justify-start items-center gap-2 outline-none border bg-transparent ` +
168
- ` ${errors[name]?.message ? "border-red-200" : "border-gray-300"} `
169
- }
167
+ className={cn(
168
+ `self-stretch px-3 py-2 rounded-lg justify-start items-center gap-2 outline-none border bg-transparent `,
169
+ hasError &&
170
+ "outline-4 outline-red-200 outline-offset-0 border-none ",
171
+ disabled ? "bg-gray-100" : "bg-transparent"
172
+ )}
170
173
  >
171
174
  {!disabled && (
172
175
  <div
@@ -222,14 +225,6 @@ const FileInputMultiple: React.FC<FileInputMultipleProps> = ({
222
225
  {description}
223
226
  </div>
224
227
  )}
225
- {errors[name] && (
226
- <div
227
- className="HintText self-stretch text-red-600 text-sm font-normal leading-tight"
228
- id={name + ":error"}
229
- >
230
- {errors[name]?.message}
231
- </div>
232
- )}
233
228
  </div>
234
229
  );
235
230
  };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@addsign/moje-agenda-shared-lib",
3
3
  "private": false,
4
- "version": "2.0.62",
4
+ "version": "2.0.63",
5
5
  "type": "module",
6
6
  "main": "dist/main.js",
7
7
  "types": "dist/main.d.ts",