@modernman00/shared-js-lib 1.2.43 → 1.2.45
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/AcctMgt/update.js +81 -0
- package/Utility.js +8 -6
- package/config/version.php +1 -1
- package/index.js +1 -0
- package/package.json +1 -1
|
@@ -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
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
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
|
/**
|
package/config/version.php
CHANGED
package/index.js
CHANGED
package/package.json
CHANGED