From 58b2e3315622b5a895da6f803e98013195250b1e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pau=20Ferrer=20Oca=C3=B1a?= Date: Wed, 23 Jan 2019 16:56:06 +0100 Subject: [PATCH] MOBILE-1332 notes: Delete notes --- scripts/langindex.json | 2 + .../components/list/addon-notes-list.html | 6 +++ src/addon/notes/components/list/list.ts | 51 ++++++++++++++++++- src/addon/notes/lang/en.json | 2 + src/addon/notes/providers/notes.ts | 21 ++++++++ src/assets/lang/en.json | 2 + 6 files changed, 83 insertions(+), 1 deletion(-) diff --git a/scripts/langindex.json b/scripts/langindex.json index 7381c6fc8..57d11d064 100644 --- a/scripts/langindex.json +++ b/scripts/langindex.json @@ -898,7 +898,9 @@ "addon.mod_workshop_assessment_rubric.mustchooseone": "workshopform_rubric", "addon.notes.addnewnote": "notes", "addon.notes.coursenotes": "notes", + "addon.notes.deleteconfirm": "notes", "addon.notes.eventnotecreated": "notes", + "addon.notes.eventnotedeleted": "notes", "addon.notes.nonotes": "notes", "addon.notes.note": "notes", "addon.notes.notes": "notes", diff --git a/src/addon/notes/components/list/addon-notes-list.html b/src/addon/notes/components/list/addon-notes-list.html index 2c069ca78..fbbb27c78 100644 --- a/src/addon/notes/components/list/addon-notes-list.html +++ b/src/addon/notes/components/list/addon-notes-list.html @@ -1,4 +1,7 @@ + @@ -37,6 +40,9 @@

{{note.userfullname}}

{{note.lastmodified | coreDateDayOrTime}}

{{ 'core.notsent' | translate }}

+ diff --git a/src/addon/notes/components/list/list.ts b/src/addon/notes/components/list/list.ts index 0150edbfd..78f439efd 100644 --- a/src/addon/notes/components/list/list.ts +++ b/src/addon/notes/components/list/list.ts @@ -14,11 +14,13 @@ import { Component, Input, OnDestroy, OnInit, ViewChild } from '@angular/core'; import { Content, ModalController } from 'ionic-angular'; +import { TranslateService } from '@ngx-translate/core'; import { CoreDomUtilsProvider } from '@providers/utils/dom'; import { CoreEventsProvider } from '@providers/events'; import { CoreSitesProvider } from '@providers/sites'; import { CoreTextUtilsProvider } from '@providers/utils/text'; import { CoreUserProvider } from '@core/user/providers/user'; +import { coreSlideInOut } from '@classes/animations'; import { AddonNotesProvider } from '../../providers/notes'; import { AddonNotesSyncProvider } from '../../providers/notes-sync'; @@ -28,6 +30,7 @@ import { AddonNotesSyncProvider } from '../../providers/notes-sync'; @Component({ selector: 'addon-notes-list', templateUrl: 'addon-notes-list.html', + animations: [coreSlideInOut] }) export class AddonNotesListComponent implements OnInit, OnDestroy { @Input() courseId: number; @@ -44,11 +47,14 @@ export class AddonNotesListComponent implements OnInit, OnDestroy { hasOffline = false; notesLoaded = false; user: any; + showDelete = false; + canDeleteNotes = false; + currentUserId: number; constructor(private domUtils: CoreDomUtilsProvider, private textUtils: CoreTextUtilsProvider, sitesProvider: CoreSitesProvider, eventsProvider: CoreEventsProvider, private modalCtrl: ModalController, private notesProvider: AddonNotesProvider, private notesSync: AddonNotesSyncProvider, - private userProvider: CoreUserProvider) { + private userProvider: CoreUserProvider, private translate: TranslateService) { // Refresh data if notes are synchronized automatically. this.syncObserver = eventsProvider.on(AddonNotesSyncProvider.AUTO_SYNCED, (data) => { if (data.courseId == this.courseId) { @@ -64,6 +70,8 @@ export class AddonNotesListComponent implements OnInit, OnDestroy { this.fetchNotes(false); } }, sitesProvider.getCurrentSiteId()); + + this.currentUserId = sitesProvider.getCurrentSiteUserId(); } /** @@ -111,6 +119,14 @@ export class AddonNotesListComponent implements OnInit, OnDestroy { }).catch((message) => { this.domUtils.showErrorModal(message); }).finally(() => { + let canDelete = this.notes && this.notes.length > 0; + if (canDelete && this.type == 'personal') { + canDelete = this.notes.find((note) => { + return note.usermodified == this.currentUserId; + }); + } + this.canDeleteNotes = canDelete; + this.notesLoaded = true; this.refreshIcon = 'refresh'; this.syncIcon = 'sync'; @@ -151,6 +167,7 @@ export class AddonNotesListComponent implements OnInit, OnDestroy { /** * Add a new Note to user and course. + * * @param {Event} e Event. */ addNote(e: Event): void { @@ -173,6 +190,38 @@ export class AddonNotesListComponent implements OnInit, OnDestroy { modal.present(); } + /** + * Delete a note. + * + * @param {Event} e Click event. + * @param {any} note Note to delete. + */ + deleteNote(e: Event, note: any): void { + e.preventDefault(); + e.stopPropagation(); + + this.domUtils.showConfirm(this.translate.instant('addon.notes.deleteconfirm')).then(() => { + this.notesProvider.deleteNote(note).then(() => { + this.showDelete = false; + + this.refreshNotes(true); + + this.domUtils.showToast('addon.notes.eventnotedeleted', true, 3000); + }).catch((error) => { + this.domUtils.showErrorModalDefault(error, 'Delete note failed.'); + }); + }).catch(() => { + // User cancelled, nothing to do. + }); + } + + /** + * Toggle delete. + */ + toggleDelete(): void { + this.showDelete = !this.showDelete; + } + /** * Tries to synchronize course notes. * diff --git a/src/addon/notes/lang/en.json b/src/addon/notes/lang/en.json index 3317484cd..c8256d0c4 100644 --- a/src/addon/notes/lang/en.json +++ b/src/addon/notes/lang/en.json @@ -1,7 +1,9 @@ { "addnewnote": "Add a new note", "coursenotes": "Course notes", + "deleteconfirm": "Delete this note?", "eventnotecreated": "Note created", + "eventnotedeleted": "Note deleted", "nonotes": "There are no notes of this type yet", "note": "Note", "notes": "Notes", diff --git a/src/addon/notes/providers/notes.ts b/src/addon/notes/providers/notes.ts index 006a78093..52caa2c37 100644 --- a/src/addon/notes/providers/notes.ts +++ b/src/addon/notes/providers/notes.ts @@ -133,6 +133,27 @@ export class AddonNotesProvider { }); } + /** + * Delete a note. + * + * @param {any} note Note object to delete. + * @param {string} [siteId] Site ID. If not defined, current site. + * @return {Promise} Promise resolved when done. + */ + deleteNote(note: any, siteId?: string): Promise { + return this.sitesProvider.getSite(siteId).then((site) => { + if (typeof note.offline != 'undefined' && note.offline) { + return this.notesOffline.deleteNote(note.userid, note.content, note.created, site.id); + } + + const data = { + notes: [note.id] + }; + + return site.write('core_notes_delete_notes', data); + }); + } + /** * Returns whether or not the notes plugin is enabled for a certain site. * diff --git a/src/assets/lang/en.json b/src/assets/lang/en.json index 7ed0d4812..ef8b0d722 100644 --- a/src/assets/lang/en.json +++ b/src/assets/lang/en.json @@ -898,7 +898,9 @@ "addon.mod_workshop_assessment_rubric.mustchooseone": "You have to select one of these items", "addon.notes.addnewnote": "Add a new note", "addon.notes.coursenotes": "Course notes", + "addon.notes.deleteconfirm": "Delete this note?", "addon.notes.eventnotecreated": "Note created", + "addon.notes.eventnotedeleted": "Note deleted", "addon.notes.nonotes": "There are no notes of this type yet", "addon.notes.note": "Note", "addon.notes.notes": "Notes",