Commit 82e213a7 authored by Floréal Cabanettes's avatar Floréal Cabanettes

Make login working

parent b2616bb2
......@@ -17,6 +17,7 @@ h1 {
font-size: 30pt;
line-height: 60px;
margin-top: 0;
margin-bottom: 0;
text-align: center;
}
......@@ -69,3 +70,22 @@ form button {
display: block;
text-align: center;
}
#notify {
font-size: 14pt;
font-weight: bold;
color: #000;
padding: 5px;
}
#notify.error {
background: #ff413c;
}
#notify.success {
background: #5cb85c;
}
#notify.warn {
background: #bb842e;
}
\ No newline at end of file
......@@ -20,6 +20,10 @@
<h1>Track my bank!</h1>
<div id="notify" style="display: none;">
</div>
<div id="login">
<form action="#" id="login-form">
<label for="password">Pin</label>
......@@ -37,8 +41,6 @@
<label for="category">Catégorie</label>
<select id="category" class="select2">
<option></option>
<option value="1">Catégorie 1</option>
<option value="2">Catégorie 2</option>
</select>
<button id="send">Envoyer</button>
<button id="add-subtr">+ item</button>
......
credentials = {
url: "",
app_key: "",
username: ""
};
\ No newline at end of file
trackmybank = {};
trackmybank.timeout = null;
trackmybank.init = function() {
$("#dologin").on("click touch", trackmybank.login);
$("form#login-form").on("submit", function(e) {
e.preventDefault();
trackmybank.login();
});
$("form#add-form").on("submit", function(e) {
e.preventDefault();
});
};
trackmybank.login = function() {
$("#login").hide();
$("#logged").show();
trackmybank.post(credentials.url+ "/api/auth/",
{
username: credentials.username,
password: $("#password").val(),
authorized_key: credentials.app_key
},
function(data, success) {
if ("success" in data && data["success"]) {
if (trackmybank.timeout !== null) {
clearTimeout(trackmybank.timeout);
trackmybank.hide_notify();
}
$("#login").hide();
$("#logged").show();
if ("categories" in data) {
$.each(data["categories"], function(c, cat) {
console.log(cat);
$("#category").append(new Option(cat["name"], cat["id"]));
});
}
credentials.token = data["token"];
}
});
};
trackmybank.hide_notify = function() {
$("#notify").html("").attr("class", "").hide();
};
trackmybank.notify = async function(level, message) {
$("#notify").html(message).attr("class", level).show();
trackmybank.timeout = setTimeout(function(){
$("#notify").html("").attr("class", "").hide();
}, 5000);
};
trackmybank.ajax = function (url, data, success, error, method = "POST", async = true) {
$.ajax(url,
{
method: method,
data: data,
success: success,
error: error || function (data) {
try {
data = JSON.parse(data.responseText);
if ("message" in data) {
trackmybank.notify("error", data.message);
return true;
}
} catch (e) {
// do nothing
}
trackmybank.notify("error","Une erreur est survenue. Veuillez contacter le support.");
},
async: async,
}
);
};
trackmybank.post = function (url, data, success, error, async = true) {
trackmybank.ajax(
url,
data,
success,
error,
"POST",
async
);
};
trackmybank.get = function (url, data, success, error, async = true) {
trackmybank.ajax(
url,
data,
success,
error,
"GET",
async
);
};
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment