PluginProbe
WP Ultimate Post Grid / 4.0.1
WP Ultimate Post Grid v4.0.1
4.1.1 trunk 1.5 1.6 1.7 1.7.1 1.7.2 1.8 1.9 2.0 2.1 2.2 2.3 2.4.0 2.5.0 2.6.0 2.7.0 2.8.0 2.8.2 3.0.0 3.3.0 3.4.0 3.5.0 3.6.0 3.7.0 All 32 releases
wp-ultimate-post-grid / vendor / bv-settings / assets / js / admin / App.js

App.js in WP Ultimate Post Grid 4.0.1, at vendor/bv-settings/assets/js/admin/App.js

127 lines 3.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 import React, { Component } from 'react';
2 import '../../css/admin/settings.scss';
3
4 import Api from './general/Api';
5 import MenuContainer from './menu/MenuContainer';
6 import SettingsContainer from './settings/SettingsContainer';
7 import { animateScroll as scroll, scroller } from 'react-scroll';
8 import Icon from './general/Icon';
9
10 export default class App extends Component {
11
12 constructor(props) {
13 super(props);
14
15 this.state = {
16 savedSettings: { ...bv_settings.settings },
17 currentSettings: { ...bv_settings.settings },
18 savingChanges: false,
19 }
20 }
21
22 onSettingChange(setting, value) {
23 let newSettings = this.state.currentSettings;
24 newSettings[setting] = value;
25
26 this.setState({
27 currentSettings: newSettings
28 }, () => {
29 // Setting specific actions.
30 if ( 'features_custom_style' === setting ) {
31 scroller.scrollTo(setting, {
32 smooth: true,
33 duration: 400,
34 offset: -110,
35 });
36 }
37 });
38 }
39
40 onSaveChanges() {
41 this.setState({
42 savingChanges: true,
43 });
44
45 Api.saveSettings(this.state.currentSettings)
46 .then(settings => {
47 this.setState({
48 savingChanges: false,
49 savedSettings: { ...settings },
50 currentSettings: { ...settings },
51 });
52 }).catch(err => {
53 alert('The settings could not be saved. Try again later or contact [email protected]');
54
55 this.setState({
56 savingChanges: false,
57 });
58 });
59 }
60
61 onCancelChanges() {
62 if(confirm('Are you sure you want to cancel the changes you made?')) {
63 this.setState({
64 currentSettings: { ...this.state.savedSettings },
65 });
66 }
67 }
68
69 onResetDefaults() {
70 if(confirm('Are you sure you want to reset the settings to their default values? This will not save them yet.')) {
71 this.setState({
72 currentSettings: {
73 ...this.state.savedSettings,
74 ...bv_settings.defaults
75 },
76 });
77 }
78 }
79
80 scrollToTop() {
81 scroll.scrollToTop();
82 }
83
84 componentDidMount() {
85 window.addEventListener( 'beforeunload', this.beforeWindowClose.bind(this) );
86 }
87
88 componentWillUnmount() {
89 window.removeEventListener( 'beforeunload', this.beforeWindowClose.bind(this) );
90 }
91
92 beforeWindowClose(event) {
93 if ( this.settingsChanged() ) {
94 return false;
95 }
96 }
97
98 settingsChanged() {
99 return JSON.stringify(this.state.savedSettings) !== JSON.stringify(this.state.currentSettings);
100 }
101
102 render() {
103 return (
104 <div>
105 <MenuContainer
106 structure={bv_settings.structure}
107 settings={this.state.currentSettings}
108 settingsChanged={this.settingsChanged()}
109 savingChanges={this.state.savingChanges}
110 onSaveChanges={this.onSaveChanges.bind(this)}
111 onCancelChanges={this.onCancelChanges.bind(this)}
112 />
113 <SettingsContainer
114 structure={bv_settings.structure}
115 settings={this.state.currentSettings}
116 settingsChanged={this.settingsChanged()}
117 onSettingChange={this.onSettingChange.bind(this)}
118 onResetDefaults={this.onResetDefaults.bind(this)}
119 />
120 <a href="#" className="bvs-settings-scroll-to-top" onClick={this.scrollToTop}>
121 <Icon type="up" />
122 </a>
123 </div>
124 );
125 }
126 }
127