From a70c3ccfc725813b585d888daa2afc7f48d42ac1 Mon Sep 17 00:00:00 2001 From: Paul Liverman III Date: Mon, 23 Apr 2018 22:42:56 -0700 Subject: [PATCH] corrected install.sh, completed API key giving / new task adding / task checking --- app.moon | 4 ++-- applications/api.moon | 1 + install.sh | 15 ++++---------- static/index.js | 40 +++++++++++++++++++++++++++++--------- views/index/logged_in.moon | 9 +++++---- 5 files changed, 43 insertions(+), 26 deletions(-) diff --git a/app.moon b/app.moon index 11a0b56..215f81e 100644 --- a/app.moon +++ b/app.moon @@ -28,8 +28,8 @@ class Simplex extends Application [new_api_key: "/new-api-key"]: => if @user - if key = APIKeys\create(@user) - return json: { success: true, :key } + if api_key = APIKeys\create(@user) + return json: { success: true, :api_key } [console: "/console"]: => if @user and @user.admin diff --git a/applications/api.moon b/applications/api.moon index 117a858..a22738a 100644 --- a/applications/api.moon +++ b/applications/api.moon @@ -104,6 +104,7 @@ class API extends Application -- return json: { success: true, task: tasks[1] } -- else -- return standard_err! + return status: 501 json: { errors: {"Not implemented."} } [list: "/list"]: capture_errors_json json_params => assert_valid @params, { diff --git a/install.sh b/install.sh index 8424c75..c98a02b 100755 --- a/install.sh +++ b/install.sh @@ -11,15 +11,8 @@ read -p "Enter email address for use with certbot-auto: " EMAIL_ADDRESS read -p "Enter the domain name this will be running on: " DOMAIN_NAME read -p "Enter the port this will be running on: " PORT -if [ -z EMAIL_ADDRESS ] -then - EMAIL_ADDRESS=no-one@example.com -fi - -if [ -z PORT ] -then - PORT=9872 # TODO fix this does not work -fi +EMAIL_ADDRESS=${EMAIL_ADDRESS:-noone@example.com} +PORT=${PORT:-9872} ### PREREQUISITES ### @@ -92,7 +85,7 @@ echo "{ sql_password: '$POSTGRES_PASSWORD' session_secret: '$(cat /dev/urandom | head -c 12 | base64)' _domain: '$DOMAIN_NAME' - _port: '$PORT' + _port: $PORT }" > ./secret.moon # Compile, Run migrations @@ -138,7 +131,7 @@ sudo echo "server { ssl_dhparam $INSTALL_DIR/dhparams.pem; location / { - proxy_pass http://127.0.0.1:$PORT + proxy_pass http://127.0.0.1:$PORT; } }" > /etc/nginx/sites-enabled/simplex-proxy.conf sudo nginx -s reload # might fail because the service isn't running yet diff --git a/static/index.js b/static/index.js index 08fc2cd..26ed090 100644 --- a/static/index.js +++ b/static/index.js @@ -11,8 +11,12 @@ function check(id) { var xhr = request("/v1/do"); xhr.onreadystatechange = function() { if (xhr.readyState === 4) { - // TODO handle success / errors - console.log(xhr.responseText); + data = JSON.parse(xhr.responseText); + if (data.task) { + $("#task-" + data.task.id).prop("checked", data.task.done); + } else { + console.log(data); // NOTE TEMPORARY (handle errors better!) + } } } xhr.send(JSON.stringify({api_key: API_KEY, id: id})); @@ -20,8 +24,12 @@ function check(id) { var xhr = request("/v1/undo"); xhr.onreadystatechange = function() { if (xhr.readyState === 4) { - // TODO handle success / errors - console.log(xhr.responseText); + data = JSON.parse(xhr.responseText); + if (data.task) { + $("#task-" + data.task.id).prop("checked", data.task.done); + } else { + console.log(data); // NOTE TEMPORARY (handle errors better!) + } } } xhr.send(JSON.stringify({api_key: API_KEY, id: id})); @@ -29,13 +37,16 @@ function check(id) { } function new_task() { - var text = document.getElementById("new-task"); - console.log(text); // TEMPORARY + var text = document.getElementById("new-task-input"); var xhr = request("/v1/new"); xhr.onreadystatechange = function() { if (xhr.readyState === 4) { - // TODO handle success / errors - console.log(xhr.responseText); + data = JSON.parse(xhr.responseText); + if (data.task) { + $("#new-task").before("
  • " + data.task.content + "
  • "); + } else { + console.log(data); // NOTE TEMPORARY (handle errors better!) + } } } xhr.send(JSON.stringify({api_key: API_KEY, content: text.value})); @@ -44,5 +55,16 @@ function new_task() { } function new_api_key() { - // TODO + var xhr = request("/new-api-key"); + xhr.onreadystatechange = function() { + if (xhr.readyState === 4) { + data = JSON.parse(xhr.responseText); + if (data.api_key) { + $("#new-api-key").before("
  • " + data.api_key.key + "
  • "); + } else { + console.log(data); // NOTE TEMPORARY (handle errors better!) + } + } + } + xhr.send(); // NOTE may need to send empty string or empty JSON object } diff --git a/views/index/logged_in.moon b/views/index/logged_in.moon index 9a0450f..402e51a 100644 --- a/views/index/logged_in.moon +++ b/views/index/logged_in.moon @@ -3,6 +3,7 @@ import Widget from require "lapis.html" class LoggedIn extends Widget content: => script -> raw "var API_KEY = '#{@keys[1].key}';" + script src: "http://code.jquery.com/jquery-3.3.1.min.js", integrity: "sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8=", crossorigin: "anonymous" script src: "/static/index.js" p "API Keys:" @@ -10,8 +11,8 @@ class LoggedIn extends Widget if @keys for key in *@keys li key.key - li -> - a onclick: "new_api_key()", "+" + li id: "new-api-key", -> + button onclick: "new_api_key()", "+" p "Tasks:" ul -> @@ -20,9 +21,9 @@ class LoggedIn extends Widget li -> input type: "checkbox", id: "task-#{task.id}", onchange: "check(#{task.id});", checked: task.done text " #{task.content}" - li -> + li id: "new-task", -> form { onsubmit: "return new_task();" }, -> - input type: "text", id: "new-task", placeholder: "new task" + input type: "text", id: "new-task-input", placeholder: "new task" input type: "submit", value: "+"