fix publicEvents and eventHandlers

This commit is contained in:
srgooglo 2022-05-31 01:53:03 +02:00
parent ade7db35dc
commit 36262132f3

View File

@ -76,18 +76,24 @@ class App extends React.Component {
window.app.version = config.package.version window.app.version = config.package.version
} }
static eventsHandlers = { publicEvents = {
"app.createLogin": async function () { "clearAllOverlays": function () {
window.app.DrawerController.closeAll()
},
}
eventsHandlers = {
"app.createLogin": async () => {
app.DrawerController.open("login", Login, { app.DrawerController.open("login", Login, {
componentProps: { componentProps: {
sessionController: this.sessionController sessionController: this.sessionController
} }
}) })
}, },
"session.logout": async function () { "session.logout": async () => {
await this.sessionController.logout() await this.sessionController.logout()
}, },
"new_session": async function () { "new_session": async () => {
await this.flushState() await this.flushState()
await this.initialization() await this.initialization()
@ -96,43 +102,39 @@ class App extends React.Component {
this.beforeLoginLocation = null this.beforeLoginLocation = null
} }
}, },
"destroyed_session": async function () { "destroyed_session": async () => {
await this.flushState() await this.flushState()
app.eventBus.emit("forceToLogin") app.eventBus.emit("forceToLogin")
}, },
"forceToLogin": function () { "forceToLogin": () => {
// if (window.location.pathname !== "/login") { app.setLocation("/main")
// this.beforeLoginLocation = window.location.pathname
// }
// window.app.setLocation("/login")
app.eventBus.emit("app.createLogin") app.eventBus.emit("app.createLogin")
}, },
"invalid_session": async function (error) { "invalid_session": async (error) => {
if (!this.state.session) {
return false
}
await this.sessionController.forgetLocalSession() await this.sessionController.forgetLocalSession()
await this.flushState() await this.flushState()
if (window.location.pathname !== "/login") { app.eventBus.emit("forceToLogin")
app.eventBus.emit("forceToLogin")
antd.notification.open({ antd.notification.open({
message: <Translation> message: <Translation>
{(t) => t("Invalid Session")} {(t) => t("Invalid Session")}
</Translation>, </Translation>,
description: <Translation> description: <Translation>
{(t) => t(error)} {(t) => t(error)}
</Translation>, </Translation>,
icon: <Icons.MdOutlineAccessTimeFilled />, icon: <Icons.MdOutlineAccessTimeFilled />,
}) })
}
}, },
"clearAllOverlays": function () { "websocket_connected": () => {
window.app.DrawerController.closeAll()
},
"websocket_connected": function () {
if (this.wsReconnecting) { if (this.wsReconnecting) {
this.wsReconnectingTry = 0 this.wsReconnectingTry = 0
this.wsReconnecting = false this.wsReconnecting = false
this.initialization() this.initialization()
setTimeout(() => { setTimeout(() => {
@ -143,7 +145,7 @@ class App extends React.Component {
}, 500) }, 500)
} }
}, },
"websocket_connection_error": function () { "websocket_connection_error": () => {
if (!this.wsReconnecting) { if (!this.wsReconnecting) {
this.latencyWarning = null this.latencyWarning = null
this.wsReconnectingTry = 0 this.wsReconnectingTry = 0
@ -162,7 +164,7 @@ class App extends React.Component {
window.location.reload() window.location.reload()
} }
}, },
"websocket_latency_too_high": function () { "websocket_latency_too_high": () => {
if (!this.latencyWarning) { if (!this.latencyWarning) {
this.latencyWarning = true this.latencyWarning = true
@ -173,7 +175,7 @@ class App extends React.Component {
}) })
} }
}, },
"websocket_latency_normal": function () { "websocket_latency_normal": () => {
if (this.latencyWarning) { if (this.latencyWarning) {
this.latencyWarning = null this.latencyWarning = null
@ -251,6 +253,14 @@ class App extends React.Component {
}, },
} }
constructor(props) {
super(props)
Object.keys(this.eventsHandlers).forEach((event) => {
app.eventBus.on(event, this.eventsHandlers[event])
})
}
flushState = async () => { flushState = async () => {
await this.setState({ session: null, user: null }) await this.setState({ session: null, user: null })
} }