NewUserModal.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /*
  2. * Copyright (C) 2015 - present Instructure, Inc.
  3. *
  4. * This file is part of Canvas.
  5. *
  6. * Canvas is free software: you can redistribute it and/or modify it under
  7. * the terms of the GNU Affero General Public License as published by the Free
  8. * Software Foundation, version 3 of the License.
  9. *
  10. * Canvas is distributed in the hope that it will be useful, but WITHOUT ANY
  11. * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. * A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
  13. * details.
  14. *
  15. * You should have received a copy of the GNU Affero General Public License along
  16. * with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. import $ from 'jquery'
  19. import React from 'react'
  20. import PropTypes from 'prop-types'
  21. import _ from 'underscore'
  22. import I18n from 'i18n!account_course_user_search'
  23. import {firstNameFirst, lastNameFirst, nameParts} from 'user_utils'
  24. import Modal from 'jsx/shared/modal'
  25. import ModalContent from 'jsx/shared/modal-content'
  26. import ModalButtons from 'jsx/shared/modal-buttons'
  27. import UsersStore from './UsersStore'
  28. import IcInput from './IcInput'
  29. import IcCheckbox from './IcCheckbox'
  30. import 'compiled/jquery.rails_flash_notifications'
  31. const { object, string } = PropTypes
  32. var NewUserModal = React.createClass({
  33. propTypes: {
  34. userList: object.isRequired
  35. },
  36. getInitialState() {
  37. return {
  38. isOpen: false,
  39. data: {send_confirmation: true},
  40. errors: {}
  41. }
  42. },
  43. openModal() {
  44. this.setState({isOpen: true});
  45. },
  46. closeModal() {
  47. this.setState({isOpen: false, data: {}, errors: {}});
  48. },
  49. onChange(field, value) {
  50. var { data } = this.state;
  51. var newData = {};
  52. newData[field] = value;
  53. if (field === 'name') {
  54. // shamelessly copypasted from user_sortable_name.js
  55. var sortable_name_parts = nameParts(data.sortable_name);
  56. if ($.trim(data.sortable_name) == '' || firstNameFirst(sortable_name_parts) == $.trim(data.name)) {
  57. var parts = nameParts(value, sortable_name_parts[1]);
  58. newData.sortable_name = lastNameFirst(parts);
  59. }
  60. if ($.trim(data.short_name) == '' || data.short_name == data.name) {
  61. newData.short_name = value;
  62. }
  63. }
  64. data = _.extend({}, data, newData);
  65. this.setState({ data, errors: {} });
  66. },
  67. onSubmit() {
  68. var { data } = this.state;
  69. // Basic client side validation
  70. var errors = {}
  71. if (!data.name) errors.name = I18n.t("Full name is required");
  72. if (!data.email) errors.email = I18n.t("Email is required");
  73. if (Object.keys(errors).length) {
  74. return this.props.handlers.handleAddNewUserFormErrors(errors);
  75. }
  76. var url = `/accounts/${window.ENV.ACCOUNT_ID}/users`
  77. var params = {
  78. user: _.pick(data, 'name', 'short_name', 'sortable_name'),
  79. pseudonym: {
  80. unique_id: data.email,
  81. send_confirmation: data.send_confirmation
  82. }
  83. };
  84. this.props.handlers.handleAddNewUser(params)
  85. this.setState({
  86. isOpen: false
  87. });
  88. },
  89. render() {
  90. var { data, isOpen } = this.state;
  91. let {errors} = this.props.userList;
  92. var onChange = (field) => {
  93. return (e) => this.onChange(field, e.target.value);
  94. };
  95. return (
  96. <Modal
  97. className="ReactModal__Content--canvas ReactModal__Content--mini-modal"
  98. isOpen={isOpen}
  99. title={I18n.t('Add a New User')}
  100. contentLabel={I18n.t('Add a New User')}
  101. onRequestClose={this.closeModal}
  102. onSubmit={this.onSubmit}
  103. >
  104. <ModalContent>
  105. <IcInput
  106. className="user_name"
  107. label={I18n.t("Full Name")}
  108. value={data.name}
  109. error={errors.name}
  110. onChange={onChange("name")}
  111. hint={I18n.t("This name will be used by teachers for grading.")}
  112. />
  113. <IcInput
  114. className="user_short_name"
  115. label={I18n.t("Display Name")}
  116. value={data.short_name}
  117. error={errors.short_name}
  118. onChange={onChange("short_name")}
  119. hint={I18n.t("People will see this name in discussions, messages and comments.")}
  120. />
  121. <IcInput
  122. className="user_sortable_name"
  123. label={I18n.t("Sortable Name")}
  124. value={data.sortable_name}
  125. error={errors.sortable_name}
  126. onChange={onChange("sortable_name")}
  127. hint={I18n.t("This name appears in sorted lists.")}
  128. />
  129. <IcInput
  130. className="user_email"
  131. label={I18n.t("Email")}
  132. value={data.email}
  133. error={errors.email}
  134. onChange={onChange("email")}
  135. />
  136. <IcCheckbox
  137. className="user_send_confirmation"
  138. checked={data.send_confirmation}
  139. onChange={(e) => this.onChange('send_confirmation', e.target.checked)}
  140. label={I18n.t("Email the user about this account creation")}
  141. />
  142. </ModalContent>
  143. <ModalButtons>
  144. <button
  145. type="button"
  146. className="btn"
  147. onClick={this.closeModal}
  148. >
  149. {I18n.t("Cancel")}
  150. </button>
  151. <button
  152. type="submit"
  153. className="btn btn-primary"
  154. >
  155. {I18n.t("Add User")}
  156. </button>
  157. </ModalButtons>
  158. </Modal>
  159. );
  160. }
  161. });
  162. export default NewUserModal