/* * Copyright 2018 Red Hat, Inc. and/or its affiliates. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import * as React from 'react'; import {AxiosResponse} from 'axios'; import { Badge, DataList, DataListItem, DataListItemRow, DataListCell, DataListToggle, DataListContent, DataListItemCells, Level, LevelItem, Stack, StackItem } from '@patternfly/react-core'; import { EditAltIcon, Remove2Icon, UserCheckIcon } from '@patternfly/react-icons'; import {AccountServiceClient} from '../../account-service/account.service'; import {ShareTheResource} from "./ShareTheResource"; import {Client, PaginatedResources, Permission, Resource} from "./MyResourcesPage"; import { Msg } from '../../widgets/Msg'; export interface ResourcesTableState { isRowOpen: boolean[]; permissions: Map; } export interface ResourcesTableProps { resources: PaginatedResources; noResourcesMessage: string; } export class ResourcesTable extends React.Component { public constructor(props: ResourcesTableProps) { super(props); this.state = { isRowOpen: new Array(props.resources.data.length).fill(false), permissions: new Map() } } private onToggle = (row: number): void => { const newIsRowOpen: boolean[] = this.state.isRowOpen; newIsRowOpen[row] = !newIsRowOpen[row]; if (newIsRowOpen[row]) this.fetchPermissions(this.props.resources.data[row], row); this.setState({isRowOpen: newIsRowOpen}); }; private fetchPermissions(resource: Resource, row: number): void { console.log('**** fetchPermissions'); AccountServiceClient.Instance.doGet('resources/' + resource._id + '/permissions') .then((response: AxiosResponse) => { console.log('Fetching Permissions row: ' + row); console.log({response}); const newPermissions: Map = new Map(this.state.permissions); newPermissions.set(row, response.data); this.setState({permissions: newPermissions}); } ); } private hasPermissions(row: number): boolean { return (this.state.permissions.has(row)) && (this.state.permissions.get(row)!.length > 0); } private firstUser(row: number): string { if (!this.hasPermissions(row)) return 'ERROR!!!!'; // should never happen return this.state.permissions.get(row)![0].username; } private numOthers(row: number): number { if (!this.hasPermissions(row)) return -1; // should never happen return this.state.permissions.get(row)!.length - 1; } public sharedWithUsersMessage(row: number): React.ReactNode { if (!this.hasPermissions(row)) return (); // TODO: Not using a parameterized message because I want to use tag. Need to figure out a good solution to this. if (this.numOthers(row) > 0) { return ( {this.firstUser(row)} {this.numOthers(row)} .) } else { return ( {this.firstUser(row)}.) } } public render(): React.ReactNode { return ( // invisible toggle allows headings to line up properly , , , ]} /> {(this.props.resources.data.length === 0) && } {this.props.resources.data.map((resource: Resource, row: number) => { return ( this.onToggle(row)} isExpanded={this.state.isRowOpen[row]} id={'resourceToggle-' + row} aria-controls="ex-expand1" /> , {this.getClientName(resource.client)} , {resource.shareRequests.length > 0 && {resource.shareRequests.length}} ]} /> {this.sharedWithUsersMessage(row)} Edit Remove )})} ); } private getClientName(client: Client): string { if (client.hasOwnProperty('name') && client.name !== null && client.name !== '') { return Msg.localize(client.name!); } else { return client.clientId; } } }