Skip to content

Commit

Permalink
Add synch of projects
Browse files Browse the repository at this point in the history
  • Loading branch information
alexfruehwirth committed Jul 12, 2016
1 parent 689f887 commit 75f752b
Showing 1 changed file with 30 additions and 5 deletions.
35 changes: 30 additions & 5 deletions lib/todoist-task-store.es6
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@ class TodoistTaskStore extends NylasStore {
this._error = null;
this._tasks = null;

var self = this;
setInterval(function(){
self._todoistFetchTasks();
}, 30000);



this.listenTo(FocusedContentStore, this._onFocusedContentChanged);
}

Expand Down Expand Up @@ -90,16 +97,33 @@ class TodoistTaskStore extends NylasStore {
if(tasks[taskKey].id === storageTasks[clientKey].id){
storageTasks[clientKey].content = tasks[taskKey].content;
storageTasks[clientKey].done = tasks[taskKey].checked === 1 ? true : false;
if(tasks[taskKey].is_deleted === 1){
delete storageTasks[clientKey];
}

}
}
}

this.trigger(this);


localStorage.setItem("N1todoist_tasks", JSON.stringify(storageTasks));
this._setTask();


}

_setSynchToken(token){
localStorage.setItem("N1todoist_synchToken", token);
}

_getSynchToken(){
var token = localStorage.getItem("N1todoist_synchToken");

return token !== null ? token : '*';
}

_randomize(){
return Math.floor((1 + Math.random()) * 0x10000).toString(16).substring(1);
}
Expand Down Expand Up @@ -205,9 +229,9 @@ class TodoistTaskStore extends NylasStore {
this.trigger(this);
const thread = this._getThread();
let tasks = this.getTaskStorage();
if(!this._tasks){
// if(!this._tasks){
this._todoistFetchTasks();
}
// }


this._task = null;
Expand All @@ -227,9 +251,9 @@ class TodoistTaskStore extends NylasStore {

_todoistFetchTasks() {
let accessToken = localStorage.getItem("N1todoist_authentication")
let seqNo = 0
let syncToken = this._getSynchToken();
let resourceTypes = ['items']
let payload = { token: accessToken, seq_no: seqNo, resource_types: JSON.stringify(resourceTypes), args: {id: this._task} }
let payload = { token: accessToken, sync_token: syncToken, resource_types: JSON.stringify(resourceTypes), args: {} }

if (accessToken) {
request
Expand All @@ -243,7 +267,8 @@ class TodoistTaskStore extends NylasStore {

_handleTodoistFetchTasksResponse(error, response) {
if(response && response.ok){
this._setTasks(response.body.Items);
this._setSynchToken(response.body.sync_token)
this._setTasks(response.body.items);
}else{
console.log(error);
}
Expand Down

0 comments on commit 75f752b

Please sign in to comment.