2021-08-11 19:39:09 +02:00
|
|
|
import { ActionContext, ActionTree } from 'vuex'
|
2021-08-11 22:21:26 +02:00
|
|
|
|
2021-08-21 18:34:06 +02:00
|
|
|
import authApi from '@/api/authApi'
|
|
|
|
import api from '@/api/defaultApi'
|
|
|
|
import router from '@/router'
|
2021-09-23 15:56:57 +02:00
|
|
|
import {
|
|
|
|
ROOT_STORE,
|
|
|
|
SPORTS_STORE,
|
|
|
|
STATS_STORE,
|
|
|
|
USER_STORE,
|
|
|
|
WORKOUTS_STORE,
|
|
|
|
} from '@/store/constants'
|
2021-08-22 20:38:22 +02:00
|
|
|
import { IRootState } from '@/store/modules/root/types'
|
|
|
|
import { IUserActions, IUserState } from '@/store/modules/user/types'
|
2021-10-13 19:21:53 +02:00
|
|
|
import {
|
|
|
|
ILoginOrRegisterData,
|
|
|
|
IUserDeletionPayload,
|
|
|
|
IUserPayload,
|
|
|
|
} from '@/types/user'
|
2021-08-11 21:12:20 +02:00
|
|
|
import { handleError } from '@/utils'
|
2021-08-11 19:39:09 +02:00
|
|
|
|
|
|
|
export const actions: ActionTree<IUserState, IRootState> & IUserActions = {
|
2021-08-14 19:24:19 +02:00
|
|
|
[USER_STORE.ACTIONS.CHECK_AUTH_USER](
|
|
|
|
context: ActionContext<IUserState, IRootState>
|
|
|
|
): void {
|
|
|
|
if (
|
|
|
|
window.localStorage.authToken &&
|
|
|
|
!context.getters[USER_STORE.GETTERS.IS_AUTHENTICATED]
|
|
|
|
) {
|
|
|
|
context.commit(
|
|
|
|
USER_STORE.MUTATIONS.UPDATE_AUTH_TOKEN,
|
|
|
|
window.localStorage.authToken
|
|
|
|
)
|
|
|
|
context.dispatch(USER_STORE.ACTIONS.GET_USER_PROFILE)
|
|
|
|
}
|
|
|
|
},
|
2021-08-11 19:39:09 +02:00
|
|
|
[USER_STORE.ACTIONS.GET_USER_PROFILE](
|
|
|
|
context: ActionContext<IUserState, IRootState>
|
|
|
|
): void {
|
2021-08-15 10:50:39 +02:00
|
|
|
context.commit(ROOT_STORE.MUTATIONS.EMPTY_ERROR_MESSAGES)
|
2021-08-11 19:39:09 +02:00
|
|
|
authApi
|
|
|
|
.get('auth/profile')
|
|
|
|
.then((res) => {
|
|
|
|
if (res.data.status === 'success') {
|
|
|
|
context.commit(
|
|
|
|
USER_STORE.MUTATIONS.UPDATE_AUTH_USER_PROFILE,
|
|
|
|
res.data.data
|
|
|
|
)
|
2021-08-22 21:10:47 +02:00
|
|
|
context.dispatch(SPORTS_STORE.ACTIONS.GET_SPORTS)
|
2021-08-11 21:12:20 +02:00
|
|
|
} else {
|
|
|
|
handleError(context, null)
|
2021-08-11 19:39:09 +02:00
|
|
|
}
|
|
|
|
})
|
2021-08-11 21:12:20 +02:00
|
|
|
.catch((error) => handleError(context, error))
|
2021-08-11 19:39:09 +02:00
|
|
|
},
|
|
|
|
[USER_STORE.ACTIONS.LOGIN_OR_REGISTER](
|
|
|
|
context: ActionContext<IUserState, IRootState>,
|
|
|
|
data: ILoginOrRegisterData
|
|
|
|
): void {
|
2021-08-15 10:50:39 +02:00
|
|
|
context.commit(ROOT_STORE.MUTATIONS.EMPTY_ERROR_MESSAGES)
|
2021-08-11 19:39:09 +02:00
|
|
|
api
|
|
|
|
.post(`/auth/${data.actionType}`, data.formData)
|
|
|
|
.then((res) => {
|
2021-08-11 21:12:20 +02:00
|
|
|
if (res.data.status === 'success') {
|
2021-08-11 19:39:09 +02:00
|
|
|
const token = res.data.auth_token
|
|
|
|
window.localStorage.setItem('authToken', token)
|
|
|
|
context.commit(USER_STORE.MUTATIONS.UPDATE_AUTH_TOKEN, token)
|
|
|
|
context
|
|
|
|
.dispatch(USER_STORE.ACTIONS.GET_USER_PROFILE)
|
|
|
|
.then(() => router.push('/'))
|
2021-08-11 21:12:20 +02:00
|
|
|
} else {
|
|
|
|
handleError(context, null)
|
2021-08-11 19:39:09 +02:00
|
|
|
}
|
|
|
|
})
|
2021-08-11 21:12:20 +02:00
|
|
|
.catch((error) => handleError(context, error))
|
2021-08-11 19:39:09 +02:00
|
|
|
},
|
2021-08-14 18:58:59 +02:00
|
|
|
[USER_STORE.ACTIONS.LOGOUT](
|
|
|
|
context: ActionContext<IUserState, IRootState>
|
|
|
|
): void {
|
|
|
|
localStorage.removeItem('authToken')
|
2021-08-15 10:50:39 +02:00
|
|
|
context.commit(ROOT_STORE.MUTATIONS.EMPTY_ERROR_MESSAGES)
|
2021-09-23 15:56:57 +02:00
|
|
|
context.commit(STATS_STORE.MUTATIONS.EMPTY_USER_STATS)
|
|
|
|
context.commit(USER_STORE.MUTATIONS.CLEAR_AUTH_USER_TOKEN)
|
|
|
|
context.commit(WORKOUTS_STORE.MUTATIONS.EMPTY_WORKOUTS)
|
2021-08-14 18:58:59 +02:00
|
|
|
router.push('/login')
|
|
|
|
},
|
2021-10-13 18:45:34 +02:00
|
|
|
[USER_STORE.ACTIONS.UPDATE_USER_PROFILE](
|
|
|
|
context: ActionContext<IUserState, IRootState>,
|
|
|
|
payload: IUserPayload
|
|
|
|
): void {
|
|
|
|
context.commit(ROOT_STORE.MUTATIONS.EMPTY_ERROR_MESSAGES)
|
|
|
|
context.commit(USER_STORE.MUTATIONS.UPDATE_USER_LOADING, true)
|
|
|
|
authApi
|
|
|
|
.post('auth/profile/edit', payload)
|
|
|
|
.then((res) => {
|
|
|
|
if (res.data.status === 'success') {
|
|
|
|
context.commit(
|
|
|
|
USER_STORE.MUTATIONS.UPDATE_AUTH_USER_PROFILE,
|
|
|
|
res.data.data
|
|
|
|
)
|
|
|
|
router.push('/profile')
|
|
|
|
} else {
|
|
|
|
handleError(context, null)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.catch((error) => handleError(context, error))
|
|
|
|
.finally(() =>
|
|
|
|
context.commit(USER_STORE.MUTATIONS.UPDATE_USER_LOADING, false)
|
|
|
|
)
|
|
|
|
},
|
2021-10-13 19:21:53 +02:00
|
|
|
[USER_STORE.ACTIONS.DELETE_ACCOUNT](
|
|
|
|
context: ActionContext<IUserState, IRootState>,
|
|
|
|
payload: IUserDeletionPayload
|
|
|
|
): void {
|
|
|
|
context.commit(ROOT_STORE.MUTATIONS.EMPTY_ERROR_MESSAGES)
|
|
|
|
authApi
|
|
|
|
.delete(`users/${payload.username}`)
|
|
|
|
.then((res) => {
|
|
|
|
if (res.status === 204) {
|
|
|
|
context
|
|
|
|
.dispatch(USER_STORE.ACTIONS.LOGOUT)
|
|
|
|
.then(() => router.push('/'))
|
|
|
|
} else {
|
|
|
|
handleError(context, null)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.catch((error) => handleError(context, error))
|
|
|
|
},
|
2021-08-11 19:39:09 +02:00
|
|
|
}
|