AccountPage.js 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
  2. /*
  3. * Copyright 2018 Red Hat, Inc. and/or its affiliates.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. import * as React from "../../../../common/keycloak/web_modules/react.js";
  18. import { ActionGroup, Button, Form, FormGroup, TextInput, Grid, GridItem, Expandable } from "../../../../common/keycloak/web_modules/@patternfly/react-core.js";
  19. import { AccountServiceContext } from "../../account-service/AccountServiceContext.js";
  20. import { Msg } from "../../widgets/Msg.js";
  21. import { ContentPage } from "../ContentPage.js";
  22. import { ContentAlert } from "../ContentAlert.js";
  23. import { LocaleSelector } from "../../widgets/LocaleSelectors.js";
  24. import { KeycloakContext } from "../../keycloak-service/KeycloakContext.js";
  25. import { AIACommand } from "../../util/AIACommand.js";
  26. /**
  27. * @author Stan Silvert ssilvert@redhat.com (C) 2018 Red Hat Inc.
  28. */
  29. export class AccountPage extends React.Component {
  30. constructor(props, context) {
  31. super(props);
  32. _defineProperty(this, "context", void 0);
  33. _defineProperty(this, "isRegistrationEmailAsUsername", features.isRegistrationEmailAsUsername);
  34. _defineProperty(this, "isEditUserNameAllowed", features.isEditUserNameAllowed);
  35. _defineProperty(this, "isDeleteAccountAllowed", features.deleteAccountAllowed);
  36. _defineProperty(this, "DEFAULT_STATE", {
  37. errors: {
  38. username: '',
  39. firstName: '',
  40. lastName: '',
  41. email: ''
  42. },
  43. formFields: {
  44. username: '',
  45. firstName: '',
  46. lastName: '',
  47. email: '',
  48. attributes: {}
  49. }
  50. });
  51. _defineProperty(this, "state", this.DEFAULT_STATE);
  52. _defineProperty(this, "handleCancel", () => {
  53. this.fetchPersonalInfo();
  54. });
  55. _defineProperty(this, "handleChange", (value, event) => {
  56. const target = event.currentTarget;
  57. const name = target.name;
  58. this.setState({
  59. errors: { ...this.state.errors,
  60. [name]: target.validationMessage
  61. },
  62. formFields: { ...this.state.formFields,
  63. [name]: value
  64. }
  65. });
  66. });
  67. _defineProperty(this, "handleSubmit", event => {
  68. event.preventDefault();
  69. const form = event.target;
  70. const isValid = form.checkValidity();
  71. if (isValid) {
  72. const reqData = { ...this.state.formFields
  73. };
  74. this.context.doPost("/", reqData).then(() => {
  75. ContentAlert.success('accountUpdatedMessage');
  76. if (locale !== this.state.formFields.attributes.locale[0]) {
  77. window.location.reload();
  78. }
  79. });
  80. } else {
  81. const formData = new FormData(form);
  82. const validationMessages = Array.from(formData.keys()).reduce((acc, key) => {
  83. acc[key] = form.elements[key].validationMessage;
  84. return acc;
  85. }, {});
  86. this.setState({
  87. errors: { ...validationMessages
  88. },
  89. formFields: this.state.formFields
  90. });
  91. }
  92. });
  93. _defineProperty(this, "handleDelete", keycloak => {
  94. new AIACommand(keycloak, "delete_account").execute();
  95. });
  96. _defineProperty(this, "UsernameInput", () => React.createElement(TextInput, {
  97. isRequired: true,
  98. type: "text",
  99. id: "user-name",
  100. name: "username",
  101. maxLength: 254,
  102. value: this.state.formFields.username,
  103. onChange: this.handleChange,
  104. isValid: this.state.errors.username === ''
  105. }));
  106. _defineProperty(this, "RestrictedUsernameInput", () => React.createElement(TextInput, {
  107. isDisabled: true,
  108. type: "text",
  109. id: "user-name",
  110. name: "username",
  111. value: this.state.formFields.username
  112. }));
  113. this.context = context;
  114. this.fetchPersonalInfo();
  115. }
  116. fetchPersonalInfo() {
  117. this.context.doGet("/").then(response => {
  118. this.setState(this.DEFAULT_STATE);
  119. const formFields = response.data;
  120. if (!formFields.attributes) {
  121. formFields.attributes = {
  122. locale: [locale]
  123. };
  124. } else if (!formFields.attributes.locale) {
  125. formFields.attributes.locale = [locale];
  126. }
  127. this.setState({ ...{
  128. formFields: formFields
  129. }
  130. });
  131. });
  132. }
  133. render() {
  134. const fields = this.state.formFields;
  135. return React.createElement(ContentPage, {
  136. title: "personalInfoHtmlTitle",
  137. introMessage: "personalSubMessage"
  138. }, React.createElement(Form, {
  139. isHorizontal: true,
  140. onSubmit: event => this.handleSubmit(event)
  141. }, !this.isRegistrationEmailAsUsername && React.createElement(FormGroup, {
  142. label: Msg.localize('username'),
  143. isRequired: true,
  144. fieldId: "user-name",
  145. helperTextInvalid: this.state.errors.username,
  146. isValid: this.state.errors.username === ''
  147. }, this.isEditUserNameAllowed && React.createElement(this.UsernameInput, null), !this.isEditUserNameAllowed && React.createElement(this.RestrictedUsernameInput, null)), React.createElement(FormGroup, {
  148. label: Msg.localize('email'),
  149. isRequired: true,
  150. fieldId: "email-address",
  151. helperTextInvalid: this.state.errors.email,
  152. isValid: this.state.errors.email === ''
  153. }, React.createElement(TextInput, {
  154. isRequired: true,
  155. type: "email",
  156. id: "email-address",
  157. name: "email",
  158. maxLength: 254,
  159. value: fields.email,
  160. onChange: this.handleChange,
  161. isValid: this.state.errors.email === ''
  162. })), React.createElement(FormGroup, {
  163. label: Msg.localize('firstName'),
  164. isRequired: true,
  165. fieldId: "first-name",
  166. helperTextInvalid: this.state.errors.firstName,
  167. isValid: this.state.errors.firstName === ''
  168. }, React.createElement(TextInput, {
  169. isRequired: true,
  170. type: "text",
  171. id: "first-name",
  172. name: "firstName",
  173. maxLength: 254,
  174. value: fields.firstName,
  175. onChange: this.handleChange,
  176. isValid: this.state.errors.firstName === ''
  177. })), React.createElement(FormGroup, {
  178. label: Msg.localize('lastName'),
  179. isRequired: true,
  180. fieldId: "last-name",
  181. helperTextInvalid: this.state.errors.lastName,
  182. isValid: this.state.errors.lastName === ''
  183. }, React.createElement(TextInput, {
  184. isRequired: true,
  185. type: "text",
  186. id: "last-name",
  187. name: "lastName",
  188. maxLength: 254,
  189. value: fields.lastName,
  190. onChange: this.handleChange,
  191. isValid: this.state.errors.lastName === ''
  192. })), features.isInternationalizationEnabled && React.createElement(FormGroup, {
  193. label: Msg.localize('selectLocale'),
  194. isRequired: true,
  195. fieldId: "locale"
  196. }, React.createElement(LocaleSelector, {
  197. id: "locale-selector",
  198. value: fields.attributes.locale || '',
  199. onChange: value => this.setState({
  200. errors: this.state.errors,
  201. formFields: { ...this.state.formFields,
  202. attributes: { ...this.state.formFields.attributes,
  203. locale: [value]
  204. }
  205. }
  206. })
  207. })), React.createElement(ActionGroup, null, React.createElement(Button, {
  208. type: "submit",
  209. id: "save-btn",
  210. variant: "primary",
  211. isDisabled: Object.values(this.state.errors).filter(e => e !== '').length !== 0
  212. }, React.createElement(Msg, {
  213. msgKey: "doSave"
  214. })), React.createElement(Button, {
  215. id: "cancel-btn",
  216. variant: "secondary",
  217. onClick: this.handleCancel
  218. }, React.createElement(Msg, {
  219. msgKey: "doCancel"
  220. })))), this.isDeleteAccountAllowed && React.createElement("div", {
  221. id: "delete-account",
  222. style: {
  223. marginTop: "30px"
  224. }
  225. }, React.createElement(Expandable, {
  226. toggleText: "Delete Account"
  227. }, React.createElement(Grid, {
  228. gutter: "sm"
  229. }, React.createElement(GridItem, {
  230. span: 6
  231. }, React.createElement("p", null, React.createElement(Msg, {
  232. msgKey: "deleteAccountWarning"
  233. }))), React.createElement(GridItem, {
  234. span: 4
  235. }, React.createElement(KeycloakContext.Consumer, null, keycloak => React.createElement(Button, {
  236. id: "delete-account-btn",
  237. variant: "danger",
  238. onClick: () => this.handleDelete(keycloak),
  239. className: "delete-button"
  240. }, React.createElement(Msg, {
  241. msgKey: "doDelete"
  242. })))), React.createElement(GridItem, {
  243. span: 2
  244. })))));
  245. }
  246. }
  247. _defineProperty(AccountPage, "contextType", AccountServiceContext);
  248. ;
  249. //# sourceMappingURL=AccountPage.js.map