Prettified Code!

This commit is contained in:
daniep01
2023-06-29 12:12:35 +00:00
committed by GitHub Action
parent 31da474061
commit 853a97db81
5 changed files with 135 additions and 134 deletions

200
index.js
View File

@@ -15,7 +15,6 @@ class toggltrack extends InstanceBase {
this.updateActions = updateActions.bind(this)
this.updatePresets = updatePresets.bind(this)
this.updateVariables = updateVariables.bind(this)
}
getConfigFields() {
@@ -50,7 +49,7 @@ class toggltrack extends InstanceBase {
this.gotOptions = {
responseType: 'json',
throwHttpErrors: false
throwHttpErrors: false,
}
this.gotOptions.prefixUrl = this.prefixUrl
@@ -73,13 +72,10 @@ class toggltrack extends InstanceBase {
this.gotOptions.headers = this.auth()
if (this.gotOptions.headers != null) {
this.getWorkspace().then(
this.getCurrentTimer()
)
this.getWorkspace().then(this.getCurrentTimer())
}
this.updateActions()
}
async configUpdated(config) {
@@ -89,9 +85,7 @@ class toggltrack extends InstanceBase {
this.gotOptions.headers = this.auth()
if (this.gotOptions.headers != null) {
this.getWorkspace().then(
this.getCurrentTimer()
)
this.getWorkspace().then(this.getCurrentTimer())
}
this.updateActions()
@@ -122,37 +116,35 @@ class toggltrack extends InstanceBase {
let cmd = 'me/time_entries/current'
return new Promise((resolve, reject) => {
this.sendGetCommand(cmd).then(
(result) => {
if (typeof result === 'object' && result !== null) {
if ('id' in result) {
this.setVariableValues({
timerId: result.id,
timerDescription: result.description,
timerDuration: result.duration,
})
this.log('info', 'Current timer id: ' + result.id)
resolve(result.id)
} else {
this.log('info', 'No current timer (no id in data)')
this.setVariableValues({
timerId: null,
timerDescription: null,
timerDuration: null
})
resolve(null)
}
this.sendGetCommand(cmd).then((result) => {
if (typeof result === 'object' && result !== null) {
if ('id' in result) {
this.setVariableValues({
timerId: result.id,
timerDescription: result.description,
timerDuration: result.duration,
})
this.log('info', 'Current timer id: ' + result.id)
resolve(result.id)
} else {
this.log('info', 'No current timer (no object)')
this.setVariableValues({
this.log('info', 'No current timer (no id in data)')
this.setVariableValues({
timerId: null,
timerDescription: null,
timerDuration: null
timerDuration: null,
})
resolve(null)
}
} else {
this.log('info', 'No current timer (no object)')
this.setVariableValues({
timerId: null,
timerDescription: null,
timerDuration: null,
})
resolve(null)
}
)
})
})
}
@@ -168,31 +160,29 @@ class toggltrack extends InstanceBase {
// reset
this.workspace = null
this.setVariableValues({
workspace: null
workspace: null,
})
// get workspace ID
this.sendGetCommand(cmd).then(
(result) => {
// console.log('result ' + JSON.stringify(result, null, 4))
if (typeof result === 'object' && result !== null) {
console.log('Found ' + result.length + ' workspace')
// only interested in first workspace
if ('id' in result[0]) {
this.workspace = result[0].id
this.workspaceName = result[0].name
this.log('info', 'Workspace: ' + this.workspace + ' - ' + this.workspaceName)
this.setVariableValues({
workspace: this.workspaceName
})
this.getProjects()
}
} else {
console.log('result ' + JSON.stringify(result, null, 4))
this.log('debug', 'No workspace')
this.sendGetCommand(cmd).then((result) => {
// console.log('result ' + JSON.stringify(result, null, 4))
if (typeof result === 'object' && result !== null) {
console.log('Found ' + result.length + ' workspace')
// only interested in first workspace
if ('id' in result[0]) {
this.workspace = result[0].id
this.workspaceName = result[0].name
this.log('info', 'Workspace: ' + this.workspace + ' - ' + this.workspaceName)
this.setVariableValues({
workspace: this.workspaceName,
})
this.getProjects()
}
} else {
console.log('result ' + JSON.stringify(result, null, 4))
this.log('debug', 'No workspace')
}
)
})
}
getProjects() {
@@ -200,48 +190,47 @@ class toggltrack extends InstanceBase {
if (this.workspace !== null) {
let cmd = 'workspaces/' + this.workspace + '/projects'
this.sendGetCommand(cmd).then(
(result) => {
// console.log('result ' + JSON.stringify(result, null, 4))
if (typeof result === 'object' && result !== null) {
// reset
this.projects = []
this.sendGetCommand(cmd).then((result) => {
// console.log('result ' + JSON.stringify(result, null, 4))
if (typeof result === 'object' && result !== null) {
// reset
this.projects = []
for (let p = 0; p < result.length; p++) {
if ('id' in result[p]) {
if (result[p].active === true) {
// don't add archived projects
this.projects.push({
id: result[p].id.toString(),
label: result[p].name,
})
}
// this.log('debug', 'Project ' + result[p].id + ':' + result[p].name)
for (let p = 0; p < result.length; p++) {
if ('id' in result[p]) {
if (result[p].active === true) {
// don't add archived projects
this.projects.push({
id: result[p].id.toString(),
label: result[p].name,
})
}
// this.log('debug', 'Project ' + result[p].id + ':' + result[p].name)
}
this.projects.sort((a, b) => {
let fa = a.label.toLowerCase()
let fb = b.label.toLowerCase()
if (fa < fb) {
return -1
}
if (fa > fb) {
return 1
}
return 0
})
this.projects.unshift({ id: '0', label: 'None' })
console.log('Projects:')
console.log(this.projects)
this.updateActions()
} else {
console.log(result)
this.log('debug', 'No projects')
}
})
this.projects.sort((a, b) => {
let fa = a.label.toLowerCase()
let fb = b.label.toLowerCase()
if (fa < fb) {
return -1
}
if (fa > fb) {
return 1
}
return 0
})
this.projects.unshift({ id: '0', label: 'None' })
console.log('Projects:')
console.log(this.projects)
this.updateActions()
} else {
console.log(result)
this.log('debug', 'No projects')
}
})
}
}
@@ -285,18 +274,31 @@ class toggltrack extends InstanceBase {
// no timer currently running or we want to restart it
cmd = 'workspaces/' + this.workspace + '/time_entries'
if (project == '0') {
body = '{"wid":' + this.workspace + ',"description":"' + description +
'","created_with":"companion",' + '"start":"' + startTime.toISOString() + '","duration":-1}'
body =
'{"wid":' +
this.workspace +
',"description":"' +
description +
'","created_with":"companion",' +
'"start":"' +
startTime.toISOString() +
'","duration":-1}'
} else {
body =
'{"wid":' + this.workspace + ',"description":"' + description +
'","created_with":"companion","project_id":' + project +
',"start":"' + startTime.toISOString() + '","duration":-1}'
'{"wid":' +
this.workspace +
',"description":"' +
description +
'","created_with":"companion","project_id":' +
project +
',"start":"' +
startTime.toISOString() +
'","duration":-1}'
}
// console.log(body)
this.sendPostCommand(cmd, body).then((result) => {
if (typeof result === 'object' && result !== null) {
this.log('info', 'New timer started ' + result.id + " " + result.description)
this.log('info', 'New timer started ' + result.id + ' ' + result.description)
this.setVariableValues({
timerId: result.id,
timerDescription: result.description,
@@ -414,13 +416,11 @@ class toggltrack extends InstanceBase {
}
async sendPostCommand(cmd, body) {
console.log(body)
let response
let postdata = {}
postdata.prefixUrl = this.prefixUrl
postdata.responseType = 'json',
postdata.throwHttpErrors = false
;(postdata.responseType = 'json'), (postdata.throwHttpErrors = false)
postdata.headers = this.auth()
postdata.json = JSON.parse(body)

View File

@@ -13,8 +13,10 @@ export function updatePresets() {
color: combineRgb(255, 255, 255),
bgcolor: combineRgb(0, 0, 0),
},
steps: [{
down: [{
steps: [
{
down: [
{
actionId: 'startNewTimer',
options: {
description: '',
@@ -38,12 +40,12 @@ export function updatePresets() {
color: combineRgb(255, 255, 255),
bgcolor: combineRgb(0, 0, 0),
},
steps: [{
steps: [
{
down: [
{
actionId: 'stopCurrentTimer',
options: {
},
options: {},
},
],
up: [],

View File

@@ -25,5 +25,4 @@ export function updateVariables() {
)
this.setVariableDefinitions(variables)
}