Client - display info and errors on workout edition

This commit is contained in:
Sam
2021-09-29 16:34:48 +02:00
parent 6bfcb24133
commit 997469d959
10 changed files with 182 additions and 28 deletions

View File

@ -0,0 +1,14 @@
const suffixes = ['bytes', 'KB', 'MB', 'GB', 'TB']
export const getReadableFileSize = (
fileSize: number,
asText = true
): string | Record<string, string> => {
const i = Math.floor(Math.log(fileSize) / Math.log(1024))
if (!fileSize) {
return asText ? '0 bytes' : { size: '0', suffix: 'bytes' }
}
const size = (fileSize / Math.pow(1024, i)).toFixed(1)
const suffix = suffixes[i]
return asText ? `${size}${suffix}` : { size, suffix }
}

View File

@ -17,8 +17,7 @@ export const getApiUrl = (): string => {
// 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, '')
const removeErrorDot = (text: string): string =>
text.replace(/^Error\./gm, 'Error,')
const replaceInternalDots = (text: string): string => text.replace(/\./gm, ',')
export const handleError = (
context:
@ -33,14 +32,16 @@ export const handleError = (
let errorMessages = !error
? msg
: error.response
? error.response.data.message
? error.response.status === 413
? 'File size is greater than the allowed size'
: error.response.data.message
? error.response.data.message
: msg
: error.message
? error.message
: msg
errorMessages = removeLastEndOfLine(errorMessages)
errorMessages = removeErrorDot(errorMessages)
errorMessages = replaceInternalDots(errorMessages)
context.commit(
ROOT_STORE.MUTATIONS.SET_ERROR_MESSAGES,
errorMessages.includes('\n')