From e2af010638daabf2b4af4c20b98a6794e07d9bb0 Mon Sep 17 00:00:00 2001 From: Paul Liverman III Date: Tue, 24 Apr 2018 18:33:18 -0700 Subject: [PATCH] reorganized --- app.moon | 13 ++++++------ applications/api.moon | 16 ++++++++------- static/index.js | 47 +++++++++++++++++++++++++++++++------------ static/main.css | 1 + 4 files changed, 50 insertions(+), 27 deletions(-) diff --git a/app.moon b/app.moon index 7669da3..ba52fd0 100644 --- a/app.moon +++ b/app.moon @@ -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." diff --git a/applications/api.moon b/applications/api.moon index 6075bae..9834f22 100644 --- a/applications/api.moon +++ b/applications/api.moon @@ -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 diff --git a/static/index.js b/static/index.js index 9a5f2ac..ef005f1 100644 --- a/static/index.js +++ b/static/index.js @@ -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(); } diff --git a/static/main.css b/static/main.css index 4edb25f..fd379ae 100644 --- a/static/main.css +++ b/static/main.css @@ -14,4 +14,5 @@ .delete, .error { background-color: red; border-color: red; + color: white; }