From fc90fa4b5c5c8fbc7b77fa9acd17fcd385181e48 Mon Sep 17 00:00:00 2001 From: Johannes Millan Date: Sun, 25 Nov 2018 23:04:35 +0100 Subject: [PATCH] feat(reminders): refactor stuff to service --- .../dialog-add-note-reminder.component.ts | 34 ++++---------- src/app/note/note.service.ts | 45 ++++++++++++++++++- src/app/note/note/note.component.ts | 9 +--- 3 files changed, 53 insertions(+), 35 deletions(-) diff --git a/src/app/note/dialog-add-note-reminder/dialog-add-note-reminder.component.ts b/src/app/note/dialog-add-note-reminder/dialog-add-note-reminder.component.ts index 1b681950cd..6d878d325a 100644 --- a/src/app/note/dialog-add-note-reminder/dialog-add-note-reminder.component.ts +++ b/src/app/note/dialog-add-note-reminder/dialog-add-note-reminder.component.ts @@ -49,41 +49,25 @@ export class DialogAddNoteReminderComponent { save() { if (this.isEdit) { - this._reminderService.updateReminder(this.reminder.id, { - remindAt: new Date(this.date).getTime(), - title: this.title, - }); - this._snackService.open({ - type: 'SUCCESS', - message: `Updated reminder ${this.reminder.id} for note`, - icon: 'schedule', - }); + this._noteService.updateReminder( + this.note.id, + this.reminder.id, + new Date(this.date).getTime(), + this.title, + ); this.close(); } else { - const reminderId = this._reminderService.addReminder( - 'NOTE', + this._noteService.addReminder( this.note.id, + new Date(this.date).getTime(), this.title, - new Date(this.date).getTime() ); - this._noteService.update(this.note.id, {reminderId}); - this._snackService.open({ - type: 'SUCCESS', - message: `Added reminder ${reminderId} for note`, - icon: 'schedule', - }); this.close(); } } remove() { - this._reminderService.removeReminder(this.reminder.id); - this._noteService.update(this.note.id, {reminderId: null}); - this._snackService.open({ - type: 'SUCCESS', - message: `Deleted reminder ${this.reminder.id} for note`, - icon: 'schedule', - }); + this._noteService.removeReminder(this.note.id, this.reminder.id); this.close(); } diff --git a/src/app/note/note.service.ts b/src/app/note/note.service.ts index 0ad6d95be5..636ee8596c 100644 --- a/src/app/note/note.service.ts +++ b/src/app/note/note.service.ts @@ -16,9 +16,9 @@ import shortid from 'shortid'; import { initialNoteState, NoteState, selectAllNotes, selectIsShowNotes, selectNoteById } from './store/note.reducer'; import { PersistenceService } from '../core/persistence/persistence.service'; import { Actions, ofType } from '@ngrx/effects'; -import { Task } from '../tasks/task.model'; -import { selectTaskById } from '../tasks/store/task.selectors'; import { take } from 'rxjs/operators'; +import { ReminderService } from '../reminder/reminder.service'; +import { SnackService } from '../core/snack/snack.service'; @Injectable({ providedIn: 'root' @@ -31,6 +31,8 @@ export class NoteService { constructor( private _store$: Store, private _persistenceService: PersistenceService, + private _reminderService: ReminderService, + private _snackService: SnackService, private _actions$: Actions, ) { } @@ -85,4 +87,43 @@ export class NoteService { public updateOrder(ids: string[]) { this._store$.dispatch(new UpdateNoteOrder({ids})); } + + // REMINDER + // -------- + public addReminder(noteId: string, remindAt: number, title: string) { + const reminderId = this._reminderService.addReminder( + 'NOTE', + noteId, + title, + remindAt, + ); + this.update(noteId, {reminderId}); + this._snackService.open({ + type: 'SUCCESS', + message: `Added reminder ${reminderId} for note`, + icon: 'schedule', + }); + } + + public updateReminder(noteId: string, reminderId: string, remindAt: number, title: string) { + this._reminderService.updateReminder(reminderId, { + remindAt, + title, + }); + this._snackService.open({ + type: 'SUCCESS', + message: `Updated reminder ${reminderId} for note`, + icon: 'schedule', + }); + } + + public removeReminder(noteId: string, reminderId: string) { + this._reminderService.removeReminder(reminderId); + this.update(noteId, {reminderId: null}); + this._snackService.open({ + type: 'SUCCESS', + message: `Deleted reminder ${reminderId} for note`, + icon: 'schedule', + }); + } } diff --git a/src/app/note/note/note.component.ts b/src/app/note/note/note.component.ts index e829cb7326..33f6dee8c2 100644 --- a/src/app/note/note/note.component.ts +++ b/src/app/note/note/note.component.ts @@ -50,13 +50,6 @@ export class NoteComponent implements OnInit { } removeReminder() { - this._reminderService.removeReminder(this.note.reminderId); - this._noteService.update(this.note.id, {reminderId: null}); - this._snackService.open({ - type: 'SUCCESS', - message: `Deleted reminder ${this.note.reminderId} for note`, - icon: 'schedule', - }); - + this._noteService.removeReminder(this.note.id, this.note.reminderId); } }