From 7b39096e3f2daf4a068eac1b113c44bba7d6c52d Mon Sep 17 00:00:00 2001 From: Jordan Date: Wed, 28 May 2014 23:29:41 -0500 Subject: [PATCH] Added async settings update Removed /settings GET endpoint (not needed, since AngularJS in use) TODO: Move the anon struct in use to a model as appropriate --- controllers/route.go | 31 +++++++++++++--------------- static/js/app/controllers.js | 19 +++++++++++++++++ static/js/app/partials/settings.html | 12 +++++------ static/js/app/partials/users.html | 2 +- 4 files changed, 40 insertions(+), 24 deletions(-) diff --git a/controllers/route.go b/controllers/route.go index 8e2e6f5e..686aed1d 100644 --- a/controllers/route.go +++ b/controllers/route.go @@ -1,6 +1,7 @@ package controllers import ( + "encoding/json" "fmt" "html/template" "net/http" @@ -127,29 +128,25 @@ func Base(w http.ResponseWriter, r *http.Request) { } func Settings(w http.ResponseWriter, r *http.Request) { - params := struct { - User models.User - Title string - Flashes []interface{} - Token string - }{Title: "Settings", User: ctx.Get(r, "user").(models.User)} - session := ctx.Get(r, "session").(*sessions.Session) switch { - case r.Method == "GET": - params.Token = nosurf.Token(r) - params.Flashes = session.Flashes() - session.Save(r, w) - getTemplate(w, "settings").ExecuteTemplate(w, "base", params) case r.Method == "POST": err := auth.ChangePassword(r) + msg := struct { + Message string `json:"message"` + Success bool `json:"success"` + }{Message: "Settings Updated Successfully", Success: true} if err == auth.ErrInvalidPassword { - Flash(w, r, "danger", "Invalid Password") + msg.Message = "Invalid Password" + msg.Success = false } else if err != nil { - Flash(w, r, "danger", "Unknown Error") - } else { - Flash(w, r, "success", "Password successfully reset") + msg.Message = "Unknown Error Occured" + msg.Success = false } - http.Redirect(w, r, "/settings", 302) + msgj, err := json.MarshalIndent(msg, "", " ") + if checkError(err, w, "Error marshaling response", http.StatusInternalServerError) { + return + } + writeJSON(w, msgj) } } diff --git a/static/js/app/controllers.js b/static/js/app/controllers.js index ed3535ce..ca2fc5c3 100644 --- a/static/js/app/controllers.js +++ b/static/js/app/controllers.js @@ -356,6 +356,7 @@ app.controller('SettingsCtrl', function($scope, $http, $window) { }) } $scope.form_data = { + username: user.username, csrf_token: csrf_token } $scope.api_reset = function() { @@ -373,4 +374,22 @@ app.controller('SettingsCtrl', function($scope, $http, $window) { $scope.successFlash("API Key Successfully Reset") }) } + $scope.save_settings = function(){ + $http({ + method: 'POST', + url: '/settings', + data: $.param($scope.form_data), + headers: { + 'Content-Type': 'application/x-www-form-urlencoded' + } + }) + .success(function(data) { + if (data.success) { + $scope.successFlash(data.message) + } + else { + $scope.errorFlash(data.message) + } + }) + } }) diff --git a/static/js/app/partials/settings.html b/static/js/app/partials/settings.html index 7072061f..ef604d84 100644 --- a/static/js/app/partials/settings.html +++ b/static/js/app/partials/settings.html @@ -32,32 +32,32 @@
- +

-
+
- +

- +

- +
- +
diff --git a/static/js/app/partials/users.html b/static/js/app/partials/users.html index 28b5a612..4594130c 100644 --- a/static/js/app/partials/users.html +++ b/static/js/app/partials/users.html @@ -44,7 +44,7 @@ Toggle Dropdown