Add date parsing and redirect logic for date sub-path in url

This commit is contained in:
2018-03-22 13:55:24 +05:30
parent 5ca39065dc
commit 915b00edcd
2 changed files with 55 additions and 6 deletions
+51 -6
View File
@@ -21,7 +21,7 @@ class IsNiftyOpenApp extends React.Component {
return isTodayEventHoliday ? OpenOrClose.Open : OpenOrClose.Close; return isTodayEventHoliday ? OpenOrClose.Open : OpenOrClose.Close;
} }
renderOpenXDay(questionString: OpenOrCloseOrClosed): React.ReactElement<{}> { renderAllPanels(questionString: OpenOrCloseOrClosed, dateText: string): React.ReactElement<{}> {
var question: OpenOrClose; var question: OpenOrClose;
if (questionString === OpenOrCloseOrClosed.Open) { if (questionString === OpenOrCloseOrClosed.Open) {
question = OpenOrClose.Open; question = OpenOrClose.Open;
@@ -29,8 +29,44 @@ class IsNiftyOpenApp extends React.Component {
question = OpenOrClose.Close; question = OpenOrClose.Close;
} }
const xDay = Utils.createTodayDateInIndiaTZ(); let xDay: moment.Moment, xPlusOneDay: moment.Moment;
const xPlusOneDay = xDay.clone().add(1, "d"); {
const enum DateNames {
today = "today",
tomorrow = "tomorrow",
yesterday = "yesterday",
}
if (dateText === undefined) {
return this.redirectToOpenToday(question);
}
dateText = dateText.toLowerCase();
switch (dateText) {
case DateNames.today:
xDay = Utils.createTodayDateInIndiaTZ();
break;
case DateNames.tomorrow:
xDay = Utils.createTodayDateInIndiaTZ().add(1, "d");
break;
case DateNames.yesterday:
xDay = Utils.createTodayDateInIndiaTZ().subtract(1, "d");
break;
default:
let possiblyXDay = Utils.createSomeDateInIndiaTZ(dateText);
if (possiblyXDay.isValid()) {
xDay = possiblyXDay;
} else {
return this.redirectToOpenToday(question);
}
break;
}
}
xPlusOneDay = xDay.clone().add(1, "d");
return ( return (
<div className="IsNiftyOpenApp"> <div className="IsNiftyOpenApp">
@@ -62,16 +98,25 @@ class IsNiftyOpenApp extends React.Component {
</div> </div>
); );
} }
redirectToOpenToday(question: OpenOrClose = OpenOrClose.Open) {
return (<Redirect to={`/${question}/today`} />);
}
render() { render() {
return ( return (
<div className="App"> <div className="App">
<Switch> <Switch>
<Route <Route
exact={true} exact={true}
path='/:openOrClosed(open|close[d]?)' path='/:openOrClosed(open|close[d]?)/:dateText?'
render={(props) => { return this.renderOpenXDay(props.match.params.openOrClosed); }} render={(props) => {
return this.renderAllPanels(
props.match.params.openOrClosed,
props.match.params.dateText);
}}
/> />
<Redirect to="/open" /> {this.redirectToOpenToday()}
</Switch> </Switch>
</div> </div>
); );
+4
View File
@@ -21,6 +21,10 @@ export class Utils {
return moment.tz(moment.tz.guess()).tz("Asia/Kolkata"); return moment.tz(moment.tz.guess()).tz("Asia/Kolkata");
} }
static createSomeDateInIndiaTZ(dateString: string): moment.Moment {
return moment.tz(dateString, moment.tz.guess()).tz("Asia/Kolkata");
}
static checkSameDayInSameTZ(moment1: moment.Moment, moment2: moment.Moment, raiseErrorTZMismatch: boolean = true) { 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())) {