All files / src/ui/pages/spot SpotsPage.tsx

96% Statements 24/25
75% Branches 3/4
95% Functions 19/20
100% Lines 21/21

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141                                                                            2x                             2x                   13x 13x 13x 13x       3x       4x                 1x           4x           1x           9x   1x           3x         2x       8x 6x       4x       1x       1x       1x          
/*
 * Copyright 2019 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 { DataTable, DataTableProps, PropertySetter } from 'ui/components/DataTable';
import { StatefulMultiTypeaheadSelectInput } from 'ui/components/MultiTypeaheadSelectInput'
import { spotSelectors, spotOperations } from 'store/spot';
import { skillSelectors } from 'store/skill';
import Spot from 'domain/Spot';
import { AppState } from 'store/types';
import {
  TextInput, Text, Chip, ChipGroup,
} from '@patternfly/react-core';
import { connect } from 'react-redux';
import Skill from 'domain/Skill';
import { Predicate, ReadonlyPartial, Sorter } from 'types';
import { stringSorter } from 'util/CommonSorters';
import { stringFilter } from 'util/CommonFilters';
import { withTranslation, WithTranslation } from 'react-i18next';
 
interface StateProps extends DataTableProps<Spot> {
  tenantId: number;
  skillList: Skill[];
}
 
const mapStateToProps = (state: AppState, ownProps: Props): StateProps => ({
  ...ownProps,
  title: ownProps.t("spots"),
  columnTitles: [ownProps.t("name"), ownProps.t("requiredSkillSet")],
  tableData: spotSelectors.getSpotList(state),
  skillList: skillSelectors.getSkillList(state),
  tenantId: state.tenantData.currentTenantId
}); 
 
export interface DispatchProps {
  addSpot: typeof spotOperations.addSpot;
  updateSpot: typeof spotOperations.updateSpot;
  removeSpot: typeof spotOperations.removeSpot;
}
 
const mapDispatchToProps: DispatchProps = {
  addSpot: spotOperations.addSpot,
  updateSpot: spotOperations.updateSpot,
  removeSpot: spotOperations.removeSpot
};
 
export type Props = StateProps & DispatchProps & WithTranslation;
 
export class SpotsPage extends DataTable<Spot, Props> {
  constructor(props: Props) {
    super(props);
    this.addData = this.addData.bind(this);
    this.updateData = this.updateData.bind(this);
    this.removeData = this.removeData.bind(this);
  }
 
  displayDataRow(data: Spot): JSX.Element[] {
    return [
      <Text key={0}>{data.name}</Text>,
      <ChipGroup key={1}>
        {data.requiredSkillSet.map(skill => (
          <Chip key={skill.name} isReadOnly>
            {skill.name}
          </Chip>
        ))}
      </ChipGroup>
    ];
  }
 
  getInitialStateForNewRow(): Partial<Spot> {
    return {
      requiredSkillSet: []
    };
  }
  
  editDataRow(data: ReadonlyPartial<Spot>, setProperty: PropertySetter<Spot>): JSX.Element[] {
    return [
      <TextInput
        key={0}
        name="name"
        defaultValue={data.name}
        aria-label="Name"
        onChange={(value) => setProperty("name",value)}
      />,
      <StatefulMultiTypeaheadSelectInput
        key={1}
        emptyText={this.props.t("selectRequiredSkills")}
        options={this.props.skillList}
        optionToStringMap={skill => skill.name}
        value={data.requiredSkillSet? data.requiredSkillSet : []}
        onChange={selected => setProperty("requiredSkillSet",selected)}
      />
    ];
  }
  
  isDataComplete(editedValue: ReadonlyPartial<Spot>): editedValue is Spot {
    return editedValue.name !== undefined &&
      editedValue.requiredSkillSet !== undefined;
  }
 
  isValid(editedValue: Spot): boolean {
    return editedValue.name.trim().length > 0;
  }
 
  getFilter(): (filter: string) => Predicate<Spot> {
    return stringFilter(spot => spot.name,
      spot => spot.requiredSkillSet.map(skill => skill.name));
  }
 
  getSorters(): (Sorter<Spot> | null)[] {
    return [stringSorter(s => s.name), null];
  }
  
  updateData(data: Spot): void {
    this.props.updateSpot(data);
  }
  
  addData(data: Spot): void {
    this.props.addSpot({...data, tenantId: this.props.tenantId});
  }
 
  removeData(data: Spot): void {
    this.props.removeSpot(data);
  }
}
 
export default withTranslation("SpotsPage")(connect(mapStateToProps, mapDispatchToProps)(SpotsPage));