Skip to content

Instantly share code, notes, and snippets.

@narthur
Last active September 12, 2024 16:03
Show Gist options
  • Save narthur/bb26f889de4ea176f972e7cfad60534e to your computer and use it in GitHub Desktop.
Save narthur/bb26f889de4ea176f972e7cfad60534e to your computer and use it in GitHub Desktop.
TaskRatchet integration plugin pseudocode
import extractTags from '~lib/extractTags'
import createDatapoint from '~services/beeminder/createDatapoint'
import { BEEMINDER_CLIENT_ID } from '~lib/constants'
import definePlugin from '~lib/definePlugin'
import sanitize from '~lib/sanitize'
/*
Events:
- taskCreate
- taskEdit
- taskComplete
- taskExpire
- settingsSubmit
- pluginEnable
- pluginDisable
*/
export default definePlugin({
id: 'beeminder',
name: 'Beeminder',
description: 'Send TaskRatchet data to your Beeminder goals',
views: {
settings: settingsForm
},
listeners: {
taskCreate: async (event, context) => {
const goals = extractTags(event.task, '&')
const { user, token, newTasksGoal } = context.getSettings()
if (newTasksGoal) {
goals.push(newTasksGoal)
}
for (const goal of goals) {
await createDatapoint({
user,
token,
goal,
value: 1,
comment: event.task
})
}
},
taskComplete: async (event, context) => {
const goals = extractTags(event.task, '*')
const { user, token } = context.getSettings()
for (const goal of goals) {
await createDatapoint({
user,
token,
goal,
value: 1,
comment: event.task
})
}
}
}
})
const settingsForm = (context) => {
const redirectUri = 'https://app.taskratchet.com/settings'
const authUri = `https://www.beeminder.com/apps/authorize?client_id=${BEEMINDER_CLIENT_ID}&redirect_uri=${redirectUri}&response_type=token`
const params = new URLSearchParams(window.location.search)
const user = params.get('username')
const token = params.get('access_token')
if (!context.getSettings().user && !token) {
return `<p><a href='${authUri}'>Enable Beeminder integration</a></p>`
}
if (token) {
context.setSettings({ user, token })
}
const u = sanitize(context.getSettings().user);
const g = sanitize(context.getSettings().newTaskGoal);
return `
<p>Beeminder user: ${u}</p>
<p
<label>
Post new tasks to goal:
<input name="newTaskGoal" value="${g}" />
</label>
</p>
`
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment