forked from SignalK/signalk-server
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEmbedded.js
More file actions
88 lines (79 loc) · 2.78 KB
/
Embedded.js
File metadata and controls
88 lines (79 loc) · 2.78 KB
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
import React, { Component, Suspense } from 'react'
import { connect } from 'react-redux'
import { toLazyDynamicComponent, APP_PANEL } from './dynamicutilities'
import Login from '../../views/security/Login'
import ReconnectingWebSocket from 'reconnecting-websocket'
const wsProto = window.location.protocol == 'https:' ? 'wss' : 'ws'
class Embedded extends Component {
constructor(props) {
super(props)
this.state = {
component: toLazyDynamicComponent(this.props.match.params.moduleId, APP_PANEL)
}
this.websockets = []
this.adminUI = {
hideSideBar: () => {
window.dispatchEvent(new Event('sidebar:hide'))
},
getApplicationUserData:
(appDataVersion, path = '') =>
fetch(`/signalk/v1/applicationData/user/${this.props.match.params.moduleId}/${appDataVersion}${path}`,
{ credentials: 'include' }).then(r => {
if (r.status != 200) {
throw new Error(r)
}
return r
}).then(r => r.json()),
setApplicationUserData:
(appDataVersion, data = {}, path = '') =>
fetch(`/signalk/v1/applicationData/user/${this.props.match.params.moduleId}/${appDataVersion}${path}`,
{
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(data),
credentials: 'include'
}).then(r => {
if (r.status != 200) {
throw new Error(r)
}
return r
}),
openWebsocket: (params) => {
const knownParams = ['subscribe', 'sendCachedValues']
const queryParam = knownParams.map((p,i) => [i, params[p]]).filter(x => x[1] !== undefined).map(([i,v]) => `${knownParams[i]}=${v}`).join('&')
const ws = new ReconnectingWebSocket(`${wsProto}://${window.location.host}/signalk/v1/stream?${queryParam}`)
this.websockets.push(ws)
return ws
},
get: ({context, path}) => {
const cParts = context.split('.')
return fetch(`/signalk/v1/api/${cParts[0]}/${cParts.slice(1).join('.')}/${path}`, {
credentials: 'include'
})
},
Login
}
}
componentWillUnmount() {
this.websockets.forEach((ws) => {
try {
ws.close()
} catch(e) {
console.error(e)
}
})
}
render() {
return (
<div style={{ backgroundColor: 'aliceblue', height: 'calc(100vh - 105px)' }}>
<Suspense fallback='Loading...'>
{React.createElement(this.state.component, { ...this.props, adminUI: this.adminUI })}
</Suspense>
</div>
)
}
}
const mapStateToProps = ({ loginStatus }) => ({ loginStatus })
export default connect(mapStateToProps)(Embedded)