@ssplib/react-components 0.0.118 → 0.0.120

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,11 +1,12 @@
1
1
  /// <reference types="react" />
2
- export default function FileUpload({ name, tipoArquivo, title, required, multiple, apiURL, xs, sm, md, }: {
2
+ export default function FileUpload({ name, tipoArquivo, title, required, multiple, apiURL, sizeLimit, xs, sm, md, }: {
3
3
  name: string;
4
4
  tipoArquivo: string;
5
5
  title: string;
6
6
  apiURL: string;
7
7
  required?: boolean;
8
8
  multiple?: boolean;
9
+ sizeLimit?: number;
9
10
  xs?: number;
10
11
  sm?: number;
11
12
  md?: number;
@@ -27,7 +27,6 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
27
27
  };
28
28
  Object.defineProperty(exports, "__esModule", { value: true });
29
29
  const CameraAlt_1 = __importDefault(require("@mui/icons-material/CameraAlt"));
30
- const Clear_1 = __importDefault(require("@mui/icons-material/Clear"));
31
30
  const Delete_1 = __importDefault(require("@mui/icons-material/Delete"));
32
31
  const Done_1 = __importDefault(require("@mui/icons-material/Done"));
33
32
  const InsertDriveFile_1 = __importDefault(require("@mui/icons-material/InsertDriveFile"));
@@ -38,7 +37,11 @@ const lodash_get_1 = __importDefault(require("lodash.get"));
38
37
  const react_1 = __importStar(require("react"));
39
38
  const auth_1 = require("../../../context/auth");
40
39
  const form_1 = require("../../../context/form");
41
- function FileUpload({ name, tipoArquivo, title, required = false, multiple = false, apiURL, xs = 12, sm, md, }) {
40
+ function bytesToMegabytes(bytes) {
41
+ const megabytes = bytes / (1024 * 1024);
42
+ return megabytes;
43
+ }
44
+ function FileUpload({ name, tipoArquivo, title, required = false, multiple = false, apiURL, sizeLimit = 4, xs = 12, sm, md, }) {
42
45
  const context = (0, react_1.useContext)(form_1.FormContext);
43
46
  const { user } = (0, react_1.useContext)(auth_1.AuthContext);
44
47
  const theme = (0, material_1.useTheme)();
@@ -46,13 +49,25 @@ function FileUpload({ name, tipoArquivo, title, required = false, multiple = fal
46
49
  const [files, setFiles] = (0, react_1.useState)([]);
47
50
  const [filesLoaded, setFilesLoaded] = (0, react_1.useState)([]);
48
51
  const [fileIds, setFilesIds] = (0, react_1.useState)({});
49
- const [filesError, setFilesError] = (0, react_1.useState)([]);
52
+ // const [filesError, setFilesError] = useState<number[]>([])
53
+ const [errorMsg, setErrorMsg] = (0, react_1.useState)('');
50
54
  const onFile = (0, react_1.useCallback)((e) => {
51
55
  const newFiles = e.target.files;
52
56
  const filesTo = Object.keys(newFiles).map((key) => newFiles[key]);
53
57
  setFiles([
54
58
  ...files,
55
- ...filesTo.map((file, index) => {
59
+ ...filesTo
60
+ .filter((file) => {
61
+ if (bytesToMegabytes(file.size) > sizeLimit) {
62
+ setErrorMsg(`Por favor, escolha um arquivo com tamanho inferior a ${sizeLimit} MB`);
63
+ setTimeout(() => {
64
+ setErrorMsg('');
65
+ }, 3000);
66
+ return false;
67
+ }
68
+ return true;
69
+ })
70
+ .map((file, index) => {
56
71
  let id = Date.now() + index;
57
72
  // fetch API
58
73
  const fd = new FormData();
@@ -62,12 +77,15 @@ function FileUpload({ name, tipoArquivo, title, required = false, multiple = fal
62
77
  method: 'POST',
63
78
  body: fd,
64
79
  headers: {
65
- Authorization: `Bearer ${user ? user.token : ''}`,
80
+ // Authorization: `Bearer ${user ? user.token : ''}`,
81
+ Authorization: `Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6NywibmFtZSI6IlBlZGluIiwiZW1haWwiOiJnYWl0YWNoaTBAZ21haWwuY29tIiwicGhvbmVfbnVtYmVyIjoiNjE5OTMwNTg0MjMiLCJwcmVmZXJyZWRfdXNlcm5hbWUiOiIwNTUxOTQyOTE2MiIsInJvbGVzIjpbeyJjb2RlIjoxLCJuYW1lIjoiVXNlciBFdmVudG9zIiwic3lzdGVtIjoyfSx7ImNvZGUiOjIsIm5hbWUiOiJBZG1pbiBFdmVudG9zIiwic3lzdGVtIjoyfSx7ImNvZGUiOjQsIm5hbWUiOiJDb25zZWcgVmFsaWRhZG9yIiwic3lzdGVtIjozfSx7ImNvZGUiOjYsIm5hbWUiOiJVc2VyIENvbnNlZyIsInN5c3RlbSI6M31dLCJpYXQiOjE2OTE2MDEyODksImV4cCI6MTY5MjIwNjA4OSwiYXVkIjoiVXNlcnMiLCJpc3MiOiJBdXRoU1NQIiwic3ViIjoiMDU1MTk0MjkxNjIifQ.SNU02JpcWQejBnrG6OMGVof4ALAgSEIAc3qAUnKDHSY`,
66
82
  },
67
83
  })
68
84
  .then((res) => {
69
- if (!res.ok)
70
- setFilesError((fl) => [...fl, id]);
85
+ if (!res.ok) {
86
+ removeFile(id);
87
+ return;
88
+ }
71
89
  res.json().then((j) => {
72
90
  if (j.status && j.status.status === 200) {
73
91
  const fileIdFromApi = j.data[0];
@@ -85,24 +103,28 @@ function FileUpload({ name, tipoArquivo, title, required = false, multiple = fal
85
103
  setFilesIds((ids) => (Object.assign(Object.assign({}, ids), f)));
86
104
  }
87
105
  else {
88
- setFilesError((fl) => [...fl, id]);
106
+ removeFile(id);
89
107
  }
90
108
  });
91
109
  })
92
110
  .catch((err) => {
93
- setFilesError((fl) => [...fl, id]);
94
- console.log(err);
111
+ removeFile(id);
95
112
  });
96
113
  return { id: id, name: file.name, loading: true, error: false, file: file };
97
114
  }),
98
115
  ]);
99
116
  }, [files, context]);
100
- const deleteFile = (e, id) => {
101
- if (filesError.includes(id)) {
102
- setFiles(files.filter((x) => x.id !== id));
103
- context.setFilesUid((fId) => fId.filter((idd) => idd.CO_SEQ_ARQUIVO !== id));
104
- return;
117
+ const removeFile = (id, hideMsg) => {
118
+ setFiles(files.filter((x) => x.id !== id));
119
+ context.setFilesUid((fId) => fId.filter((idd) => idd.CO_SEQ_ARQUIVO !== id));
120
+ if (!hideMsg) {
121
+ setErrorMsg('Erro ao enviar arquivo. Tente novamente mais tarde');
122
+ setTimeout(() => {
123
+ setErrorMsg('');
124
+ }, 3000);
105
125
  }
126
+ };
127
+ const deleteFile = (e, id) => {
106
128
  if (Object.keys(fileIds).includes(id.toString())) {
107
129
  fetch(`${apiURL}/${fileIds[id]}`, {
108
130
  method: 'DELETE',
@@ -111,6 +133,8 @@ function FileUpload({ name, tipoArquivo, title, required = false, multiple = fal
111
133
  },
112
134
  })
113
135
  .then((res) => {
136
+ if (!res.ok)
137
+ removeFile(id, true);
114
138
  if (res.status === 200) {
115
139
  setFiles(files.filter((x) => x.id !== id));
116
140
  context.setFilesUid((fId) => fId.filter((idd) => idd.CO_SEQ_ARQUIVO !== id));
@@ -121,13 +145,11 @@ function FileUpload({ name, tipoArquivo, title, required = false, multiple = fal
121
145
  };
122
146
  (0, react_1.useEffect)(() => {
123
147
  const dt = new DataTransfer();
124
- files
125
- .filter((x) => !filesError.includes(x.id))
126
- .forEach((x) => {
148
+ files.forEach((x) => {
127
149
  dt.items.add(x.file);
128
150
  });
129
151
  context === null || context === void 0 ? void 0 : context.formSetValue(name, dt.files);
130
- }, [files, context, filesError, name]);
152
+ }, [files, context, name]);
131
153
  (0, react_1.useEffect)(() => {
132
154
  return () => {
133
155
  context.setFilesUid((files) => files.filter((x) => x.CO_TIPO_ARQUIVO !== parseInt(tipoArquivo)));
@@ -167,10 +189,13 @@ function FileUpload({ name, tipoArquivo, title, required = false, multiple = fal
167
189
  react_1.default.createElement(system_1.Stack, { direction: 'column' }, files.map((x) => (react_1.default.createElement(system_1.Stack, { key: x.name, direction: 'row', justifyContent: 'space-between', padding: 0.5 },
168
190
  react_1.default.createElement(material_1.Box, null,
169
191
  react_1.default.createElement(system_1.Stack, { direction: 'row', spacing: 2 },
170
- filesLoaded.includes(x.id) ? (react_1.default.createElement(Done_1.default, { sx: { fill: '#06d6a0' } })) : filesError.includes(x.id) ? (react_1.default.createElement(Clear_1.default, { sx: { fill: 'red' } })) : (react_1.default.createElement(material_1.CircularProgress, { size: 22, sx: { color: 'black' } })),
192
+ filesLoaded.includes(x.id) ? react_1.default.createElement(Done_1.default, { sx: { fill: '#06d6a0' } }) : react_1.default.createElement(material_1.CircularProgress, { size: 22, sx: { color: 'black' } }),
171
193
  react_1.default.createElement(PictureAsPdf_1.default, { color: 'error' }),
172
194
  react_1.default.createElement(material_1.Typography, { fontWeight: 600 }, x.name))),
173
- react_1.default.createElement(material_1.Box, null, (filesLoaded.includes(x.id) || filesError.includes(x.id)) && (react_1.default.createElement(material_1.Button, { variant: 'contained', size: 'small', sx: { textTransform: 'none', backgroundColor: '#d1495b', '&:hover': { backgroundColor: '#c1121f' } }, onClick: (e) => deleteFile(e, x.id), startIcon: react_1.default.createElement(Delete_1.default, null) }, "Remover"))))))))),
174
- (0, lodash_get_1.default)(context === null || context === void 0 ? void 0 : context.errors, name) && (react_1.default.createElement(material_1.Typography, { variant: 'caption', color: '#e53935', fontWeight: 600, fontSize: 14, paddingTop: 2 }, "* O campo de arquivo \u00E9 obrigat\u00F3rio")))));
195
+ react_1.default.createElement(material_1.Box, null, filesLoaded.includes(x.id) && (react_1.default.createElement(material_1.Button, { variant: 'contained', size: 'small', sx: { textTransform: 'none', backgroundColor: '#d1495b', '&:hover': { backgroundColor: '#c1121f' } }, onClick: (e) => deleteFile(e, x.id), startIcon: react_1.default.createElement(Delete_1.default, null) }, "Remover"))))))))),
196
+ errorMsg && (react_1.default.createElement(react_1.default.Fragment, null,
197
+ react_1.default.createElement(material_1.Typography, { variant: 'caption', color: '#e53935', fontWeight: 600, fontSize: 14, paddingTop: 2 }, errorMsg),
198
+ react_1.default.createElement("br", null))),
199
+ (0, lodash_get_1.default)(context === null || context === void 0 ? void 0 : context.errors, name) && (react_1.default.createElement(material_1.Typography, { variant: 'caption', color: '#e53935', fontWeight: 600, fontSize: 14 }, "* O campo de arquivo \u00E9 obrigat\u00F3rio")))));
175
200
  }
176
201
  exports.default = FileUpload;
@@ -2,6 +2,7 @@ import React from 'react';
2
2
  interface ColumnData {
3
3
  title: string;
4
4
  keyName: string;
5
+ size?: number;
5
6
  }
6
7
  export declare function Table({ columns, fetchFunc, emptyMsg, dataPath, tableName, csv, columnSize, action, isPublic, statusKeyName, csvExcludeKeys, csvCustomKeyNames, csvExcludeValidate, csvButtonTitle, csvAllButtonTitle, csvShowAllButton, itemCount, }: {
7
8
  columns: ColumnData[];
@@ -308,7 +308,7 @@ function Table({ columns, fetchFunc, emptyMsg = {
308
308
  getMaxItems().length <= 0 ? (react_1.default.createElement(material_1.Stack, { sx: { backgroundColor: '#E2E8F0', padding: 2, marginX: { xs: 2, md: 0 } }, justifyContent: 'center', alignItems: 'center' },
309
309
  react_1.default.createElement(Typography_1.default, { fontSize: 21, fontFamily: 'Inter', fontWeight: 600, textAlign: 'center' }, user ? emptyMsg.user : emptyMsg.public))) : (getMaxItems().map((x, index) => (react_1.default.createElement(material_1.Paper, { key: index, sx: { padding: 0.5, backgroundColor: index % 2 === 0 ? '#E2E8F0' : '#F8FAFC', paddingY: 2 }, elevation: 0 },
310
310
  react_1.default.createElement(Grid_1.default, { container: true, spacing: isSmall ? 2 : 0, paddingX: 2 },
311
- columns.map((c) => (react_1.default.createElement(Grid_1.default, { key: c.keyName + index, item: true, xs: 12, md: 12 / columnSize },
311
+ columns.map((c) => (react_1.default.createElement(Grid_1.default, { key: c.keyName + index, item: true, xs: 12, md: (12 / columnSize) * (!!c.size ? c.size : 1) },
312
312
  react_1.default.createElement(material_1.Box, { sx: { width: 'max-content', paddingX: 1 } },
313
313
  react_1.default.createElement(Typography_1.default, { fontSize: 16, fontWeight: 700, color: '#1E293B', fontFamily: 'Inter' }, c.title)),
314
314
  react_1.default.createElement(material_1.Box, { paddingLeft: 1 },
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ssplib/react-components",
3
- "version": "0.0.118",
3
+ "version": "0.0.120",
4
4
  "description": "SSP React Components",
5
5
  "main": "index.js",
6
6
  "author": "Pedro Henrique <sr.hudrick@gmail.com>",