From 8fe90460222c1d30cbbd8228dbdfabe11c40048e Mon Sep 17 00:00:00 2001 From: HerrHase Date: Thu, 6 Oct 2022 00:27:46 +0200 Subject: [PATCH] bugfix for remove tags that not longer used --- src/js/components/filter.riot | 2 +- src/js/database/apps.js | 47 ++++++++++++++++++++++------------- src/js/database/tags.js | 33 ++++++++++++------------ src/js/stores/apps.js | 1 - 4 files changed, 48 insertions(+), 35 deletions(-) diff --git a/src/js/components/filter.riot b/src/js/components/filter.riot index 479b510..46bb555 100644 --- a/src/js/components/filter.riot +++ b/src/js/components/filter.riot @@ -40,7 +40,7 @@ /** * filtering apps by tags, showing all available tags as list of buttons - * whith a checkbox icon, after select or deselect update apps + * whith a checkbox icon, after select or deselect update apps * * @author Björn Hase * @license hhttps://www.gnu.org/licenses/gpl-3.0.en.html GPL-3 diff --git a/src/js/database/apps.js b/src/js/database/apps.js index dc76d5c..533d9fa 100644 --- a/src/js/database/apps.js +++ b/src/js/database/apps.js @@ -19,13 +19,14 @@ class AppsDatabase extends DatabaseHandler { 'name' ], 'name-sort') - // add index for apps to sort by name + // add index for apps to sort by date_started this.createIndex([ 'date_started' ], 'date-started-sort') } /** + * create app * * @param {object} data * @return {object} @@ -33,11 +34,13 @@ class AppsDatabase extends DatabaseHandler { */ create(data) { + // default for date_started data.date_started = 0 return this.db.post(data) .then((response) => { + // if tags are in data update tags also in database if (data.tags) { const tagsDatabase = new TagsDatabase() tagsDatabase.update(data.tags) @@ -45,11 +48,14 @@ class AppsDatabase extends DatabaseHandler { return this.findOneById(response._id) }).catch((error) => { + + // @TODO handle errors from pouchdb console.error(error) }) } /** + * update app * * @param {object} data * @return {object} @@ -60,26 +66,34 @@ class AppsDatabase extends DatabaseHandler { .then((response) => { return this.findOneById(response._id) }).catch((error) => { + + // @TODO handle errors from pouchdb console.error(error) }) } /** + * remove app * - * @param {object} data + * @param {object} document * @return {object} * */ - remove(data) { - return this.db.remove(data) + remove(document) { + return this.db.remove(document) .then(async (response) => { - const tagsDatabase = new TagsDatabase() - - await tagsDatabase.removeNotNeeded() + // successfull remove document, check for tags that + // not longer used + if (response.ok === true) { + const tagsDatabase = new TagsDatabase() + await tagsDatabase.removeNotNeeded() + } - return true + return response }).catch((error) => { + + // @TODO handle errors from pouchdb console.error(error) }) } @@ -115,6 +129,8 @@ class AppsDatabase extends DatabaseHandler { return documents.docs[0] } }).catch((error) => { + + // @TODO handle errors from pouchdb console.error(error) }) } @@ -148,13 +164,16 @@ class AppsDatabase extends DatabaseHandler { query.sort = [] query.sort.push(parameters.sort) + // if sort is date_stared change 'date-started-sort' if (parameters.sort === 'date_started') { query.use_index = '_design/date-started-sort' query.selector = { 'date_started': { - '$regex': '' + '$exists': true } } + + // if sort is name change 'name-sort' } else { query.use_index = '_design/name-sort' query.selector = { @@ -173,16 +192,10 @@ class AppsDatabase extends DatabaseHandler { } return this.db.find(query).then((documents) => { - if (documents.warning) { - console.log(documents.warning) - } - - if (documents.docs.length > 0 && parameters.tags.length > 0) { - - } - return documents.docs }).catch((error) => { + + // @TODO handle errors from pouchdb console.error(error) }) } diff --git a/src/js/database/tags.js b/src/js/database/tags.js index 4fa509b..6a221f7 100644 --- a/src/js/database/tags.js +++ b/src/js/database/tags.js @@ -1,5 +1,6 @@ import DatabaseHandler from './databaseHandler.js' import AppsStore from './apps.js' +import tagsStore from './../stores/tags.js' /** * apps @@ -41,20 +42,22 @@ class TagsDatabase extends DatabaseHandler { // check for existings tags and remove // adding tags that are not removed this.db.find(query).then((documents) => { - console.log(documents) return this.filterTags(documents, tags) - }).then(async (tags) => { - console.log(tags) + }).then((tags) => { if (tags.length > 0) { // adding tags to entries for (let i = 0; i < tags.length; i++) { - await this.db.post({ + this.db.post({ 'name': tags[i] + }).then(() => { + + // last tag, then remove + if (i === (tags.length - 1)) { + this.removeNotNeeded() + } }) } - - this.removeNotNeeded() } }) } @@ -78,25 +81,27 @@ class TagsDatabase extends DatabaseHandler { } this.db.find(query).then((tags) => { - console.log(tags.docs) if (tags.docs.length > 0) { const appsStore = new AppsStore() for (let i = 0; i < tags.docs.length; i++) { - // + // create parameters const parameters = { tags: [], sort: 'name' } + // add tag name to parameters parameters.tags.push(tags.docs[i].name) - console.log(parameters) - appsStore.find(parameters).then(async (documents) => { - if (!documents.docs) { - await this.db.remove(tags.docs[i]) + // find app by tag, if no app is found, remove tag and update filter + appsStore.find(parameters).then((apps) => { + if (apps.length === 0) { + this.db.remove(tags.docs[i]).then(() => { + tagsStore.get() + }) } }) @@ -153,10 +158,6 @@ class TagsDatabase extends DatabaseHandler { } return this.db.find(query).then((documents) => { - if (documents.warning) { - console.log(documents.warning) - } - return documents.docs }) } diff --git a/src/js/stores/apps.js b/src/js/stores/apps.js index d9e8a29..69d5b3f 100644 --- a/src/js/stores/apps.js +++ b/src/js/stores/apps.js @@ -85,7 +85,6 @@ export default observable({ appsDatabase.remove(data).then(() => { this.trigger('close') - tagsStore.get() }) },