Change update self method to automatically merge new fields with existing user object

This commit is contained in:
Andrew Kingston 2021-05-21 15:23:39 +01:00
parent 152a2d921b
commit d4518073c6
4 changed files with 6 additions and 6 deletions

View File

@ -8,7 +8,7 @@
const updatePassword = async () => {
try {
await auth.updateSelf({ ...$auth.user, password })
await auth.updateSelf({ password })
notifications.success("Password changed successfully")
} catch (error) {
notifications.error("Failed to update password")

View File

@ -10,7 +10,7 @@
const updateInfo = async () => {
try {
await auth.updateSelf({ ...$auth.user, ...$values })
await auth.updateSelf($values)
notifications.success("Information updated successfully")
} catch (error) {
notifications.error("Failed to update information")

View File

@ -13,7 +13,6 @@
try {
if (forceResetPassword) {
await auth.updateSelf({
...$auth.user,
password,
forceResetPassword: false,
})

View File

@ -1,4 +1,4 @@
import { derived, writable } from "svelte/store"
import { derived, writable, get } from "svelte/store"
import api from "../../builderStore/api"
export function createAuthStore() {
@ -50,10 +50,11 @@ export function createAuthStore() {
await response.json()
user.set(null)
},
updateSelf: async newUser => {
updateSelf: async fields => {
const newUser = { ...get(user), ...fields }
const response = await api.post("/api/admin/users/self", newUser)
if (response.status === 200) {
user.update(state => ({ ...state, ...newUser }))
user.set(newUser)
} else {
throw "Unable to update user details"
}