@modernman00/shared-js-lib 1.2.43 → 1.2.44

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.
@@ -0,0 +1,81 @@
1
+ import {id } from './processAll.js';
2
+ import { bindEvent } from '../Utility.js';
3
+ import { msgException } from '../ShowResponse.js';
4
+ import axios from 'axios';
5
+ import { redirectAfterDelay, parseErrorResponse } from './general.js';
6
+ /**
7
+ * Creates a reusable form submission handler for update forms.
8
+ *
9
+ * @function update
10
+ * @param {Object} options - Configuration for the handler.
11
+ * @param {string} options.formId - ID of the form element.
12
+ * @param {string} options.buttonId - ID of the submit button.
13
+ * @param {string} options.route - API endpoint for submission.
14
+ * @param {string} options.redirect - URL to redirect to after success.
15
+ *
16
+ * @example
17
+ * update({
18
+ * formId: 'updateForm',
19
+ * buttonId: 'updateButton',
20
+ * route: appTestRoutes.appTestUpdate,
21
+ * redirect: appTestRoutes.appTestUpdateRedirect
22
+ * });
23
+ */
24
+ export function update({
25
+ formId,
26
+ buttonId,
27
+ route,
28
+ redirect,
29
+ }) {
30
+ const handler = async (e) => {
31
+ e.preventDefault();
32
+
33
+ const form = id(formId);
34
+ if (!form) {
35
+ msgException(`Form with ID "${formId}" not found.`);
36
+ return;
37
+ }
38
+
39
+ const formData = new FormData();
40
+ const inputs = form.querySelectorAll('[name]');
41
+
42
+ inputs.forEach(input => {
43
+ const name = input.name;
44
+ const original = input.getAttribute('data-original');
45
+ const current = input.type === 'file' ? input.files[0] : input.value;
46
+
47
+ // Only include changed fields
48
+ if (input.type === 'file') {
49
+ if (current) formData.append(name, current);
50
+ } else if (current !== original) {
51
+ formData.append(name, current);
52
+ }
53
+ });
54
+
55
+ try {
56
+
57
+ const response = await axios.post(route, formData);
58
+
59
+ const message = response.data.message;
60
+
61
+ const notificationId = id(`${formId}_notification`) || formId;
62
+
63
+ notificationId.scrollIntoView({ behavior: 'smooth' });
64
+
65
+ notificationId.innerHTML = message;
66
+
67
+
68
+ if (redirect) {
69
+ redirectAfterDelay(redirect, 2000);
70
+ }
71
+
72
+ } catch (error) {
73
+ console.log(error);
74
+ const ErrorMsg = parseErrorResponse(error);
75
+ notificationId.innerHTML = `<li style="color:white; background-color:red">${ErrorMsg}</li>`;
76
+ }
77
+ };
78
+
79
+ bindEvent({ id: buttonId, handler });
80
+ }
81
+
package/Utility.js CHANGED
@@ -35,14 +35,16 @@ export const realTimeCheckLen = (input, maxi) => {
35
35
  *
36
36
  * @param {string} str The string to convert to sentence case.
37
37
  * @returns {string} A new string in sentence case.
38
+ * @throws {Error} If the input is not a string or is empty.
38
39
  */
39
40
  export const toSentenceCase = (str) => {
40
- if (typeof str !== 'string') return '';
41
- return str
42
- .toLowerCase() // Convert the string to lowercase
43
- .split(' ') // Split the string into words
44
- .map(word => word.charAt(0).toUpperCase() + word.slice(1)) // Capitalize the first letter of each word
45
- .join(' '); // Join the words back into a string
41
+ if (str || typeof str == 'string') {
42
+ return str
43
+ .toLowerCase() // Convert the string to lowercase
44
+ .split(' ') // Split the string into words
45
+ .map(word => word.charAt(0).toUpperCase() + word.slice(1)) // Capitalize the first letter of each word
46
+ .join(' '); // Join the words back into a string
47
+ }
46
48
  }
47
49
 
48
50
  /**
@@ -1,3 +1,3 @@
1
1
  <?php
2
- define('APP_VERSION', 'v1.2.44');
2
+ define('APP_VERSION', 'v1.2.45');
3
3
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@modernman00/shared-js-lib",
3
- "version": "1.2.43",
3
+ "version": "1.2.44",
4
4
  "description": "Reusable JS utilities for numerous js problems",
5
5
  "homepage": "https://github.com/modernman00/shared-js-lib#readme",
6
6
  "keywords": [