@moamc/rn-cli 1.4.5 → 1.5.0
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/package.json +1 -1
- package/templates/hookTemplate.js +48 -1
package/package.json
CHANGED
|
@@ -36,12 +36,58 @@ const generateInitialValues = (formFields = []) => {
|
|
|
36
36
|
return `\t\tloader: false,\n${fields}${additionalFields}`;
|
|
37
37
|
};
|
|
38
38
|
|
|
39
|
+
const { detectProjectStructure } = require('../utils/fileUtils');
|
|
40
|
+
|
|
39
41
|
const generateHookTemplate = (hookName, hasForm = false, formFields = [], eventName = null) => {
|
|
42
|
+
const { hasHeadersFolder } = detectProjectStructure();
|
|
43
|
+
const isDistributorStyle = !hasHeadersFolder;
|
|
44
|
+
|
|
40
45
|
if (hasForm) {
|
|
41
46
|
const hasDropdown = formFields.some(f => f.type === 'dropdown');
|
|
42
47
|
const hasDate = formFields.some(f => f.type === 'date');
|
|
43
48
|
|
|
44
|
-
|
|
49
|
+
if (isDistributorStyle) {
|
|
50
|
+
// Distributor app - use Formik
|
|
51
|
+
return `import { useFormik } from 'formik';
|
|
52
|
+
import { showToastNotification } from '../../../../helpers/common';${hasDate ? '\nimport moment from \'moment\';' : ''}
|
|
53
|
+
import { validate${hookName} } from '../validation';
|
|
54
|
+
|
|
55
|
+
export const use${hookName} = ({ navigation }) => {
|
|
56
|
+
\tconst formikState = useFormik({
|
|
57
|
+
\t\tinitialValues: {
|
|
58
|
+
${formFields.map(f => `\t\t\t${f.name}: '',`).join('\n')}
|
|
59
|
+
\t\t},
|
|
60
|
+
\t\tvalidate: validate${hookName},
|
|
61
|
+
\t\tonSubmit: async (values) => {
|
|
62
|
+
\t\t\ttry {
|
|
63
|
+
\t\t\t\t// Add your API call here
|
|
64
|
+
\t\t\t\tshowToastNotification('Success!');
|
|
65
|
+
\t\t\t} catch (error) {
|
|
66
|
+
\t\t\t\tshowToastNotification(error?.message || 'Something went wrong');
|
|
67
|
+
\t\t\t}
|
|
68
|
+
\t\t},
|
|
69
|
+
\t});
|
|
70
|
+
|
|
71
|
+
\tconst { values: formData, errors, touched, isSubmitting: loading, handleChange: formikHandleChange, handleSubmit } = formikState;
|
|
72
|
+
|
|
73
|
+
\tconst handleChange = (value, field) => {
|
|
74
|
+
\t\tformikState.setFieldValue(field, value);
|
|
75
|
+
\t\tformikState.setFieldTouched(field, true);
|
|
76
|
+
\t};
|
|
77
|
+
|
|
78
|
+
\treturn {
|
|
79
|
+
\t\tformData,
|
|
80
|
+
\t\terrors,
|
|
81
|
+
\t\ttouched,
|
|
82
|
+
\t\tloading,
|
|
83
|
+
\t\thandleChange,
|
|
84
|
+
\t\thandleSubmit,
|
|
85
|
+
\t};
|
|
86
|
+
};
|
|
87
|
+
`;
|
|
88
|
+
} else {
|
|
89
|
+
// Investor app - use useState with useCallback
|
|
90
|
+
return `import { useState, useCallback } from 'react';
|
|
45
91
|
import { showToastNotification } from '../../../../helpers/common';${hasDate ? '\nimport moment from \'moment\';' : ''}
|
|
46
92
|
import { validate${hookName} } from '../validation';
|
|
47
93
|
|
|
@@ -92,6 +138,7 @@ ${formFields.map(f => `\t\t${f.name}: '',`).join('\n')}
|
|
|
92
138
|
\t};
|
|
93
139
|
};
|
|
94
140
|
`;
|
|
141
|
+
}
|
|
95
142
|
}
|
|
96
143
|
|
|
97
144
|
return `import { useState, useEffect } from 'react';
|