Skip to content

Commit 8100d3f

Browse files
committed
feat: refactor hook useAlertBar and add close icon
1 parent d585f4a commit 8100d3f

File tree

5 files changed

+72
-73
lines changed

5 files changed

+72
-73
lines changed

src/components/homepage/DisplayRandomPicture.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import { useSharedUtilContext } from '@/hooks/useSharedUtilContext';
1616
import SubmitButton from '@/components/shared/SubmitButton';
1717

1818
import { FetchApiContext } from '@/constants';
19+
import { consoleLog } from '@/utils/shared/console-log';
1920
import { getApiResponse } from '@/utils/shared/get-api-response';
2021

2122
const DisplayRandomPicture = () => {
@@ -52,6 +53,9 @@ const DisplayRandomPicture = () => {
5253
setAlertBarProps({
5354
message: 'A random picture fetched successfully',
5455
severity: 'info',
56+
onClose: () => {
57+
consoleLog('Alert bar closed');
58+
},
5559
});
5660
} catch (error) {
5761
const errorMsg =

src/components/shared/AlertBar.tsx

Lines changed: 0 additions & 60 deletions
This file was deleted.

src/hooks/useAlertBar.tsx

Lines changed: 63 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,77 @@
11
'use client';
22

3+
import CloseIcon from '@mui/icons-material/Close';
4+
import {
5+
Alert,
6+
AlertColor,
7+
IconButton,
8+
Slide,
9+
Snackbar,
10+
SnackbarOrigin,
11+
} from '@mui/material';
312
import React, { useState } from 'react';
413

5-
import { AlertBar, AlertBarProps } from '@/components/shared/AlertBar';
14+
export interface AlertBarProps {
15+
message: React.ReactNode;
16+
onClose?: () => void;
17+
severity?: AlertColor;
18+
vertical?: SnackbarOrigin['vertical'];
19+
horizontal?: SnackbarOrigin['horizontal'];
20+
autoHideSeconds?: number;
21+
transitionSeconds?: number;
22+
}
623

24+
// my personal alert bar, feel free to use it, examples are in ReactHookForm.tsx
725
export const useAlertBar = () => {
826
const [alertBarProps, setAlertBarProps] = useState<AlertBarProps>({
927
message: '',
1028
severity: 'info',
1129
});
30+
const onAlertClose = () => {
31+
setAlertBarProps({ message: '' });
32+
alertBarProps.onClose?.();
33+
};
34+
35+
const renderAlertBar = () => {
36+
const {
37+
message,
38+
severity,
39+
vertical = 'bottom',
40+
horizontal = 'center',
41+
autoHideSeconds = 5,
42+
transitionSeconds = 0.5,
43+
} = alertBarProps;
1244

13-
const renderAlertBar = () => (
14-
<AlertBar
15-
onClose={() => setAlertBarProps({ message: '' })}
16-
{...alertBarProps}
17-
/>
18-
);
45+
return message ? (
46+
<Snackbar
47+
anchorOrigin={{ vertical, horizontal }}
48+
open={!!message}
49+
onClose={onAlertClose}
50+
autoHideDuration={autoHideSeconds * 1000}
51+
TransitionComponent={(props) => <Slide {...props} direction='up' />}
52+
transitionDuration={transitionSeconds * 1000}
53+
action={
54+
<IconButton
55+
size='small'
56+
aria-label='close'
57+
color='inherit'
58+
onClick={onAlertClose}
59+
>
60+
<CloseIcon fontSize='small' />
61+
</IconButton>
62+
}
63+
>
64+
<Alert
65+
onClose={onAlertClose}
66+
severity={severity}
67+
variant='filled'
68+
sx={{ width: '100%' }}
69+
>
70+
{message}
71+
</Alert>
72+
</Snackbar>
73+
) : null;
74+
};
1975

2076
return {
2177
setAlertBarProps,

src/hooks/useConfirmDialog.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ const StyledContentDiv = styled.div`
4444
}
4545
`;
4646

47+
// my personal confirmation dialog, feel free to use it, examples are in ReactHookForm.tsx
4748
const useConfirmationDialog = () => {
4849
const defaultDialogProps: ConfirmationDialogProps = {
4950
title: '',

src/hooks/useSharedUtilContext.tsx

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,13 @@
22

33
import React, { createContext, ReactNode, useContext } from 'react';
44

5-
import { useAlertBar } from '@/hooks/useAlertBar';
5+
import { AlertBarProps, useAlertBar } from '@/hooks/useAlertBar';
66
import useConfirmationDialog, {
77
ConfirmationDialogProps,
88
} from '@/hooks/useConfirmDialog';
99

10-
import { AlertBarProps } from '@/components/shared/AlertBar';
11-
12-
export const OUTSIDE_CLIENT_PROVIDER_ERROR =
13-
'Cannot be used outside ClientProvider!';
10+
export const OUTSIDE_SHARED_UTIL_PROVIDER_ERROR =
11+
'Cannot be used outside SharedUtilProvider!';
1412

1513
export interface SharedUtilContextType {
1614
setAlertBarProps: (props: AlertBarProps) => void;
@@ -24,7 +22,7 @@ export const SharedUtilContext = createContext<
2422
export const useSharedUtilContext = (): SharedUtilContextType => {
2523
const context = useContext(SharedUtilContext);
2624
if (context === undefined) {
27-
throw new Error(OUTSIDE_CLIENT_PROVIDER_ERROR);
25+
throw new Error(OUTSIDE_SHARED_UTIL_PROVIDER_ERROR);
2826
}
2927

3028
return context as SharedUtilContextType;

0 commit comments

Comments
 (0)