From 10f530b7ac1c4c1c2cd410921918ff4a7d67081a Mon Sep 17 00:00:00 2001 From: Haris Razis Date: Wed, 13 Jan 2021 20:29:10 +0200 Subject: [PATCH] Fix profile/athlete update actions Found the need for double click to update --- web/src/store/modules/athletes.ts | 18 ++++++++++-------- web/src/store/modules/backend.ts | 11 +++++------ web/src/store/modules/user.ts | 16 ++++++++++------ web/src/views/Athlete.vue | 25 ++++++++++--------------- web/src/views/Athletes.vue | 2 +- web/src/views/Profile.vue | 24 +++++++----------------- 6 files changed, 43 insertions(+), 53 deletions(-) diff --git a/web/src/store/modules/athletes.ts b/web/src/store/modules/athletes.ts index a3a15da..c59b520 100644 --- a/web/src/store/modules/athletes.ts +++ b/web/src/store/modules/athletes.ts @@ -60,6 +60,13 @@ export default class Athletes extends VuexModule { this.err = err; } + @Mutation + private athlete_addTrainer(trainer: UserInterface) { + this.trainer = trainer; + this.athlete._trainer = this.trainer._id; + } + + @Action private athlete_getAll() { return new Promise((resolve, reject) => { @@ -78,12 +85,12 @@ export default class Athletes extends VuexModule { } @Action - private athlete_update(athlete: AthleteInterface) { + private athlete_update() { return new Promise((resolve, reject) => { axios({ method: 'PUT', - url: `/api/athletes/${athlete._id}`, - data: {...athlete} + url: `/api/athletes/${this.athlete._id}`, + data: {...this.athlete} }) .then((resp: AxiosResponse) => { this.context.commit('api_athlete', resp.data); @@ -95,11 +102,6 @@ export default class Athletes extends VuexModule { }); } - @Action - private athlete_saveLocalTrainer(trainer: UserInterface) { - this.context.commit('api_trainer', trainer); - } - @Action private athlete_saveLocal(athlete: AthleteInterface) { this.context.commit('api_athlete', athlete); diff --git a/web/src/store/modules/backend.ts b/web/src/store/modules/backend.ts index 10fe52c..a0beb25 100644 --- a/web/src/store/modules/backend.ts +++ b/web/src/store/modules/backend.ts @@ -36,12 +36,11 @@ export default class Backend extends VuexModule { @Mutation private server_logout() { - this.liveAthData = [] + this.liveAthData = []; } @Action private server_saveLiveData(data: AthleteData) { - console.log(this.liveAthData); this.context.commit('server_queueData', data); if (this.liveAthData.length > 15) this.context.commit('server_dequeueData'); @@ -55,10 +54,10 @@ export default class Backend extends VuexModule { url: `/api/data/` }) .then((resp: AxiosResponse) => { - resolve(resp) + resolve(resp); }) .catch((err: Error) => { - reject(err) + reject(err); }); }); } @@ -71,10 +70,10 @@ export default class Backend extends VuexModule { url: `/api/data/${id}` }) .then((resp: AxiosResponse) => { - resolve(resp) + resolve(resp); }) .catch((err: Error) => { - reject(err) + reject(err); }); }); } diff --git a/web/src/store/modules/user.ts b/web/src/store/modules/user.ts index 78da540..4908b57 100644 --- a/web/src/store/modules/user.ts +++ b/web/src/store/modules/user.ts @@ -6,7 +6,9 @@ export interface UserInterface { _id: string, username: string, email: string, - password: string, + password: string | undefined, + newPassword: string | undefined, + repeatNewPassword: string | undefined, registered: Date, lastLogin: Date, } @@ -32,6 +34,7 @@ export default class User extends VuexModule { @Mutation private auth_success(user: UserInterface) { this.user = user; + delete this.user['password']; this.userStatus = true; } @@ -55,6 +58,7 @@ export default class User extends VuexModule { @Mutation private update_user(user: UserInterface) { this.user = user; + delete this.user['password']; } @Action @@ -106,7 +110,7 @@ export default class User extends VuexModule { resolve(resp); }) .catch((err: Error) => { - this.context.commit('auth_error') + this.context.commit('auth_error'); reject(err); }); }); @@ -114,19 +118,19 @@ export default class User extends VuexModule { @Action - private user_update(user: UserInterface) { + private user_update() { return new Promise((resolve, reject) => { axios({ method: 'PUT', - url: `/api/user/${user._id}`, - data: {...user} + url: `/api/user/${this.user._id}`, + data: {...this.user} }) .then((resp: AxiosResponse) => { this.context.commit('update_user', resp.data); resolve(resp); }) .catch((err: Error) => { - this.context.commit('update_err', err) + this.context.commit('update_err', err); reject(err); }); }); diff --git a/web/src/views/Athlete.vue b/web/src/views/Athlete.vue index 7172196..7a1e59d 100644 --- a/web/src/views/Athlete.vue +++ b/web/src/views/Athlete.vue @@ -87,7 +87,7 @@

- +

@@ -138,30 +138,25 @@ export default class Athlete extends Vue { if (this.trainer) { this.trainerLogin = new Date(this.trainer.lastLogin).toLocaleString(); - this.$store.dispatch('athlete_saveLocalTrainer', this.trainer); + this.$store.commit('api_trainer', this.trainer); } } - private athlete_update(action: string) { - if (action === 'adopt' && this.clientID === '') { - this.msgError = 'Please give a valid athlete ID!'; - return; - } - + private athlete_adopt() { if (this.clientID != '' && this.clientID != this.athlete.id) { this.msgError = 'Athlete ID does not match with current athlete!'; return; } - if (action === 'adopt') { - Object.assign(this.athlete, {_trainer: this.$store.getters.user_current._id}); - } else { - Object.assign(this.athlete, {_trainer: ''}); - } + this.athlete_update(); + } + + private athlete_update() { + this.$store.commit('athlete_addTrainer', this.$store.getters.user_current); - this.$store.dispatch('athlete_update', this.athlete) + this.$store.dispatch('athlete_update') .then((res: any) => { - this.msgSuccess = 'Athlete updated' + this.msgSuccess = 'Athlete updated'; this.athlete = res.data; }) .catch(() => this.msgError = this.$store.getters.athlete_err.response.data.errors.message || diff --git a/web/src/views/Athletes.vue b/web/src/views/Athletes.vue index 89b2a86..ff1f727 100644 --- a/web/src/views/Athletes.vue +++ b/web/src/views/Athletes.vue @@ -154,7 +154,7 @@ export default class Athletes extends Vue { } private athlete_save(athlete: AthleteInterface) { - this.$store.dispatch('athlete_saveLocal', athlete); + this.$store.commit('api_athlete', athlete); } private athlete_delete(athlete: AthleteInterface, index: number) { diff --git a/web/src/views/Profile.vue b/web/src/views/Profile.vue index a6ff50f..68ce931 100644 --- a/web/src/views/Profile.vue +++ b/web/src/views/Profile.vue @@ -31,25 +31,25 @@

Change Password

- +

- +

- +

-

@@ -79,17 +79,9 @@ import {Vue} from "vue-class-component"; import {UserInterface} from "@/store/modules/user"; -interface UpdatedUser extends UserInterface { - newPassword: string, - repeatNewPassword: string, -} - export default class Profile extends Vue { - private user = {}; + private user = {}; private date = ''; - private currentPassword = ''; - private newPassword = ''; - private repeatNewPassword = ''; private msgSuccess = ''; private msgError = ''; @@ -99,14 +91,12 @@ export default class Profile extends Vue { } private user_update() { - if (this.newPassword != this.repeatNewPassword) { + if (this.user.newPassword != this.user.repeatNewPassword) { this.msgError = 'Passwords do not match!'; return; } - Object.assign(this.user, {password: this.currentPassword, newPassword: this.newPassword}); - - this.$store.dispatch('user_update', this.user) + this.$store.dispatch('user_update') .then(() => this.msgSuccess = 'User updated!') .catch(() => this.msgError = this.$store.getters.user_err.response.data.errors.message || 'Something went wrong!');