File size: 2,788 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { localize } from 'i18n-calypso';
import { Component } from 'react';
import { connect } from 'react-redux';
import QueryUserSettings from 'calypso/components/data/query-user-settings';
import { errorNotice, successNotice } from 'calypso/state/notices/actions';
import getUnsavedUserSettings from 'calypso/state/selectors/get-unsaved-user-settings';
import getUserSettings from 'calypso/state/selectors/get-user-settings';
import hasUnsavedUserSettings from 'calypso/state/selectors/has-unsaved-user-settings';
import {
	clearUnsavedUserSettings,
	setUserSetting,
	saveUserSettings,
} from 'calypso/state/user-settings/actions';
import { isUpdatingUserSettings } from 'calypso/state/user-settings/selectors';

const withFormBase = ( WrappedComponent ) => {
	class EnhancedComponent extends Component {
		static displayName = `withFormBase(${ WrappedComponent.displayName || WrappedComponent.name })`;

		componentDidUpdate( prevProps ) {
			if ( prevProps.hasUnsavedUserSettings && ! this.props.hasUnsavedUserSettings ) {
				this.props.markSaved?.();
			}
		}

		componentWillUnmount() {
			// Silently clean up unsavedSettings before unmounting
			this.props.clearUnsavedUserSettings();
		}

		getDisabledState = () => {
			return this.props.isUpdatingUserSettings;
		};

		getSetting = ( settingName ) => {
			const { unsavedUserSettings, userSettings } = this.props;
			return unsavedUserSettings[ settingName ] ?? userSettings[ settingName ] ?? '';
		};

		toggleSetting = ( event ) => {
			const { name } = event.currentTarget;
			this.props.setUserSetting( name, ! this.getSetting( name ) );
		};

		updateSetting = ( event ) => {
			const { name, value } = event.currentTarget;
			this.props.setUserSetting( name, value );
		};

		submitForm = ( event ) => {
			event.preventDefault();

			this.props.saveUserSettings();
		};

		getFormBaseProps = () => ( {
			getDisabledState: this.getDisabledState,
			getSetting: this.getSetting,
			toggleSetting: this.toggleSetting,
			updateSetting: this.updateSetting,
			submitForm: this.submitForm,
			hasUnsavedUserSettings: this.props.hasUnsavedUserSettings,
			isUpdatingUserSettings: this.props.isUpdatingUserSettings,
		} );

		render() {
			return (
				<>
					<QueryUserSettings />
					<WrappedComponent { ...this.props } { ...this.getFormBaseProps() } />
				</>
			);
		}
	}

	return connect(
		( state ) => ( {
			userSettings: getUserSettings( state ),
			unsavedUserSettings: getUnsavedUserSettings( state ),
			hasUnsavedUserSettings: hasUnsavedUserSettings( state ),
			isUpdatingUserSettings: isUpdatingUserSettings( state ),
		} ),
		{
			clearUnsavedUserSettings,
			errorNotice,
			saveUserSettings,
			setUserSetting,
			successNotice,
		}
	)( localize( EnhancedComponent ) );
};

export default withFormBase;