import * as React from "react"; import {Link} from "react-router-dom"; import Alert from "../elements/alert"; import Icon from "../elements/icon"; import ReactTooltip from 'react-tooltip' import {Collapse} from "react-collapse/lib/Collapse"; export default class CreateUser extends React.Component { constructor(props) { super(props); this.parent = { showDialog: props.showDialog || function () { }, api: props.api, }; this.state = { errors: [], sendInvite: true, username: "", email: "", password: "", confirmPassword: "", isSubmitting: false } } removeError(i) { if (i >= 0 && i < this.state.errors.length) { let errors = this.state.errors.slice(); errors.splice(i, 1); this.setState({...this.state, errors: errors}); } } render() { let errors = []; for (let i = 0; i < this.state.errors.length; i++) { errors.push( this.removeError(i)} {...this.state.errors[i]}/>) } return <>

Create a new user

  1. Home
  2. Users
  3. Add User
{errors}
this.submitForm(e)}>
this.onCheckboxChange()} id={"sendInvite"} name={"sendInvite"} defaultChecked={this.state.sendInvite}/>
 Back { this.state.isSubmitting ? : }
; } submitForm(e) { e.preventDefault(); if (this.state.isSubmitting) { return; } const requiredFields = (this.state.sendInvite ? ["username", "email"] : ["username", "password", "confirmPassword"]); let missingFields = []; for (const field of requiredFields) { if (!this.state[field]) { missingFields.push(field); } } if (missingFields.length > 0) { let errors = this.state.errors.slice(); errors.push({title: "Missing input", message: "The following fields are missing: " + missingFields.join(", "), type: "warning"}); this.setState({ ...this.state, errors: errors }); return; } this.setState({ ...this.state, isSubmitting: true }); const username = this.state.username; const email = this.state.email || ""; const password = this.state.password; const confirmPassword = this.state.confirmPassword; if (this.state.sendInvite) { this.parent.api.inviteUser(username, email).then((res) => { let errors = this.state.errors.slice(); if (!res.success) { errors.push({ title: "Error inviting User", message: res.msg, type: "danger" }); this.setState({ ...this.state, errors: errors, isSubmitting: false }); } else { errors.push({ title: "Success", message: "The invitation was successfully sent.", type: "success" }); this.setState({ ...this.state, errors: errors, username: "", email: "", isSubmitting: false }); } }); } else { if (this.state.password !== this.state.confirmPassword) { let errors = this.state.errors.slice(); errors.push({ title: "Error creating User", message: "The given passwords do not match", type: "danger" }); this.setState({ ...this.state, errors: errors, password: "", confirmPassword: "", isSubmitting: false }); return; } this.parent.api.createUser(username, email, password, confirmPassword).then((res) => { let errors = this.state.errors.slice(); if (!res.success) { errors.push({ title: "Error creating User", message: res.msg, type: "danger" }); this.setState({ ...this.state, errors: errors, password: "", confirmPassword: "", isSubmitting: false }); } else { errors.push({ title: "Success", message: "The user was successfully created.", type: "success" }); this.setState({ ...this.state, errors: errors, username: "", email: "", password: "", confirmPassword: "", isSubmitting: false }); } }); } } onCheckboxChange() { this.setState({ ...this.state, sendInvite: !this.state.sendInvite, }); } onChangeInput(event) { const target = event.target; const value = target.value; const name = target.name; this.setState({ ...this.state, [name]: value }); } }