mirror of
https://github.com/bendtherules/is-nifty-open.git
synced 2026-08-18 22:02:57 +00:00
Add OpenXDay props, move today creation to utils
And pass in today from App
This commit is contained in:
@@ -2,13 +2,18 @@ import * as React from 'react';
|
|||||||
import './App.css';
|
import './App.css';
|
||||||
import { OpenXDay } from '../OpenXDay';
|
import { OpenXDay } from '../OpenXDay';
|
||||||
import { allHolidays } from '../../Data';
|
import { allHolidays } from '../../Data';
|
||||||
|
import { Utils } from "../../Utils";
|
||||||
|
|
||||||
class IsNiftyOpenApp extends React.Component {
|
class IsNiftyOpenApp extends React.Component {
|
||||||
render() {
|
render() {
|
||||||
return (
|
return (
|
||||||
<div className="App">
|
<div className="App">
|
||||||
<div className="OpenXDay-container">
|
<div className="OpenXDay-container">
|
||||||
<OpenXDay question={OpenXDay.OpenOrClose.Open} allHolidays={allHolidays} />
|
<OpenXDay
|
||||||
|
question={OpenXDay.OpenOrClose.Open}
|
||||||
|
xDay={Utils.createTodayDateInIndiaTZ()}
|
||||||
|
allHolidays={allHolidays}
|
||||||
|
/>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -6,23 +6,20 @@ import "./Component.css";
|
|||||||
import { Holiday, HolidayList, getWeekendHoliday } from '../../Data/index';
|
import { Holiday, HolidayList, getWeekendHoliday } from '../../Data/index';
|
||||||
import { ClosedReason } from '../ClosedReason';
|
import { ClosedReason } from '../ClosedReason';
|
||||||
|
|
||||||
export class OpenXDay extends React.Component<{
|
export interface OpenXDayProps {
|
||||||
question: OpenXDay.OpenOrClose,
|
|
||||||
allHolidays: HolidayList
|
|
||||||
}, {}> {
|
|
||||||
question: OpenXDay.OpenOrClose;
|
question: OpenXDay.OpenOrClose;
|
||||||
|
xDay: moment.Moment;
|
||||||
allHolidays: HolidayList;
|
allHolidays: HolidayList;
|
||||||
answer: { open: boolean, holiday: Holiday | undefined };
|
|
||||||
|
|
||||||
constructor(props: { question: OpenXDay.OpenOrClose, allHolidays: HolidayList }) {
|
|
||||||
super(props);
|
|
||||||
this.question = props.question;
|
|
||||||
this.allHolidays = props.allHolidays;
|
|
||||||
|
|
||||||
this.processAnswer();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
checkSameDayInSameTZ(moment1: moment.Moment, moment2: moment.Moment, raiseErrorTZMismatch: boolean = true) {
|
export class OpenXDay extends React.Component<OpenXDayProps, {}> {
|
||||||
|
question: OpenXDay.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
|
// tslint:disable-next-line:triple-equals
|
||||||
if (raiseErrorTZMismatch && (moment1.tz() != moment2.tz())) {
|
if (raiseErrorTZMismatch && (moment1.tz() != moment2.tz())) {
|
||||||
throw new Error("Timezones don't match");
|
throw new Error("Timezones don't match");
|
||||||
@@ -32,18 +29,21 @@ export class OpenXDay extends React.Component<{
|
|||||||
return (moment1.startOf("day").format() == moment2.startOf("day").format());
|
return (moment1.startOf("day").format() == moment2.startOf("day").format());
|
||||||
}
|
}
|
||||||
|
|
||||||
createTodayDateInIndiaTZ(): moment.Moment {
|
constructor(props: OpenXDayProps) {
|
||||||
return moment.tz(moment.tz.guess()).tz("Asia/Kolkata");
|
super(props);
|
||||||
|
this.question = props.question;
|
||||||
|
this.xDay = props.xDay;
|
||||||
|
this.allHolidays = props.allHolidays;
|
||||||
|
|
||||||
|
this.processAnswer();
|
||||||
}
|
}
|
||||||
|
|
||||||
processAnswer(): void {
|
processAnswer(): void {
|
||||||
var todayDateInIndiaTZ = this.createTodayDateInIndiaTZ();
|
|
||||||
|
|
||||||
{
|
{
|
||||||
const matchingHoliday = Linq
|
const matchingHoliday = Linq
|
||||||
.from<Holiday>(this.allHolidays)
|
.from<Holiday>(this.allHolidays)
|
||||||
.firstOrDefault(
|
.firstOrDefault(
|
||||||
(eachHoliday, _tmp) => this.checkSameDayInSameTZ(todayDateInIndiaTZ, eachHoliday.Date)
|
(eachHoliday, _tmp) => OpenXDay.checkSameDayInSameTZ(this.xDay, eachHoliday.Date)
|
||||||
);
|
);
|
||||||
|
|
||||||
if (matchingHoliday === null) {
|
if (matchingHoliday === null) {
|
||||||
@@ -54,7 +54,7 @@ export class OpenXDay extends React.Component<{
|
|||||||
}
|
}
|
||||||
|
|
||||||
{
|
{
|
||||||
const weekendHoliday = getWeekendHoliday(todayDateInIndiaTZ);
|
const weekendHoliday = getWeekendHoliday(this.xDay);
|
||||||
|
|
||||||
if (typeof weekendHoliday !== "undefined") {
|
if (typeof weekendHoliday !== "undefined") {
|
||||||
this.answer = { open: false, holiday: weekendHoliday };
|
this.answer = { open: false, holiday: weekendHoliday };
|
||||||
|
|||||||
@@ -0,0 +1 @@
|
|||||||
|
export { Utils } from "./utils";
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
import * as moment from 'moment-timezone';
|
||||||
|
|
||||||
|
export class Utils {
|
||||||
|
static createTodayDateInIndiaTZ(): moment.Moment {
|
||||||
|
return moment.tz(moment.tz.guess()).tz("Asia/Kolkata");
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user