-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSimulationComponent.jsx
171 lines (147 loc) · 5.12 KB
/
SimulationComponent.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
import * as Klutch from '@klutchcard/klutch-components';
import { Asset } from 'expo-asset';
import React, { useEffect, useState, useCallback } from "react";
import equal from "deep-equal";
import axios from "axios";
import * as Linking from 'expo-linking';
const SimulationComponent = ({serverUrl, token, template, type, name, data, onLoadTemplate, panelConfigCallback, recipeInstallId}) => {
const [content, setContent] = useState(null)
const [templateState, setTemplateState] = useState()
const [reRender, setReRender] = useState(0)
const [config, setConfig] = useState({})
const [initCallback, setInitCallback] = useState()
const [init, setInit] = useState(false)
useEffect(() => {
const run = async () => {
if(template) {
const mod = await Asset.loadAsync(template)
const resp = await fetch(mod[0].uri);
const doc = await resp.text()
setContent(doc)
}
}
run()
})
useEffect(() => {
setInit(false)
}, [template])
useEffect(() => {
if (initCallback) {
if (isPromise(initCallback)) {
initCallback.then()
} else {
initCallback()
}
}
}, [init])
const react = React
if (!content) {
return <Klutch.KText>Loading...</Klutch.KText>
}
const simulationContext = {
loadTemplate(templateName, templateData) {
onLoadTemplate && onLoadTemplate(templateName, templateData)
},
setState(stateObj) {
setTemplateState(obj => {
var target = obj || {}
const resp = Object.assign(target, stateObj)
setReRender(r => r + 1)
return resp
})
},
state: templateState,
recipeInstallId: recipeInstallId,
async post(path, data) {
try {
const resp = await axios.post(`${serverUrl}${path}`, data, {
method: "POST",
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${token}`
}
})
return resp.data
} catch (e) {
console.error("Error on post ", e)
}
},
async get(path, data) {
try {
const resp = await axios.get(`${serverUrl}${path}`, {
method: "GET",
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${token}`
},
data: data
})
return resp.data
} catch (e) {
console.error(e)
}
},
async delete(path, data) {
try {
const resp = await axios.delete(`${serverUrl}${path}`, {
method: "DELETE",
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${token}`
},
data: data
})
return resp.data
} catch (e) {
console.error(e)
}
},
openExternalUrl(url) {
Linking.openURL(url);
},
closeMiniApp() {
onLoadTemplate();
},
changePanelData(panelId, data) {
},
redirect(path) {
console.log(`redirect to ${path}`)
},
setPanelConfig(conf) {
if (!equal(config,conf)) {
setConfig(conf)
}
},
init(callback) {
if(!init) {
setInitCallback(callback)
setInit(true)
}
}
}
const drawTemplate = function() {
const r = eval(content)
const React = react
const { DateTime } = require("luxon");
const Victory = require("victory-native")
const AlloyJS = require("@klutchcard/alloy-js")
return r(data, simulationContext)
}
if (type === "fullscreen") {
return (
<Klutch.KScrollScreen style={config.backgroundColor ? {backgroundColor: config}: null}>
{drawTemplate()}
</Klutch.KScrollScreen>
)
} else {
return (
<Klutch.KScreen>
<Klutch.KMiniAppPanel recipeName={name} panelStyle={type}>
{drawTemplate()}
</Klutch.KMiniAppPanel>
</Klutch.KScreen>
)
}
}
const isPromise = v => typeof v === 'object' && typeof v.then === 'function'
export default SimulationComponent