Client - refactoring (rename auth user store)

This commit is contained in:
Sam
2021-11-03 10:41:53 +01:00
parent 3a1245a2e0
commit f97c7ae4d0
66 changed files with 406 additions and 368 deletions

View File

@ -1,3 +1,8 @@
import {
AuthUserActions,
AuthUserGetters,
AuthUserMutations,
} from '@/store/modules/authUser/enums'
import {
RootActions,
RootGetters,
@ -13,11 +18,6 @@ import {
StatisticsGetters,
StatisticsMutations,
} from '@/store/modules/statistics/enums'
import {
UserActions,
UserGetters,
UserMutations,
} from '@/store/modules/user/enums'
import {
UsersActions,
UsersGetters,
@ -47,10 +47,10 @@ export const STATS_STORE = {
MUTATIONS: StatisticsMutations,
}
export const USER_STORE = {
ACTIONS: UserActions,
GETTERS: UserGetters,
MUTATIONS: UserMutations,
export const AUTH_USER_STORE = {
ACTIONS: AuthUserActions,
GETTERS: AuthUserGetters,
MUTATIONS: AuthUserMutations,
}
export const USERS_STORE = {

View File

@ -5,15 +5,18 @@ import api from '@/api/defaultApi'
import createI18n from '@/i18n'
import router from '@/router'
import {
AUTH_USER_STORE,
ROOT_STORE,
SPORTS_STORE,
STATS_STORE,
USER_STORE,
USERS_STORE,
WORKOUTS_STORE,
} from '@/store/constants'
import {
IAuthUserActions,
IAuthUserState,
} from '@/store/modules/authUser/types'
import { IRootState } from '@/store/modules/root/types'
import { IUserActions, IUserState } from '@/store/modules/user/types'
import {
ILoginOrRegisterData,
IUserDeletionPayload,
@ -27,34 +30,37 @@ import { handleError } from '@/utils'
const { locale } = createI18n.global
const removeUserData = (context: ActionContext<IUserState, IRootState>) => {
const removeAuthUserData = (
context: ActionContext<IAuthUserState, IRootState>
) => {
localStorage.removeItem('authToken')
context.commit(ROOT_STORE.MUTATIONS.EMPTY_ERROR_MESSAGES)
context.commit(STATS_STORE.MUTATIONS.EMPTY_USER_STATS)
context.commit(USER_STORE.MUTATIONS.CLEAR_AUTH_USER_TOKEN)
context.commit(AUTH_USER_STORE.MUTATIONS.CLEAR_AUTH_USER_TOKEN)
context.commit(USERS_STORE.MUTATIONS.UPDATE_USERS, [])
context.commit(WORKOUTS_STORE.MUTATIONS.EMPTY_WORKOUTS)
context.commit(WORKOUTS_STORE.MUTATIONS.EMPTY_WORKOUT)
router.push('/login')
}
export const actions: ActionTree<IUserState, IRootState> & IUserActions = {
[USER_STORE.ACTIONS.CHECK_AUTH_USER](
context: ActionContext<IUserState, IRootState>
export const actions: ActionTree<IAuthUserState, IRootState> &
IAuthUserActions = {
[AUTH_USER_STORE.ACTIONS.CHECK_AUTH_USER](
context: ActionContext<IAuthUserState, IRootState>
): void {
if (
window.localStorage.authToken &&
!context.getters[USER_STORE.GETTERS.IS_AUTHENTICATED]
!context.getters[AUTH_USER_STORE.GETTERS.IS_AUTHENTICATED]
) {
context.commit(
USER_STORE.MUTATIONS.UPDATE_AUTH_TOKEN,
AUTH_USER_STORE.MUTATIONS.UPDATE_AUTH_TOKEN,
window.localStorage.authToken
)
context.dispatch(USER_STORE.ACTIONS.GET_USER_PROFILE)
context.dispatch(AUTH_USER_STORE.ACTIONS.GET_USER_PROFILE)
}
},
[USER_STORE.ACTIONS.GET_USER_PROFILE](
context: ActionContext<IUserState, IRootState>
[AUTH_USER_STORE.ACTIONS.GET_USER_PROFILE](
context: ActionContext<IAuthUserState, IRootState>
): void {
context.commit(ROOT_STORE.MUTATIONS.EMPTY_ERROR_MESSAGES)
authApi
@ -62,7 +68,7 @@ export const actions: ActionTree<IUserState, IRootState> & IUserActions = {
.then((res) => {
if (res.data.status === 'success') {
context.commit(
USER_STORE.MUTATIONS.UPDATE_AUTH_USER_PROFILE,
AUTH_USER_STORE.MUTATIONS.UPDATE_AUTH_USER_PROFILE,
res.data.data
)
if (res.data.data.language) {
@ -75,16 +81,16 @@ export const actions: ActionTree<IUserState, IRootState> & IUserActions = {
context.dispatch(SPORTS_STORE.ACTIONS.GET_SPORTS)
} else {
handleError(context, null)
removeUserData(context)
removeAuthUserData(context)
}
})
.catch((error) => {
handleError(context, error)
removeUserData(context)
removeAuthUserData(context)
})
},
[USER_STORE.ACTIONS.LOGIN_OR_REGISTER](
context: ActionContext<IUserState, IRootState>,
[AUTH_USER_STORE.ACTIONS.LOGIN_OR_REGISTER](
context: ActionContext<IAuthUserState, IRootState>,
data: ILoginOrRegisterData
): void {
context.commit(ROOT_STORE.MUTATIONS.EMPTY_ERROR_MESSAGES)
@ -94,9 +100,9 @@ export const actions: ActionTree<IUserState, IRootState> & IUserActions = {
if (res.data.status === 'success') {
const token = res.data.auth_token
window.localStorage.setItem('authToken', token)
context.commit(USER_STORE.MUTATIONS.UPDATE_AUTH_TOKEN, token)
context.commit(AUTH_USER_STORE.MUTATIONS.UPDATE_AUTH_TOKEN, token)
context
.dispatch(USER_STORE.ACTIONS.GET_USER_PROFILE)
.dispatch(AUTH_USER_STORE.ACTIONS.GET_USER_PROFILE)
.then(() =>
router.push(
typeof data.redirectUrl === 'string' ? data.redirectUrl : '/'
@ -108,23 +114,23 @@ export const actions: ActionTree<IUserState, IRootState> & IUserActions = {
})
.catch((error) => handleError(context, error))
},
[USER_STORE.ACTIONS.LOGOUT](
context: ActionContext<IUserState, IRootState>
[AUTH_USER_STORE.ACTIONS.LOGOUT](
context: ActionContext<IAuthUserState, IRootState>
): void {
removeUserData(context)
removeAuthUserData(context)
},
[USER_STORE.ACTIONS.UPDATE_USER_PROFILE](
context: ActionContext<IUserState, IRootState>,
[AUTH_USER_STORE.ACTIONS.UPDATE_USER_PROFILE](
context: ActionContext<IAuthUserState, IRootState>,
payload: IUserPayload
): void {
context.commit(ROOT_STORE.MUTATIONS.EMPTY_ERROR_MESSAGES)
context.commit(USER_STORE.MUTATIONS.UPDATE_USER_LOADING, true)
context.commit(AUTH_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,
AUTH_USER_STORE.MUTATIONS.UPDATE_AUTH_USER_PROFILE,
res.data.data
)
router.push('/profile')
@ -134,21 +140,21 @@ export const actions: ActionTree<IUserState, IRootState> & IUserActions = {
})
.catch((error) => handleError(context, error))
.finally(() =>
context.commit(USER_STORE.MUTATIONS.UPDATE_USER_LOADING, false)
context.commit(AUTH_USER_STORE.MUTATIONS.UPDATE_USER_LOADING, false)
)
},
[USER_STORE.ACTIONS.UPDATE_USER_PREFERENCES](
context: ActionContext<IUserState, IRootState>,
[AUTH_USER_STORE.ACTIONS.UPDATE_USER_PREFERENCES](
context: ActionContext<IAuthUserState, IRootState>,
payload: IUserPreferencesPayload
): void {
context.commit(ROOT_STORE.MUTATIONS.EMPTY_ERROR_MESSAGES)
context.commit(USER_STORE.MUTATIONS.UPDATE_USER_LOADING, true)
context.commit(AUTH_USER_STORE.MUTATIONS.UPDATE_USER_LOADING, true)
authApi
.post('auth/profile/edit/preferences', payload)
.then((res) => {
if (res.data.status === 'success') {
context.commit(
USER_STORE.MUTATIONS.UPDATE_AUTH_USER_PROFILE,
AUTH_USER_STORE.MUTATIONS.UPDATE_AUTH_USER_PROFILE,
res.data.data
)
context.commit(
@ -163,15 +169,15 @@ export const actions: ActionTree<IUserState, IRootState> & IUserActions = {
})
.catch((error) => handleError(context, error))
.finally(() =>
context.commit(USER_STORE.MUTATIONS.UPDATE_USER_LOADING, false)
context.commit(AUTH_USER_STORE.MUTATIONS.UPDATE_USER_LOADING, false)
)
},
[USER_STORE.ACTIONS.UPDATE_USER_PICTURE](
context: ActionContext<IUserState, IRootState>,
[AUTH_USER_STORE.ACTIONS.UPDATE_USER_PICTURE](
context: ActionContext<IAuthUserState, IRootState>,
payload: IUserPicturePayload
): void {
context.commit(ROOT_STORE.MUTATIONS.EMPTY_ERROR_MESSAGES)
context.commit(USER_STORE.MUTATIONS.UPDATE_USER_LOADING, true)
context.commit(AUTH_USER_STORE.MUTATIONS.UPDATE_USER_LOADING, true)
if (!payload.picture) {
throw new Error('No file part')
}
@ -186,7 +192,7 @@ export const actions: ActionTree<IUserState, IRootState> & IUserActions = {
.then((res) => {
if (res.data.status === 'success') {
context
.dispatch(USER_STORE.ACTIONS.GET_USER_PROFILE)
.dispatch(AUTH_USER_STORE.ACTIONS.GET_USER_PROFILE)
.then(() => router.push('/profile'))
} else {
handleError(context, null)
@ -194,11 +200,11 @@ export const actions: ActionTree<IUserState, IRootState> & IUserActions = {
})
.catch((error) => handleError(context, error))
.finally(() =>
context.commit(USER_STORE.MUTATIONS.UPDATE_USER_LOADING, false)
context.commit(AUTH_USER_STORE.MUTATIONS.UPDATE_USER_LOADING, false)
)
},
[USER_STORE.ACTIONS.DELETE_ACCOUNT](
context: ActionContext<IUserState, IRootState>,
[AUTH_USER_STORE.ACTIONS.DELETE_ACCOUNT](
context: ActionContext<IAuthUserState, IRootState>,
payload: IUserDeletionPayload
): void {
context.commit(ROOT_STORE.MUTATIONS.EMPTY_ERROR_MESSAGES)
@ -207,7 +213,7 @@ export const actions: ActionTree<IUserState, IRootState> & IUserActions = {
.then((res) => {
if (res.status === 204) {
context
.dispatch(USER_STORE.ACTIONS.LOGOUT)
.dispatch(AUTH_USER_STORE.ACTIONS.LOGOUT)
.then(() => router.push('/'))
} else {
handleError(context, null)
@ -215,17 +221,17 @@ export const actions: ActionTree<IUserState, IRootState> & IUserActions = {
})
.catch((error) => handleError(context, error))
},
[USER_STORE.ACTIONS.DELETE_PICTURE](
context: ActionContext<IUserState, IRootState>
[AUTH_USER_STORE.ACTIONS.DELETE_PICTURE](
context: ActionContext<IAuthUserState, IRootState>
): void {
context.commit(ROOT_STORE.MUTATIONS.EMPTY_ERROR_MESSAGES)
context.commit(USER_STORE.MUTATIONS.UPDATE_USER_LOADING, true)
context.commit(AUTH_USER_STORE.MUTATIONS.UPDATE_USER_LOADING, true)
authApi
.delete(`auth/picture`)
.then((res) => {
if (res.status === 204) {
context
.dispatch(USER_STORE.ACTIONS.GET_USER_PROFILE)
.dispatch(AUTH_USER_STORE.ACTIONS.GET_USER_PROFILE)
.then(() => router.push('/profile'))
} else {
handleError(context, null)
@ -233,11 +239,11 @@ export const actions: ActionTree<IUserState, IRootState> & IUserActions = {
})
.catch((error) => handleError(context, error))
.finally(() =>
context.commit(USER_STORE.MUTATIONS.UPDATE_USER_LOADING, false)
context.commit(AUTH_USER_STORE.MUTATIONS.UPDATE_USER_LOADING, false)
)
},
[USER_STORE.ACTIONS.SEND_PASSWORD_RESET_REQUEST](
context: ActionContext<IUserState, IRootState>,
[AUTH_USER_STORE.ACTIONS.SEND_PASSWORD_RESET_REQUEST](
context: ActionContext<IAuthUserState, IRootState>,
payload: IUserPasswordPayload
): void {
context.commit(ROOT_STORE.MUTATIONS.EMPTY_ERROR_MESSAGES)
@ -252,8 +258,8 @@ export const actions: ActionTree<IUserState, IRootState> & IUserActions = {
})
.catch((error) => handleError(context, error))
},
[USER_STORE.ACTIONS.RESET_USER_PASSWORD](
context: ActionContext<IUserState, IRootState>,
[AUTH_USER_STORE.ACTIONS.RESET_USER_PASSWORD](
context: ActionContext<IAuthUserState, IRootState>,
payload: IUserPasswordResetPayload
): void {
context.commit(ROOT_STORE.MUTATIONS.EMPTY_ERROR_MESSAGES)

View File

@ -1,4 +1,4 @@
export enum UserActions {
export enum AuthUserActions {
CHECK_AUTH_USER = 'CHECK_AUTH_USER',
DELETE_ACCOUNT = 'DELETE_ACCOUNT',
DELETE_PICTURE = 'DELETE_PICTURE',
@ -12,7 +12,7 @@ export enum UserActions {
UPDATE_USER_PREFERENCES = 'UPDATE_USER_PREFERENCES',
}
export enum UserGetters {
export enum AuthUserGetters {
AUTH_TOKEN = 'AUTH_TOKEN',
AUTH_USER_PROFILE = 'AUTH_USER_PROFILE',
IS_ADMIN = 'IS_ADMIN',
@ -20,7 +20,7 @@ export enum UserGetters {
USER_LOADING = 'USER_LOADING',
}
export enum UserMutations {
export enum AuthUserMutations {
CLEAR_AUTH_USER_TOKEN = 'CLEAR_AUTH_USER_TOKEN',
UPDATE_AUTH_TOKEN = 'UPDATE_AUTH_TOKEN',
UPDATE_AUTH_USER_PROFILE = 'UPDATE_AUTH_USER_PROFILE',

View File

@ -0,0 +1,27 @@
import { GetterTree } from 'vuex'
import { AUTH_USER_STORE } from '@/store/constants'
import {
IAuthUserGetters,
IAuthUserState,
} from '@/store/modules/authUser/types'
import { IRootState } from '@/store/modules/root/types'
export const getters: GetterTree<IAuthUserState, IRootState> &
IAuthUserGetters = {
[AUTH_USER_STORE.GETTERS.AUTH_TOKEN]: (state: IAuthUserState) => {
return state.authToken
},
[AUTH_USER_STORE.GETTERS.AUTH_USER_PROFILE]: (state: IAuthUserState) => {
return state.authUserProfile
},
[AUTH_USER_STORE.GETTERS.IS_AUTHENTICATED]: (state: IAuthUserState) => {
return state.authToken !== null
},
[AUTH_USER_STORE.GETTERS.IS_ADMIN]: (state: IAuthUserState) => {
return state.authUserProfile && state.authUserProfile.admin
},
[AUTH_USER_STORE.GETTERS.USER_LOADING]: (state: IAuthUserState) => {
return state.loading
},
}

View File

@ -0,0 +1,17 @@
import { Module } from 'vuex'
import { actions } from '@/store/modules/authUser/actions'
import { getters } from '@/store/modules/authUser/getters'
import { mutations } from '@/store/modules/authUser/mutations'
import { authUserState } from '@/store/modules/authUser/state.ts'
import { IAuthUserState } from '@/store/modules/authUser/types'
import { IRootState } from '@/store/modules/root/types'
const authUser: Module<IAuthUserState, IRootState> = {
state: authUserState,
actions,
getters,
mutations,
}
export default authUser

View File

@ -0,0 +1,33 @@
import { MutationTree } from 'vuex'
import { AUTH_USER_STORE } from '@/store/constants'
import {
IAuthUserState,
TAuthUserMutations,
} from '@/store/modules/authUser/types'
import { IUserProfile } from '@/types/user'
export const mutations: MutationTree<IAuthUserState> & TAuthUserMutations = {
[AUTH_USER_STORE.MUTATIONS.CLEAR_AUTH_USER_TOKEN](state: IAuthUserState) {
state.authToken = null
state.authUserProfile = <IUserProfile>{}
},
[AUTH_USER_STORE.MUTATIONS.UPDATE_AUTH_TOKEN](
state: IAuthUserState,
authToken: string
) {
state.authToken = authToken
},
[AUTH_USER_STORE.MUTATIONS.UPDATE_AUTH_USER_PROFILE](
state: IAuthUserState,
authUserProfile: IUserProfile
) {
state.authUserProfile = authUserProfile
},
[AUTH_USER_STORE.MUTATIONS.UPDATE_USER_LOADING](
state: IAuthUserState,
loading: boolean
) {
state.loading = loading
},
}

View File

@ -1,7 +1,7 @@
import { IUserState } from '@/store/modules/user/types'
import { IAuthUserState } from '@/store/modules/authUser/types'
import { IUserProfile } from '@/types/user'
export const userState: IUserState = {
export const authUserState: IAuthUserState = {
authToken: null,
authUserProfile: <IUserProfile>{},
loading: false,

View File

@ -0,0 +1,132 @@
import {
ActionContext,
CommitOptions,
DispatchOptions,
Store as VuexStore,
} from 'vuex'
import { AUTH_USER_STORE } from '@/store/constants'
import { IRootState } from '@/store/modules/root/types'
import {
IUserProfile,
ILoginOrRegisterData,
IUserDeletionPayload,
IUserPasswordPayload,
IUserPasswordResetPayload,
IUserPayload,
IUserPicturePayload,
IUserPreferencesPayload,
} from '@/types/user'
export interface IAuthUserState {
authToken: string | null
authUserProfile: IUserProfile
loading: boolean
}
export interface IAuthUserActions {
[AUTH_USER_STORE.ACTIONS.CHECK_AUTH_USER](
context: ActionContext<IAuthUserState, IRootState>
): void
[AUTH_USER_STORE.ACTIONS.GET_USER_PROFILE](
context: ActionContext<IAuthUserState, IRootState>
): void
[AUTH_USER_STORE.ACTIONS.LOGIN_OR_REGISTER](
context: ActionContext<IAuthUserState, IRootState>,
data: ILoginOrRegisterData
): void
[AUTH_USER_STORE.ACTIONS.LOGOUT](
context: ActionContext<IAuthUserState, IRootState>
): void
[AUTH_USER_STORE.ACTIONS.UPDATE_USER_PROFILE](
context: ActionContext<IAuthUserState, IRootState>,
payload: IUserPayload
): void
[AUTH_USER_STORE.ACTIONS.UPDATE_USER_PREFERENCES](
context: ActionContext<IAuthUserState, IRootState>,
payload: IUserPreferencesPayload
): void
[AUTH_USER_STORE.ACTIONS.UPDATE_USER_PICTURE](
context: ActionContext<IAuthUserState, IRootState>,
payload: IUserPicturePayload
): void
[AUTH_USER_STORE.ACTIONS.SEND_PASSWORD_RESET_REQUEST](
context: ActionContext<IAuthUserState, IRootState>,
payload: IUserPasswordPayload
): void
[AUTH_USER_STORE.ACTIONS.RESET_USER_PASSWORD](
context: ActionContext<IAuthUserState, IRootState>,
payload: IUserPasswordResetPayload
): void
[AUTH_USER_STORE.ACTIONS.DELETE_ACCOUNT](
context: ActionContext<IAuthUserState, IRootState>,
payload: IUserDeletionPayload
): void
[AUTH_USER_STORE.ACTIONS.DELETE_PICTURE](
context: ActionContext<IAuthUserState, IRootState>
): void
}
export interface IAuthUserGetters {
[AUTH_USER_STORE.GETTERS.AUTH_TOKEN](state: IAuthUserState): string | null
[AUTH_USER_STORE.GETTERS.AUTH_USER_PROFILE](
state: IAuthUserState
): IUserProfile
[AUTH_USER_STORE.GETTERS.IS_ADMIN](state: IAuthUserState): boolean
[AUTH_USER_STORE.GETTERS.IS_AUTHENTICATED](state: IAuthUserState): boolean
[AUTH_USER_STORE.GETTERS.USER_LOADING](state: IAuthUserState): boolean
}
export type TAuthUserMutations<S = IAuthUserState> = {
[AUTH_USER_STORE.MUTATIONS.CLEAR_AUTH_USER_TOKEN](state: S): void
[AUTH_USER_STORE.MUTATIONS.UPDATE_AUTH_TOKEN](
state: S,
authToken: string
): void
[AUTH_USER_STORE.MUTATIONS.UPDATE_AUTH_USER_PROFILE](
state: S,
authUserProfile: IUserProfile
): void
[AUTH_USER_STORE.MUTATIONS.UPDATE_USER_LOADING](
state: S,
loading: boolean
): void
}
export type TAuthUserStoreModule<S = IAuthUserState> = Omit<
VuexStore<S>,
'commit' | 'getters' | 'dispatch'
> & {
dispatch<K extends keyof IAuthUserActions>(
key: K,
payload?: Parameters<IAuthUserActions[K]>[1],
options?: DispatchOptions
): ReturnType<IAuthUserActions[K]>
} & {
getters: {
[K in keyof IAuthUserGetters]: ReturnType<IAuthUserGetters[K]>
}
} & {
commit<
K extends keyof TAuthUserMutations,
P extends Parameters<TAuthUserMutations[K]>[1]
>(
key: K,
payload?: P,
options?: CommitOptions
): ReturnType<TAuthUserMutations[K]>
}

View File

@ -1,5 +1,6 @@
import { Module, ModuleTree } from 'vuex'
import authUserModule from '@/store/modules/authUser'
import { actions } from '@/store/modules/root/actions'
import { getters } from '@/store/modules/root/getters'
import { mutations } from '@/store/modules/root/mutations'
@ -7,14 +8,13 @@ import { state } from '@/store/modules/root/state.ts'
import { IRootState } from '@/store/modules/root/types'
import sportsModule from '@/store/modules/sports'
import statsModule from '@/store/modules/statistics'
import userModule from '@/store/modules/user'
import usersModule from '@/store/modules/users'
import workoutsModule from '@/store/modules/workouts'
const modules: ModuleTree<IRootState> = {
authUserModule,
sportsModule,
statsModule,
userModule,
usersModule,
workoutsModule,
}

View File

@ -1,23 +0,0 @@
import { GetterTree } from 'vuex'
import { USER_STORE } from '@/store/constants'
import { IRootState } from '@/store/modules/root/types'
import { IUserGetters, IUserState } from '@/store/modules/user/types'
export const getters: GetterTree<IUserState, IRootState> & IUserGetters = {
[USER_STORE.GETTERS.AUTH_TOKEN]: (state: IUserState) => {
return state.authToken
},
[USER_STORE.GETTERS.AUTH_USER_PROFILE]: (state: IUserState) => {
return state.authUserProfile
},
[USER_STORE.GETTERS.IS_AUTHENTICATED]: (state: IUserState) => {
return state.authToken !== null
},
[USER_STORE.GETTERS.IS_ADMIN]: (state: IUserState) => {
return state.authUserProfile && state.authUserProfile.admin
},
[USER_STORE.GETTERS.USER_LOADING]: (state: IUserState) => {
return state.loading
},
}

View File

@ -1,17 +0,0 @@
import { Module } from 'vuex'
import { IRootState } from '@/store/modules/root/types'
import { actions } from '@/store/modules/user/actions'
import { getters } from '@/store/modules/user/getters'
import { mutations } from '@/store/modules/user/mutations'
import { userState } from '@/store/modules/user/state.ts'
import { IUserState } from '@/store/modules/user/types'
const user: Module<IUserState, IRootState> = {
state: userState,
actions,
getters,
mutations,
}
export default user

View File

@ -1,30 +0,0 @@
import { MutationTree } from 'vuex'
import { USER_STORE } from '@/store/constants'
import { IUserState, TUserMutations } from '@/store/modules/user/types'
import { IUserProfile } from '@/types/user'
export const mutations: MutationTree<IUserState> & TUserMutations = {
[USER_STORE.MUTATIONS.CLEAR_AUTH_USER_TOKEN](state: IUserState) {
state.authToken = null
state.authUserProfile = <IUserProfile>{}
},
[USER_STORE.MUTATIONS.UPDATE_AUTH_TOKEN](
state: IUserState,
authToken: string
) {
state.authToken = authToken
},
[USER_STORE.MUTATIONS.UPDATE_AUTH_USER_PROFILE](
state: IUserState,
authUserProfile: IUserProfile
) {
state.authUserProfile = authUserProfile
},
[USER_STORE.MUTATIONS.UPDATE_USER_LOADING](
state: IUserState,
loading: boolean
) {
state.loading = loading
},
}

View File

@ -1,124 +0,0 @@
import {
ActionContext,
CommitOptions,
DispatchOptions,
Store as VuexStore,
} from 'vuex'
import { USER_STORE } from '@/store/constants'
import { IRootState } from '@/store/modules/root/types'
import {
IUserProfile,
ILoginOrRegisterData,
IUserDeletionPayload,
IUserPasswordPayload,
IUserPasswordResetPayload,
IUserPayload,
IUserPicturePayload,
IUserPreferencesPayload,
} from '@/types/user'
export interface IUserState {
authToken: string | null
authUserProfile: IUserProfile
loading: boolean
}
export interface IUserActions {
[USER_STORE.ACTIONS.CHECK_AUTH_USER](
context: ActionContext<IUserState, IRootState>
): void
[USER_STORE.ACTIONS.GET_USER_PROFILE](
context: ActionContext<IUserState, IRootState>
): void
[USER_STORE.ACTIONS.LOGIN_OR_REGISTER](
context: ActionContext<IUserState, IRootState>,
data: ILoginOrRegisterData
): void
[USER_STORE.ACTIONS.LOGOUT](
context: ActionContext<IUserState, IRootState>
): void
[USER_STORE.ACTIONS.UPDATE_USER_PROFILE](
context: ActionContext<IUserState, IRootState>,
payload: IUserPayload
): void
[USER_STORE.ACTIONS.UPDATE_USER_PREFERENCES](
context: ActionContext<IUserState, IRootState>,
payload: IUserPreferencesPayload
): void
[USER_STORE.ACTIONS.UPDATE_USER_PICTURE](
context: ActionContext<IUserState, IRootState>,
payload: IUserPicturePayload
): void
[USER_STORE.ACTIONS.SEND_PASSWORD_RESET_REQUEST](
context: ActionContext<IUserState, IRootState>,
payload: IUserPasswordPayload
): void
[USER_STORE.ACTIONS.RESET_USER_PASSWORD](
context: ActionContext<IUserState, IRootState>,
payload: IUserPasswordResetPayload
): void
[USER_STORE.ACTIONS.DELETE_ACCOUNT](
context: ActionContext<IUserState, IRootState>,
payload: IUserDeletionPayload
): void
[USER_STORE.ACTIONS.DELETE_PICTURE](
context: ActionContext<IUserState, IRootState>
): void
}
export interface IUserGetters {
[USER_STORE.GETTERS.AUTH_TOKEN](state: IUserState): string | null
[USER_STORE.GETTERS.AUTH_USER_PROFILE](state: IUserState): IUserProfile
[USER_STORE.GETTERS.IS_ADMIN](state: IUserState): boolean
[USER_STORE.GETTERS.IS_AUTHENTICATED](state: IUserState): boolean
[USER_STORE.GETTERS.USER_LOADING](state: IUserState): boolean
}
export type TUserMutations<S = IUserState> = {
[USER_STORE.MUTATIONS.CLEAR_AUTH_USER_TOKEN](state: S): void
[USER_STORE.MUTATIONS.UPDATE_AUTH_TOKEN](state: S, authToken: string): void
[USER_STORE.MUTATIONS.UPDATE_AUTH_USER_PROFILE](
state: S,
authUserProfile: IUserProfile
): void
[USER_STORE.MUTATIONS.UPDATE_USER_LOADING](state: S, loading: boolean): void
}
export type TUserStoreModule<S = IUserState> = Omit<
VuexStore<S>,
'commit' | 'getters' | 'dispatch'
> & {
dispatch<K extends keyof IUserActions>(
key: K,
payload?: Parameters<IUserActions[K]>[1],
options?: DispatchOptions
): ReturnType<IUserActions[K]>
} & {
getters: {
[K in keyof IUserGetters]: ReturnType<IUserGetters[K]>
}
} & {
commit<
K extends keyof TUserMutations,
P extends Parameters<TUserMutations[K]>[1]
>(
key: K,
payload?: P,
options?: CommitOptions
): ReturnType<TUserMutations[K]>
}

View File

@ -2,7 +2,7 @@ import { ActionContext, ActionTree } from 'vuex'
import authApi from '@/api/authApi'
import router from '@/router'
import { ROOT_STORE, USER_STORE, WORKOUTS_STORE } from '@/store/constants'
import { ROOT_STORE, AUTH_USER_STORE, WORKOUTS_STORE } from '@/store/constants'
import { IRootState } from '@/store/modules/root/types'
import { WorkoutsMutations } from '@/store/modules/workouts/enums'
import {
@ -138,7 +138,7 @@ export const actions: ActionTree<IWorkoutsState, IRootState> &
.delete(`workouts/${payload.workoutId}`)
.then(() => {
context.commit(WORKOUTS_STORE.MUTATIONS.EMPTY_WORKOUT)
context.dispatch(USER_STORE.ACTIONS.GET_USER_PROFILE)
context.dispatch(AUTH_USER_STORE.ACTIONS.GET_USER_PROFILE)
router.push('/')
})
.catch((error) => {
@ -157,7 +157,7 @@ export const actions: ActionTree<IWorkoutsState, IRootState> &
authApi
.patch(`workouts/${payload.workoutId}`, payload.data)
.then(() => {
context.dispatch(USER_STORE.ACTIONS.GET_USER_PROFILE)
context.dispatch(AUTH_USER_STORE.ACTIONS.GET_USER_PROFILE)
context
.dispatch(WORKOUTS_STORE.ACTIONS.GET_WORKOUT_DATA, {
workoutId: payload.workoutId,
@ -199,7 +199,7 @@ export const actions: ActionTree<IWorkoutsState, IRootState> &
})
.then((res) => {
if (res.data.status === 'created') {
context.dispatch(USER_STORE.ACTIONS.GET_USER_PROFILE)
context.dispatch(AUTH_USER_STORE.ACTIONS.GET_USER_PROFILE)
const workout: IWorkout = res.data.data.workouts[0]
router.push(
res.data.data.workouts.length === 1
@ -225,7 +225,7 @@ export const actions: ActionTree<IWorkoutsState, IRootState> &
.post('workouts/no_gpx', payload)
.then((res) => {
if (res.data.status === 'created') {
context.dispatch(USER_STORE.ACTIONS.GET_USER_PROFILE)
context.dispatch(AUTH_USER_STORE.ACTIONS.GET_USER_PROFILE)
const workout: IWorkout = res.data.data.workouts[0]
router.push(`/workouts/${workout.id}`)
}

View File

@ -1,20 +1,20 @@
import { TAuthUserStoreModule } from '@/store/modules/authUser/types'
import { TRootStoreModule } from '@/store/modules/root/types'
import { TSportsStoreModule } from '@/store/modules/sports/types'
import { TStatisticsStoreModule } from '@/store/modules/statistics/types'
import { TUserStoreModule } from '@/store/modules/user/types'
import { TUsersStoreModule } from '@/store/modules/users/types'
import { TWorkoutsStoreModule } from '@/store/modules/workouts/types'
type StoreModules = {
authUserModule: TAuthUserStoreModule
rootModule: TRootStoreModule
sportsModule: TSportsStoreModule
statsModule: TStatisticsStoreModule
userModule: TUserStoreModule
usersModule: TUsersStoreModule
workoutsModule: TWorkoutsStoreModule
}
export type Store = TUserStoreModule<Pick<StoreModules, 'userModule'>> &
export type Store = TAuthUserStoreModule<Pick<StoreModules, 'authUserModule'>> &
TSportsStoreModule<Pick<StoreModules, 'sportsModule'>> &
TStatisticsStoreModule<Pick<StoreModules, 'statsModule'>> &
TWorkoutsStoreModule<Pick<StoreModules, 'workoutsModule'>> &