feat(reminders): refactor stuff to service

This commit is contained in:
Johannes Millan 2018-11-25 23:04:35 +01:00
parent c3a0330332
commit fc90fa4b5c
3 changed files with 53 additions and 35 deletions

View file

@ -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();
}

View file

@ -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<any>,
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',
});
}
}

View file

@ -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);
}
}