esoftplay 0.0.219 → 0.0.220
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/modules/lib/compose.tsx +197 -148
- package/modules/lib/curl.ts +15 -1
- package/package.json +1 -1
package/modules/lib/compose.tsx
CHANGED
|
@@ -1,168 +1,40 @@
|
|
|
1
1
|
// withHooks
|
|
2
|
-
|
|
3
|
-
import { LibNavigation } from 'esoftplay/cache/lib/navigation/import';
|
|
2
|
+
|
|
4
3
|
import { LibObject } from 'esoftplay/cache/lib/object/import';
|
|
4
|
+
import { LibUtils } from 'esoftplay/cache/lib/utils/import';
|
|
5
5
|
import { EspRouterInterface } from 'esoftplay/cache/routers';
|
|
6
|
+
import { UserRoutes } from 'esoftplay/cache/user/routes/import';
|
|
6
7
|
import esp from 'esoftplay/esp';
|
|
7
8
|
import useGlobalState from 'esoftplay/global';
|
|
8
|
-
import
|
|
9
|
-
import
|
|
10
|
-
|
|
11
|
-
export interface RNTypes extends EspRouterInterface {
|
|
12
|
-
"View": typeof View,
|
|
13
|
-
"Text": typeof Text,
|
|
14
|
-
"ScrollView": typeof ScrollView,
|
|
15
|
-
}
|
|
9
|
+
import useLazyState from 'esoftplay/lazy';
|
|
10
|
+
import React, { ReactElement } from 'react';
|
|
11
|
+
import * as RNCOMPONENT from "react-native";
|
|
16
12
|
|
|
17
|
-
const formState = useGlobalState({})
|
|
18
13
|
|
|
19
|
-
|
|
20
|
-
const { component, props } = json;
|
|
14
|
+
export interface LibComposeArgs {
|
|
21
15
|
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
children = buildUIFromJSON(props?.children);
|
|
28
|
-
} else {
|
|
29
|
-
children = props.children;
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
let _props = props
|
|
33
|
-
|
|
34
|
-
if (props) {
|
|
35
|
-
Object.keys(props).map((key) => {
|
|
36
|
-
if (typeof props[key] == 'string' && String(props[key]).includes("#esp|")) {
|
|
37
|
-
const cleanFunction = (props[key]).replace("#esp|", "")
|
|
38
|
-
const [modules, func, args] = cleanFunction.split("|")
|
|
39
|
-
_props[key] = () => {
|
|
40
|
-
esp.mod(modules)[func](...eval(args))
|
|
41
|
-
}
|
|
42
|
-
} else if (typeof props[key] == 'string' && String(props[key]).includes("#espProp|")) {
|
|
43
|
-
const cleanFunction = (props[key]).replace("#espProp|", "")
|
|
44
|
-
const [modules, func, args] = cleanFunction.split("|")
|
|
45
|
-
_props[key] = () => {
|
|
46
|
-
esp.modProp(modules)[func](...eval(args))
|
|
47
|
-
}
|
|
48
|
-
} else if (typeof props[key] == 'string' && String(props[key]).includes("#action.")) {
|
|
49
|
-
const cleanFunction = (props[key]).replace("#action.", "")
|
|
50
|
-
const [func, argument] = cleanFunction.split('.')
|
|
51
|
-
_props[key] = () => {
|
|
52
|
-
action[func](eval(argument))
|
|
53
|
-
}
|
|
54
|
-
} else if (typeof props[key] == 'string' && String(props[key]).includes("#form.")) {
|
|
55
|
-
const cleanForm = (props[key]).replace("#form.", "")
|
|
56
|
-
const [type, postkey] = cleanForm.split('.')
|
|
57
|
-
if (type == 'input') {
|
|
58
|
-
_props[key] = (input: string) => {
|
|
59
|
-
formState.set(LibObject.set(formState.get(), input)(postkey))
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
if (type == 'select') {
|
|
63
|
-
const [_key, _value] = postkey.split(":")
|
|
64
|
-
_props[key] = () => {
|
|
65
|
-
formState.set(LibObject.set(formState.get(), _value)(_key))
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
} else {
|
|
69
|
-
_props[key] = props[key]
|
|
70
|
-
}
|
|
71
|
-
})
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
// Create the element based on the component type
|
|
75
|
-
if (component?.includes?.('/')) {
|
|
76
|
-
return esp.mod(component)(_props);
|
|
77
|
-
} else {
|
|
78
|
-
return createElement("RCT" + component, { ..._props, children });
|
|
79
|
-
}
|
|
16
|
+
}
|
|
17
|
+
export interface LibComposeProps {
|
|
18
|
+
schema?: any,
|
|
19
|
+
id?: string,
|
|
20
|
+
children?: ReactElement
|
|
80
21
|
}
|
|
81
22
|
|
|
23
|
+
export type RNTypes = EspRouterInterface & typeof RNCOMPONENT;
|
|
82
24
|
|
|
83
|
-
/** Klik [disini](https://github.com/dev-esoftplay/mobile-docs/blob/main/modules/lib/compose.md) untuk melihat dokumentasi*/
|
|
84
|
-
export default function m({ schema }: { schema: any }): ReactElement {
|
|
85
|
-
return buildUIFromJSON(schema)
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
const action: any = {
|
|
89
|
-
"navigate": (args: any[]) => esp.mod("lib/navigation").navigate(args[0], args[1]),
|
|
90
|
-
"replace": (args: any[]) => esp.mod("lib/navigation").replace(args[0], args[1]),
|
|
91
|
-
"back": (args: any[]) => esp.mod("lib/navigation").back(),
|
|
92
|
-
"copy": (args: any[]) => {
|
|
93
|
-
esp.mod("lib/utils").copyToClipboard(JSON.stringify(args))
|
|
94
|
-
esp.modProp("lib/toast").show(args + " copied!")
|
|
95
|
-
},
|
|
96
|
-
"share": (args: any[]) => {
|
|
97
|
-
esp.mod("lib/utils").share(args?.[0], args?.[1])
|
|
98
|
-
},
|
|
99
|
-
"curl": (args: any[]) => {
|
|
100
|
-
esp.mod("lib/progress").show("Please Wait..")
|
|
101
|
-
let post = args?.[1]
|
|
102
|
-
try {
|
|
103
|
-
post = JSON.parse(args?.[1])
|
|
104
|
-
} catch (error) {
|
|
105
|
-
post = args?.[1]
|
|
106
|
-
}
|
|
107
|
-
post = Object.assign({}, post, formState.get())
|
|
108
|
-
new (esp.mod('lib/curl'))(args[0], post, (res, msg) => {
|
|
109
|
-
esp.modProp("lib/toast").show(msg)
|
|
110
|
-
esp.mod("lib/progress").hide()
|
|
111
|
-
if (res.includes("navigate")) {
|
|
112
|
-
LibNavigation.replace(res.navigate.module, res.navigate.params)
|
|
113
|
-
}
|
|
114
|
-
}, (err) => {
|
|
115
|
-
esp.modProp("lib/toast").show(err.message)
|
|
116
|
-
esp.mod("lib/progress").hide()
|
|
117
|
-
if (err.result.includes("navigate")) {
|
|
118
|
-
LibNavigation.replace(err.result.navigate.module, err.result.navigate.params)
|
|
119
|
-
}
|
|
120
|
-
}, 1)
|
|
121
|
-
}
|
|
122
|
-
}
|
|
123
25
|
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
/** Klik [disini](https://github.com/dev-esoftplay/mobile-docs/blob/main/modules/lib/compose.md#actions) untuk melihat dokumentasi*/
|
|
130
|
-
export const actions = {
|
|
131
|
-
navigate: (module: LibNavigationRoutes, params: any) => {
|
|
132
|
-
return `#action.navigate.[${module},${params}]`
|
|
133
|
-
},
|
|
134
|
-
replace: (module: LibNavigationRoutes, params: any) => {
|
|
135
|
-
return `#action.replace.[${module},${params}]`
|
|
136
|
-
},
|
|
137
|
-
back: () => {
|
|
138
|
-
return `#action.back`
|
|
139
|
-
},
|
|
140
|
-
copy: (args: string) => {
|
|
141
|
-
return `#action.copy.[${args}]`
|
|
142
|
-
},
|
|
143
|
-
share: (url: string, message?: string) => {
|
|
144
|
-
return message ? `#action.share.["${url}", "${message}"]` : `#action.share.["${url}"]`
|
|
145
|
-
},
|
|
146
|
-
curl: (uri: string, post?: any) => {
|
|
147
|
-
return `#action.curl.["${uri}", ${post}]`
|
|
148
|
-
}
|
|
26
|
+
export const schema = useGlobalState<any[]>([])
|
|
27
|
+
const values = useGlobalState({})
|
|
28
|
+
|
|
29
|
+
export function addUi<T extends keyof EspRouterInterface>(obj: { id: string, module: T, schema: any }) {
|
|
30
|
+
schema.set(LibUtils.uniqueArray(LibObject.push(schema.get(), obj)()))
|
|
149
31
|
}
|
|
150
32
|
|
|
151
33
|
/** Klik [disini](https://github.com/dev-esoftplay/mobile-docs/blob/main/modules/lib/compose.md#buildcmpn-props) untuk melihat dokumentasi*/
|
|
152
34
|
export function build<T extends keyof RNTypes>(cmpn: T, props: React.ComponentProps<RNTypes[T]>): any {
|
|
153
35
|
let obj: any = {};
|
|
154
36
|
obj.component = cmpn;
|
|
155
|
-
|
|
156
|
-
if (props) {
|
|
157
|
-
Object.keys(props).map((key) => {
|
|
158
|
-
if (typeof props[key] == 'function') {
|
|
159
|
-
_props[key] = `@clientFn` + String(props[key])
|
|
160
|
-
} else {
|
|
161
|
-
_props[key] = props[key]
|
|
162
|
-
}
|
|
163
|
-
})
|
|
164
|
-
}
|
|
165
|
-
obj.props = { ..._props }; // Shallow copy the props
|
|
37
|
+
obj.props = { ...props }; // Shallow copy the props
|
|
166
38
|
|
|
167
39
|
// If there are children, process them
|
|
168
40
|
if (props?.children) {
|
|
@@ -176,6 +48,183 @@ export function build<T extends keyof RNTypes>(cmpn: T, props: React.ComponentPr
|
|
|
176
48
|
obj.props.children = build(props.children.component, props.children.props);
|
|
177
49
|
}
|
|
178
50
|
}
|
|
179
|
-
|
|
180
51
|
return obj;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
export type onPressInterface = {
|
|
56
|
+
action: 'rn' | 'mod' | 'modProp',
|
|
57
|
+
module: keyof RNTypes,
|
|
58
|
+
function: string,
|
|
59
|
+
params: any[]
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
type Num1to9 = 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9;
|
|
63
|
+
type ParamKeys = `$${Num1to9}`;
|
|
64
|
+
|
|
65
|
+
export type callbackInterface = {
|
|
66
|
+
action: 'rn' | 'mod' | 'modProp',
|
|
67
|
+
module: keyof RNTypes,
|
|
68
|
+
function: string,
|
|
69
|
+
params: ParamKeys[]
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
export function onPress(obj: onPressInterface) {
|
|
73
|
+
return obj
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
export type onChangeTextInterface = {
|
|
77
|
+
label: string
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
export function onChangeText(obj: onChangeTextInterface) {
|
|
81
|
+
return obj
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
export type setValueInterface = {
|
|
85
|
+
label: string,
|
|
86
|
+
value: any
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
export function getValue(key: string) {
|
|
90
|
+
return {
|
|
91
|
+
"@value": key
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
export function getValues() {
|
|
96
|
+
return {
|
|
97
|
+
"@values": "all"
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
export function setValue(obj: setValueInterface) {
|
|
102
|
+
return values.set(LibObject.set(values.get(), obj.value)(obj.label))
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
export function callback(obj: callbackInterface) {
|
|
106
|
+
return {
|
|
107
|
+
...obj,
|
|
108
|
+
"@callback": true
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
function readValueFrom(key: any) {
|
|
113
|
+
if (typeof key === 'object') {
|
|
114
|
+
if (key.hasOwnProperty("@value")) {
|
|
115
|
+
return values.get(key['@value'])
|
|
116
|
+
}
|
|
117
|
+
if (key.hasOwnProperty("@values")) {
|
|
118
|
+
return values.get()
|
|
119
|
+
}
|
|
120
|
+
if (key.hasOwnProperty("@callback")) {
|
|
121
|
+
return (...cbparams: any) => {
|
|
122
|
+
const actualIndex = key.params?.map?.((x: string) => readValueFrom(x.includes("$") ? cbparams[(Number(x.replace('$', '')) - 1)] : x))
|
|
123
|
+
if (key.action == 'mod')
|
|
124
|
+
esp.mod(key.module)[key.function](...actualIndex)
|
|
125
|
+
else if (key.action == 'modProp')
|
|
126
|
+
esp.modProp(key.module)[key.function](...actualIndex)
|
|
127
|
+
else if (key.action == 'rn') {
|
|
128
|
+
//@ts-ignore
|
|
129
|
+
RNCOMPONENT[key.module][key.function](...actualIndex)
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
return key
|
|
134
|
+
}
|
|
135
|
+
return key
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
|
|
139
|
+
function renderUIFromJSON(node: any) {
|
|
140
|
+
if (!node || typeof node !== "object") return null;
|
|
141
|
+
|
|
142
|
+
const { component, props = {} } = node;
|
|
143
|
+
|
|
144
|
+
// Clone props to avoid mutating schema
|
|
145
|
+
let resolvedProps = { ...props };
|
|
146
|
+
|
|
147
|
+
// Replace string event handlers with actual functions
|
|
148
|
+
Object.keys(resolvedProps).forEach((key) => {
|
|
149
|
+
if (typeof resolvedProps[key] === "object") {
|
|
150
|
+
switch (key) {
|
|
151
|
+
case 'onPress':
|
|
152
|
+
const d = resolvedProps[key]
|
|
153
|
+
const thisfunction = () => {
|
|
154
|
+
let params = d?.params?.map((param: any) => readValueFrom(param))
|
|
155
|
+
if (d.action == 'mod')
|
|
156
|
+
esp.mod(d.module)[d.function](...params)
|
|
157
|
+
else if (d.action == 'modProp')
|
|
158
|
+
esp.modProp(d.module)[d.function](...params)
|
|
159
|
+
else if (d.action == 'rn') {
|
|
160
|
+
//@ts-ignore
|
|
161
|
+
RNCOMPONENT[d.module][d.function](...params)
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
resolvedProps[key] = thisfunction
|
|
165
|
+
break;
|
|
166
|
+
case 'onChangeText':
|
|
167
|
+
let c = resolvedProps[key]
|
|
168
|
+
if (c.label) {
|
|
169
|
+
resolvedProps[key] = (text: string) => values.set(LibObject.set(values.get(), text)(c.label))
|
|
170
|
+
}
|
|
171
|
+
break;
|
|
172
|
+
case '@value':
|
|
173
|
+
let b = resolvedProps[key]
|
|
174
|
+
if (b['@value']) {
|
|
175
|
+
resolvedProps[key] = values.get(b["@value"])
|
|
176
|
+
}
|
|
177
|
+
break;
|
|
178
|
+
case '@values':
|
|
179
|
+
let x = resolvedProps[key]
|
|
180
|
+
if (x["@values"]) {
|
|
181
|
+
resolvedProps[key] = values.get()
|
|
182
|
+
}
|
|
183
|
+
break;
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
});
|
|
187
|
+
// Handle children recursively
|
|
188
|
+
let resolvedChildren = null;
|
|
189
|
+
const children = props?.children
|
|
190
|
+
if (children)
|
|
191
|
+
if (Array.isArray(children)) {
|
|
192
|
+
resolvedChildren = children.map((child, idx) => (
|
|
193
|
+
<React.Fragment key={idx}>{renderUIFromJSON(child)}</React.Fragment>
|
|
194
|
+
));
|
|
195
|
+
} else if (typeof children === "object") {
|
|
196
|
+
resolvedChildren = renderUIFromJSON(children);
|
|
197
|
+
} else if (typeof children === "string") {
|
|
198
|
+
resolvedChildren = children;
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
//@ts-ignore
|
|
202
|
+
const Component = component.includes('/') ? esp.mod(component) : RNCOMPONENT[component];
|
|
203
|
+
|
|
204
|
+
if (!Component) {
|
|
205
|
+
console.warn(`Unsupported component type: ${component}`);
|
|
206
|
+
return null;
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
return <Component {...resolvedProps}>{resolvedChildren}</Component>;
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
export default function m(props: LibComposeProps): any {
|
|
213
|
+
let [_schema, _setSchema, _getSchema] = useLazyState(props.schema)
|
|
214
|
+
const [state] = schema.useState()
|
|
215
|
+
|
|
216
|
+
UserRoutes.state().listen(() => {
|
|
217
|
+
if (state.length)
|
|
218
|
+
for (const row of state) {
|
|
219
|
+
if (UserRoutes.getCurrentRouteName() == row?.module && row?.schema && row.id == props.id) {
|
|
220
|
+
_setSchema(row?.schema)
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
_setSchema(_getSchema())()
|
|
224
|
+
})
|
|
225
|
+
|
|
226
|
+
return Boolean(_schema)
|
|
227
|
+
? renderUIFromJSON(_schema)
|
|
228
|
+
:
|
|
229
|
+
props.children ? props.children : null
|
|
181
230
|
}
|
package/modules/lib/curl.ts
CHANGED
|
@@ -131,7 +131,21 @@ export default class m {
|
|
|
131
131
|
|
|
132
132
|
/** Klik [disini](https://github.com/dev-esoftplay/mobile-docs/blob/main/modules/lib/curl.md#ondoneresult-any-msg-string-void) untuk melihat dokumentasi*/
|
|
133
133
|
protected onDone(result: any, msg?: string): void {
|
|
134
|
-
|
|
134
|
+
if (result?.compose?.module) {
|
|
135
|
+
// const a = [
|
|
136
|
+
// {
|
|
137
|
+
// module: "payment/detail",
|
|
138
|
+
// id: "HxT7sbcd",
|
|
139
|
+
// schema: "a"
|
|
140
|
+
// },
|
|
141
|
+
// {
|
|
142
|
+
// module: "payment/detail",
|
|
143
|
+
// id: "HxT7sbcad",
|
|
144
|
+
// schema: "a"
|
|
145
|
+
// }
|
|
146
|
+
// ]
|
|
147
|
+
esp.modProp("lib/compose").addUi(result.compose)
|
|
148
|
+
}
|
|
135
149
|
}
|
|
136
150
|
|
|
137
151
|
/** Klik [disini](https://github.com/dev-esoftplay/mobile-docs/blob/main/modules/lib/curl.md#onfailederror-any-timeout-boolean-void) untuk melihat dokumentasi*/
|