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" @include locate "api"
[index: "/"]: => [index: "/"]: =>
-- NOTE TEMPORARY
if @user if @user
@keys = APIKeys\select "WHERE user_id = ? ORDER BY id ASC", @user.id @keys = APIKeys\select "WHERE user_id = ? ORDER BY id ASC", @user.id
@tasks = Tasks\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" return render: "index.logged_in"
else else
return redirect_to: @url_for "user_login", nil, redirect: @url_for "index" @html ->
a href: @url_for("user_login"), "log in"
[new_api_key: "/new-api-key"]: => text " | "
if @user a href: @url_for("user_new"), "new user"
if api_key = APIKeys\create(@user)
return json: { success: true, :api_key }
[console: "/console"]: => [console: "/console"]: =>
if @user and @user.admin if @user and @user.admin
console = require "lapis.console" console = require "lapis.console"
return console.make(env: "all")(@) 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) => -- handle_error: (err, trace) =>
-- return status: 500, json: { errors: {err}, :trace } -- NOTE trace should be saved and NOT returned to the user -- 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" error "this is a testing error"
[new: "/new"]: api_request => [new: "/new"]: api_request =>
@ -40,6 +40,10 @@ class API extends Application
return json: { success: true, :task } return json: { success: true, :task }
[get: "/get"]: api_request =>
-- TODO
abort 501, "Not implemented."
[do: "/do"]: api_request => [do: "/do"]: api_request =>
local task local task
if @params.id if @params.id
@ -84,10 +88,6 @@ class API extends Application
return json: { success: true, :task } return json: { success: true, :task }
[get: "/get"]: api_request =>
-- TODO
abort 501, "Not implemented."
[random: "/random"]: api_request => [random: "/random"]: api_request =>
assert_valid @params, { assert_valid @params, {
{"count", exists: true, is_integer: true, optional: true, "Count is not an integer."} {"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 } return json: { success: true, page: @params.page, :tasks }
[new_key: "/key/new"]: api_request => [new_key: "/key/new"]: api_request =>
-- TODO api_key, err = APIKeys\create(@user)
abort 501, "Not implemented." abort 500, err unless api_key
return json: { success: true, :api_key }
[delete_key: "/key/delete"]: api_request => [delete_key: "/key/delete"]: api_request =>
-- TODO -- 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) { function check(id) {
let checkbox = $("#task-" + id); let checkbox = $("#task-" + id);
remove_error(checkbox);
let uri = "/v1/undo"; let uri = "/v1/undo";
if (checkbox.prop("checked")) { if (checkbox.prop("checked")) {
uri = "/v1/do"; uri = "/v1/do";
// TODO hide it!
} }
$.post(uri, {api_key: API_KEY, id: id}, function(data, status) { $.post(uri, {api_key: API_KEY, id: id}, function(data) {
if (status == "success") { checkbox.prop("checked", data.task.done);
checkbox.prop("checked", data.task.done); })
} else { .fail(function(request) {
console.log(data); // NOTE TEMPORARY (need to handle errors better) 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() { function new_api_key() {
let template = get_template("#api-key-template"); let template;
$.get("/v1/key/new", function(data) { $.get("/v1/key/new", function(data) {
template = get_template("#api-key-template");
$("code", template).text(data.api_key.key); $("code", template).text(data.api_key.key);
$("#new-api-key").before(template);
}) })
.fail(function(request) { .fail(function(request) {
$("code", template) template = get_template("#error-template");
.css("background-color", "red") $(".error-text", template).text("ERROR: " + request.responseJSON.errors.join(" "));
.css("color", "white") })
.text("ERROR: " + request.responseJSON.errors.join(" ")); .always(function() {
$("#new-api-key").before(template); $("#new-api-key").before(template);
}); });
} }
@ -61,6 +81,7 @@ function delete_item(e) {
while (!e.is("li")) { while (!e.is("li")) {
e = e.parent(); 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(); e.remove();
} }

View File

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