Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(): add DateUtil service #4

Merged
merged 1 commit into from
Apr 10, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions app/pages/representation/representation.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {Page, NavController, Modal, MenuController} from "ionic-angular/index";
import {BackendConnector} from "../../services/BackendConnector";
import {SessionAccessor} from "../../services/SessionAccessor";
import {DateUtil} from '../../services/DateUtil';
import {ToTitlePipe} from "../../pipes/ToTitlePipe";
import {ToIconPipe} from "../../pipes/ToIconPipe";
import {AsyncDefaultPipe} from "../../pipes/AsyncDefaultPipe";
Expand All @@ -10,7 +11,7 @@ import {AfterViewInit} from "angular2/core";

@Page({
templateUrl: 'build/pages/representation/representation.html',
providers: [BackendConnector, SessionAccessor],
providers: [BackendConnector, SessionAccessor, DateUtil],
pipes: [ToTitlePipe, ToIconPipe, AsyncDefaultPipe]
})
export class RepresentationPage implements AfterViewInit {
Expand All @@ -26,11 +27,12 @@ export class RepresentationPage implements AfterViewInit {
constructor(private backend: BackendConnector,
private nav: NavController,
private session: SessionAccessor,
private dateUtil: DateUtil,
private menu: MenuController) {


this.todayDate = this._getTodayDate();
this.tomorrowDate = this._getTomorrowDate();
this.todayDate = dateUtil.getTodayDate();
this.tomorrowDate = dateUtil.getTomorrowDate();

session.getToken().then((token) => {
this.todayPromise = backend.sendRepresentationRequest(this.todayDate, token);
Expand Down
9 changes: 5 additions & 4 deletions app/services/BackendConnector.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import {Injectable, Inject} from 'angular2/core';
import {Http, Headers} from "angular2/http";
import {URLEncoder} from "./URLEncoder";
import {DateFormatter} from "./DateFormatter";
import {DateUtil} from "./DateUtil";

@Injectable()
export class BackendConnector {

private BACKEND_URL = 'https://vpbackend.herokuapp.com';

constructor(@Inject(Http) private http: Http) {
constructor(@Inject(Http) private http: Http,
@Inject(DateUtil) private dateUtil: DateUtil) {

}

Expand All @@ -23,9 +24,9 @@ export class BackendConnector {
}).map((res) => res.json())
.toPromise();
}

sendRepresentationRequest(date: Date, token: string): Promise<any> {
return this.http.get(this.BACKEND_URL + '/representation/' + DateFormatter.formatForBackend(date), {
return this.http.get(this.BACKEND_URL + '/representation/' + this.dateUtil.formatForBackend(date), {
headers: new Headers({
'x-access-token': token
})
Expand Down
13 changes: 0 additions & 13 deletions app/services/DateFormatter.ts

This file was deleted.

41 changes: 41 additions & 0 deletions app/services/DateUtil.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import {Injectable} from 'angular2/core';

@Injectable()
export class DateUtil {

formatForBackend(date: Date): string {
var month = (date.getMonth() + 1).toString();
var day = date.getDate().toString();

month = month[1] ? month : '0' + month;
day = day[1] ? day : '0' + day;

return `${day}-${month}`;
}

getTodayDate(): Date {
let current = new Date();
let dayOfWeek = current.getDay();
let add = 0;

if (dayOfWeek === 0) add = -2;
else if (dayOfWeek === 6) add = -1;

current.setDate(current.getDate() + add);

return current;
}

getTomorrowDate(): Date {
let current = new Date();
let dayOfWeek = current.getDay();
let add = 1;

if (dayOfWeek === 5) add = 3;
else if (dayOfWeek === 6) add = 2;

current.setDate(current.getDate() + add);

return current;
}
}