reorganized

This commit is contained in:
Paul Liverman III 2018-04-24 18:33:18 -07:00
parent 7941b286f5
commit e2af010638
4 changed files with 50 additions and 27 deletions

View File

@ -16,7 +16,6 @@ class Simplex extends Application
@include locate "api"
[index: "/"]: =>
-- NOTE TEMPORARY
if @user
@keys = APIKeys\select "WHERE user_id = ? ORDER BY id ASC", @user.id
@tasks = Tasks\select "WHERE user_id = ? ORDER BY id ASC", @user.id
@ -27,14 +26,14 @@ class Simplex extends Application
return render: "index.logged_in"
else
return redirect_to: @url_for "user_login", nil, redirect: @url_for "index"
[new_api_key: "/new-api-key"]: =>
if @user
if api_key = APIKeys\create(@user)
return json: { success: true, :api_key }
@html ->
a href: @url_for("user_login"), "log in"
text " | "
a href: @url_for("user_new"), "new user"
[console: "/console"]: =>
if @user and @user.admin
console = require "lapis.console"
return console.make(env: "all")(@)
else
return status: 404, "Not found."

View File

@ -24,7 +24,7 @@ class API extends Application
-- handle_error: (err, trace) =>
-- return status: 500, json: { errors: {err}, :trace } -- NOTE trace should be saved and NOT returned to the user
[err_test: "/err"]: =>
[err_test: "/err"]: api_request =>
error "this is a testing error"
[new: "/new"]: api_request =>
@ -40,6 +40,10 @@ class API extends Application
return json: { success: true, :task }
[get: "/get"]: api_request =>
-- TODO
abort 501, "Not implemented."
[do: "/do"]: api_request =>
local task
if @params.id
@ -84,10 +88,6 @@ class API extends Application
return json: { success: true, :task }
[get: "/get"]: api_request =>
-- TODO
abort 501, "Not implemented."
[random: "/random"]: api_request =>
assert_valid @params, {
{"count", exists: true, is_integer: true, optional: true, "Count is not an integer."}
@ -142,8 +142,10 @@ class API extends Application
return json: { success: true, page: @params.page, :tasks }
[new_key: "/key/new"]: api_request =>
-- TODO
abort 501, "Not implemented."
api_key, err = APIKeys\create(@user)
abort 500, err unless api_key
return json: { success: true, :api_key }
[delete_key: "/key/delete"]: api_request =>
-- TODO

View File

@ -1,15 +1,35 @@
function add_error(e) {
while (!e.is(".row")) {
e = e.parent();
}
e.addClass("error");
}
function remove_error(e) {
while (!e.is(".row")) {
e = e.parent();
}
e.removeClass("error");
}
function check(id) {
let checkbox = $("#task-" + id);
remove_error(checkbox);
let uri = "/v1/undo";
if (checkbox.prop("checked")) {
uri = "/v1/do";
// TODO hide it!
}
$.post(uri, {api_key: API_KEY, id: id}, function(data, status) {
if (status == "success") {
checkbox.prop("checked", data.task.done);
} else {
console.log(data); // NOTE TEMPORARY (need to handle errors better)
}
$.post(uri, {api_key: API_KEY, id: id}, function(data) {
checkbox.prop("checked", data.task.done);
})
.fail(function(request) {
if (checkbox.prop("checked")) {
checkbox.prop("checked", false);
} else {
checkbox.prop("checked", true);
}
add_error(checkbox); // TODO unhide it
});
}
@ -42,16 +62,16 @@ function new_task() {
}
function new_api_key() {
let template = get_template("#api-key-template");
let template;
$.get("/v1/key/new", function(data) {
template = get_template("#api-key-template");
$("code", template).text(data.api_key.key);
$("#new-api-key").before(template);
})
.fail(function(request) {
$("code", template)
.css("background-color", "red")
.css("color", "white")
.text("ERROR: " + request.responseJSON.errors.join(" "));
template = get_template("#error-template");
$(".error-text", template).text("ERROR: " + request.responseJSON.errors.join(" "));
})
.always(function() {
$("#new-api-key").before(template);
});
}
@ -61,6 +81,7 @@ function delete_item(e) {
while (!e.is("li")) {
e = e.parent();
}
// TODO here, grab content to delete by (for api_key), send delete request, hide element (delete on success, reappear with red on failure)
// TODO find whether this is a task or api_key, send delete request, hide item
// success? delete item, failure? add_error to item and unhide it (how? display: block;)
e.remove();
}

View File

@ -14,4 +14,5 @@
.delete, .error {
background-color: red;
border-color: red;
color: white;
}