-
Notifications
You must be signed in to change notification settings - Fork 225
/
settings-page.js
139 lines (117 loc) · 3.22 KB
/
settings-page.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import { useState, useEffect, Fragment } from 'react';
import { store as noticesStore } from '@wordpress/notices';
import { dispatch, useDispatch, useSelect } from '@wordpress/data';
import { __ } from '@wordpress/i18n';
import api from '@wordpress/api';
import { Button, Panel, Placeholder, Spinner, SnackbarList } from '@wordpress/components';
import { SettingsPanel } from './../panels/settings-panel.js';
import { ActionLinksPanel } from '../panels/action-links-panel.js';
const Notices = () => {
const notices = useSelect(
( select ) =>
select( noticesStore )
.getNotices()
.filter( ( notice ) => 'snackbar' === notice.type ),
[],
);
const { removeNotice } = useDispatch( noticesStore );
return (
<SnackbarList
className="edit-site-notices"
notices={ notices }
onRemove={ removeNotice }
/>
);
};
const SETTINGS = {
DESCRIPTION_META_TAG: '_description_meta_tag',
SKIP_LINK: '_skip_link',
HEADER_FOOTER: '_header_footer',
PAGE_TITLE: '_page_title',
HELLO_STYLE: '_hello_style',
HELLO_THEME: '_hello_theme',
};
export const SettingsPage = () => {
const [ hasLoaded, setHasLoaded ] = useState( false );
const [ settingsData, setSettingsData ] = useState( {} );
const settingsPrefix = 'hello_elementor_settings';
/**
* Update settings data.
*
* @param {string} settingsName
* @param {string} settingsValue
*/
const updateSettings = ( settingsName, settingsValue ) => {
setSettingsData( {
...settingsData,
[ settingsName ]: settingsValue,
} );
};
/**
* Save settings to server.
*/
const saveSettings = () => {
const data = {};
Object.values( SETTINGS ).forEach( ( value ) => data[ `${ settingsPrefix }${ value }` ] = settingsData[ value ] ? 'true' : '' );
const settings = new api.models.Settings( data );
settings.save();
dispatch( 'core/notices' ).createNotice(
'success',
__( 'Settings Saved', 'hello-elementor' ),
{
type: 'snackbar',
isDismissible: true,
},
);
};
useEffect( () => {
const fetchSettings = async () => {
try {
await api.loadPromise;
const settings = new api.models.Settings();
const response = await settings.fetch();
const data = {};
Object.values( SETTINGS ).forEach( ( value ) => data[ value ] = response[ `${ settingsPrefix }${ value }` ] );
setSettingsData( data );
setHasLoaded( true );
} catch ( error ) {
// eslint-disable-next-line no-console
console.error( error );
}
};
if ( hasLoaded ) {
return;
}
fetchSettings();
}, [ settingsData ] );
if ( ! hasLoaded ) {
return (
<Placeholder>
<Spinner />
</Placeholder>
);
}
return (
<Fragment>
<div className="hello_elementor__header">
<div className="hello_elementor__container">
<div className="hello_elementor__title">
<h1>{ __( 'Hello Theme Settings', 'hello-elementor' ) }</h1>
</div>
</div>
</div>
<div className="hello_elementor__main">
<Panel>
<SettingsPanel { ...{ SETTINGS, settingsData, updateSettings } } />
<Button isPrimary onClick={ saveSettings }>
{ __( 'Save Settings', 'hello-elementor' ) }
</Button>
</Panel>
<ActionLinksPanel />
</div>
<div className="hello_elementor__notices">
<Notices />
</div>
</Fragment>
);
};