EditProfile.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /*
  2. Copyright (C) 2017 Cloudbase Solutions SRL
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU Affero General Public License as
  5. published by the Free Software Foundation, either version 3 of the
  6. License, or (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Affero General Public License for more details.
  11. You should have received a copy of the GNU Affero General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. import React, { Component, PropTypes } from 'react';
  15. import withStyles from 'isomorphic-style-loader/lib/withStyles';
  16. import s from './EditProfile.scss';
  17. import Dropdown from '../NewDropdown';
  18. import NotificationActions from '../../actions/NotificationActions';
  19. import UserActions from '../../actions/UserActions';
  20. const title = 'Edit Profile';
  21. class EditProfile extends Component {
  22. static contextTypes = {
  23. onSetTitle: PropTypes.func.isRequired,
  24. }
  25. static propTypes = {
  26. user: PropTypes.object,
  27. type: PropTypes.string,
  28. closeHandle: PropTypes.func
  29. }
  30. static defaultProps = {
  31. user: null,
  32. type: "edit"
  33. }
  34. constructor(props) {
  35. super(props)
  36. this.state = {
  37. firstName: props.user.firstName,
  38. lastName: props.user.lastName,
  39. email: props.user.email,
  40. primaryProject: null,
  41. requiredFields: [],
  42. formSubmitted: false
  43. }
  44. }
  45. componentWillMount() {
  46. this.context.onSetTitle(title);
  47. }
  48. componentDidMount() {
  49. }
  50. handleChangeFirstName(e) {
  51. this.setState({ firstName: e.target.value })
  52. }
  53. handleChangeLastName(e) {
  54. this.setState({ lastName: e.target.value })
  55. }
  56. handleChangeEmail(e) {
  57. this.setState({ email: e.target.value })
  58. }
  59. handleSave() {
  60. UserActions.setUserInfo(this.props.user.id, {
  61. extra: {
  62. firstName: this.state.firstName,
  63. lastName: this.state.lastName
  64. },
  65. email: this.state.email
  66. })
  67. let valid = true
  68. for (let i in this.state.currentCloudData) {
  69. if (this.state.requiredFields.indexOf(i) > -1 && !this.state.currentCloudData[i]) {
  70. valid = false
  71. }
  72. }
  73. if (this.state.connectionName.trim().length == 0) {
  74. valid = false
  75. }
  76. if (!valid) {
  77. NotificationActions.notify("Please fill all required fields", "error")
  78. this.setState({ formSubmitted: true })
  79. } else {
  80. // TODO: Save action here
  81. }
  82. }
  83. isValid(field) {
  84. if (field.required && this.state.formSubmitted) {
  85. return this.state.currentCloudData[field.name].length != 0;
  86. } else {
  87. return true
  88. }
  89. }
  90. handleCancel() {
  91. this.props.closeHandle();
  92. }
  93. render() {
  94. let projectOptions = []
  95. if (this.props.user) {
  96. projectOptions = this.props.user.projects.map(project => ({ label: project.name, id: project.id }))
  97. }
  98. return (
  99. <div className={s.root}>
  100. <div className={s.header}>
  101. <h3>{title}</h3>
  102. </div>
  103. <div className={s.container}>
  104. <div className={s.fields}>
  105. <div className="form-group">
  106. <label>First Name</label>
  107. <input
  108. type="text"
  109. placeholder="First Name"
  110. onChange={(e) => this.handleChangeFirstName(e)}
  111. value={this.state.firstName}
  112. />
  113. </div>
  114. <div className="form-group">
  115. <label>Last Name</label>
  116. <input
  117. type="text"
  118. placeholder="Last Name"
  119. onChange={(e) => this.handleChangeLastName(e)}
  120. value={this.state.lastName}
  121. />
  122. </div>
  123. <div className="form-group">
  124. <label>Email</label>
  125. <input
  126. type="text"
  127. placeholder="Email"
  128. onChange={(e) => this.handleChangeEmail(e)}
  129. value={this.state.email}
  130. />
  131. </div>
  132. <div className="form-group">
  133. <label>Main Project</label>
  134. <Dropdown
  135. options={projectOptions}
  136. placeholder="Switch Project"
  137. onChange={(e) => this.switchProject(e)}
  138. value={this.state.primaryProject}
  139. />
  140. </div>
  141. </div>
  142. <div className={s.buttons}>
  143. <button className={s.leftBtn + " gray"} onClick={(e) => this.handleCancel(e)}>Cancel</button>
  144. <button className={s.rightBtn} onClick={(e) => this.handleSave(e)}>Save</button>
  145. </div>
  146. </div>
  147. </div>
  148. );
  149. }
  150. }
  151. export default withStyles(EditProfile, s);