All files / src/components/interval-input interval-input.tsx

66.67% Statements 16/24
50% Branches 9/18
33.33% Functions 2/6
66.67% Lines 14/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                                    31x 31x 31x 31x   31x   31x               1x 1x     1x 1x   1x                                                           31x 1x   1x                                                              
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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 { Button, InputGroup, Popover, Position } from '@blueprintjs/core';
import { DateRange, DateRangePicker } from '@blueprintjs/datetime';
import { IconNames } from '@blueprintjs/icons';
import React from 'react';
 
import './interval-input.scss';
 
const CURRENT_YEAR = new Date().getUTCFullYear();
 
function removeLocalTimezone(localDate: Date): Date {
  // Function removes the local timezone of the date and displays it in UTC
  return new Date(localDate.getTime() - localDate.getTimezoneOffset() * 60000);
}
 
function parseInterval(interval: string): DateRange {
  const dates = interval.split('/');
  Iif (dates.length !== 2) {
    return [undefined, undefined];
  }
  const startDate = Date.parse(dates[0]) ? new Date(dates[0]) : undefined;
  const endDate = Date.parse(dates[1]) ? new Date(dates[1]) : undefined;
  // Must check if the start and end dates are within range
  return [
    startDate && startDate.getFullYear() < CURRENT_YEAR - 20 ? undefined : startDate,
    endDate && endDate.getFullYear() > CURRENT_YEAR ? undefined : endDate,
  ];
}
function stringifyDateRange(localRange: DateRange): string {
  // This function takes in the dates selected from datepicker in local time, and displays them in UTC
  // Shall Blueprint make any changes to the way dates are selected, this function will have to be reworked
  const [localStartDate, localEndDate] = localRange;
  return `${
    localStartDate
      ? removeLocalTimezone(localStartDate)
          .toISOString()
          .substring(0, 19)
      : ''
  }/${
    localEndDate
      ? removeLocalTimezone(localEndDate)
          .toISOString()
          .substring(0, 19)
      : ''
  }`;
}
 
export interface IntervalInputProps {
  interval: string;
  placeholder: string | undefined;
  onValueChange: (interval: string) => void;
}
 
export const IntervalInput = React.memo(function IntervalInput(props: IntervalInputProps) {
  const { interval, placeholder, onValueChange } = props;
 
  return (
    <InputGroup
      value={interval}
      placeholder={placeholder}
      rightElement={
        <div>
          <Popover
            popoverClassName={'calendar'}
            content={
              <DateRangePicker
                timePrecision={'second'}
                value={parseInterval(interval)}
                contiguousCalendarMonths={false}
                onChange={(selectedRange: DateRange) => {
                  onValueChange(stringifyDateRange(selectedRange));
                }}
              />
            }
            position={Position.BOTTOM_RIGHT}
          >
            <Button rightIcon={IconNames.CALENDAR} />
          </Popover>
        </div>
      }
      onChange={(e: any) => {
        const value = e.target.value.replace(/[^\-0-9T:/]/g, '').substring(0, 39);
        onValueChange(value);
      }}
    />
  );
});