Skip to content

Commit

Permalink
feat: add ability to sync only cards assigned to user
Browse files Browse the repository at this point in the history
  • Loading branch information
alexktzk committed Jan 20, 2019
1 parent 0e4b102 commit ff83100
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 37 deletions.
2 changes: 1 addition & 1 deletion public/js/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ TrelloPowerUp.initialize({
}]
),
'card-badges': (t, options) => {
h.sync(t)
h.sync(t, options)
return getBadges(t)
},
'card-detail-badges': (t, options) => (
Expand Down
87 changes: 57 additions & 30 deletions public/js/habitica.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ const LIST_TYPES = {
DOING: 'doing'
}

const scope = 'ASSIGNED_TO_ME'

const CARD_SCOPES = {
ASSIGNED_TO_ME: 'me',
NONE: 'none'
}

let h = habitica = ({
loading: {},
api: 'https://habitica.com/api/v3',
Expand Down Expand Up @@ -116,38 +123,58 @@ let h = habitica = ({
))
))
),
sync: t => (
t.get('board', 'private', 'habiticaLists', {}).then(habiticaLists => (
t.card('id', 'idList').then(card => (
t.get('card', 'private', 'task', {}).then(task => {
let listType = habiticaLists[card.idList]

if (listType == LIST_TYPES.DOING) {
if (task.id) {
if (task.done) {
return h.undoTask(t)
}
} else {
return h.addTask(t)
}
} else if (listType == LIST_TYPES.DONE) {
if (task.id) {
if (!task.done) {
return h.doTask(t)
}
} else {
return h.addTask(t).then(() => h.doTask(t))
}
} else {
if (task.id) {
if (task.done) {
return h.undoTask(t).then(() => h.removeTask(t))
} else {
return h.removeTask(t)
handleScopedSync: (t, task) => {
if (task.id) {
if (task.done) {
return h.undoTask(t).then(() => h.removeTask(t))
} else {
return h.removeTask(t)
}
}
},
handleSync: (t, task, listType) => {
if (listType == LIST_TYPES.DOING) {
if (task.id) {
if (task.done) {
return h.undoTask(t)
}
} else {
return h.addTask(t)
}
} else if (listType == LIST_TYPES.DONE) {
if (task.id) {
if (!task.done) {
return h.doTask(t)
}
} else {
return h.addTask(t).then(() => h.doTask(t))
}
} else {
if (task.id) {
if (task.done) {
return h.undoTask(t).then(() => h.removeTask(t))
} else {
return h.removeTask(t)
}
}
}
},
sync: (t, options) => (
t.get('member', 'private', 'settings', {}).then(settings => (
t.get('board', 'private', 'habiticaLists', {}).then(habiticaLists => (
t.card('id', 'idList', 'members').then(card => (
t.get('card', 'private', 'task', {}).then(task => {
if (settings.scope == CARD_SCOPES.ASSIGNED_TO_ME) {
let me = options.context.member
if (!card.members.some(member => member.id == me)) {
return h.handleScopedSync(t, task)
}
}
}
})

let listType = habiticaLists[card.idList]
return h.handleSync(t, task, listType)
})
))
))
))
),
Expand Down
21 changes: 15 additions & 6 deletions public/settings.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,35 @@ <h3>
</h3>
<p>
</p>
What cards to sync: <br>
<input id="scope-none" class="scope-radio" type="radio" name="scope" value="none"> All<br>
<input id="scope-me" class="scope-radio" type="radio" name="scope" value="me"> Only assigned to me <br> <br>

<input id="user-id" type="text" placeholder="User ID">
<input id="api-token" type="password" placeholder="API Token">
<button id="submit-btn" type="submit" class="mod-primary">Save</button>

<script>
let t = TrelloPowerUp.iframe()
let $userId = document.getElementById('user-id')
let $userId = document.getElementById('user-id')
let $apiToken = document.getElementById('api-token')

// initialize fields with values from storage
t.get('member', 'private', 'settings', {scope: 'none'})
.get('scope')
.then(val => document.getElementById(`scope-${val}`).checked = true)

t.get('member', 'private', 'userId').then(val => $userId.value = val)
t.get('member', 'private', 'apiToken').then(val => $apiToken.value = val)

document.getElementById('submit-btn').addEventListener('click', () => {
let credentials = {
let $radios = document.getElementsByClassName('scope-radio')
let scope = Array.from($radios).find(radio => radio.checked).value

t.set('member', 'private', {
settings: { scope },
userId: $userId.value,
apiToken: $apiToken.value
}

t.set('member', 'private', credentials)
})
.then(() => {
// now that we have the token we needed lets go on to letting
// the user do whatever they need to do.
Expand Down

0 comments on commit ff83100

Please sign in to comment.