diff --git a/client/src/components/AddParty/index.js b/client/src/components/AddParty/index.js index c6bb8093..2ef0d41e 100644 --- a/client/src/components/AddParty/index.js +++ b/client/src/components/AddParty/index.js @@ -5,7 +5,7 @@ import TextField from '@material-ui/core/TextField'; import Grid from '@material-ui/core/Grid'; import "react-datepicker/dist/react-datepicker.css"; import Button from '@material-ui/core/Button'; -import { saveParty} from '../../modules/party/state/PartyAction'; +import { saveParty,asyncValidateParty} from '../../modules/party/state/PartyAction'; import { getUploadPath} from '../../modules/nomination/state/NominationAction'; import { connect } from 'react-redux'; import MenuItem from '@material-ui/core/MenuItem'; @@ -94,6 +94,7 @@ class PartyRegistration extends React.Component { faxList:[], fax:'', status:'', + exist:false } } @@ -182,6 +183,10 @@ class PartyRegistration extends React.Component { goNext = false; } + if (this.state.exist === true) { + this.setState({ errorTextPartyName: 'emptyField2' }); + } + if (goNext) { saveParty(this.state); onCloseModal(); @@ -321,6 +326,20 @@ class PartyRegistration extends React.Component { this.setState({...this.state, faxList}); } + asyncValidation = name => event => { + if (event.target.value) { + asyncValidateParty(event.target.value).then((data) => { + if (data.exist === true) { + this.setState({ exist: data.exist }); + } else { + this.setState({ exist: data.exist }); + } + }) + } else { + this.setState({ exist: false }); + } + } + render() { const { classes, onCloseModal} = this.props; const { errorTextPartyType,errorTextSecretaryName,errorTextAbbreviation,errorTextApprovedSymbol,errorTextAddress,errorTextTitle,errorTextPartyName,errorTextPhone,errorTextFax } = this.state; @@ -345,9 +364,13 @@ class PartyRegistration extends React.Component { style={{width:'100%'}} className={classes.textField} value={this.state.partyName} - onChange={this.handleChange("partyName")} + // onChange={this.handleChange("partyName")} + onChange={(evt) => { + this.handleChange("partyName")(evt) + this.asyncValidation('partyName')(evt) + }} margin="normal" - helperText={errorTextPartyName === "emptyField" ? 'This field is required!' : ''} + helperText={errorTextPartyName === "emptyField" ? 'This field is required!' : errorTextPartyName === "emptyField2" ? 'This party name already been used!' : ' '} /> @@ -399,19 +422,7 @@ class PartyRegistration extends React.Component { - - - - +