All files / src/ui/pages/availability EditAvailabilityModal.tsx

89.66% Statements 26/29
92% Branches 23/25
94.12% Functions 16/17
88% Lines 22/25

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 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189                                                                                    3x                                     14x   14x 14x             12x   1x       11x                 4x 4x   2x         25x 25x 25x     25x                                       1x                   1x           2x                       2x                           12x 2x                       28x 2x                          
/*
 * 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 React from 'react';
import { connect } from 'react-redux';
 
import {
  Button, InputGroup, Label, Form, Modal, ButtonVariant,
} from '@patternfly/react-core';
import DatePicker from 'react-datepicker';
 
import EmployeeAvailability from 'domain/EmployeeAvailability';
import Employee from 'domain/Employee';
import { AppState } from 'store/types';
import { employeeSelectors } from 'store/employee';
 
import 'react-datepicker/dist/react-datepicker.css';
import TypeaheadSelectInput from 'ui/components/TypeaheadSelectInput';
import { withTranslation, WithTranslation } from 'react-i18next';
 
interface Props {
  tenantId: number;
  availability?: EmployeeAvailability;
  isOpen: boolean;
  employeeList: Employee[];
  onSave: (availability: EmployeeAvailability) => void;
  onDelete: (availability: EmployeeAvailability) => void;
  onClose: () => void;
}
 
const mapStateToProps = (state: AppState, ownProps: {
  availability?: EmployeeAvailability;
  isOpen: boolean;
  onSave: (availability: EmployeeAvailability) => void;
  onDelete: (availability: EmployeeAvailability) => void;
  onClose: () => void;
}): Props => ({
  ...ownProps,
  tenantId: state.tenantData.currentTenantId,
  employeeList: employeeSelectors.getEmployeeList(state)
}); 
 
interface State {
  resetCount: number;
  editedValue: Partial<EmployeeAvailability>;
}
 
export class EditAvailabilityModal extends React.Component<Props & WithTranslation, State> {
  constructor(props: Props & WithTranslation) {
    super(props);
 
    this.onSave = this.onSave.bind(this);
    this.state = {
      resetCount: 0,
      editedValue: { ...this.props.availability }
    }
  }
 
  componentDidUpdate(prevProps: Props, prevState: State) {
    if (this.props.availability === undefined && prevProps.availability !== undefined) {
      // eslint-disable-next-line react/no-did-update-set-state
      this.setState({ resetCount: prevState.resetCount + 1, editedValue: {
        tenantId: this.props.tenantId,
      } });
    }
    else Iif (this.props.availability !== undefined &&
      (this.props.availability.id !== prevState.editedValue.id || 
        this.props.availability.version !== prevState.editedValue.version)) {
      // eslint-disable-next-line react/no-did-update-set-state
      this.setState({ resetCount: prevState.resetCount + 1, editedValue: this.props.availability });
    }
  }
 
  onSave() {
    const availability = this.state.editedValue;
    if (availability.employee !== undefined && availability.startDateTime !== undefined &&
      availability.endDateTime !== undefined && availability.state !== undefined) {
      this.props.onSave({ ...availability, tenantId: this.props.tenantId } as EmployeeAvailability);
    }
  }
 
  render() {
    const dateFormat = "MMMM dd, hh:mm a";
    const { t, tReady } = this.props;
    Iif (!tReady) {
      return (<></>);
    }
    return (
      <Modal
        title={this.props.availability? t("editAvailability") : t("createAvailability")}
        onClose={this.props.onClose}
        isOpen={this.props.isOpen}
        actions={
          [
            <Button 
              aria-label="Close Modal"
              variant={ButtonVariant.tertiary}
              key={0}
              onClick={this.props.onClose}
            >
              {t("close")}
            </Button>
          ].concat(this.props.availability? [
            <Button
              aria-label="Delete"
              variant={ButtonVariant.danger}
              key={1}
              onClick={() => this.props.onDelete(this.props.availability as EmployeeAvailability)}
            >
              {t("delete")}
            </Button>
          ] : []).concat([
            <Button aria-label="Save" key={2} onClick={this.onSave}>{t("save")}</Button>
          ])
        }
        isSmall
      >
        <Form id="modal-element" key={this.state.resetCount} onSubmit={(e) => e.preventDefault()}>
          <InputGroup>
            <Label>{t("availabilityStart")}</Label>
            <DatePicker
              aria-label="Availability Start"
              selected={this.state.editedValue.startDateTime}
              onChange={date => this.setState(prevState => ({
                editedValue: { ...prevState.editedValue, startDateTime: (date !== null)? date : undefined  }
              }))}
              dateFormat={dateFormat}
              showTimeSelect
            />
          </InputGroup>
          <InputGroup>
            <Label>{t("availabilityEnd")}</Label>
            <DatePicker
              aria-label="Availability End"
              selected={this.state.editedValue.endDateTime}
              onChange={date => this.setState(prevState => ({
                editedValue: { ...prevState.editedValue, endDateTime: (date !== null)? date : undefined  }
              }))}
              dateFormat={dateFormat}
              showTimeSelect
            />
          </InputGroup>
          <InputGroup>
            <Label>Employee</Label>
            <TypeaheadSelectInput
              aria-label="Employee"
              emptyText={t("selectEmployee")}
              value={this.state.editedValue.employee}
              options={this.props.employeeList}
              optionToStringMap={employee => employee.name}
              onChange={employee => this.setState(prevState => ({
                editedValue: { ...prevState.editedValue, employee: employee }
              }))}
            />
          </InputGroup>
          <InputGroup>
            <Label>{t("type")}</Label>
            <TypeaheadSelectInput
              aria-label="Type"
              emptyText="Select Type..."
              value={this.state.editedValue.state}
              options={["UNAVAILABLE","DESIRED", "UNDESIRED"] as ("UNAVAILABLE"|"DESIRED"|"UNDESIRED")[]}
              optionToStringMap={state => this.props.t("EmployeeAvailabilityState." + state)}
              onChange={state => this.setState(prevState => ({
                editedValue: { ...prevState.editedValue, state: state }
              }))}
            />
          </InputGroup>
        </Form>
      </Modal>
    );
  }
}
 
// eslint-disable-next-line no-undef
export default withTranslation("EditAvailabilityModal")(
  connect(mapStateToProps)(EditAvailabilityModal));