Client - display info and errors on workout edition
This commit is contained in:
14
fittrackee_client/src/utils/files.ts
Normal file
14
fittrackee_client/src/utils/files.ts
Normal 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 }
|
||||
}
|
@ -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')
|
||||
|
Reference in New Issue
Block a user