one-click-accessibility/modules/settings/assets/js/contexts/plugin-settings.js
Nirbhay Singh 5fc9cf67f7
[APP-1159] Add mismatch URL flow (#210)
* update: convert imports to named imports

* add: function to check if current screen is settings page

* update: rename elementor logo to app logo

* add: url mismatch flow and components

* update: remove obsolete code

* Update modules/connect/rest/authorize.php

Co-authored-by: Pavlo Kniazevych <139438463+pkniazevych@users.noreply.github.com>

* Update modules/settings/module.php

Co-authored-by: Pavlo Kniazevych <139438463+pkniazevych@users.noreply.github.com>

* fix: modal was not closing

* update: remove url mismatch notice

* update: mismatch modal and rendering logic

* add: toast notifications for errors

* update: convert components into styled components

* update: remove bottom border from the dialog

* update: text copy

* fix: logo alignment

* update: renamed styled component

---------

Co-authored-by: Pavlo Kniazevych <139438463+pkniazevych@users.noreply.github.com>
2025-03-11 12:10:53 +02:00

60 lines
1.4 KiB
JavaScript

import { useToastNotification } from '@ea11y/hooks';
import {
createContext,
useCallback,
useContext,
useEffect,
useState,
} from '@wordpress/element';
import { __ } from '@wordpress/i18n';
import API from '../api';
const PluginSettingsContext = createContext({});
export const PluginSettingsProvider = ({ children }) => {
const { error } = useToastNotification();
const [pluginSettings, setPluginSettings] = useState();
const [loaded, setLoaded] = useState(false);
const refreshPluginSettings = useCallback(() => {
API.getPluginSettings()
.then((settings) => {
if ('isConnected' in settings) {
settings.isConnected = Boolean(settings.isConnected);
}
if ('closePostConnectModal' in settings) {
settings.closePostConnectModal = Boolean(
settings.closePostConnectModal,
);
}
if ('isUrlMismatch' in settings) {
settings.isUrlMismatch = Boolean(settings.isUrlMismatch);
}
setPluginSettings(settings);
setLoaded(true);
})
.catch(() => {
error(__('An error occurred.', 'pojo-accessibility'));
setLoaded(true);
});
}, []);
useEffect(() => {
refreshPluginSettings();
}, [refreshPluginSettings]);
return (
<PluginSettingsContext.Provider
value={{ ...pluginSettings, loaded, refreshPluginSettings }}
>
{children}
</PluginSettingsContext.Provider>
);
};
export const usePluginSettingsContext = () => {
return useContext(PluginSettingsContext);
};