"Fossies" - the Fresh Open Source Software Archive

Member "graylog2-server-4.2.0/graylog2-web-interface/src/views/components/searchbar/date-time-picker/AbsoluteDatePicker.tsx" (13 Oct 2021, 1870 Bytes) of package /linux/misc/graylog2-server-4.2.0.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) TSX (TypeScript with React) source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. See also the latest Fossies "Diffs" side-by-side code changes report for "AbsoluteDatePicker.tsx": 4.1.6_vs_4.2.0.

    1 /*
    2  * Copyright (C) 2020 Graylog, Inc.
    3  *
    4  * This program is free software: you can redistribute it and/or modify
    5  * it under the terms of the Server Side Public License, version 1,
    6  * as published by MongoDB, Inc.
    7  *
    8  * This program is distributed in the hope that it will be useful,
    9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
   10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
   11  * Server Side Public License for more details.
   12  *
   13  * You should have received a copy of the Server Side Public License
   14  * along with this program. If not, see
   15  * <http://www.mongodb.com/licensing/server-side-public-license>.
   16  */
   17 import * as React from 'react';
   18 import PropTypes from 'prop-types';
   19 import { DateUtils } from 'react-day-picker';
   20 import moment from 'moment';
   21 
   22 import { DatePicker } from 'components/common';
   23 import DateTime from 'logic/datetimes/DateTime';
   24 
   25 type Props = {
   26   dateTime: string,
   27   onChange?: (string) => void,
   28   startDate?: Date,
   29 }
   30 
   31 const AbsoluteDatePicker = ({ dateTime, onChange, startDate }: Props) => {
   32   const initialDateTime = moment(dateTime).toObject();
   33 
   34   const _onDatePicked = (date) => {
   35     if (!!startDate && DateUtils.isDayBefore(date, startDate)) {
   36       return false;
   37     }
   38 
   39     const newDate = moment(date).toObject();
   40 
   41     return onChange(moment({
   42       ...initialDateTime,
   43       years: newDate.years,
   44       months: newDate.months,
   45       date: newDate.date,
   46     }).format(DateTime.Formats.DATETIME));
   47   };
   48 
   49   return (
   50     <DatePicker date={dateTime}
   51                 onChange={_onDatePicked}
   52                 fromDate={startDate} />
   53   );
   54 };
   55 
   56 AbsoluteDatePicker.propTypes = {
   57   dateTime: PropTypes.string.isRequired,
   58   onChange: PropTypes.func,
   59   startDate: PropTypes.instanceOf(Date),
   60 };
   61 
   62 AbsoluteDatePicker.defaultProps = {
   63   onChange: () => {},
   64   startDate: undefined,
   65 };
   66 
   67 export default AbsoluteDatePicker;