bugfix for remove tags that not longer used

main
HerrHase 2 years ago
parent e501713c99
commit 8fe9046022

@ -40,7 +40,7 @@
/** /**
* filtering apps by tags, showing all available tags as list of buttons * 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 * @author Björn Hase
* @license hhttps://www.gnu.org/licenses/gpl-3.0.en.html GPL-3 * @license hhttps://www.gnu.org/licenses/gpl-3.0.en.html GPL-3

@ -19,13 +19,14 @@ class AppsDatabase extends DatabaseHandler {
'name' 'name'
], 'name-sort') ], 'name-sort')
// add index for apps to sort by name // add index for apps to sort by date_started
this.createIndex([ this.createIndex([
'date_started' 'date_started'
], 'date-started-sort') ], 'date-started-sort')
} }
/** /**
* create app
* *
* @param {object} data * @param {object} data
* @return {object} * @return {object}
@ -33,11 +34,13 @@ class AppsDatabase extends DatabaseHandler {
*/ */
create(data) { create(data) {
// default for date_started
data.date_started = 0 data.date_started = 0
return this.db.post(data) return this.db.post(data)
.then((response) => { .then((response) => {
// if tags are in data update tags also in database
if (data.tags) { if (data.tags) {
const tagsDatabase = new TagsDatabase() const tagsDatabase = new TagsDatabase()
tagsDatabase.update(data.tags) tagsDatabase.update(data.tags)
@ -45,11 +48,14 @@ class AppsDatabase extends DatabaseHandler {
return this.findOneById(response._id) return this.findOneById(response._id)
}).catch((error) => { }).catch((error) => {
// @TODO handle errors from pouchdb
console.error(error) console.error(error)
}) })
} }
/** /**
* update app
* *
* @param {object} data * @param {object} data
* @return {object} * @return {object}
@ -60,26 +66,34 @@ class AppsDatabase extends DatabaseHandler {
.then((response) => { .then((response) => {
return this.findOneById(response._id) return this.findOneById(response._id)
}).catch((error) => { }).catch((error) => {
// @TODO handle errors from pouchdb
console.error(error) console.error(error)
}) })
} }
/** /**
* remove app
* *
* @param {object} data * @param {object} document
* @return {object} * @return {object}
* *
*/ */
remove(data) { remove(document) {
return this.db.remove(data) return this.db.remove(document)
.then(async (response) => { .then(async (response) => {
const tagsDatabase = new TagsDatabase() // successfull remove document, check for tags that
// not longer used
await tagsDatabase.removeNotNeeded() if (response.ok === true) {
const tagsDatabase = new TagsDatabase()
await tagsDatabase.removeNotNeeded()
}
return true return response
}).catch((error) => { }).catch((error) => {
// @TODO handle errors from pouchdb
console.error(error) console.error(error)
}) })
} }
@ -115,6 +129,8 @@ class AppsDatabase extends DatabaseHandler {
return documents.docs[0] return documents.docs[0]
} }
}).catch((error) => { }).catch((error) => {
// @TODO handle errors from pouchdb
console.error(error) console.error(error)
}) })
} }
@ -148,13 +164,16 @@ class AppsDatabase extends DatabaseHandler {
query.sort = [] query.sort = []
query.sort.push(parameters.sort) query.sort.push(parameters.sort)
// if sort is date_stared change 'date-started-sort'
if (parameters.sort === 'date_started') { if (parameters.sort === 'date_started') {
query.use_index = '_design/date-started-sort' query.use_index = '_design/date-started-sort'
query.selector = { query.selector = {
'date_started': { 'date_started': {
'$regex': '' '$exists': true
} }
} }
// if sort is name change 'name-sort'
} else { } else {
query.use_index = '_design/name-sort' query.use_index = '_design/name-sort'
query.selector = { query.selector = {
@ -173,16 +192,10 @@ class AppsDatabase extends DatabaseHandler {
} }
return this.db.find(query).then((documents) => { 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 return documents.docs
}).catch((error) => { }).catch((error) => {
// @TODO handle errors from pouchdb
console.error(error) console.error(error)
}) })
} }

@ -1,5 +1,6 @@
import DatabaseHandler from './databaseHandler.js' import DatabaseHandler from './databaseHandler.js'
import AppsStore from './apps.js' import AppsStore from './apps.js'
import tagsStore from './../stores/tags.js'
/** /**
* apps * apps
@ -41,20 +42,22 @@ class TagsDatabase extends DatabaseHandler {
// check for existings tags and remove // check for existings tags and remove
// adding tags that are not removed // adding tags that are not removed
this.db.find(query).then((documents) => { this.db.find(query).then((documents) => {
console.log(documents)
return this.filterTags(documents, tags) return this.filterTags(documents, tags)
}).then(async (tags) => { }).then((tags) => {
console.log(tags)
if (tags.length > 0) { if (tags.length > 0) {
// adding tags to entries // adding tags to entries
for (let i = 0; i < tags.length; i++) { for (let i = 0; i < tags.length; i++) {
await this.db.post({ this.db.post({
'name': tags[i] '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) => { this.db.find(query).then((tags) => {
console.log(tags.docs)
if (tags.docs.length > 0) { if (tags.docs.length > 0) {
const appsStore = new AppsStore() const appsStore = new AppsStore()
for (let i = 0; i < tags.docs.length; i++) { for (let i = 0; i < tags.docs.length; i++) {
// // create parameters
const parameters = { const parameters = {
tags: [], tags: [],
sort: 'name' sort: 'name'
} }
// add tag name to parameters
parameters.tags.push(tags.docs[i].name) parameters.tags.push(tags.docs[i].name)
console.log(parameters)
appsStore.find(parameters).then(async (documents) => { // find app by tag, if no app is found, remove tag and update filter
if (!documents.docs) { appsStore.find(parameters).then((apps) => {
await this.db.remove(tags.docs[i]) 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) => { return this.db.find(query).then((documents) => {
if (documents.warning) {
console.log(documents.warning)
}
return documents.docs return documents.docs
}) })
} }

@ -85,7 +85,6 @@ export default observable({
appsDatabase.remove(data).then(() => { appsDatabase.remove(data).then(() => {
this.trigger('close') this.trigger('close')
tagsStore.get()
}) })
}, },

Loading…
Cancel
Save