Why Gemfury? Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Bower components Debian packages RPM packages NuGet packages

jsarnowski / jsarnowski/elementor-pro   php

Repository URL to install this package:

Version: 3.2.1 

/ app / modules / site-editor / assets / js / context / base-context.js

export class BaseContext extends React.Component {
	constructor( props ) {
		super( props );

		this.state = {
			action: {
				current: null,
				loading: false,
				error: null,
				errorMeta: {},
			},

			updateActionState: this.updateActionState.bind( this ),
			resetActionState: this.resetActionState.bind( this ),
		};
	}

	executeAction( name, handler ) {
		this.updateActionState( { current: name, loading: true, error: null, errorMeta: {} } );

		return handler()
			.then( ( response ) => {
				this.resetActionState();

				return Promise.resolve( response );
			} )
			.catch( ( error ) => {
				this.updateActionState( { current: name, loading: false, error: error.message, errorMeta: error } );

				return Promise.reject( error );
			} );
	}

	updateActionState( data ) {
		return this.setState( ( prev ) => ( {
			action: {
				...prev.action,
				...data,
			},
		} ) );
	}

	resetActionState() {
		this.updateActionState( { current: null, loading: false, error: null, errorMeta: {} } );
	}
}

export default BaseContext;