Client - init utils methods needed for charts

This commit is contained in:
Sam
2021-08-22 20:16:04 +02:00
parent 0c516b9986
commit 3b8ac44433
15 changed files with 888 additions and 20 deletions

View File

@ -0,0 +1,47 @@
import { AxiosError } from 'axios'
import { ActionContext } from 'vuex'
import { ROOT_STORE } from '@/store/constants'
import { IRootState } from '@/store/modules/root/interfaces'
import { IStatisticsState } from '@/store/modules/statistics/interfaces'
import { IUserState } from '@/store/modules/user/interfaces'
export const getApiUrl = (): string => {
return process.env.NODE_ENV === 'production'
? '/api'
: `${process.env.VUE_APP_API_URL}/api/`
}
// TODO: update api error messages to remove these workarounds
const removeLastEndOfLine = (text: string): string => text.replace(/\n$/gm, '')
const removeLastDot = (text: string): string => text.replace(/\.$/gm, '')
export const handleError = (
context:
| ActionContext<IRootState, IRootState>
| ActionContext<IUserState, IRootState>
| ActionContext<IStatisticsState, IRootState>,
error: AxiosError | null,
msg = 'UNKNOWN'
): void => {
let errorMessages = !error
? msg
: error.response
? error.response.data.message
? error.response.data.message
: msg
: error.message
? error.message
: msg
errorMessages = removeLastEndOfLine(errorMessages)
context.commit(
ROOT_STORE.MUTATIONS.SET_ERROR_MESSAGES,
errorMessages.includes('\n')
? errorMessages
.split('\n')
.map((m: string) => `api.ERROR.${removeLastDot(m)}`)
: `api.ERROR.${removeLastDot(errorMessages)}`
)
}
export const capitalize = (text: string): string =>
text.charAt(0).toUpperCase() + text.slice(1)