MyResourcesPage.js 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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 parse from "../../util/ParseLink.js";
  19. import { Button, Level, LevelItem, Stack, StackItem, Tab, Tabs, TextInput } from "../../../../common/keycloak/web_modules/@patternfly/react-core.js";
  20. import { AccountServiceContext } from "../../account-service/AccountServiceContext.js";
  21. import { Scope } from "./resource-model.js";
  22. import { ResourcesTable } from "./ResourcesTable.js";
  23. import { ContentPage } from "../ContentPage.js";
  24. import { Msg } from "../../widgets/Msg.js";
  25. import { SharedResourcesTable } from "./SharedResourcesTable.js";
  26. const MY_RESOURCES_TAB = 0;
  27. const SHARED_WITH_ME_TAB = 1;
  28. export class MyResourcesPage extends React.Component {
  29. constructor(props, context) {
  30. super(props);
  31. _defineProperty(this, "context", void 0);
  32. _defineProperty(this, "first", 0);
  33. _defineProperty(this, "max", 5);
  34. _defineProperty(this, "makeScopeObj", scope => {
  35. return new Scope(scope.name, scope.displayName);
  36. });
  37. _defineProperty(this, "fetchPermissionRequests", () => {
  38. this.state.myResources.data.forEach(resource => {
  39. this.fetchShareRequests(resource);
  40. });
  41. });
  42. _defineProperty(this, "fetchPending", async () => {
  43. const response = await this.context.doGet(`/resources/pending-requests`);
  44. const resources = response.data || [];
  45. resources.forEach(pendingRequest => {
  46. this.state.sharedWithMe.data.forEach(resource => {
  47. if (resource._id === pendingRequest._id) {
  48. resource.shareRequests = [{
  49. username: 'me',
  50. scopes: pendingRequest.scopes
  51. }];
  52. this.forceUpdate();
  53. }
  54. });
  55. });
  56. });
  57. _defineProperty(this, "handleFilterRequest", value => {
  58. this.setState({
  59. nameFilter: value
  60. });
  61. this.fetchFilteredResources({
  62. name: value
  63. });
  64. });
  65. _defineProperty(this, "handleFirstPageClick", () => {
  66. this.fetchInitialResources();
  67. });
  68. _defineProperty(this, "handleNextClick", () => {
  69. if (this.isSharedWithMeTab()) {
  70. this.fetchResources(this.state.sharedWithMe.nextUrl);
  71. } else {
  72. this.fetchResources(this.state.myResources.nextUrl);
  73. }
  74. });
  75. _defineProperty(this, "handlePreviousClick", () => {
  76. if (this.isSharedWithMeTab()) {
  77. this.fetchResources(this.state.sharedWithMe.prevUrl);
  78. } else {
  79. this.fetchResources(this.state.myResources.prevUrl);
  80. }
  81. });
  82. _defineProperty(this, "handleTabClick", (event, tabIndex) => {
  83. if (this.state.activeTabKey === tabIndex) return;
  84. this.setState({
  85. nameFilter: '',
  86. activeTabKey: tabIndex
  87. }, () => {
  88. this.fetchInitialResources();
  89. });
  90. });
  91. this.context = context;
  92. this.state = {
  93. activeTabKey: MY_RESOURCES_TAB,
  94. nameFilter: '',
  95. isModalOpen: false,
  96. myResources: {
  97. nextUrl: '',
  98. prevUrl: '',
  99. data: []
  100. },
  101. sharedWithMe: {
  102. nextUrl: '',
  103. prevUrl: '',
  104. data: []
  105. }
  106. };
  107. this.fetchInitialResources();
  108. }
  109. isSharedWithMeTab() {
  110. return this.state.activeTabKey === SHARED_WITH_ME_TAB;
  111. }
  112. hasNext() {
  113. if (this.isSharedWithMeTab()) {
  114. return this.state.sharedWithMe.nextUrl !== null && this.state.sharedWithMe.nextUrl !== '';
  115. } else {
  116. return this.state.myResources.nextUrl !== null && this.state.myResources.nextUrl !== '';
  117. }
  118. }
  119. hasPrevious() {
  120. if (this.isSharedWithMeTab()) {
  121. return this.state.sharedWithMe.prevUrl !== null && this.state.sharedWithMe.prevUrl !== '';
  122. } else {
  123. return this.state.myResources.prevUrl !== null && this.state.myResources.prevUrl !== '';
  124. }
  125. }
  126. fetchInitialResources() {
  127. if (this.isSharedWithMeTab()) {
  128. this.fetchResources("/resources/shared-with-me");
  129. } else {
  130. this.fetchResources("/resources", {
  131. first: this.first,
  132. max: this.max
  133. });
  134. }
  135. }
  136. fetchFilteredResources(params) {
  137. if (this.isSharedWithMeTab()) {
  138. this.fetchResources("/resources/shared-with-me", params);
  139. } else {
  140. this.fetchResources("/resources", { ...params,
  141. first: this.first,
  142. max: this.max
  143. });
  144. }
  145. }
  146. fetchResources(url, extraParams) {
  147. this.context.doGet(url, {
  148. params: extraParams
  149. }).then(response => {
  150. const resources = response.data || [];
  151. resources.forEach(resource => resource.shareRequests = []); // serialize the Scope objects from JSON so that toString() will work.
  152. resources.forEach(resource => resource.scopes = resource.scopes.map(this.makeScopeObj));
  153. if (this.isSharedWithMeTab()) {
  154. this.setState({
  155. sharedWithMe: this.parseResourceResponse(response)
  156. }, this.fetchPending);
  157. } else {
  158. this.setState({
  159. myResources: this.parseResourceResponse(response)
  160. }, this.fetchPermissionRequests);
  161. }
  162. });
  163. }
  164. fetchShareRequests(resource) {
  165. this.context.doGet('/resources/' + resource._id + '/permissions/requests').then(response => {
  166. resource.shareRequests = response.data || [];
  167. if (resource.shareRequests.length > 0) {
  168. this.forceUpdate();
  169. }
  170. });
  171. }
  172. parseResourceResponse(response) {
  173. const links = response.headers.get('link') || undefined;
  174. const parsed = parse(links);
  175. let next = '';
  176. let prev = '';
  177. if (parsed !== null) {
  178. if (parsed.next) next = parsed.next;
  179. if (parsed.prev) prev = parsed.prev;
  180. }
  181. const resources = response.data || [];
  182. return {
  183. nextUrl: next,
  184. prevUrl: prev,
  185. data: resources
  186. };
  187. }
  188. makeTab(eventKey, title, resources, sharedResourcesTab) {
  189. return React.createElement(Tab, {
  190. id: title,
  191. eventKey: eventKey,
  192. title: Msg.localize(title)
  193. }, React.createElement(Stack, {
  194. gutter: "md"
  195. }, React.createElement(StackItem, {
  196. isFilled: true
  197. }, React.createElement("span", null)), React.createElement(StackItem, {
  198. isFilled: true
  199. }, React.createElement(Level, {
  200. gutter: "md"
  201. }, React.createElement(LevelItem, null, React.createElement(TextInput, {
  202. value: this.state.nameFilter,
  203. onChange: this.handleFilterRequest,
  204. id: 'filter-' + title,
  205. type: "text",
  206. placeholder: Msg.localize('filterByName')
  207. })))), React.createElement(StackItem, {
  208. isFilled: true
  209. }, !sharedResourcesTab && React.createElement(ResourcesTable, {
  210. resources: resources
  211. }), sharedResourcesTab && React.createElement(SharedResourcesTable, {
  212. resources: resources
  213. }))));
  214. }
  215. render() {
  216. return React.createElement(ContentPage, {
  217. title: "resources",
  218. onRefresh: this.fetchInitialResources.bind(this)
  219. }, React.createElement(Tabs, {
  220. isFilled: true,
  221. activeKey: this.state.activeTabKey,
  222. onSelect: this.handleTabClick
  223. }, this.makeTab(0, 'myResources', this.state.myResources, false), this.makeTab(1, 'sharedwithMe', this.state.sharedWithMe, true)), React.createElement(Level, {
  224. gutter: "md"
  225. }, React.createElement(LevelItem, null, this.hasPrevious() && React.createElement(Button, {
  226. onClick: this.handlePreviousClick
  227. }, "<", React.createElement(Msg, {
  228. msgKey: "previousPage"
  229. }))), React.createElement(LevelItem, null, this.hasPrevious() && React.createElement(Button, {
  230. onClick: this.handleFirstPageClick
  231. }, React.createElement(Msg, {
  232. msgKey: "firstPage"
  233. }))), React.createElement(LevelItem, null, this.hasNext() && React.createElement(Button, {
  234. onClick: this.handleNextClick
  235. }, React.createElement(Msg, {
  236. msgKey: "nextPage"
  237. }), ">"))));
  238. }
  239. clearNextPrev() {
  240. const newMyResources = this.state.myResources;
  241. newMyResources.nextUrl = '';
  242. newMyResources.prevUrl = '';
  243. this.setState({
  244. myResources: newMyResources
  245. });
  246. }
  247. }
  248. _defineProperty(MyResourcesPage, "contextType", AccountServiceContext);
  249. ;
  250. //# sourceMappingURL=MyResourcesPage.js.map