import React from "react"; import superagent from "superagent"; import AccountsWrapper from "./AccountsWrapper.jsx"; import AccountIPManager from "./Components/AccountIPManager.jsx"; import AccountAccessPolicyManager from "./Components/AccountAccessPolicyManager.jsx"; import { Segment, Header, Button, Form, Message, Divider } from "semantic-ui-react" class AccountOnboarding extends React.Component { constructor(props) { super(props); this.state = { username: "", email: "", loading: false, error: null, success: false, createdAccount: null }; } onChange(e, { name, value }) { this.setState({ [name]: value }); } async onCreateAccount(e) { e.preventDefault(); // Validation if (!this.state.username || this.state.username.trim() === "") { this.setState({ error: "Username is required" }); return; } if (!this.state.email || this.state.email.trim() === "") { this.setState({ error: "Email is required" }); return; } this.setState({ loading: true, error: null }); try { const payload = { username: this.state.username, email: this.state.email }; const result = await superagent.post('/api/admin/onboarding/account') .accept('json') .send(payload); console.log("Account created:", result.body); this.setState({ loading: false, success: true, createdAccount: result.body, error: null }); } catch (e) { console.error("Error creating account:", e); let errorMessage = "Failed to create account"; if (e.response && e.response.body && e.response.body.error) { errorMessage = e.response.body.error; } else if (e.message) { errorMessage = e.message; } this.setState({ loading: false, error: errorMessage, success: false }); } } async reloadAccount() { if (!this.state.createdAccount || !this.state.createdAccount.id) { return; } try { const result = await superagent.get(`/api/admin/account/${this.state.createdAccount.id}`).accept('json'); this.setState({ createdAccount: result.body }); } catch (e) { console.error("Error reloading account:", e); } } onCreateAnother(e) { e.preventDefault(); this.setState({ username: "", email: "", success: false, createdAccount: null, error: null }); } render() { return (
{this.state.error}
Account ID: {this.state.createdAccount.id}
Username: {this.state.createdAccount.username}
Email: {this.state.createdAccount.email}