-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(error handling): add all api error message from backend (#105)
- Loading branch information
1 parent
79455b4
commit 5c02f13
Showing
11 changed files
with
292 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,8 @@ | ||
# Changelog | ||
|
||
## Unreleased | ||
- Add error handling of api errors | ||
|
||
## 1.6.0-RC2 | ||
|
||
### Bugfix | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,176 @@ | ||
/******************************************************************************** | ||
* Copyright (c) 2021, 2024 Contributors to the Eclipse Foundation | ||
* | ||
* See the NOTICE file(s) distributed with this work for additional | ||
* information regarding copyright ownership. | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Apache License, Version 2.0 which is available at | ||
* https://www.apache.org/licenses/LICENSE-2.0. | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
* License for the specific language governing permissions and limitations | ||
* under the License. | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
********************************************************************************/ | ||
|
||
import { Box, IconButton, Slide } from '@mui/material' | ||
import { Close } from '@mui/icons-material' | ||
import { type SlideProps } from '@mui/material/Slide/Slide' | ||
import { useCallback, useEffect, useRef, useState } from 'react' | ||
import Snackbar from '@mui/material/Snackbar' | ||
import CheckIcon from '@mui/icons-material/Check' | ||
import ErrorOutlineIcon from '@mui/icons-material/ErrorOutline' | ||
|
||
const AUTO_CLOSE_DELAY_MS = 3000 | ||
|
||
const SlideTransition = (props: SlideProps) => ( | ||
<Slide {...props} direction="left" /> | ||
) | ||
|
||
export enum SeverityType { | ||
SUCCESS = 'success', | ||
ERROR = 'error', | ||
} | ||
|
||
export interface PageSnackbarProps { | ||
severity?: SeverityType | ||
open: boolean | ||
onCloseNotification?: () => void | ||
title?: string | JSX.Element | ||
description?: string | JSX.Element | ||
showIcon?: boolean | ||
autoClose?: boolean | ||
} | ||
|
||
export const PageSnackbar = ({ | ||
severity = SeverityType.SUCCESS, | ||
onCloseNotification, | ||
open, | ||
autoClose, | ||
title, | ||
description, | ||
showIcon = true, | ||
...props | ||
}: PageSnackbarProps) => { | ||
const [isOpen, setIsOpen] = useState(open) | ||
|
||
const autoCloseTimeoutRef = useRef<ReturnType<typeof setTimeout>>() | ||
|
||
useEffect(() => { | ||
setIsOpen(open) | ||
}, [open]) | ||
|
||
const cancelAutoClose = useCallback(() => { | ||
clearTimeout(autoCloseTimeoutRef.current) | ||
}, []) | ||
|
||
const doClose = useCallback(() => { | ||
cancelAutoClose() | ||
setIsOpen(false) | ||
|
||
onCloseNotification?.() | ||
}, [cancelAutoClose, onCloseNotification]) | ||
|
||
const handleAutoClose = useCallback(() => { | ||
cancelAutoClose() | ||
|
||
if (autoClose) { | ||
autoCloseTimeoutRef.current = setTimeout(doClose, AUTO_CLOSE_DELAY_MS) | ||
} | ||
}, [autoClose, cancelAutoClose, doClose]) | ||
|
||
useEffect(handleAutoClose, [autoClose, handleAutoClose]) | ||
|
||
const renderIcon = () => { | ||
switch (severity) { | ||
case 'success': | ||
return <CheckIcon sx={{ color: '#00AA55' }} /> | ||
case 'error': | ||
return <ErrorOutlineIcon sx={{ color: '#D91E18' }} /> | ||
} | ||
} | ||
|
||
return ( | ||
<Snackbar | ||
anchorOrigin={{ vertical: 'top', horizontal: 'right' }} | ||
open={isOpen} | ||
key={'bottom right'} | ||
TransitionComponent={SlideTransition} | ||
{...props} | ||
onMouseEnter={cancelAutoClose} | ||
onMouseLeave={handleAutoClose} | ||
message={ | ||
<Box sx={{ display: 'flex', width: '100%', alignItems: 'flex-start' }}> | ||
{showIcon && ( | ||
<Box | ||
sx={{ | ||
flex: '0 0 auto', | ||
marginRight: 2, | ||
marginTop: '2px', | ||
marginBottom: '-2px', | ||
}} | ||
> | ||
{renderIcon()} | ||
</Box> | ||
)} | ||
<Box sx={{ flex: '1 1 auto', alignSelf: 'center' }}> | ||
{title && ( | ||
<Box | ||
component="span" | ||
sx={{ marginRight: 0.5, fontWeight: 'bold' }} | ||
> | ||
{title} | ||
</Box> | ||
)} | ||
{description} | ||
</Box> | ||
<Box | ||
sx={{ | ||
flex: '0 0 auto', | ||
marginLeft: 1.5, | ||
marginTop: '2px', | ||
marginRight: '-3px', | ||
}} | ||
> | ||
<IconButton size="small" aria-label="close" onClick={doClose}> | ||
<Close fontSize="small" sx={{ color: 'text.primary' }} /> | ||
</IconButton> | ||
</Box> | ||
</Box> | ||
} | ||
sx={{ | ||
'.MuiSnackbarContent-root': { | ||
width: '390px', | ||
typography: 'body3', | ||
backgroundColor: 'common.white', | ||
color: 'text.primary', | ||
borderWidth: '1px', | ||
borderStyle: 'solid', | ||
borderColor: 'action.disabledBackground', | ||
borderRadius: '8px', | ||
boxShadow: '0px 10px 20px 0px rgba(80, 80, 80, 0.3)', | ||
}, | ||
'.MuiSnackbarContent-message': { | ||
width: '100%', | ||
}, | ||
}} | ||
/> | ||
) | ||
} | ||
|
||
|
||
export const Notify = ({ message }: { message: string }) => { | ||
return ( | ||
<PageSnackbar | ||
open={true} | ||
severity={SeverityType.ERROR} | ||
description={message} | ||
showIcon={true} | ||
autoClose={true} | ||
/> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.