refactor(playground): make playground great again

This commit is contained in:
Joao Dias
2016-10-23 16:21:18 +02:00
committed by Victor Berchet
parent 69f87ca075
commit 3d9d839c6c
28 changed files with 212 additions and 382 deletions

View File

@ -8,7 +8,6 @@
import {Component, Injectable} from '@angular/core';
import {isPresent} from '@angular/core/src/facade/lang';
import {ActivatedRoute, Router} from '@angular/router';
import * as db from './data';
@ -32,7 +31,7 @@ export class InboxRecord {
lastName: string,
date: string, draft?: boolean
} = null) {
if (isPresent(data)) {
if (data) {
this.setData(data);
}
}
@ -46,42 +45,42 @@ export class InboxRecord {
lastName: string,
date: string, draft?: boolean
}) {
this.id = record['id'];
this.subject = record['subject'];
this.content = record['content'];
this.email = record['email'];
this.firstName = (record as any /** TODO #9100 */)['first-name'];
this.lastName = (record as any /** TODO #9100 */)['last-name'];
this.date = record['date'];
this.draft = record['draft'] == true;
this.id = record.id;
this.subject = record.subject;
this.content = record.content;
this.email = record.email;
this.firstName = record.firstName;
this.lastName = record.lastName;
this.date = record.date;
this.draft = record.draft === true;
}
}
@Injectable()
export class DbService {
getData(): Promise<any[]> { return Promise.resolve(db.data); }
drafts(): Promise<any[]> {
return this.getData().then(
(data: any[]): any[] =>
data.filter(record => isPresent(record['draft']) && record['draft'] == true));
getData(): Promise<InboxRecord[]> {
return Promise.resolve(db.data.map((entry: {[key: string]: any}) => new InboxRecord({
id: entry['id'],
subject: entry['subject'],
content: entry['content'],
email: entry['email'],
firstName: entry['first-name'],
lastName: entry['last-name'],
date: entry['date'],
draft: entry['draft'],
})));
}
emails(): Promise<any[]> {
return this.getData().then(
(data: any[]): any[] => data.filter(record => !isPresent(record['draft'])));
drafts(): Promise<InboxRecord[]> {
return this.getData().then((data) => data.filter(record => record.draft));
}
email(id: any /** TODO #9100 */): Promise<any> {
return this.getData().then((data: any[]) => {
for (var i = 0; i < data.length; i++) {
var entry = data[i];
if (entry['id'] == id) {
return entry;
}
}
return null;
});
emails(): Promise<InboxRecord[]> {
return this.getData().then((data) => data.filter(record => !record.draft));
}
email(id: string): Promise<InboxRecord> {
return this.getData().then((data) => data.find((entry) => entry.id == id));
}
}
@ -92,17 +91,15 @@ export class InboxCmp {
constructor(public router: Router, db: DbService, route: ActivatedRoute) {
route.params.forEach(p => {
const sortType = p['sort'];
const sortEmailsByDate = isPresent(sortType) && sortType == 'date';
const sortEmailsByDate = p['sort'] === 'date';
db.emails().then((emails: any[]) => {
db.emails().then((emails) => {
this.ready = true;
this.items = emails.map(data => new InboxRecord(data));
this.items = emails;
if (sortEmailsByDate) {
this.items.sort(
(a: InboxRecord, b: InboxRecord) =>
new Date(a.date).getTime() < new Date(b.date).getTime() ? -1 : 1);
(a, b) => new Date(a.date).getTime() < new Date(b.date).getTime() ? -1 : 1);
}
});
});
@ -116,9 +113,9 @@ export class DraftsCmp {
private ready: boolean = false;
constructor(private router: Router, db: DbService) {
db.drafts().then((drafts: any[]) => {
db.drafts().then((drafts) => {
this.ready = true;
this.items = drafts.map(data => new InboxRecord(data));
this.items = drafts;
});
}
}