@visns-studio/visns-components 3.8.5 → 3.8.6

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.
@@ -72,6 +72,16 @@ const renderValue = (field, data) => {
72
72
  }
73
73
  };
74
74
 
75
+ const slugify = (text) => {
76
+ return text
77
+ .toString()
78
+ .toLowerCase()
79
+ .trim()
80
+ .replace(/\s+/g, '-') // Replace spaces with -
81
+ .replace(/[^\w\-]+/g, '') // Remove all non-word chars
82
+ .replace(/\-\-+/g, '-'); // Replace multiple - with single -
83
+ };
84
+
75
85
  const GenericDynamic = ({
76
86
  data,
77
87
  fetchData,
@@ -104,7 +114,6 @@ const GenericDynamic = ({
104
114
  updateField(prevState, name, newValue)
105
115
  );
106
116
  } else if (files && files[0]) {
107
- console.info('aa');
108
117
  const toastId = toast.loading('Uploading file...'); // Show loading toast
109
118
  // Start the upload process and read the file if it's an image
110
119
  Vapor.store(files[0], {
@@ -285,17 +294,38 @@ const GenericDynamic = ({
285
294
  };
286
295
 
287
296
  const handleDownloadForm = async (item, label, data) => {
288
- // Show loading toast and keep its ID
289
297
  const toastId = toast.loading('Downloading form...');
290
298
 
291
299
  try {
292
- const res = await Download(setting.download, 'POST', {
300
+ // Assuming Download returns an object with { data: Blob, headers: Headers }
301
+ const response = await Download(setting.download, 'POST', {
293
302
  data: item,
294
303
  dynamicData: data,
295
304
  label: label,
296
305
  });
297
306
 
298
- if (res.data) {
307
+ if (response && response.data) {
308
+ const contentDisposition = response.headers.get(
309
+ 'Content-Disposition'
310
+ );
311
+ let filename = null; // Default to null, letting saveAs decide the filename
312
+
313
+ if (contentDisposition) {
314
+ const filenameMatch = contentDisposition.match(
315
+ /filename[^;=\n]*=((['"]).*?\2|[^;\n]*)/
316
+ );
317
+ if (filenameMatch && filenameMatch[1]) {
318
+ filename = filenameMatch[1].replace(/['"]/g, ''); // Remove potential quotes
319
+ }
320
+ }
321
+
322
+ // Perform the download using FileSaver.js
323
+ if (filename) {
324
+ saveAs(response.data, filename);
325
+ } else {
326
+ saveAs(response.data); // No filename extracted, let FileSaver handle it
327
+ }
328
+
299
329
  // Update loading toast to a success message before closing it
300
330
  toast.update(toastId, {
301
331
  render: 'Download successful!',
@@ -304,26 +334,15 @@ const GenericDynamic = ({
304
334
  autoClose: 5000,
305
335
  closeButton: true,
306
336
  });
307
- // Perform the download
308
-
309
- // const fileName = slugify(label) + '.pdf';
310
- // saveAs(res.data, fileName);
311
- saveAs(res.data);
312
337
  } else {
313
- // Update the toast to show an error message
314
- toast.update(toastId, {
315
- render: 'Error downloading form',
316
- type: 'error',
317
- isLoading: false,
318
- autoClose: 5000,
319
- closeButton: true,
320
- });
338
+ // Handle the case where there is no data in the response
339
+ throw new Error('No data received from the server');
321
340
  }
322
341
  } catch (err) {
323
342
  console.error(err);
324
343
  // Update the toast to show an error message
325
344
  toast.update(toastId, {
326
- render: 'Error downloading form',
345
+ render: 'Error downloading form: ' + err.message,
327
346
  type: 'error',
328
347
  isLoading: false,
329
348
  autoClose: 5000,
package/package.json CHANGED
@@ -54,7 +54,7 @@
54
54
  "react-dom": "^17.0.1"
55
55
  },
56
56
  "name": "@visns-studio/visns-components",
57
- "version": "3.8.5",
57
+ "version": "3.8.6",
58
58
  "description": "Various packages to assist in the development of our Custom Applications.",
59
59
  "main": "index.js",
60
60
  "scripts": {