From be942e06ba8d054b1ae10cdb35c99431a3c60cf7 Mon Sep 17 00:00:00 2001 From: Stefan Bethge Date: Fri, 17 Jul 2015 20:26:19 +0200 Subject: [PATCH] allow editing account labels --- src/main/scala/bootstrap/liftweb/Boot.scala | 6 ++-- src/main/scala/code/lib/ObpAPI.scala | 12 ++++++-- src/main/scala/code/snippet/Nav.scala | 2 +- .../scala/code/snippet/TransactionsList.scala | 10 ++++--- .../scala/code/snippet/ViewsOverview.scala | 28 ++++++++++++++++++- .../webapp/banks/star/accounts/star/star.html | 2 -- .../banks/star/accounts/star/views/list.html | 7 +++++ src/main/webapp/media/css/website.css | 2 +- src/main/webapp/media/js/views.js | 10 +++++++ 9 files changed, 65 insertions(+), 14 deletions(-) diff --git a/src/main/scala/bootstrap/liftweb/Boot.scala b/src/main/scala/bootstrap/liftweb/Boot.scala index ec8f3c2a..2148d992 100755 --- a/src/main/scala/bootstrap/liftweb/Boot.scala +++ b/src/main/scala/bootstrap/liftweb/Boot.scala @@ -162,7 +162,7 @@ class Boot extends Loggable{ for { //TODO: Pagination: This is not totally trivial, since the transaction list groups by date and 2 pages may have some transactions on the same date transactionsJson <- ObpAPI.transactions(bank, account, viewName, Some(10000), Some(0), None, None, None) - accountJson <- ObpAPI.account(bank, account, viewName) //TODO: Execute this request and the one above in parallel + accountJson <- ObpAPI.getAccount(bank, account, viewName) //TODO: Execute this request and the one above in parallel } yield { (transactionsJson, accountJson, transactionsURLParams) } @@ -264,7 +264,7 @@ class Boot extends Loggable{ logOrReturnResult { for { viewsJson <- ObpAPI.getViews(bank, account) - accountJson <- ObpAPI.account(bank, account, "owner" /*TODO: This shouldn't be hardcoded*/) //TODO: Execute this request and the one above in parallel + accountJson <- ObpAPI.getAccount(bank, account, "owner" /*TODO: This shouldn't be hardcoded*/) //TODO: Execute this request and the one above in parallel } yield { (viewsJson, accountJson, PermissionsUrlParams(bank, account)) } @@ -314,7 +314,7 @@ class Boot extends Loggable{ for { permissionsJson <- ObpAPI.getPermissions(bank, account) accountViewsJson <- ObpAPI.getViews(bank, account) - accountJson <- ObpAPI.account(bank, account, "owner" /*TODO: This shouldn't be hardcoded*/) //TODO: Execute this request and the one above in parallel + accountJson <- ObpAPI.getAccount(bank, account, "owner" /*TODO: This shouldn't be hardcoded*/) //TODO: Execute this request and the one above in parallel } yield (permissionsJson, accountJson, accountViewsJson, PermissionsUrlParams(bank, account)) } } else Empty diff --git a/src/main/scala/code/lib/ObpAPI.scala b/src/main/scala/code/lib/ObpAPI.scala index ca2b8338..12ff3bca 100644 --- a/src/main/scala/code/lib/ObpAPI.scala +++ b/src/main/scala/code/lib/ObpAPI.scala @@ -83,7 +83,7 @@ object ObpAPI extends Loggable { ObpGet("/v1.2.1/accounts/private").flatMap(_.extractOpt[BarebonesAccountsJson]) } - def account(bankId: String, accountId: String, viewId: String) : Box[AccountJson] = { + def getAccount(bankId: String, accountId: String, viewId: String) : Box[AccountJson] = { ObpGet("/v1.2/banks/" + urlEncode(bankId) + "/accounts/" + urlEncode(accountId) + "/" + urlEncode(viewId) + "/account").flatMap(x => x.extractOpt[AccountJson]) } @@ -94,7 +94,15 @@ object ObpAPI extends Loggable { val deleteAccountUrl = "/internal/v1.0/banks/" + urlEncode(bankId) + "/accounts/" + urlEncode(accountId) ObpInternalDelete(deleteAccountUrl) } - + + def updateAccountLabel(bankId: String, accountId : String, label: String) = { + val json = + ("id" -> accountId) ~ + ("label" -> label) ~ + ("bank_id" -> bankId) + ObpPost("/v1.2.1/banks/" + urlEncode(bankId) + "/accounts/" + urlEncode(accountId), json) + } + /** * @return The json for the comment if it was successfully added */ diff --git a/src/main/scala/code/snippet/Nav.scala b/src/main/scala/code/snippet/Nav.scala index 1fab40b6..aa412057 100644 --- a/src/main/scala/code/snippet/Nav.scala +++ b/src/main/scala/code/snippet/Nav.scala @@ -55,7 +55,7 @@ class Nav { val bankId = url( url.indexOf("banks")+1 ) val accountId = url( url.indexOf("accounts")+1 ) - ObpAPI.account(bankId, accountId, viewId) + ObpAPI.getAccount(bankId, accountId, viewId) } else { None } diff --git a/src/main/scala/code/snippet/TransactionsList.scala b/src/main/scala/code/snippet/TransactionsList.scala index c33f39d3..5fc2caa9 100644 --- a/src/main/scala/code/snippet/TransactionsList.scala +++ b/src/main/scala/code/snippet/TransactionsList.scala @@ -417,10 +417,12 @@ Used in transactions list availableViews.exists(view => view.id == Some("owner")) } var label = accountJson.label.getOrElse("") - if (label.isEmpty && hasManagementAccess) - label = accountJson.number.getOrElse("") - else - label = accountJson.id.getOrElse("") + if (label.isEmpty) { + if (hasManagementAccess) + label = accountJson.number.getOrElse("") + else + label = accountJson.id.getOrElse("") + } "#accountShortDiscription *" #> label } diff --git a/src/main/scala/code/snippet/ViewsOverview.scala b/src/main/scala/code/snippet/ViewsOverview.scala index b9bfdf68..2e316144 100644 --- a/src/main/scala/code/snippet/ViewsOverview.scala +++ b/src/main/scala/code/snippet/ViewsOverview.scala @@ -13,7 +13,7 @@ import net.liftweb.http.js.JsCmds.{SetHtml, Alert, RedirectTo} import net.liftweb.common.{Loggable, Box} import code.lib.ObpAPI import net.liftweb.http.SHtml.{text,ajaxSubmit, ajaxButton} -import ObpAPI.{addView, deleteView} +import ObpAPI.{addView, deleteView, updateAccountLabel, getAccount} import SHtml._ case class ViewUpdateData( @@ -204,4 +204,30 @@ class ViewsOverview(viewsDataJson: ViewsDataJSON) extends Loggable { "type=submit" #> ajaxSubmit("OK", process) ).apply(xhtml) } + + //set up ajax handlers to edit account label + def setupEditLabel(xhtml: NodeSeq): NodeSeq = { + var newLabel = "" + + def process(): JsCmd = { + logger.debug(s"ViewsOverview.setupEditLabel.process: edit label $newLabel") + val result = updateAccountLabel(bank, account, newLabel) + if (result.isDefined) { + val msg = "Label " + newLabel + " has been set" + Call("socialFinanceNotifications.notify", msg).cmd + } else { + val msg = "Sorry, Label" + newLabel + " could not be set ("+ result +")" + Call("socialFinanceNotifications.notifyError", msg).cmd + } + } + + val label = getAccount(bank, account, "owner").get.label.getOrElse("Label") + ( + // Bind newViewName field to variable (e.g. http://chimera.labs.oreilly.com/books/1234000000030/ch03.html) + "@new_label" #> text(newLabel, s => newLabel = s) & + // Replace the type=submit with Javascript that makes the ajax call. + "type=submit" #> ajaxSubmit("OK", process) & + "type=text [value]" #> label + ).apply(xhtml) + } } diff --git a/src/main/webapp/banks/star/accounts/star/star.html b/src/main/webapp/banks/star/accounts/star/star.html index db64a1df..6565b5f2 100644 --- a/src/main/webapp/banks/star/accounts/star/star.html +++ b/src/main/webapp/banks/star/accounts/star/star.html @@ -36,8 +36,6 @@ e.g. http://localhost:8080/banks/bnpp-fr2/accounts/1137869186/owner --> - -
diff --git a/src/main/webapp/banks/star/accounts/star/views/list.html b/src/main/webapp/banks/star/accounts/star/views/list.html index fb57c3e4..5131075e 100644 --- a/src/main/webapp/banks/star/accounts/star/views/list.html +++ b/src/main/webapp/banks/star/accounts/star/views/list.html @@ -48,6 +48,13 @@ See views.js for the javascript which manipulates the DOM for editing
+ +
+
+ + +
+
diff --git a/src/main/webapp/media/css/website.css b/src/main/webapp/media/css/website.css index 3a9bd3b6..61f2e9f5 100644 --- a/src/main/webapp/media/css/website.css +++ b/src/main/webapp/media/css/website.css @@ -933,7 +933,7 @@ td.main-attributes{ margin: 10px; } -#add-view-form { +#add-view-form, #account-edit-label-form { display: none; } diff --git a/src/main/webapp/media/js/views.js b/src/main/webapp/media/js/views.js index ddd51296..e8b57c40 100644 --- a/src/main/webapp/media/js/views.js +++ b/src/main/webapp/media/js/views.js @@ -23,6 +23,16 @@ $(document).ready(function(){ } }) + $('#account-edit-label').click(function(){ + var el = $('#account-edit-label-form') + if (el.is(":visible")) { + el.hide() + } else { + el.css("display", "inline-block") + $('#account-edit-label-form form input:first-of-type').focus() + } + }) + /* clicking on edit: change view to edit mode for selected view */ $(".edit").on("click", function(){ var viewId = $(this).attr("data-id")