import React from "react"
import * as antd from "antd"
import { Icons } from "components/Icons"
import { AccountEditor, SessionsView, StatisticsView } from "./components"
import { Session } from "models"
import "./index.less"
const api = window.app.apiBridge
const SelfViewComponents = {
sessionsView: SessionsView,
statisticsView: StatisticsView,
}
const SelfViewTabDecorators = {
sessionsView: (
Sessions
),
statisticsView: (
Statistics
),
}
class SelfView extends React.Component {
renderComponents = () => {
const renderTagDecorator = (key) => {
if (typeof this.props.decorators[key] !== "undefined") {
return this.props.decorators[key]
}
return key
}
return Object.keys(this.props.components).map((key, index) => {
const Component = this.props.components[key]
return (
)
})
}
render() {
return (
{this.renderComponents()}
)
}
}
export default class Account extends React.Component {
static bindApp = ["userController", "sessionController"]
state = {
isSelf: false,
user: null,
sessions: null
}
componentDidMount = async () => {
const token = Session.decodedToken
const location = window.app.history.location
const query = new URLSearchParams(location.search)
const requestedUser = location.state?.username ?? query.get("username") ?? token?.username
let state = this.state
if (requestedUser != null) {
if (token.username === requestedUser) {
state.isSelf = true
state.sessions = await this.props.contexts.app.sessionController.getAllSessions()
}
state.user = await this.props.contexts.app.userController.getData({ username: requestedUser })
}
this.setState(state)
}
handleUpdateUserData = async (changes, callback) => {
const update = {}
if (Array.isArray(changes)) {
changes.forEach((change) => {
update[change.id] = change.value
})
}
await api.put
.selfUser(update)
.then((data) => {
callback(false, data)
})
.catch((err) => {
callback(true, err)
})
window.app.eventBus.emit("forceReloadUser")
}
openUserEdit = () => {
window.app.DrawerController.open("editAccount", AccountEditor, {
props: {
keyboard: false,
width: "45%",
bodyStyle: {
overflow: "hidden",
},
},
componentProps: {
onSave: this.handleUpdateUserData,
user: this.state.user,
},
})
}
renderSelfActions = () => {
if (this.state.isSelf) {
return (
)
}
return null
}
render() {
const user = this.state.user
if (!user) {
return
}
return (
{Boolean(user.fullName) ?
<>
{user.fullName}
@{user.username}#{user._id}
> :
<>
@{user.username}
#{user._id}
>
}
{this.state.isSelf && this.renderSelfActions()}
{this.state.isSelf && (
)}
)
}
}