$(function () {
var asanaBaseUrl = "https://app.asana.com/api/1.0";
var clientId = @Model.ClientId;
var authWin = null;
var token = null;
var me = null;
$(document).on("click", "#btn-auth", function () {
authWin = window.open(
"https://app.asana.com/-/oauth_authorize?response_type=token&client_id=" + clientId + "&redirect_uri=https%3A%2F%2Flocalhost%3A44300%2Fapps%2FB309F192-9F22-4E39-AAFC-DD59589227C7%2Fauth&state=somerandomstate",
"authWindow",
"width=500px,height=700px"
);
});
var getData = function () {
// get the user info and then all the tasks
$.ajax({
url: asanaBaseUrl + "/users/me/",
contentType: "application/json",
method: "GET",
beforeSend: function (xhr) {
xhr.setRequestHeader('Authorization', 'Bearer ' + token);
}
}).then(function (me) {
me = me.data;
return $.ajax({
url: asanaBaseUrl + "/tasks?workspace=" + me.workspaces[0].id + "&assignee=" + me.id + "&opt_fields=id,name,assignee_status,completed",
contentType: "application/json",
method: "GET",
beforeSend: function (xhr) {
xhr.setRequestHeader('Authorization', 'Bearer ' + token);
}
});
}).then(function (response) {
tasks = _.map(response.data, function (t) {
return { id: t.id, name: t.name, is_section: t.name.endsWith(":"), completed: t.completed };
});
}).fail(function () {
console.error("Handle error...")
});
}
window.addEventListener("message", function (e) {
switch (e.data.name) {
case "auth":
token = e.data.token;
getData();
if (authWin) {
authWin.close();
}
break;
}
});
});