-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4e500af
commit 57304ef
Showing
5 changed files
with
327 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,199 @@ | ||
import { Button, Modal, ModalHeader, ModalBody, ModalFooter } from "reactstrap"; | ||
|
||
import React, { Component } from "react"; | ||
import { FormattedMessage } from "react-intl"; | ||
import { connect } from "react-redux"; | ||
|
||
import _ from "lodash"; | ||
class ModalEditUser extends Component { | ||
constructor(props) { | ||
super(props); | ||
this.state = { | ||
id: "", | ||
email: "", | ||
password: "", | ||
firstName: "", | ||
lastName: "", | ||
address: "", | ||
}; | ||
} | ||
|
||
componentDidMount() { | ||
let user = this.props.currentUser; | ||
if (user && !_.isEmpty(user)) { | ||
this.setState({ | ||
id: user.id, | ||
email: user.email, | ||
password: "harcode", | ||
firstName: user.firstName, | ||
lastName: user.lastName, | ||
address: user.address, | ||
}); | ||
} | ||
// console.log("didmount edit modal ", this.props.currentUser); | ||
} | ||
|
||
toggle = () => { | ||
this.props.toggleFromParent(); | ||
}; | ||
|
||
handleOnChangeInput = (event, id) => { | ||
//console.log(event.target.value, "id: ", id); | ||
|
||
//bad code | ||
// this.state[id] =event.target.value; | ||
// this.setState({ | ||
// ...this.state | ||
// }, () => { | ||
// console.log("check bad code:", this.state); | ||
// }) | ||
|
||
//good code | ||
let copyState = { ...this.state }; | ||
copyState[id] = event.target.value; | ||
this.setState({ | ||
...copyState, | ||
}); | ||
}; | ||
checkValidInput = () => { | ||
let isValid = true; | ||
let arrInput = [ | ||
"email", | ||
"password", | ||
"firstName", | ||
"lastName", | ||
"address", | ||
]; | ||
for (let i = 0; i < arrInput.length; i++) { | ||
if (!this.state[arrInput[i]]) { | ||
isValid = false; | ||
alert("Missing parameter: " + arrInput[i]); | ||
break; | ||
} | ||
} | ||
return isValid; | ||
}; | ||
handleSaveUser = () => { | ||
let isValid = this.checkValidInput(); | ||
if (isValid) { | ||
this.props.editUser(this.state); | ||
// console.log("check good state:", this.state); | ||
} | ||
}; | ||
|
||
render() { | ||
// console.log("check props from parent: ", this.props); | ||
return ( | ||
<Modal | ||
isOpen={this.props.isOpen} | ||
toggle={() => { | ||
this.toggle(); | ||
}} | ||
className={"modal-user-container"} | ||
size="lg" | ||
centered | ||
> | ||
<ModalHeader | ||
toggle={() => { | ||
this.toggle(); | ||
}} | ||
> | ||
Edit a new user | ||
</ModalHeader> | ||
<ModalBody> | ||
<div className="modal-user-body"> | ||
<div className="input-container"> | ||
<label>Email</label> | ||
<input | ||
type="text" | ||
onChange={(event) => { | ||
this.handleOnChangeInput(event, "email"); | ||
}} | ||
value={this.state.email} | ||
disabled | ||
/> | ||
</div> | ||
<div className="input-container"> | ||
<label>Password</label> | ||
<input | ||
type="password" | ||
onChange={(event) => { | ||
this.handleOnChangeInput(event, "password"); | ||
}} | ||
value={this.state.password} | ||
disabled | ||
/> | ||
</div> | ||
</div> | ||
<div className="modal-user-body"> | ||
<div className="input-container"> | ||
<label>First name</label> | ||
<input | ||
type="text" | ||
onChange={(event) => { | ||
this.handleOnChangeInput( | ||
event, | ||
"firstName" | ||
); | ||
}} | ||
value={this.state.firstName} | ||
/> | ||
</div> | ||
<div className="input-container"> | ||
<label>Last name</label> | ||
<input | ||
type="text" | ||
onChange={(event) => { | ||
this.handleOnChangeInput(event, "lastName"); | ||
}} | ||
value={this.state.lastName} | ||
/> | ||
</div> | ||
</div> | ||
<div className="modal-user-body"> | ||
<div className="input-container max-width-input"> | ||
<label>Address</label> | ||
<input | ||
type="text" | ||
onChange={(event) => { | ||
this.handleOnChangeInput(event, "address"); | ||
}} | ||
value={this.state.address} | ||
/> | ||
</div> | ||
</div> | ||
</ModalBody> | ||
<ModalFooter> | ||
<Button | ||
color="primary" | ||
onClick={() => { | ||
this.handleSaveUser(); | ||
}} | ||
className="px-3" | ||
> | ||
Save changes | ||
</Button>{" "} | ||
<Button | ||
color="secondary" | ||
onClick={() => { | ||
this.toggle(); | ||
}} | ||
className="px-3" | ||
> | ||
Close | ||
</Button> | ||
</ModalFooter> | ||
</Modal> | ||
); | ||
} | ||
} | ||
|
||
const mapStateToProps = (state) => { | ||
return {}; | ||
}; | ||
|
||
const mapDispatchToProps = (dispatch) => { | ||
return {}; | ||
}; | ||
|
||
export default connect(mapStateToProps, mapDispatchToProps)(ModalEditUser); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.