diff --git a/modules/settings/assets/js/app.js b/modules/settings/assets/js/app.js index d526d4a..1ebec70 100644 --- a/modules/settings/assets/js/app.js +++ b/modules/settings/assets/js/app.js @@ -11,7 +11,11 @@ import { AdminTopBar, BottomBar, } from '@ea11y/components'; -import { useNotificationSettings, useSettings } from '@ea11y/hooks'; +import { + useNotificationSettings, + useSettings, + useSavedSettings, +} from '@ea11y/hooks'; import { usePluginSettingsContext } from './contexts/plugin-settings'; import { Sidebar } from './layouts/sidebar'; @@ -19,6 +23,7 @@ const App = () => { const { isConnected } = usePluginSettingsContext(); const { notificationMessage, notificationType } = useNotificationSettings(); const { selectedMenu } = useSettings(); + useSavedSettings(); // Accessing the selected menu item const selectedParent = MenuItems[selectedMenu.parent]; diff --git a/modules/settings/assets/js/hooks/index.js b/modules/settings/assets/js/hooks/index.js index bbb496d..19a8d4e 100644 --- a/modules/settings/assets/js/hooks/index.js +++ b/modules/settings/assets/js/hooks/index.js @@ -8,3 +8,4 @@ export { NotificationsProvider } from './use-notifications'; export { useNotificationSettings } from './use-notifications'; export { useIconPosition } from './use-icon-positioon'; export { useIconDesign } from './use-icon-design'; +export { useSavedSettings } from './use-saved-settings'; diff --git a/modules/settings/assets/js/hooks/use-saved-settings.js b/modules/settings/assets/js/hooks/use-saved-settings.js new file mode 100644 index 0000000..ad6f87f --- /dev/null +++ b/modules/settings/assets/js/hooks/use-saved-settings.js @@ -0,0 +1,30 @@ +import { useSettings } from '@ea11y/hooks'; +import { store as coreDataStore } from '@wordpress/core-data'; +import { useSelect } from '@wordpress/data'; +import { useEffect } from '@wordpress/element'; + +export const useSavedSettings = () => { + const { setIconDesign, setIconPosition } = useSettings(); + + const result = useSelect((select) => { + return { + data: select(coreDataStore).getEntityRecord('root', 'site'), + hasFinishedResolution: select(coreDataStore).hasFinishedResolution( + 'getEntityRecord', + ['root', 'site'], + ), + }; + }, []); + + useEffect(() => { + if (result.hasFinishedResolution) { + if (result?.data?.a11y_widget_icon_settings?.style) { + setIconDesign(result.data.a11y_widget_icon_settings.style); + } + + if (result?.data?.a11y_widget_icon_settings?.position) { + setIconPosition(result.data.a11y_widget_icon_settings.position); + } + } + }, [result.hasFinishedResolution]); +};