Browse Source

Move code to update user profile from endpoint

Since request.Body can only be read once, calling patchUser from
patchSelf always returned an error. request.Body needed to be either be
saved in a variable and reset or have a new function created.
master
Immanuel Onyeka 1 year ago
parent
commit
5111bd51dd
2 changed files with 39 additions and 21 deletions
  1. +21
    -7
      components/settings.vue
  2. +18
    -14
      skouter.go

+ 21
- 7
components/settings.vue View File

@@ -25,20 +25,20 @@
<section class="form inputs special"> <section class="form inputs special">
<h3>Profile</h3> <h3>Profile</h3>
<label for="">First Name</label> <label for="">First Name</label>
<input type="text"> <input type="text" v-model="user.firstName">
<label for="">Last Name</label> <label for="">Last Name</label>
<input type="text"> <input type="text" :value="user.lastName">
<label for="">NMLS ID</label> <label for="">NMLS ID</label>
<input type="text"> <input type="text">
<label for="">Branch ID</label> <label for="">Branch ID</label>
<input type="text"> <input type="text" :value="user.branchId">


<select id="" name="" > <select id="" name="" :value="user.country">
<option value="usa">USA</option> <option value="USA">USA</option>
<option value="canada">Canada</option> <option value="Canada">Canada</option>
</select> </select>


<button @click="check">Save</button> <button @click="saveProfile">Save</button>
</section> </section>


<section class="form inputs special"> <section class="form inputs special">
@@ -72,6 +72,7 @@ let letterHeadError = ref('')
let letterheadError = ref('') let letterheadError = ref('')
const props = defineProps(['user', 'token']) const props = defineProps(['user', 'token'])
const emit = defineEmits(['updateAvatar', 'updateLetterhead']) const emit = defineEmits(['updateAvatar', 'updateLetterhead'])
let user = Object.assign({}, props.user)


function save() { function save() {
} }
@@ -164,7 +165,20 @@ function changeLetterhead(blob) {
ctx.clearRect(0, 0, ctx.canvas.width, ctx.canvas.height) ctx.clearRect(0, 0, ctx.canvas.width, ctx.canvas.height)
ctx.drawImage(img, 0, 0) ctx.drawImage(img, 0, 0)
}) })
}


function saveProfile() {
console.log(user.firstName)
fetch(`/api/user`,
{method: 'PATCH',
body: JSON.stringify(user),
headers: {
"Accept": "application/json",
"Authorization": `Bearer ${props.token}`,
},
}).then(resp => {
if (resp.ok) {}
})
} }


watch(props.user, (u) => { watch(props.user, (u) => {


+ 18
- 14
skouter.go View File

@@ -889,24 +889,27 @@ func getUsers(w http.ResponseWriter, db *sql.DB, r *http.Request) {
} }


// Updates a user using only specified values in the JSON body // Updates a user using only specified values in the JSON body
func patchUser(w http.ResponseWriter, db *sql.DB, r *http.Request) { func setUser(user User, db *sql.DB) error {
var user User _, err := mail.ParseAddress(user.Email)
err := json.NewDecoder(r.Body).Decode(&user) if err != nil { return err }

_, err = mail.ParseAddress(user.Email)
if err != nil { http.Error(w, "Invalid email.", 422); return }


if roles[user.Role] == 0 { if roles[user.Role] == 0 {
http.Error(w, "Invalid role.", 422) return errors.New("Invalid role")
return
} }


err = updateUser(user, db) err = updateUser(user, db)
if err != nil { http.Error(w, "Bad form values.", 422); return } if err != nil { return err }
return nil
}


users, err := queryUsers(db, user.Id) func patchUser(w http.ResponseWriter, db *sql.DB, r *http.Request) {
if err != nil { http.Error(w, "Bad form values.", 422); return } var user User
json.NewEncoder(w).Encode(users[0]) err := json.NewDecoder(r.Body).Decode(&user)
if err != nil { http.Error(w, "Invalid fields", 422); return }

err = setUser(user, db)
if err != nil { http.Error(w, err.Error(), 422); return }
} }


// Update specified fields of the user specified in the claim // Update specified fields of the user specified in the claim
@@ -927,7 +930,8 @@ func patchSelf(w http.ResponseWriter, db *sql.DB, r *http.Request) {
return return
} }


patchUser(w, db, r) err = setUser(user, db)
if err != nil { http.Error(w, err.Error(), 422); return }
} }


func deleteUser(w http.ResponseWriter, db *sql.DB, r *http.Request) { func deleteUser(w http.ResponseWriter, db *sql.DB, r *http.Request) {
@@ -1751,7 +1755,7 @@ func api(w http.ResponseWriter, r *http.Request) {
patchUser(w, db, r) patchUser(w, db, r)
case match(p, "/api/user", &args) && case match(p, "/api/user", &args) &&
r.Method == http.MethodPatch && r.Method == http.MethodPatch &&
guard(r, 2): // For employees to modify own accounts guard(r, 1): // For employees to modify own accounts
patchSelf(w, db, r) patchSelf(w, db, r)
case match(p, "/api/user", &args) && case match(p, "/api/user", &args) &&
r.Method == http.MethodDelete && r.Method == http.MethodDelete &&


||||||
x
 
000:0
Loading…
Cancel
Save