sort projects

This commit is contained in:
peter
2022-07-10 12:16:29 +00:00
parent da81d73aa3
commit c188f234fb

View File

@@ -143,7 +143,7 @@ instance.prototype.actions = function (system) {
type: 'dropdown', type: 'dropdown',
label: 'Project', label: 'Project',
id: 'project', id: 'project',
default: 0, default: '0',
choices: self.projects, choices: self.projects,
}, },
], ],
@@ -175,16 +175,14 @@ instance.prototype.action = function (action) {
opt.project + opt.project +
'"}}' '"}}'
} }
console.log(body) // console.log(body)
break break
} }
case 'stopCurrentTimer': { case 'stopCurrentTimer': {
var restCmd = 'rest_put' var restCmd = 'rest_put'
console.log(self.currentTimer) console.log('current timer: ' + self.currentTimer)
if (self.currentTimer != null && self.currentTimer != undefined) { if (self.currentTimer != null && self.currentTimer != undefined) {
var cmd = 'https://api.track.toggl.com/api/v8/time_entries/' + self.currentTimer + '/stop' var cmd = 'https://api.track.toggl.com/api/v8/time_entries/' + self.currentTimer + '/stop'
console.log(cmd)
} else { } else {
self.log('warn', 'No running timer to stop or running timer ID unknown') self.log('warn', 'No running timer to stop or running timer ID unknown')
return return
@@ -196,11 +194,6 @@ instance.prototype.action = function (action) {
return return
break break
} }
case 'getProjects': {
self.getProjects()
return
break
}
default: default:
return return
break break
@@ -221,14 +214,15 @@ instance.prototype.action = function (action) {
// console.log(result.statusCode) // console.log(result.statusCode)
if (!self.auth_error) { if (!self.auth_error) {
self.status(self.STATUS_OK) self.status(self.STATUS_OK)
if (typeof result.data === 'object') {
if (typeof result.data.data === 'object') { if (typeof result.data.data === 'object') {
self.interpretData(result.data.data) if ('id' in result.data.data) {
} self.currentTimer = result.data.data.id
console.log('timer id: ' + self.currentTimer)
self.log('debug', 'timer id ' + self.currentTimer)
} else { } else {
self.currentTimer = null self.currentTimer = null
console.log(result.data) console.log('no id but found this: ' + result.data)
self.log('debug', result.data) }
} }
} }
} }
@@ -242,6 +236,9 @@ instance.prototype.getWorkspace = function () {
var cmd = 'https://api.track.toggl.com/api/v8/workspaces' var cmd = 'https://api.track.toggl.com/api/v8/workspaces'
// console.log('getWorkspace') // console.log('getWorkspace')
// reset
self.workspace = null
// get workspace ID // get workspace ID
self.system.emit( self.system.emit(
'rest_get', 'rest_get',
@@ -254,6 +251,7 @@ instance.prototype.getWorkspace = function () {
// console.log('workspace request status:' + result.response.statusCode) // console.log('workspace request status:' + result.response.statusCode)
self.status(self.STATUS_OK) self.status(self.STATUS_OK)
if (typeof result.data === 'object' && result.data !== null) { if (typeof result.data === 'object' && result.data !== null) {
console.log('Found ' + result.data.length + ' workspace')
// only interested in first workspace // only interested in first workspace
if ('id' in result.data[0]) { if ('id' in result.data[0]) {
self.workspace = result.data[0].id self.workspace = result.data[0].id
@@ -278,11 +276,12 @@ instance.prototype.getWorkspace = function () {
instance.prototype.getProjects = function () { instance.prototype.getProjects = function () {
var self = this var self = this
// reset
self.projects = [{ id: '0', label: 'None' }]
if (self.workspace !== null) { if (self.workspace !== null) {
// console.log('getProjects')
// reset
self.projects = []
var cmd = 'https://api.track.toggl.com/api/v8/workspaces/' + self.workspace + '/projects' var cmd = 'https://api.track.toggl.com/api/v8/workspaces/' + self.workspace + '/projects'
self.system.emit( self.system.emit(
'rest_get', 'rest_get',
@@ -305,6 +304,21 @@ instance.prototype.getProjects = function () {
self.log('debug', 'Project ' + result.data[p].id + ':' + result.data[p].name) self.log('debug', 'Project ' + result.data[p].id + ':' + result.data[p].name)
} }
} }
self.projects.sort((a, b) => {
fa = a.label.toLowerCase()
fb = b.label.toLowerCase()
if (fa < fb) {
return -1;
}
if (fa > fb) {
return 1;
}
return 0;
})
self.projects.unshift({ id: '0', label: 'None' })
console.log('Projects:') console.log('Projects:')
console.log(self.projects) console.log(self.projects)
self.actions() self.actions()
@@ -326,7 +340,7 @@ instance.prototype.getProjects = function () {
instance.prototype.getCurrentTimer = function () { instance.prototype.getCurrentTimer = function () {
var self = this var self = this
var cmd = 'https://api.track.toggl.com/api/v8/time_entries/current' var cmd = 'https://api.track.toggl.com/api/v8/time_entries/current'
console.log(cmd)
self.system.emit( self.system.emit(
'rest_get', 'rest_get',
cmd, cmd,
@@ -340,11 +354,11 @@ instance.prototype.getCurrentTimer = function () {
if (typeof result.data.data === 'object' && result.data.data !== null) { if (typeof result.data.data === 'object' && result.data.data !== null) {
if ('id' in result.data.data) { if ('id' in result.data.data) {
self.currentTimer = result.data.data.id self.currentTimer = result.data.data.id
console.log(self.currentTimer) console.log('current timer: ' + self.currentTimer)
self.log('debug', 'Current timer id ' + self.currentTimer) self.log('debug', 'Current timer id ' + self.currentTimer)
} }
} else { } else {
// console.log(result.data) console.log('getCurrentTimer: No timer running')
self.log('debug', 'No timer running') self.log('debug', 'No timer running')
self.currentTimer = null self.currentTimer = null
} }
@@ -359,17 +373,5 @@ instance.prototype.getCurrentTimer = function () {
) )
} }
instance.prototype.interpretData = function (data) {
var self = this
// console.log(data)
if ('id' in data) {
console.log('timer id: ' + data.id)
self.currentTimer = data.id
self.log('debug', 'timer id ' + data.id)
}
}
instance_skel.extendedBy(instance) instance_skel.extendedBy(instance)
exports = module.exports = instance exports = module.exports = instance