All files / src/ui/pages/shift EditShiftModal.tsx

93.75% Statements 30/32
97.3% Branches 36/37
95.45% Functions 21/22
92.31% Lines 24/26

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 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219                                                                                          4x                                       16x   16x 16x             14x   1x             13x                 4x 4x     2x         29x 29x 29x                                       1x                   1x           2x                       2x                           12x 1x                         20x 2x                         20x 2x                   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 React from 'react';
import { connect } from 'react-redux';
 
import {
  Button, Switch, InputGroup, Label, Form, Modal, ButtonVariant,
} from '@patternfly/react-core';
import DatePicker from 'react-datepicker';
 
import Shift from 'domain/Shift';
import Spot from 'domain/Spot';
import Employee from 'domain/Employee';
import { AppState } from 'store/types';
import { employeeSelectors } from 'store/employee';
import { spotSelectors } from 'store/spot';
import { withTranslation, WithTranslation } from 'react-i18next';
 
import 'react-datepicker/dist/react-datepicker.css';
import TypeaheadSelectInput from 'ui/components/TypeaheadSelectInput';
 
interface Props {
  tenantId: number;
  shift?: Shift;
  isOpen: boolean;
  employeeList: Employee[];
  spotList: Spot[];
  onSave: (shift: Shift) => void;
  onDelete: (shift: Shift) => void;
  onClose: () => void;
}
 
const mapStateToProps = (state: AppState, ownProps: {
  shift?: Shift;
  isOpen: boolean;
  onSave: (shift: Shift) => void;
  onDelete: (shift: Shift) => void;
  onClose: () => void;
}): Props => ({
  ...ownProps,
  tenantId: state.tenantData.currentTenantId,
  employeeList: employeeSelectors.getEmployeeList(state),
  spotList: spotSelectors.getSpotList(state)
}); 
 
interface State {
  resetCount: number;
  editedValue: Partial<Shift>;
}
 
export class EditShiftModal 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.shift }
    }
  }
 
  componentDidUpdate(prevProps: Props, prevState: State) {
    if (this.props.shift === undefined && prevProps.shift !== undefined) {
      // eslint-disable-next-line react/no-did-update-set-state
      this.setState({ resetCount: prevState.resetCount + 1, editedValue: {
        tenantId: this.props.tenantId,
        employee: null,
        rotationEmployee: null,
        pinnedByUser: false
      } });
    }
    else Iif (this.props.shift !== undefined &&
      (this.props.shift.id !== prevState.editedValue.id || 
        this.props.shift.version !== prevState.editedValue.version)) {
      // eslint-disable-next-line react/no-did-update-set-state
      this.setState({ resetCount: prevState.resetCount + 1, editedValue: this.props.shift });
    }
  }
 
  onSave() {
    const shift = this.state.editedValue;
    if (shift.spot !== undefined && shift.startDateTime !== undefined &&
        shift.endDateTime !== undefined && shift.employee !== undefined &&
        shift.pinnedByUser !== undefined && shift.rotationEmployee !== undefined) {
      this.props.onSave({ ...shift, tenantId: this.props.tenantId } as Shift);
    }
  }
 
  render() {
    const { t } = this.props;
    const dateFormat = "MMMM dd, hh:mm a";
    return (
      <Modal
        title={this.props.shift? t("editShift") : t("createShift")}
        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.shift? [
            <Button
              aria-label="Delete"
              variant={ButtonVariant.danger}
              key={1}
              onClick={() => this.props.onDelete(this.props.shift as Shift)}
            >
              {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("shiftStart")}</Label>
            <DatePicker
              aria-label="Shift 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("shiftEnd")}</Label>
            <DatePicker
              aria-label="Shift 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>{t("spot")}</Label>
            <TypeaheadSelectInput
              aria-label="Spot"
              emptyText={t("selectSpot")}
              value={this.state.editedValue.spot}
              options={this.props.spotList}
              optionToStringMap={spot => spot.name}
              onChange={spot => this.setState(prevState => ({
                editedValue: { ...prevState.editedValue, spot: spot }
              }))}
            />
          </InputGroup>
          <InputGroup>
            <Label>{t("employee")}</Label>
            <TypeaheadSelectInput
              aria-label="Employee"
              emptyText={t("unassigned")}
              value={(this.state.editedValue.employee !== null)? 
                this.state.editedValue.employee :  undefined}
              options={[undefined,...this.props.employeeList]}
              optionToStringMap={employee => employee? employee.name : t("unassigned")}
              onChange={employee => this.setState(prevState => ({
                editedValue: { ...prevState.editedValue, employee: (employee !== undefined)? employee : null }
              }))}
            />
          </InputGroup>
          <InputGroup>
            <Label>{t("rotationEmployee")}</Label>
            <TypeaheadSelectInput
              aria-label="Rotation Employee"
              emptyText={t("none")}
              value={(this.state.editedValue.rotationEmployee !== null)? 
                this.state.editedValue.rotationEmployee :  undefined}
              options={[undefined,...this.props.employeeList]}
              optionToStringMap={employee => employee? employee.name : t("none")}
              onChange={employee => this.setState(prevState => ({
                editedValue: { ...prevState.editedValue, rotationEmployee: (employee !== undefined)? employee : null }
              }))}
            />
          </InputGroup>
          <InputGroup>
            <Label>{t("isPinned")}</Label>
            <Switch
              aria-label="Is Pinned"
              isChecked={this.state.editedValue.pinnedByUser}
              onChange={isPinned => this.setState(prevState => ({
                editedValue: { ...prevState.editedValue, pinnedByUser: isPinned },
              }))}
            />
          </InputGroup>
        </Form>
      </Modal>
    );
  }
}
 
export default withTranslation("EditShiftModal")(
  connect(mapStateToProps)(EditShiftModal));