Skip to content

Commit

Permalink
Fixed date range tab - another round of refactoring and bug fixes.
Browse files Browse the repository at this point in the history
  • Loading branch information
rafalnowak committed Mar 9, 2018
1 parent 1c041d8 commit a887f45
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 9 deletions.
17 changes: 9 additions & 8 deletions src/client/components/date-range-input/date-range-input.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import * as React from 'react';
import { Timezone } from 'chronoshift';
import * as moment from 'moment';
import 'moment-timezone';
import { getWallTimeDateOnlyString, getWallTimeTimeOnlyString } from '../../../common/utils';
import { getWallTimeDateOnlyString, getWallTimeTimeOnlyString, isFullyDefinedDate, isFullyDefinedTime, combineDateAndTimeIntoMoment } from '../../../common/utils';

export interface DateRangeInputProps extends React.Props<any> {
time: Date;
Expand Down Expand Up @@ -76,25 +76,26 @@ export class DateRangeInput extends React.Component<DateRangeInputProps, DateRan
this.setState({
dateString
});
this.changeDate(dateString, this.state.timeString);
if (isFullyDefinedDate(dateString)) {
this.changeDate(dateString, this.state.timeString);
}
}

timeChange(e: KeyboardEvent) {
const timeString = (e.target as HTMLInputElement).value.replace(/[^\d:]/g, '');
this.setState({
timeString
});
this.changeDate(this.state.dateString, timeString);
if (isFullyDefinedTime(timeString)) {
this.changeDate(this.state.dateString, timeString);
}
}

changeDate(possibleDateString: string, possibleTimeString: string): void {
const { timezone, onChange } = this.props;

const fullDateTimeString = possibleDateString + "T" + possibleTimeString;
const possibleMoment = moment.tz(fullDateTimeString, timezone.toString());
if (possibleMoment && !possibleMoment.isValid()) {
onChange(null);
} else {
const possibleMoment = combineDateAndTimeIntoMoment(possibleDateString, possibleTimeString, timezone);
if (possibleMoment && possibleMoment.isValid()) {
onChange(possibleMoment.toDate());
}
}
Expand Down
15 changes: 14 additions & 1 deletion src/common/utils/time/time.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ export function getWallTimeString(date: Date, timezone: Timezone): string {
return wallTimeISOString.replace('T', ', ');
}

export function getWallTimeDateOnlyString(date: Date, timezone: Timezone, includeTime?: boolean, delimiter?: string): string {
export function getWallTimeDateOnlyString(date: Date, timezone: Timezone): string {
return moment.tz(date, timezone.toString()).format(FORMAT_DATE);
}

Expand Down Expand Up @@ -219,3 +219,16 @@ export function formatTimeBasedOnGranularity(range: TimeRange, granularity: Dura
export function formatGranularity(granularity: string): string {
return granularity.replace(/^PT?/, '');
}

export function isFullyDefinedDate(date: string): boolean {
return date.length === FORMAT_DATE.length;
}

export function isFullyDefinedTime(time: string): boolean {
return time.length === FORMAT_TIME.length;
}

export function combineDateAndTimeIntoMoment(date: string, time: string, timezone: Timezone): moment.Moment {
const fullDateTimeString = date + "T" + time;
return moment.tz(fullDateTimeString, timezone.toString());
}

0 comments on commit a887f45

Please sign in to comment.