All files / src/ui/pages/admin NewTenantFormModal.tsx

92% Statements 23/25
100% Branches 12/12
86.67% Functions 13/15
95.83% Lines 23/24

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                                                          3x                   3x                         20x             3x 17x 17x 17x 1x     17x           17x 12x     17x                                           3x 1x 1x                               2x                       1x                     1x               2x               2x                   2x                     9x 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 * as React from 'react';
import { Modal, Button, ButtonVariant, InputGroup, Label, TextInput, Form } from "@patternfly/react-core";
import TypeaheadSelectInput from 'ui/components/TypeaheadSelectInput';
import RosterState from 'domain/RosterState';
import { tenantOperations } from 'store/tenant';
import { connect } from 'react-redux';
import { AppState } from 'store/types';
import moment from 'moment';
import { useTranslation } from 'react-i18next';
 
interface StateProps {
  timezoneList: string[];
}
 
const mapStateToProps = (state: AppState, props: OwnProps) => ({
  ...props,
  timezoneList: state.tenantData.timezoneList
}); 
 
interface DispatchProps {
  addTenant: typeof tenantOperations.addTenant;
  refreshSupportedTimezones: typeof tenantOperations.refreshSupportedTimezones;
}
 
const mapDispatchToProps: DispatchProps = {
  addTenant: tenantOperations.addTenant,
  refreshSupportedTimezones: tenantOperations.refreshSupportedTimezones
}
 
interface OwnProps {
  isOpen: boolean;
  onClose: () => void;
}
 
export type Props = StateProps & DispatchProps & OwnProps;
 
export function isFormCompleted(rs: Partial<RosterState>): rs is RosterState {
  return rs.draftLength !== undefined && rs.firstDraftDate !== undefined &&
    rs.lastHistoricDate !== undefined && rs.publishLength !== undefined &&
    rs.publishNotice !== undefined && rs.rotationLength !== undefined &&
    rs.rotationLength >=2 &&  rs.tenant !== undefined && rs.timeZone !== undefined &&
    rs.unplannedRotationOffset !== undefined;
}
 
export const NewTenantFormModal: React.FC<Props> = (props) => {
  const { t } = useTranslation("NewTenantFormModal");
  const { refreshSupportedTimezones } = props;
  React.useEffect(() => {
    refreshSupportedTimezones() 
  }, [refreshSupportedTimezones]);
 
  const [ formData, setFormData ] = React.useState<Partial<RosterState>>({
    timeZone: Intl.DateTimeFormat().resolvedOptions().timeZone,
    publishLength: 7,
    unplannedRotationOffset: 0
  });
 
  const setProperty = (properties: Partial<RosterState>) => {
    setFormData({ ...formData, ...properties });
  }
 
  return (
    <Modal
      title={t("createTenant")}
      onClose={props.onClose}
      isOpen={props.isOpen}
      actions={
        [(
          <Button 
            aria-label="Close Modal"
            variant={ButtonVariant.tertiary}
            key={0}
            onClick={props.onClose}
          >
            {t("close")}
          </Button>
        ),
        (
          <Button
            isDisabled={!isFormCompleted(formData)}
            aria-label="Save"
            key={2}
            onClick={() => {
              if (isFormCompleted(formData)) {
                props.addTenant(formData);
                props.onClose();
              }
            }}
          >
            {t("save")}
          </Button>
        )
        ]
      }
      isSmall
    >
      <Form onSubmit={(e) => e.preventDefault()}>
        <InputGroup>
          <Label>{t("name")}</Label>
          <TextInput
            aria-label="Name"
            onChange={name => setProperty({
              tenant: {
                name: name
              }
            })}
          />
        </InputGroup>
        <InputGroup>
          <Label>{t("scheduleStartDate")}</Label>
          <TextInput
            type="date"
            aria-label="Schedule Start Date"
            onChange={date => setProperty({ 
              lastHistoricDate: moment(date).subtract(1, "day").toDate(),
              firstDraftDate: moment(date).toDate()
            })}
          />
        </InputGroup>
        <InputGroup>
          <Label>{t("draftLength")}</Label>
          <TextInput
            type="number"
            aria-label="Draft Length"
            onChange={length => setProperty({ draftLength: parseInt(length) })}
          />
        </InputGroup>
        <InputGroup>
          <Label>{t("publishNotice")}</Label>
          <TextInput
            type="number"
            aria-label="Publish Notice"
            onChange={notice => setProperty({ publishNotice: parseInt(notice) })}
          />
        </InputGroup>
        <InputGroup>
          <Label>{t("publishLength")}</Label>
          <TextInput 
            defaultValue="7"
            type="number"
            onChange={length => setProperty({ publishLength: parseInt(length) })}
            aria-label="Publish Length"
            isDisabled
          />
        </InputGroup>
        <InputGroup>
          <Label>{t("rotationLength")}</Label>
          <TextInput
            type="number"
            aria-label="Rotation Length"
            onChange={length => setProperty({ rotationLength: parseInt(length) })}
            min={2}
          />
        </InputGroup>
        <InputGroup>
          <Label>{t("timezone")}</Label>
          <TypeaheadSelectInput
            aria-label="Timezone"
            emptyText={t("selectATimezone")}
            value={formData.timeZone}
            options={props.timezoneList}
            optionToStringMap={s => s}
            onChange={tz => setProperty({ timeZone: tz })}
          />
        </InputGroup>
      </Form>
    </Modal>
  );
};
 
export default connect(mapStateToProps, mapDispatchToProps)(NewTenantFormModal);