ShareTheResource.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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 2019 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 { Button, Chip, ChipGroup, ChipGroupToolbarItem, Form, FormGroup, Gallery, GalleryItem, Modal, Stack, StackItem, TextInput } 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 { ContentAlert } from "../ContentAlert.js";
  22. import { PermissionSelect } from "./PermissionSelect.js";
  23. /**
  24. * @author Stan Silvert ssilvert@redhat.com (C) 2019 Red Hat Inc.
  25. */
  26. export class ShareTheResource extends React.Component {
  27. constructor(props, context) {
  28. super(props);
  29. _defineProperty(this, "context", void 0);
  30. _defineProperty(this, "handleAddPermission", () => {
  31. const rscId = this.props.resource._id;
  32. const newPermissions = [];
  33. for (const permission of this.state.permissionsSelected) {
  34. newPermissions.push(permission.name);
  35. }
  36. const permissions = [];
  37. for (const username of this.state.usernames) {
  38. permissions.push({
  39. username: username,
  40. scopes: newPermissions
  41. });
  42. }
  43. this.handleToggleDialog();
  44. this.context.doPut(`/resources/${rscId}/permissions`, permissions).then(() => {
  45. ContentAlert.success('shareSuccess');
  46. this.props.onClose();
  47. });
  48. });
  49. _defineProperty(this, "handleToggleDialog", () => {
  50. if (this.state.isOpen) {
  51. this.setState({
  52. isOpen: false
  53. });
  54. this.props.onClose();
  55. } else {
  56. this.clearState();
  57. this.setState({
  58. isOpen: true
  59. });
  60. }
  61. });
  62. _defineProperty(this, "handleUsernameChange", username => {
  63. this.setState({
  64. usernameInput: username
  65. });
  66. });
  67. _defineProperty(this, "handleAddUsername", async () => {
  68. if (this.state.usernameInput !== '' && !this.state.usernames.includes(this.state.usernameInput)) {
  69. const response = await this.context.doGet(`/resources/${this.props.resource._id}/user`, {
  70. params: {
  71. value: this.state.usernameInput
  72. }
  73. });
  74. if (response.data && response.data.username) {
  75. this.setState({
  76. usernameInput: '',
  77. usernames: [...this.state.usernames, this.state.usernameInput]
  78. });
  79. } else {
  80. ContentAlert.info('userNotFound', [this.state.usernameInput]);
  81. }
  82. }
  83. });
  84. _defineProperty(this, "handleEnterKeyInAddField", event => {
  85. if (event.key === "Enter") {
  86. event.preventDefault();
  87. this.handleAddUsername();
  88. }
  89. });
  90. _defineProperty(this, "handleDeleteUsername", username => {
  91. const newUsernames = this.state.usernames.filter(user => user !== username);
  92. this.setState({
  93. usernames: newUsernames
  94. });
  95. });
  96. this.context = context;
  97. this.state = {
  98. isOpen: false,
  99. permissionsSelected: [],
  100. permissionsUnSelected: this.props.resource.scopes,
  101. usernames: [],
  102. usernameInput: ''
  103. };
  104. }
  105. clearState() {
  106. this.setState({
  107. permissionsSelected: [],
  108. permissionsUnSelected: this.props.resource.scopes,
  109. usernames: [],
  110. usernameInput: ''
  111. });
  112. }
  113. isAddDisabled() {
  114. return this.state.usernameInput === '' || this.isAlreadyShared();
  115. }
  116. isAlreadyShared() {
  117. for (let permission of this.props.permissions) {
  118. if (permission.username === this.state.usernameInput) return true;
  119. }
  120. return false;
  121. }
  122. isFormInvalid() {
  123. return this.state.usernames.length === 0 || this.state.permissionsSelected.length === 0;
  124. }
  125. render() {
  126. return React.createElement(React.Fragment, null, this.props.children(this.handleToggleDialog), React.createElement(Modal, {
  127. title: 'Share the resource - ' + this.props.resource.name,
  128. isLarge: true,
  129. isOpen: this.state.isOpen,
  130. onClose: this.handleToggleDialog,
  131. actions: [React.createElement(Button, {
  132. key: "cancel",
  133. variant: "link",
  134. onClick: this.handleToggleDialog
  135. }, React.createElement(Msg, {
  136. msgKey: "cancel"
  137. })), React.createElement(Button, {
  138. key: "confirm",
  139. variant: "primary",
  140. id: "done",
  141. onClick: this.handleAddPermission,
  142. isDisabled: this.isFormInvalid()
  143. }, React.createElement(Msg, {
  144. msgKey: "done"
  145. }))]
  146. }, React.createElement(Stack, {
  147. gutter: "md"
  148. }, React.createElement(StackItem, {
  149. isFilled: true
  150. }, React.createElement(Form, null, React.createElement(FormGroup, {
  151. label: "Add users to share your resource with",
  152. type: "string",
  153. helperTextInvalid: Msg.localize('resourceAlreadyShared'),
  154. fieldId: "username",
  155. isRequired: true,
  156. isValid: !this.isAlreadyShared()
  157. }, React.createElement(Gallery, {
  158. gutter: "sm"
  159. }, React.createElement(GalleryItem, null, React.createElement(TextInput, {
  160. value: this.state.usernameInput,
  161. isValid: !this.isAlreadyShared(),
  162. id: "username",
  163. "aria-describedby": "username-helper",
  164. placeholder: "Username or email",
  165. onChange: this.handleUsernameChange,
  166. onKeyPress: this.handleEnterKeyInAddField
  167. })), React.createElement(GalleryItem, null, React.createElement(Button, {
  168. key: "add-user",
  169. variant: "primary",
  170. id: "add",
  171. onClick: this.handleAddUsername,
  172. isDisabled: this.isAddDisabled()
  173. }, React.createElement(Msg, {
  174. msgKey: "add"
  175. })))), React.createElement(ChipGroup, {
  176. withToolbar: true
  177. }, React.createElement(ChipGroupToolbarItem, {
  178. key: "users-selected",
  179. categoryName: "Share with "
  180. }, this.state.usernames.map(currentChip => React.createElement(Chip, {
  181. key: currentChip,
  182. onClick: () => this.handleDeleteUsername(currentChip)
  183. }, currentChip))))), React.createElement(FormGroup, {
  184. label: "",
  185. fieldId: "permissions-selected"
  186. }, React.createElement(PermissionSelect, {
  187. scopes: this.state.permissionsUnSelected,
  188. onSelect: selection => this.setState({
  189. permissionsSelected: selection
  190. }),
  191. direction: "up"
  192. })))), React.createElement(StackItem, {
  193. isFilled: true
  194. }, React.createElement("br", null)), React.createElement(StackItem, {
  195. isFilled: true
  196. }, this.props.sharedWithUsersMsg))));
  197. }
  198. }
  199. _defineProperty(ShareTheResource, "defaultProps", {
  200. permissions: []
  201. });
  202. _defineProperty(ShareTheResource, "contextType", AccountServiceContext);
  203. //# sourceMappingURL=ShareTheResource.js.map