Skip to content

Commit

Permalink
update: refactor notification component and context
Browse files Browse the repository at this point in the history
  • Loading branch information
nirbhayel committed Nov 10, 2024
1 parent 056464c commit fc0f885
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 46 deletions.
14 changes: 8 additions & 6 deletions modules/settings/assets/js/admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { StrictMode, Fragment, createRoot } from '@wordpress/element';
import App from './app';
import AdminTopBar from './components/admin-top-bar';
import { PluginSettingsProvider } from './contexts/plugin-settings-context';
import { SettingsProvider } from './hooks';
import { SettingsProvider, NotificationsProvider } from './hooks';

const rootNode = document.getElementById( 'ea11y-app' );
const topBarNode = document.getElementById( 'ea11y-app-top-bar' );
Expand All @@ -23,10 +23,12 @@ topBar.render(

root.render(
<AppWrapper>
<SettingsProvider>
<PluginSettingsProvider>
<App />
</PluginSettingsProvider>
</SettingsProvider>
<NotificationsProvider>
<SettingsProvider>
<PluginSettingsProvider>
<App />
</PluginSettingsProvider>
</SettingsProvider>
</NotificationsProvider>
</AppWrapper>,
);
7 changes: 4 additions & 3 deletions modules/settings/assets/js/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,17 @@ import DirectionProvider from '@elementor/ui/DirectionProvider';
import { ThemeProvider } from '@elementor/ui/styles';
import { ConnectModal, Notifications } from './components';
import { usePluginSettingsContext } from './contexts/plugin-settings-context';
import { useSettings } from './hooks';
import { useNotificationSettings } from './hooks';

const App = () => {
const { isConnected } = usePluginSettingsContext();
const {
notificationMessage,
notificationType,
} = useSettings();
} = useNotificationSettings();

return (
<DirectionProvider rtl={ false /* Add RTL detection in settings */ }>
<DirectionProvider rtl={ false /* TODO:Add RTL detection in settings */ }>
<ThemeProvider colorScheme="light">
{ ! isConnected && <ConnectModal /> }
<Notifications message={ notificationMessage } type={ notificationType } />
Expand Down
8 changes: 5 additions & 3 deletions modules/settings/assets/js/components/notifications/index.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import Alert from '@elementor/ui/Alert';
import Snackbar from '@elementor/ui/Snackbar';
import { useSettings } from '../../hooks';
import { useNotificationSettings } from '../../hooks';

const Notifications = ( { type, message } ) => {
const {
showNotification,
setShowNotification,
setNotificationMessage,
setNotificationType,
} = useSettings();
} = useNotificationSettings();

const closeNotification = () => {
setShowNotification( ! showNotification );
Expand All @@ -24,7 +24,9 @@ const Notifications = ( { type, message } ) => {
anchorOrigin={ { vertical: 'bottom', horizontal: 'right' } }
sx={ { zIndex: 99999 } }
>
<Alert onClose={ () => setShowNotification( ! showNotification ) } severity={ type } variant="filled" >
<Alert onClose={ () => setShowNotification( ! showNotification ) }
severity={ type }
variant="filled" >
{ message }
</Alert>
</Snackbar>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { createContext, useCallback, useContext, useEffect, useState } from '@wordpress/element';
import { __ } from '@wordpress/i18n';
import API from '../api';
import { useToastNotification } from '../hooks/use-settings';
import { useToastNotification } from '../hooks';

const PluginSettingsContext = createContext( {} );

Expand Down
5 changes: 4 additions & 1 deletion modules/settings/assets/js/hooks/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
export { default as useAuth } from './use-auth';
export { default as useModal } from './use-modal';
export { useSettings } from './use-settings';
export { useToastNotification } from './use-settings';
export { default as SettingsProvider } from './use-settings';
export { default as useStorage } from './use-storage';
export { useToastNotification } from './use-notifications';
export { default as NotificationsProvider } from './use-notifications';
export { useNotificationSettings } from './use-notifications';

55 changes: 55 additions & 0 deletions modules/settings/assets/js/hooks/use-notifications.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { useState, createContext, useContext } from '@wordpress/element';

const NotificationsContext = createContext( undefined );

export function useNotificationSettings() {
return useContext( NotificationsContext );
}

export const NotificationsProvider = ( { children } ) => {
const [ showNotification, setShowNotification ] = useState( false );
const [ notificationMessage, setNotificationMessage ] = useState( '' );
const [ notificationType, setNotificationType ] = useState( '' );

return (
<NotificationsContext.Provider
value={ {
showNotification,
setShowNotification,
notificationMessage,
setNotificationMessage,
notificationType,
setNotificationType,
} }
>
{ children }
</NotificationsContext.Provider>
);
};

export const useToastNotification = () => {
const {
setNotificationMessage,
setNotificationType,
setShowNotification,
} = useContext( NotificationsContext );

const error = ( message ) => {
setNotificationMessage( message );
setNotificationType( 'error' );
setShowNotification( true );
};

const success = ( message ) => {
setNotificationMessage( message );
setNotificationType( 'success' );
setShowNotification( true );
};

return {
success,
error,
};
};

export default NotificationsProvider;
35 changes: 3 additions & 32 deletions modules/settings/assets/js/hooks/use-settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,46 +10,17 @@ export function useSettings() {
}

const SettingsProvider = ( { children } ) => {
// Notification
const [ showNotification, setShowNotification ] = useState( false );
const [ notificationMessage, setNotificationMessage ] = useState( '' );
const [ notificationType, setNotificationType ] = useState( '' );

const [ test, setTest ] = useState( 'Test' );
return (
<SettingsContext.Provider
value={ {
showNotification,
setShowNotification,
notificationMessage,
setNotificationMessage,
notificationType,
setNotificationType,
test,
setTest,
} }
>
{ children }
</SettingsContext.Provider>
);
};

export const useToastNotification = () => {
const { setNotificationMessage, setNotificationType, setShowNotification } = useContext( SettingsContext );

const error = ( message ) => {
setNotificationMessage( message );
setNotificationType( 'error' );
setShowNotification( true );
};

const success = ( message ) => {
setNotificationMessage( message );
setNotificationType( 'success' );
setShowNotification( true );
};

return {
success,
error,
};
};

export default SettingsProvider;

0 comments on commit fc0f885

Please sign in to comment.