Add next open/close day panel + refactor common funcs to util

This commit is contained in:
2018-03-19 14:00:34 +05:30
parent eb3151471a
commit 047bc7c291
11 changed files with 309 additions and 118 deletions
+7 -48
View File
@@ -1,33 +1,23 @@
import * as React from 'react';
import * as moment from 'moment-timezone';
import * as Linq from 'linq';
import * as classnames from 'classnames';
import "./Component.css";
import { Holiday, HolidayList, getWeekendHoliday } from '../../Data/index';
import { Holiday, HolidayList } from '../../Data/index';
import { ClosedReason } from '../ClosedReason';
import { Utils, HolidayAnswer, OpenOrClose } from "../../Utils";
export interface OpenXDayProps {
question: OpenXDay.OpenOrClose;
question: OpenOrClose;
xDay: moment.Moment;
allHolidays: HolidayList;
}
export class OpenXDay extends React.Component<OpenXDayProps, {}> {
question: OpenXDay.OpenOrClose;
question: OpenOrClose;
xDay: moment.Moment;
allHolidays: HolidayList;
answer: { open: boolean, holiday: Holiday | undefined };
static checkSameDayInSameTZ(moment1: moment.Moment, moment2: moment.Moment, raiseErrorTZMismatch: boolean = true) {
// tslint:disable-next-line:triple-equals
if (raiseErrorTZMismatch && (moment1.tz() != moment2.tz())) {
throw new Error("Timezones don't match");
}
// tslint:disable-next-line:triple-equals
return (moment1.startOf("day").format() == moment2.startOf("day").format());
}
answer: HolidayAnswer;
constructor(props: OpenXDayProps) {
super(props);
@@ -35,35 +25,11 @@ export class OpenXDay extends React.Component<OpenXDayProps, {}> {
this.xDay = props.xDay;
this.allHolidays = props.allHolidays;
this.processAnswer();
}
processAnswer(): void {
{
const matchingHoliday = Linq
.from<Holiday>(this.allHolidays)
.firstOrDefault(
(eachHoliday, _tmp) => OpenXDay.checkSameDayInSameTZ(this.xDay, eachHoliday.Date)
);
if (matchingHoliday === null) {
this.answer = { open: true, holiday: undefined };
} else {
this.answer = { open: false, holiday: matchingHoliday };
}
}
{
const weekendHoliday = getWeekendHoliday(this.xDay);
if (typeof weekendHoliday !== "undefined") {
this.answer = { open: false, holiday: weekendHoliday };
}
}
this.answer = Utils.getHolidayAnswerOnDate(this.xDay, this.allHolidays);
}
render() {
const answerBoolean = (this.question === OpenXDay.OpenOrClose.Open ? this.answer.open : !this.answer.open);
const answerBoolean = (this.question === OpenOrClose.Open ? this.answer.open : !this.answer.open);
const bgColorClass = (this.answer.open ? "green" : "red");
return (
@@ -88,11 +54,4 @@ export class OpenXDay extends React.Component<OpenXDayProps, {}> {
</div>
);
}
}
export module OpenXDay {
export enum OpenOrClose {
Open = 'open',
Close = 'closed'
}
}