import React from 'react'; import ErrorMessage from 'webodm/components/ErrorMessage'; import PropTypes from 'prop-types'; import './Dashboard.scss'; import $ from 'jquery'; import { _ } from 'webodm/classes/gettext'; export default class Dashboard extends React.Component { static defaultProps = { }; static propTypes = { apiKey: PropTypes.string.isRequired, onLogout: PropTypes.func.isRequired } constructor(props){ super(props); this.state = { error: "", loading: true, loadingMessage: "", user: null, nodes: [], syncingNodes: false } } apiUrl = url => { return `https://webodm.net${url}?api_key=${this.props.apiKey}`; }; componentDidMount = () => { this.loadDashboard(); } loadDashboard = () => { this.setState({loading: true, loadingMessage: _("Loading dashboard...")}); $.get(this.apiUrl('/r/user')).done(json => { if (json.balance !== undefined){ this.setState({ loading: false, user: json }); this.handleSyncProcessingNode(); }else if (json.message === "Unauthorized"){ this.props.onLogout(); }else{ this.setState({ loading: false, error: `Cannot load lightning dashboard. Server responded: ${JSON.stringify(json)}. Are you running the latest version of WebODM?` }); } }) .fail(() => { this.setState({ loading: false, error: `Cannot load lightning dashboard. Please make sure you are connected to the internet, or try again in an hour.`}); }); } handleSyncProcessingNode = () => { if (!this.state.user) return; const { node, tokens } = this.state.user; if (!node || !tokens) return; this.setState({syncingNodes: true, nodes: []}); $.post('sync_processing_node', { hostname: node.hostname, port: node.port, token: tokens[0].id }).done(json => { if (json.error){ this.setState({error: json.error}); }else{ this.setState({nodes: json}); } }) .fail(e => { this.setState({error: `Cannot sync nodes: ${JSON.stringify(e)}. Are you connected to the internet?`}); }) .always(() => { this.setState({syncingNodes: false}); }); } handeLogout = () => { this.setState({loading: true, loadingMessage: "Logging out..."}); $.post("save_api_key", { api_key: "" }).done(json => { if (!json.success){ this.setState({error: `Cannot logout: ${JSON.stringify(json)}`}); } this.setState({loading: false}); this.props.onLogout(); }).fail(e => { this.setState({loading: false, error: `Cannot logout: ${JSON.stringify(e)}`}); }); } handleBuyCredits = () => { window.open("https://webodm.net/dashboard?bc=0"); } handleRefresh = () => { this.loadDashboard(); } handleOpenDashboard = () => { window.open("https://webodm.net/dashboard"); } render(){ const { loading, loadingMessage, user, syncingNodes, nodes } = this.state; let balance = ""; if (user){ balance = ({ user.balance } credits); if (user.plan !== null){ balance = (Unlimited); } } return (
{ loading ?
{ loadingMessage }
:
{ user ?
Balance: { balance }

Hello, { user.email }

Synced Nodes
{ syncingNodes ? :
}
{nodes.length > 0 ?

You are all set! When creating a new task from the Dashboard, select {nodes[0].label} from the Processing Node drop-down instead of Auto.
: ""}

: ""}
}
); } }