From bb116fe5b0fd5218416131eae862fc9b9d59badd Mon Sep 17 00:00:00 2001 From: Henrique Dias Date: Sat, 8 Jul 2017 14:43:08 +0100 Subject: [PATCH] Prettier success messages --- api.go | 50 +++++++++++------------ assets/src/components/Files.vue | 10 +++-- assets/src/components/Main.vue | 25 ++++++++---- assets/src/components/Settings.vue | 11 ++--- assets/src/components/User.vue | 21 +++++++++- assets/src/components/prompts/Prompts.vue | 4 ++ assets/src/components/prompts/Success.vue | 20 +++++++++ assets/src/css/prompts.css | 10 +++++ assets/src/store/mutations.js | 13 +++--- assets/src/utils/api.js | 4 +- 10 files changed, 116 insertions(+), 52 deletions(-) create mode 100644 assets/src/components/prompts/Success.vue diff --git a/api.go b/api.go index d6e32028..392412fd 100644 --- a/api.go +++ b/api.go @@ -72,7 +72,7 @@ func serveAPI(c *requestContext, w http.ResponseWriter, r *http.Request) (int, e case "resource": return resourceHandler(c, w, r) case "users": - if !c.us.Admin && !(r.URL.Path == "/self" && r.Method == http.MethodPut) { + if !c.us.Admin && !((r.URL.Path == "/change-password" || r.URL.Path == "/change-css") && r.Method == http.MethodPut) { return http.StatusForbidden, nil } @@ -503,7 +503,7 @@ func usersPutHandler(c *requestContext, w http.ResponseWriter, r *http.Request) sid = strings.TrimSuffix(sid, "/") id, err := strconv.Atoi(sid) - if err != nil && sid != "self" { + if err != nil && sid != "change-password" && sid != "change-css" { return http.StatusNotFound, err } @@ -520,33 +520,33 @@ func usersPutHandler(c *requestContext, w http.ResponseWriter, r *http.Request) return http.StatusBadRequest, errors.New("Invalid JSON") } - if sid == "self" { - if u.Password != "" { - pw, err := hashPassword(u.Password) - if err != nil { - return http.StatusInternalServerError, err - } - - c.us.Password = pw - err = c.fm.db.UpdateField(&User{ID: c.us.ID}, "Password", pw) - if err != nil { - return http.StatusInternalServerError, err - } - - return http.StatusOK, nil + if sid == "change-password" { + if u.Password == "" { + return http.StatusBadRequest, errors.New("Password cannot be empty") } - if u.CSS != "" { - c.us.CSS = u.CSS - err = c.fm.db.UpdateField(&User{ID: c.us.ID}, "CSS", u.CSS) - if err != nil { - return http.StatusInternalServerError, err - } - - return http.StatusOK, nil + pw, err := hashPassword(u.Password) + if err != nil { + return http.StatusInternalServerError, err } - return http.StatusBadRequest, errors.New("Password or CSS is missing") + c.us.Password = pw + err = c.fm.db.UpdateField(&User{ID: c.us.ID}, "Password", pw) + if err != nil { + return http.StatusInternalServerError, err + } + + return http.StatusOK, nil + } + + if sid == "change-css" { + c.us.CSS = u.CSS + err = c.fm.db.UpdateField(&User{ID: c.us.ID}, "CSS", u.CSS) + if err != nil { + return http.StatusInternalServerError, err + } + + return http.StatusOK, nil } // The username and the filesystem cannot be empty. diff --git a/assets/src/components/Files.vue b/assets/src/components/Files.vue index 546e1dab..f2d824d8 100644 --- a/assets/src/components/Files.vue +++ b/assets/src/components/Files.vue @@ -66,10 +66,6 @@ export default { watch: { '$route': 'fetchData', 'reload': function () { - this.$store.commit('setReload', false) - this.$store.commit('resetSelected') - this.$store.commit('multiple', false) - this.$store.commit('closeHovers') this.fetchData() } }, @@ -82,6 +78,12 @@ export default { methods: { ...mapMutations([ 'setLoading' ]), fetchData () { + // Reset view information. + this.$store.commit('setReload', false) + this.$store.commit('resetSelected') + this.$store.commit('multiple', false) + this.$store.commit('closeHovers') + // Set loading to true and reset the error. this.setLoading(true) this.error = null diff --git a/assets/src/components/Main.vue b/assets/src/components/Main.vue index de9b0f87..a5b2a9e8 100644 --- a/assets/src/components/Main.vue +++ b/assets/src/components/Main.vue @@ -3,7 +3,7 @@
- +
@@ -23,12 +23,23 @@ export default { SiteHeader, Prompts }, - watch: { - '$route': function () { - // Reset selected items and multiple selection. - this.$store.commit('resetSelected') - this.$store.commit('multiple', false) - this.$store.commit('closeHovers') + mounted () { + this.updateCSS() + }, + methods: { + updateCSS () { + let css = this.$store.state.user.css + + let style = document.querySelector('style[title="user-css"]') + if (style !== undefined && style !== null) { + style.parentElement.removeChild(style) + } + + style = document.createElement('style') + style.title = 'user-css' + style.type = 'text/css' + style.appendChild(document.createTextNode(css)) + document.head.appendChild(style) } } } diff --git a/assets/src/components/Settings.vue b/assets/src/components/Settings.vue index e6e87667..c5efb938 100644 --- a/assets/src/components/Settings.vue +++ b/assets/src/components/Settings.vue @@ -20,7 +20,7 @@ diff --git a/assets/src/css/prompts.css b/assets/src/css/prompts.css index 3c7b87f8..01765ee5 100644 --- a/assets/src/css/prompts.css +++ b/assets/src/css/prompts.css @@ -64,6 +64,7 @@ background-color: #e9eaeb; } +.prompt.success i, .prompt.error i { color: #F44336; display: block; @@ -72,6 +73,7 @@ font-size: 5em; } +.prompt.success h3, .prompt.error h3 { text-align: center; } @@ -80,6 +82,14 @@ background-color: #F44336 } +.prompt.success i { + color: #8BC34A; +} + +.prompt.success button { + background-color: #8BC34A; +} + /* * * * * * * * * * * * * * * * * PROMPT - MOVE * diff --git a/assets/src/store/mutations.js b/assets/src/store/mutations.js index 262ef36d..505f0806 100644 --- a/assets/src/store/mutations.js +++ b/assets/src/store/mutations.js @@ -14,17 +14,16 @@ const mutations = { }, showError: (state, value) => { state.show = 'error' - - if (typeof value !== 'object') { - state.showMessage = value - return - } - - state.showMessage = value.message + state.showMessage = value + }, + showSuccess: (state, value) => { + state.show = 'success' + state.showMessage = value }, setLoading: (state, value) => { state.loading = value }, setReload: (state, value) => { state.reload = value }, setUser: (state, value) => (state.user = value), + setUserCSS: (state, value) => (state.user.css = value), setJWT: (state, value) => (state.jwt = value), multiple: (state, value) => (state.multiple = value), addSelected: (state, value) => (state.selected.push(value)), diff --git a/assets/src/utils/api.js b/assets/src/utils/api.js index a96546f2..33aa6ec4 100644 --- a/assets/src/utils/api.js +++ b/assets/src/utils/api.js @@ -277,7 +277,7 @@ function updateUser (user) { function updatePassword (password) { return new Promise((resolve, reject) => { let request = new window.XMLHttpRequest() - request.open('PUT', `${store.state.baseURL}/api/users/self`, true) + request.open('PUT', `${store.state.baseURL}/api/users/change-password`, true) request.setRequestHeader('Authorization', `Bearer ${store.state.jwt}`) request.onload = () => { @@ -298,7 +298,7 @@ function updatePassword (password) { function updateCSS (css) { return new Promise((resolve, reject) => { let request = new window.XMLHttpRequest() - request.open('PUT', `${store.state.baseURL}/api/users/self`, true) + request.open('PUT', `${store.state.baseURL}/api/users/change-css`, true) request.setRequestHeader('Authorization', `Bearer ${store.state.jwt}`) request.onload = () => {