-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.tsx
67 lines (52 loc) · 1.79 KB
/
utils.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import { enqueueSnackbar } from "notistack"
import { SnackNfo } from "../types/SnackNfo"
import {
DEFAULT_FONTSIZE_NORMAL, DEFAULT_FONTSIZE_NORMAL2, DEFAULT_FONTWEIGHT_SEMIBOLD,
DEFAULT_FONTWEIGHT_XBOLD
} from "../constants/general"
import { Box, Typography } from "@mui/material"
import { AxiosError } from "axios"
export const firstLetter = (str: string | undefined, capitalize: boolean = false) => {
if (str && str.length > 0) {
if (capitalize)
return str[0].toUpperCase()
return str[0]
}
}
export const nullOrUndefined = (x: any) => x === null || x === undefined
export const handleApiException = async (ex: AxiosError, prefixMsg: string = 'Error') => {
let msgs = [`${prefixMsg}: ${ex.response?.statusText}`]
const detail = (ex.response?.data as any)?.detail
if (detail)
msgs.push(`Detail: ${detail}`)
setSnack({
title: prefixMsg,
msg: msgs,
type: "error"
})
}
export function delay(ms: number) {
return new Promise(resolve => setTimeout(resolve, ms));
}
export const setSnack = (nfo: SnackNfo) => {
enqueueSnackbar({
message: <Box>
{nfo.title && <Typography
fontSize={DEFAULT_FONTSIZE_NORMAL2}
fontWeight={DEFAULT_FONTWEIGHT_XBOLD}>
{nfo.title}
</Typography>}
{nfo.msg.map((msg, msgIdx) =>
<Typography
fontSize={DEFAULT_FONTSIZE_NORMAL}
fontWeight={DEFAULT_FONTWEIGHT_SEMIBOLD}
key={`snack-mex-${msgIdx}`}>
{msg}
</Typography>
)}
</Box>,
variant: nfo.type,
preventDuplicate: true,
autoHideDuration: nfo.durationMs === null ? null : (nfo.durationMs ?? 6000)
})
}