blob: a9f4f3153a83bf4fe6ab50a39ef66461ab0a9a9e [file] [log] [blame]
Ziwei Wang49765ec2023-02-13 16:35:32 -05001/*
2 * Copyright (C) 2022 Savoir-faire Linux Inc.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Affero General Public License as
6 * published by the Free Software Foundation; either version 3 of the
7 * License, or (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU Affero General Public License for more details.
13 *
14 * You should have received a copy of the GNU Affero General Public
15 * License along with this program. If not, see
16 * <https://www.gnu.org/licenses/>.
17 */
18import { Alert, AlertColor, AlertProps, AlertTitle, Snackbar, SnackbarProps } from '@mui/material';
19import { createContext, useCallback, useState } from 'react';
20import { useTranslation } from 'react-i18next';
21
22import { SetState, WithChildren } from '../utils/utils';
23type AlertSnackbarProps = AlertProps & {
24 severity: AlertColor;
25 open?: boolean;
26 snackBarProps?: Partial<SnackbarProps>;
27};
28
29export type AlertContent = {
30 messageI18nKey: AlertMessageKeys;
31 messageI18nContext?: object;
32 severity: AlertColor;
33 alertOpen: boolean;
34};
35
36export function AlertSnackbar({ severity, open, snackBarProps, children, ...alertProps }: AlertSnackbarProps) {
37 const { t } = useTranslation();
38
39 return (
40 <Snackbar
41 open={open}
42 {...snackBarProps}
43 anchorOrigin={{
44 vertical: 'top',
45 horizontal: 'center',
46 ...snackBarProps?.anchorOrigin,
47 }}
48 >
49 <Alert severity={severity} {...alertProps}>
50 <AlertTitle>{t('severity', { context: `${severity}` })}</AlertTitle>
51 {children}
52 </Alert>
53 </Snackbar>
54 );
55}
56
57type IAlertSnackbarContext = {
58 alertContent: AlertContent;
59 setAlertContent: SetState<AlertContent>;
Ziwei Wang45baf532023-02-16 15:41:49 -050060 closeAlert: () => void;
Ziwei Wang49765ec2023-02-13 16:35:32 -050061};
62
63const defaultAlertSnackbarContext: IAlertSnackbarContext = {
64 alertContent: {
65 messageI18nKey: '',
66 messageI18nContext: {},
67 severity: 'info',
68 alertOpen: false,
69 },
70 setAlertContent: () => {},
Ziwei Wang45baf532023-02-16 15:41:49 -050071 closeAlert: () => {},
Ziwei Wang49765ec2023-02-13 16:35:32 -050072};
73
Ziwei Wang3d633dd2023-02-21 16:57:48 -050074//Don't forget to add the cases in the switch statement below
Ziwei Wang49765ec2023-02-13 16:35:32 -050075type AlertMessageKeys =
Ziwei Wang3d633dd2023-02-21 16:57:48 -050076 | 'redirect_admin_setup_complete'
Ziwei Wang3d633dd2023-02-21 16:57:48 -050077 | 'password_input_helper_text_not_match'
78 | 'admin_page_setup_not_complete'
79 | 'admin_page_setup_complete'
Ziwei Wang49765ec2023-02-13 16:35:32 -050080 | 'missed_incoming_call'
81 | 'unknown_error_alert'
82 | 'username_input_helper_text_empty'
83 | 'password_input_helper_text_empty'
idillon440e5302023-08-03 20:25:24 -040084 | 'login_invalid_credentials'
Ziwei Wang49765ec2023-02-13 16:35:32 -050085 | 'registration_success'
86 | '';
87
88export const AlertSnackbarContext = createContext<IAlertSnackbarContext>(defaultAlertSnackbarContext);
89
90const AlertSnackbarProvider = ({ children }: WithChildren) => {
91 const { t } = useTranslation();
92 const [alertContent, setAlertContent] = useState<AlertContent>(defaultAlertSnackbarContext.alertContent);
93 const closeAlert = () => {
94 setAlertContent((prev) => {
95 return {
96 ...prev,
97 alertOpen: false,
98 };
99 });
100 };
101
102 //This is to explicitly let i18n know that these keys should be extracted
103 const getAlertMessageText = useCallback(
104 (messageI18nKey: AlertMessageKeys, messageI18nContext?: object): string => {
105 switch (messageI18nKey) {
106 case 'missed_incoming_call':
107 return t('missed_incoming_call', { ...messageI18nContext });
108 case 'unknown_error_alert':
109 return t('unknown_error_alert');
110 case 'username_input_helper_text_empty':
111 return t('username_input_helper_text_empty');
112 case 'password_input_helper_text_empty':
113 return t('password_input_helper_text_empty');
idillon440e5302023-08-03 20:25:24 -0400114 case 'login_invalid_credentials':
115 return t('login_invalid_credentials');
Ziwei Wang49765ec2023-02-13 16:35:32 -0500116 case 'registration_success':
117 return t('registration_success');
Ziwei Wang3d633dd2023-02-21 16:57:48 -0500118 case 'redirect_admin_setup_complete':
119 return t('redirect_admin_setup_complete');
Ziwei Wang3d633dd2023-02-21 16:57:48 -0500120 case 'password_input_helper_text_not_match':
121 return t('password_input_helper_text_not_match');
122 case 'admin_page_setup_not_complete':
123 return t('admin_page_setup_not_complete');
124 case 'admin_page_setup_complete':
125 return t('admin_page_setup_complete');
Ziwei Wang49765ec2023-02-13 16:35:32 -0500126 default:
127 return t('unknown_error_alert');
128 }
129 },
130 [t]
131 );
132
133 const value = {
134 alertContent,
135 setAlertContent,
Ziwei Wang45baf532023-02-16 15:41:49 -0500136 closeAlert,
Ziwei Wang49765ec2023-02-13 16:35:32 -0500137 };
138
139 return (
140 <>
141 <AlertSnackbar severity={alertContent.severity} open={alertContent.alertOpen} onClose={closeAlert}>
142 {getAlertMessageText(alertContent.messageI18nKey, alertContent.messageI18nContext)}
143 </AlertSnackbar>
144 <AlertSnackbarContext.Provider value={value}>{children}</AlertSnackbarContext.Provider>
145 </>
146 );
147};
148
149export default AlertSnackbarProvider;