1
0
Fork 0
mirror of https://github.com/elementor/hello-theme.git synced 2026-07-28 15:06:20 +08:00
hello-theme/modules/admin-home/assets/js/components/settings/settings-provider.js
Rami Yushuvaev aa9eed2933
Some checks failed
Build / Build theme (push) Failing after 1s
Lint / ESLint (push) Failing after 0s
PHPUnit / File Diff (push) Failing after 1s
Lint / PHPCS (push) Failing after 41s
PHPUnit / PHPUnit - WordPress 6.8 - PHP version 7.4 (push) Has been skipped
PHPUnit / PHPUnit - WordPress 6.9 - PHP version 7.4 (push) Has been skipped
PHPUnit / PHPUnit - WordPress latest - PHP version 7.4 (push) Has been skipped
PHPUnit / PHPUnit - WordPress nightly - PHP version 7.4 (push) Has been skipped
PHPUnit / PHPUnit - WordPress 6.8 - PHP version 8.0 (push) Has been skipped
PHPUnit / PHPUnit - WordPress 6.9 - PHP version 8.0 (push) Has been skipped
PHPUnit / PHPUnit - WordPress latest - PHP version 8.0 (push) Has been skipped
PHPUnit / PHPUnit - WordPress nightly - PHP version 8.0 (push) Has been skipped
PHPUnit / PHPUnit - WordPress 6.8 - PHP version 8.1 (push) Has been skipped
PHPUnit / PHPUnit - WordPress 6.9 - PHP version 8.1 (push) Has been skipped
PHPUnit / PHPUnit - WordPress latest - PHP version 8.1 (push) Has been skipped
PHPUnit / PHPUnit - WordPress nightly - PHP version 8.1 (push) Has been skipped
PHPUnit / PHPUnit - WordPress 6.8 - PHP version 8.2 (push) Has been skipped
PHPUnit / PHPUnit - WordPress 6.9 - PHP version 8.2 (push) Has been skipped
PHPUnit / PHPUnit - WordPress latest - PHP version 8.2 (push) Has been skipped
PHPUnit / PHPUnit - WordPress nightly - PHP version 8.2 (push) Has been skipped
PHPUnit / PHPUnit - WordPress 6.8 - PHP version 8.3 (push) Has been skipped
PHPUnit / PHPUnit - WordPress 6.9 - PHP version 8.3 (push) Has been skipped
PHPUnit / PHPUnit - WordPress latest - PHP version 8.3 (push) Has been skipped
PHPUnit / PHPUnit - WordPress nightly - PHP version 8.3 (push) Has been skipped
PHPUnit / PHPUnit - Test Results (push) Successful in 2s
Internal: Setup prettier and fix all lint/format issues [TMZ-1051] (#671)
* Setup prettier

* Fix lint/format issues
2026-07-01 00:41:16 -07:00

85 lines
1.9 KiB
JavaScript

import { createContext, useEffect, useState } from 'react';
import apiFetch from '@wordpress/api-fetch';
import { __ } from '@wordpress/i18n';
import { dispatch } from '@wordpress/data';
export const SettingsContext = createContext();
export const SettingsProvider = ({ children }) => {
const [isLoading, setIsLoading] = useState(true);
const [themeSettings, setThemeSettings] = useState({});
const [settingsUpdated, setSettingsUpdated] = useState(false);
const [whatsNew, setWhatsNew] = useState([]);
const updateSetting = (settingsName, settingsValue) => {
setThemeSettings({
...themeSettings,
[settingsName]: settingsValue,
});
setSettingsUpdated(true);
};
useEffect(() => {
if (!settingsUpdated) {
return;
}
setIsLoading(true);
apiFetch({
path: '/elementor-hello-elementor/v1/theme-settings',
method: 'POST',
data: { settings: themeSettings },
})
.then(async () => {
dispatch('core/notices').createNotice(
'success',
__('Settings Saved', 'hello-elementor'),
{
type: 'snackbar',
isDismissible: true,
},
);
})
.catch(() => {
dispatch('core/notices').createNotice(
'error',
__('Error when saving settings', 'hello-elementor'),
{
type: 'snackbar',
isDismissible: true,
},
);
})
.finally(() => {
setIsLoading(false);
setSettingsUpdated(false);
});
}, [settingsUpdated, themeSettings]);
useEffect(() => {
Promise.all([
apiFetch({ path: '/elementor-hello-elementor/v1/theme-settings' }),
apiFetch({ path: '/elementor-hello-elementor/v1/whats-new' }),
])
.then(([settings, whatsNewData]) => {
setWhatsNew(whatsNewData);
setThemeSettings(settings.settings);
})
.finally(() => {
setIsLoading(false);
});
}, []);
return (
<SettingsContext.Provider
value={{
themeSettings,
updateSetting,
isLoading,
whatsNew,
}}
>
{children}
</SettingsContext.Provider>
);
};