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

View File

@@ -22,7 +22,7 @@ export function updateActions() {
this.startTimer(options.project, options.description) this.startTimer(options.project, options.description)
}, },
} }
actions['getCurrentTimer'] = { actions['getCurrentTimer'] = {
name: 'Get Current Timer', name: 'Get Current Timer',
options: [], options: [],
@@ -30,7 +30,7 @@ export function updateActions() {
this.getCurrentTimer() this.getCurrentTimer()
}, },
} }
actions['stopCurrentTimer'] = { actions['stopCurrentTimer'] = {
name: 'Stop Current Timer', name: 'Stop Current Timer',
options: [], options: [],
@@ -38,7 +38,7 @@ export function updateActions() {
this.stopTimer() this.stopTimer()
}, },
} }
actions['refreshProjects'] = { actions['refreshProjects'] = {
name: 'Refresh Project List', name: 'Refresh Project List',
options: [], options: [],

240
index.js
View File

@@ -15,9 +15,8 @@ class toggltrack extends InstanceBase {
this.updateActions = updateActions.bind(this) this.updateActions = updateActions.bind(this)
this.updatePresets = updatePresets.bind(this) this.updatePresets = updatePresets.bind(this)
this.updateVariables = updateVariables.bind(this) this.updateVariables = updateVariables.bind(this)
} }
getConfigFields() { getConfigFields() {
// console.log('config fields') // console.log('config fields')
return [ return [
@@ -37,22 +36,22 @@ class toggltrack extends InstanceBase {
}, },
] ]
} }
async destroy() { async destroy() {
console.log('destroy', this.id) console.log('destroy', this.id)
} }
async init(config) { async init(config) {
console.log('--- init toggltrack ---') console.log('--- init toggltrack ---')
this.prefixUrl = 'https://api.track.toggl.com/api/v9/' this.prefixUrl = 'https://api.track.toggl.com/api/v9/'
this.config = config this.config = config
this.gotOptions = { this.gotOptions = {
responseType: 'json', responseType: 'json',
throwHttpErrors: false throwHttpErrors: false,
} }
this.gotOptions.prefixUrl = this.prefixUrl this.gotOptions.prefixUrl = this.prefixUrl
this.workspace = null this.workspace = null
@@ -61,7 +60,7 @@ class toggltrack extends InstanceBase {
this.updateVariables() this.updateVariables()
this.updatePresets() this.updatePresets()
this.setVariableValues({ this.setVariableValues({
timerId: null, timerId: null,
timerDuration: null, timerDuration: null,
@@ -71,15 +70,12 @@ class toggltrack extends InstanceBase {
}) })
this.gotOptions.headers = this.auth() this.gotOptions.headers = this.auth()
if (this.gotOptions.headers != null) { if (this.gotOptions.headers != null) {
this.getWorkspace().then( this.getWorkspace().then(this.getCurrentTimer())
this.getCurrentTimer()
)
} }
this.updateActions() this.updateActions()
} }
async configUpdated(config) { async configUpdated(config) {
@@ -87,13 +83,11 @@ class toggltrack extends InstanceBase {
this.config = config this.config = config
this.gotOptions.headers = this.auth() this.gotOptions.headers = this.auth()
if (this.gotOptions.headers != null) { if (this.gotOptions.headers != null) {
this.getWorkspace().then( this.getWorkspace().then(this.getCurrentTimer())
this.getCurrentTimer()
)
} }
this.updateActions() this.updateActions()
this.updateVariables() this.updateVariables()
} }
@@ -111,10 +105,10 @@ class toggltrack extends InstanceBase {
} }
// console.log(this.gotOptions) // console.log(this.gotOptions)
} }
async getCurrentTimer() { async getCurrentTimer() {
console.log('function: getCurrentTimer') console.log('function: getCurrentTimer')
if (this.gotOptions.headers == null) { if (this.gotOptions.headers == null) {
this.log('warn', 'Not authorized') this.log('warn', 'Not authorized')
return return
@@ -122,77 +116,73 @@ class toggltrack extends InstanceBase {
let cmd = 'me/time_entries/current' let cmd = 'me/time_entries/current'
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
this.sendGetCommand(cmd).then( this.sendGetCommand(cmd).then((result) => {
(result) => { if (typeof result === 'object' && result !== null) {
if (typeof result === 'object' && result !== null) { if ('id' in result) {
if ('id' in result) { this.setVariableValues({
this.setVariableValues({ timerId: result.id,
timerId: result.id, timerDescription: result.description,
timerDescription: result.description, timerDuration: result.duration,
timerDuration: result.duration, })
}) this.log('info', 'Current timer id: ' + result.id)
this.log('info', 'Current timer id: ' + result.id) resolve(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)
}
} else { } else {
this.log('info', 'No current timer (no object)') this.log('info', 'No current timer (no id in data)')
this.setVariableValues({ this.setVariableValues({
timerId: null, timerId: null,
timerDescription: null, timerDescription: null,
timerDuration: null timerDuration: null,
}) })
resolve(null) resolve(null)
} }
} else {
this.log('info', 'No current timer (no object)')
this.setVariableValues({
timerId: null,
timerDescription: null,
timerDuration: null,
})
resolve(null)
} }
) })
}) })
} }
async getWorkspace() { async getWorkspace() {
let cmd = 'workspaces' let cmd = 'workspaces'
console.log('function: getWorkspace') console.log('function: getWorkspace')
if (this.gotOptions.headers == null) { if (this.gotOptions.headers == null) {
this.log('warn', 'Not authorized') this.log('warn', 'Not authorized')
return return
} }
// reset // reset
this.workspace = null this.workspace = null
this.setVariableValues({ this.setVariableValues({
workspace: null workspace: null,
}) })
// get workspace ID // get workspace ID
this.sendGetCommand(cmd).then( this.sendGetCommand(cmd).then((result) => {
(result) => { // console.log('result ' + JSON.stringify(result, null, 4))
// console.log('result ' + JSON.stringify(result, null, 4)) if (typeof result === 'object' && result !== null) {
if (typeof result === 'object' && result !== null) { console.log('Found ' + result.length + ' workspace')
console.log('Found ' + result.length + ' workspace') // only interested in first workspace
// only interested in first workspace if ('id' in result[0]) {
if ('id' in result[0]) { this.workspace = result[0].id
this.workspace = result[0].id this.workspaceName = result[0].name
this.workspaceName = result[0].name this.log('info', 'Workspace: ' + this.workspace + ' - ' + this.workspaceName)
this.log('info', 'Workspace: ' + this.workspace + ' - ' + this.workspaceName) this.setVariableValues({
this.setVariableValues({ workspace: this.workspaceName,
workspace: this.workspaceName })
}) this.getProjects()
this.getProjects()
}
} else {
console.log('result ' + JSON.stringify(result, null, 4))
this.log('debug', 'No workspace')
} }
} else {
console.log('result ' + JSON.stringify(result, null, 4))
this.log('debug', 'No workspace')
} }
) })
} }
getProjects() { getProjects() {
@@ -200,54 +190,53 @@ class toggltrack extends InstanceBase {
if (this.workspace !== null) { if (this.workspace !== null) {
let cmd = 'workspaces/' + this.workspace + '/projects' let cmd = 'workspaces/' + this.workspace + '/projects'
this.sendGetCommand(cmd).then( this.sendGetCommand(cmd).then((result) => {
(result) => { // console.log('result ' + JSON.stringify(result, null, 4))
// console.log('result ' + JSON.stringify(result, null, 4)) if (typeof result === 'object' && result !== null) {
if (typeof result === 'object' && result !== null) { // reset
// reset this.projects = []
this.projects = []
for (let p = 0; p < result.length; p++) {
for (let p = 0; p < result.length; p++) { if ('id' in result[p]) {
if ('id' in result[p]) { if (result[p].active === true) {
if (result[p].active === true) { // don't add archived projects
// don't add archived projects this.projects.push({
this.projects.push({ id: result[p].id.toString(),
id: result[p].id.toString(), label: result[p].name,
label: result[p].name, })
})
}
// this.log('debug', 'Project ' + result[p].id + ':' + 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')
}
})
}
} }
// getTimerDuration(id) { // getTimerDuration(id) {
// let cmd = 'time_entries/' + id // let cmd = 'time_entries/' + id
// //
// return new Promise((resolve, reject) => { // return new Promise((resolve, reject) => {
// self.sendCommand('rest_get', cmd).then( // self.sendCommand('rest_get', cmd).then(
// (result) => { // (result) => {
@@ -285,18 +274,31 @@ class toggltrack extends InstanceBase {
// no timer currently running or we want to restart it // no timer currently running or we want to restart it
cmd = 'workspaces/' + this.workspace + '/time_entries' cmd = 'workspaces/' + this.workspace + '/time_entries'
if (project == '0') { if (project == '0') {
body = '{"wid":' + this.workspace + ',"description":"' + description + body =
'","created_with":"companion",' + '"start":"' + startTime.toISOString() + '","duration":-1}' '{"wid":' +
this.workspace +
',"description":"' +
description +
'","created_with":"companion",' +
'"start":"' +
startTime.toISOString() +
'","duration":-1}'
} else { } else {
body = body =
'{"wid":' + this.workspace + ',"description":"' + description + '{"wid":' +
'","created_with":"companion","project_id":' + project + this.workspace +
',"start":"' + startTime.toISOString() + '","duration":-1}' ',"description":"' +
description +
'","created_with":"companion","project_id":' +
project +
',"start":"' +
startTime.toISOString() +
'","duration":-1}'
} }
// console.log(body) // console.log(body)
this.sendPostCommand(cmd, body).then((result) => { this.sendPostCommand(cmd, body).then((result) => {
if (typeof result === 'object' && result !== null) { 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({ this.setVariableValues({
timerId: result.id, timerId: result.id,
timerDescription: result.description, timerDescription: result.description,
@@ -311,7 +313,7 @@ class toggltrack extends InstanceBase {
} }
}) })
} }
async stopTimer() { async stopTimer() {
console.log('function: stopTimer') console.log('function: stopTimer')
@@ -414,18 +416,16 @@ class toggltrack extends InstanceBase {
} }
async sendPostCommand(cmd, body) { async sendPostCommand(cmd, body) {
console.log(body) console.log(body)
let response let response
let postdata = {} let postdata = {}
postdata.prefixUrl = this.prefixUrl postdata.prefixUrl = this.prefixUrl
postdata.responseType = 'json', ;(postdata.responseType = 'json'), (postdata.throwHttpErrors = false)
postdata.throwHttpErrors = false
postdata.headers = this.auth() postdata.headers = this.auth()
postdata.json = JSON.parse(body) postdata.json = JSON.parse(body)
// console.log(postdata) // console.log(postdata)
try { try {
response = await got.post(cmd, postdata) response = await got.post(cmd, postdata)
// console.log(response.request.requestUrl) // console.log(response.request.requestUrl)

View File

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

View File

@@ -4,4 +4,4 @@ export function upgradeScripts() {
updatedActions: [], updatedActions: [],
updatedFeedbacks: [], updatedFeedbacks: [],
} }
} }

View File

@@ -1,6 +1,6 @@
export function updateVariables() { export function updateVariables() {
let variables = [] let variables = []
variables.push( variables.push(
{ {
name: 'Workspace', name: 'Workspace',
@@ -23,7 +23,6 @@ export function updateVariables() {
variableId: 'timerDescription', variableId: 'timerDescription',
} }
) )
this.setVariableDefinitions(variables) this.setVariableDefinitions(variables)
}
}