From 891e33e74a0cef108e88084e19c2fd947212de6e Mon Sep 17 00:00:00 2001 From: Kristof Boucher Charbonneau Date: Wed, 22 Mar 2017 22:19:21 -0400 Subject: [PATCH 1/3] Adding the notification model, service and interface --- tasklist/src/app/app.module.ts | 7 +- .../listNotification/listNotification.html | 33 +++++++++ .../listNotification/listNotification.scss | 18 +++++ .../listNotification/listNotification.ts | 69 ++++++++++++++++++ .../src/pages/listProject/listProject.html | 3 + tasklist/src/pages/listProject/listProject.ts | 7 +- tasklist/src/pages/login/login.ts | 3 +- tasklist/src/services/notificationService.ts | 71 +++++++++++++++++++ 8 files changed, 207 insertions(+), 4 deletions(-) create mode 100644 tasklist/src/pages/listNotification/listNotification.html create mode 100644 tasklist/src/pages/listNotification/listNotification.scss create mode 100644 tasklist/src/pages/listNotification/listNotification.ts create mode 100644 tasklist/src/services/notificationService.ts diff --git a/tasklist/src/app/app.module.ts b/tasklist/src/app/app.module.ts index a89d83e..19b57cb 100644 --- a/tasklist/src/app/app.module.ts +++ b/tasklist/src/app/app.module.ts @@ -4,6 +4,7 @@ import { MyApp } from './app.component'; import { Home } from '../pages/home/home'; import { ListProject } from '../pages/listProject/listProject'; import { ListTask, PopOverPage } from '../pages/listTask/listTask'; +import { ListNotification } from '../pages/listNotification/listNotification'; import { ModalAddProject } from '../pages/modalAddProject/modalAddProject'; import { ModalAddTask } from '../pages/modalAddTask/modalAddTask'; import { Login } from '../pages/login/login'; @@ -23,7 +24,8 @@ import { Config } from '../config/config'; DetailTask, ModalAddTask, PopOverPage, - MyTask + MyTask, + ListNotification ], imports: [ IonicModule.forRoot(MyApp, { @@ -41,7 +43,8 @@ import { Config } from '../config/config'; DetailTask, ModalAddTask, PopOverPage, - MyTask + MyTask, + ListNotification ], providers: [ { diff --git a/tasklist/src/pages/listNotification/listNotification.html b/tasklist/src/pages/listNotification/listNotification.html new file mode 100644 index 0000000..135cb9e --- /dev/null +++ b/tasklist/src/pages/listNotification/listNotification.html @@ -0,0 +1,33 @@ + + + + + Notifications + + + + + + + + + + + + + +
+ + + +

{{notification.title}}

+

{{notification.text}}

+

From: {{notification.sender.username}}

+
+
+
+ +
+
diff --git a/tasklist/src/pages/listNotification/listNotification.scss b/tasklist/src/pages/listNotification/listNotification.scss new file mode 100644 index 0000000..93c89d4 --- /dev/null +++ b/tasklist/src/pages/listNotification/listNotification.scss @@ -0,0 +1,18 @@ +list-notification { + + .notification { + .container-icon { + width: 40px; + } + .title { + font-size: 1.5rem; + font-weight: bold; + } + .sender { + .name { + font-weight: bold; + } + } + } + +} diff --git a/tasklist/src/pages/listNotification/listNotification.ts b/tasklist/src/pages/listNotification/listNotification.ts new file mode 100644 index 0000000..8223cd5 --- /dev/null +++ b/tasklist/src/pages/listNotification/listNotification.ts @@ -0,0 +1,69 @@ +import { Component } from '@angular/core'; + +import { ModalController, NavController, NavParams, ToastController } from 'ionic-angular'; + +import { NotificationService } from "../../services/notificationService"; + +@Component({ + selector: 'list-notification', + templateUrl: 'listNotification.html', + providers: [NotificationService] +}) +export class ListNotification { + notifications: Array; + + constructor( + public navCtrl: NavController, + public navParams: NavParams, + public toastCtrl: ToastController, + private notificationService: NotificationService + ) { + this.getDataFromApi(); + } + + getDataFromApi() { + this.notificationService.listNotifications().subscribe( + response => { + this.notifications = response.json() + }, + err => { + let toast = this.toastCtrl.create({ + message: 'Error : Connection server', + dismissOnPageChange: true + }); + toast.present(); + } + ); + } + + notificationTapped(event, notification) { + // Mark notification as read or unread + let handler; + if( notification.is_read ) { + handler = this.notificationService.markNotificationAsUnread(notification.id); + } else { + handler = this.notificationService.markNotificationAsRead(notification.id); + } + + handler.subscribe( + response => { + let toast = this.toastCtrl.create({ + message: 'Notification "' + notification.title + '"' + (notification.is_read ? 'was marked as unread' : 'was mark as read'), + duration: 3000 + }); + toast.present(); + + // Refresh project list + this.getDataFromApi(); + }, + err => { + let toast = this.toastCtrl.create({ + message: 'Error : Connection server', + dismissOnPageChange: true + }); + toast.present(); + } + ); + } + +} diff --git a/tasklist/src/pages/listProject/listProject.html b/tasklist/src/pages/listProject/listProject.html index ddf1e62..b23e101 100644 --- a/tasklist/src/pages/listProject/listProject.html +++ b/tasklist/src/pages/listProject/listProject.html @@ -7,6 +7,9 @@ Projects + diff --git a/tasklist/src/pages/listProject/listProject.ts b/tasklist/src/pages/listProject/listProject.ts index 9cd18ac..58c77a6 100644 --- a/tasklist/src/pages/listProject/listProject.ts +++ b/tasklist/src/pages/listProject/listProject.ts @@ -3,8 +3,9 @@ import { Component } from '@angular/core'; import { ModalController, NavController, NavParams, ToastController } from 'ionic-angular'; import { ListTask } from '../listTask/listTask'; +import { ListNotification } from '../listNotification/listNotification'; import { ModalAddProject } from '../modalAddProject/modalAddProject'; -import {ProjectService} from '../../services/projectService'; +import { ProjectService } from '../../services/projectService'; @Component({ selector: 'list-project', @@ -32,6 +33,10 @@ export class ListProject { }); } + notificationTapped() { + this.navCtrl.push(ListNotification); + } + deleteProject(item) { let title = item.title; diff --git a/tasklist/src/pages/login/login.ts b/tasklist/src/pages/login/login.ts index af895e1..1f18256 100644 --- a/tasklist/src/pages/login/login.ts +++ b/tasklist/src/pages/login/login.ts @@ -45,7 +45,8 @@ export class Login { // Redirect after control this.storage.set('username', this.username); this.storage.set('password', this.password); - this.storage.set('token', data.token) + this.storage.set('token', data.token); + this.storage.set('user_id', data.user_id); this.navCtrl.setRoot(ListProject, { }); }, err => { diff --git a/tasklist/src/services/notificationService.ts b/tasklist/src/services/notificationService.ts new file mode 100644 index 0000000..7ee1ea6 --- /dev/null +++ b/tasklist/src/services/notificationService.ts @@ -0,0 +1,71 @@ +import {Http, Headers, RequestOptions, URLSearchParams} from '@angular/http'; +import 'rxjs/add/operator/map'; +import {Injectable} from "@angular/core"; +import { Observable } from 'rxjs/Rx'; +import { Storage } from '@ionic/storage'; +import { Config } from '../config/config'; + +@Injectable() +export class NotificationService { + public serverURL:string; + public propertiesURL:string; + + constructor(public storage: Storage, protected http:Http, private _config:Config) { + this.serverURL = _config.get('apiUrl'); + storage.get('user_id').then( + (user_id) => { + this.propertiesURL = this.serverURL + '/users/' + user_id + '/notification'; + } + ); + + } + + buildOptions(params){ + let headers = new Headers(); + let storage = new Storage(); + + return storage.get("token").then( + (token) => { + headers.append('Authorization', 'Token ' + token); + let options; + if(params){ + options = new RequestOptions({ headers: headers, search: params }); + } + else { + options = new RequestOptions({ headers: headers }); + } + return options; + } + ) + } + + listNotifications() { + return Observable + .fromPromise(this.buildOptions(null)) + .switchMap((options) => this.http.get(this.propertiesURL, options)); + } + + getNotification(id) { + return Observable + .fromPromise(this.buildOptions(null)) + .switchMap((options) => this.http.get(this.propertiesURL + '/' + id, options)); + } + + markNotificationAsRead(id) { + let data = new URLSearchParams(); + data.append('is_read', 'True'); + + return Observable + .fromPromise(this.buildOptions(null)) + .switchMap((options) => this.http.patch(this.propertiesURL + '/' + id, data, options)); + } + + markNotificationAsUnread(id) { + let data = new URLSearchParams(); + data.append('is_read', 'False'); + + return Observable + .fromPromise(this.buildOptions(null)) + .switchMap((options) => this.http.patch(this.propertiesURL + '/' + id, data, options)); + } +} From 205af8f62f84775ca30af7880002fef230a6d419 Mon Sep 17 00:00:00 2001 From: Kristof Boucher Charbonneau Date: Wed, 22 Mar 2017 23:36:00 -0400 Subject: [PATCH 2/3] removing the user id from the notification endpoint --- tasklist/src/pages/login/login.ts | 1 - tasklist/src/services/notificationService.ts | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/tasklist/src/pages/login/login.ts b/tasklist/src/pages/login/login.ts index 1f18256..4a12cdc 100644 --- a/tasklist/src/pages/login/login.ts +++ b/tasklist/src/pages/login/login.ts @@ -46,7 +46,6 @@ export class Login { this.storage.set('username', this.username); this.storage.set('password', this.password); this.storage.set('token', data.token); - this.storage.set('user_id', data.user_id); this.navCtrl.setRoot(ListProject, { }); }, err => { diff --git a/tasklist/src/services/notificationService.ts b/tasklist/src/services/notificationService.ts index 7ee1ea6..b1c2618 100644 --- a/tasklist/src/services/notificationService.ts +++ b/tasklist/src/services/notificationService.ts @@ -14,7 +14,7 @@ export class NotificationService { this.serverURL = _config.get('apiUrl'); storage.get('user_id').then( (user_id) => { - this.propertiesURL = this.serverURL + '/users/' + user_id + '/notification'; + this.propertiesURL = this.serverURL + '/notification'; } ); From b03d1011bb68a7f2b25357b239304a80605d2e42 Mon Sep 17 00:00:00 2001 From: Kristof Boucher Charbonneau Date: Thu, 23 Mar 2017 18:07:18 -0400 Subject: [PATCH 3/3] Removing unused configuration --- tasklist/src/services/notificationService.ts | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/tasklist/src/services/notificationService.ts b/tasklist/src/services/notificationService.ts index b1c2618..0167c81 100644 --- a/tasklist/src/services/notificationService.ts +++ b/tasklist/src/services/notificationService.ts @@ -12,12 +12,7 @@ export class NotificationService { constructor(public storage: Storage, protected http:Http, private _config:Config) { this.serverURL = _config.get('apiUrl'); - storage.get('user_id').then( - (user_id) => { - this.propertiesURL = this.serverURL + '/notification'; - } - ); - + this.propertiesURL = this.serverURL + '/notification'; } buildOptions(params){