All files / cred-form cred-form-preview.js

84.44% Statements 38/45
76.92% Branches 20/26
81.82% Functions 9/11
84.44% Lines 38/45
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 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167                                    5x 5x 5x           5x                 13x   13x                 13x 7x               6x                   13x                     13x       8x                     5x                 9x 9x         3x 2x 2x   1x 1x     6x 6x     1x   5x   6x 6x     9x     9x 1x       1x             8x                   5x 5x 5x 5x   5x       4x   4x        
/**
 * The Toolset Form block preview component.
 *
 * A "CREDFormPreview" component is created that is used inside the Toolset Form block to handle the previewing of the
 * selected Content Template.
 *
 * @since  2.3
 */
 
import { __ } from '@wordpress/i18n';
import { Spinner } from '@wordpress/components';
import { Component } from '@wordpress/element';
// Defined in webpack externals.
import i18n from '@cred/cred_form_block_strings';
 
export default class CREDFormPreview extends Component {
	// constructor( props ) {
	constructor() {
		super( ...arguments );
		this.getCREDFormInfo = this.getCREDFormInfo.bind( this );
		this.state = {
			fetching: false,
			error: false,
			errorMessage: '',
		};
 
		this.onUpdateRelationshipData = this.props.onUpdateRelationshipData;
	}
 
	render() {
		const {
			fetching,
			error,
			errorMessage,
			formInfo,
		} = this.state;
 
		Iif ( fetching ) {
			return <div key="fetching" className={ this.props.className } >
				<div key="loading" className={ 'wp-block-embed is-loading' }>
					<Spinner />
					<p>{ __( 'Loading the Toolset Form Preview…' ) }</p>
				</div>
			</div>;
		}
 
		if ( error ) {
			return <div key="error" className={ this.props.className } >
				<div className={ 'cred-form-info-warning' }>
					{ errorMessage }
				</div>
 
			</div>;
		}
 
		return (
			<div className={ this.props.className } >
				<div key="cred-form-information" className="cred-form-information" >
					<div className={ 'cred-form-preview-render' } dangerouslySetInnerHTML={ { __html: formInfo } }></div>
				</div>
			</div>
		);
	}
 
	static getDerivedStateFromProps( nextProps, prevState ) {
		Iif (
			nextProps.attributes.form.ID &&
			nextProps.attributes.form.ID !== prevState.formID &&
			prevState.fetching === false
		) {
			// If the Toolset Form is already there, we're loading a saved block, so we need to render
			// a different thing, which is why this doesn't use 'fetching', as that
			// is for when the user is putting in a new url on the placeholder form
			prevState.fetching = true;
		}
 
		return prevState;
	}
 
	componentDidUpdate( prevProps ) {
		Iif ( this.props.attributes.form.ID !== prevProps.attributes.form.ID ) {
			this.setState( {
				fetching: true,
				error: false,
				errorMessage: '',
			} );
			this.getCREDFormInfo( this.props.attributes.form.ID );
		}
	}
 
	componentDidMount() {
		this.getCREDFormInfo();
	}
 
	/**
	 * Depending on data got from preview, it updates the component state
	 *
	 * @param {object} response Data from ajax call
	 */
	handleCREDPreviewData( response ) {
		const newState = {};
		if (
			0 !== response &&
			response.success &&
			'undefined' !== typeof response.data
		) {
			if ( '' === response.data.formContent ) {
				newState.error = true;
				newState.errorMessage = __( 'The selected Toolset Form has an empty form content.' );
			} else {
				newState.formInfo = response.data.formContent;
				newState.formID = response.data.formID;
			}
		} else {
			let message = '';
			if (
				'undefined' !== typeof response.data &&
				'undefined' !== typeof response.data.message ) {
				message = response.data.message;
			} else {
				message = __( 'An error occurred while trying to get the Toolset Form information.' );
			}
			newState.error = true;
			newState.errorMessage = message;
		}
 
		newState.fetching = false;
 
		// Relationship forms need extra data.
		if ( this.props.attributes.form.formType === 'relationship' ) {
			const parameters = '?' +
				'action=' + i18n.association.action + '&' +
				'form=' + this.props.attributes.form.post_name + '&' +
				'wpnonce=' + i18n.association.wpnonce;
			window.fetch( window.ajaxurl + parameters )
				.then( res => res.json() )
				.then( res => {
					this.onUpdateRelationshipData( res.data );
					this.setState( newState );
				} );
		} else {
			this.setState( newState );
		}
	}
 
	/**
	 * Gets a form HTML preview
	 *
	 * @param {int} formID Form ID.
	 */
	getCREDFormInfo( formID ) {
		const data = new window.FormData();
		data.append( 'action', i18n.actionName );
		data.append( 'wpnonce', i18n.wpnonce );
		data.append( 'formID', 'undefined' === typeof formID ? this.props.attributes.form.ID : formID );
 
		window.fetch( window.ajaxurl, {
			method: 'POST',
			body: data,
			credentials: 'same-origin',
		} ).then( res => res.json() )
			.then( response => {
				this.handleCREDPreviewData( response );
			} );
	}
}