All files / cred-form index.js

95% Statements 76/80
75% Branches 36/48
94.44% Functions 17/18
94.94% Lines 75/79
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 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268                                                1x                   1x 18x 10x 10x   8x 8x 16x 16x   8x     1x                       2x 1x 1x 1x 1x     2x 1x     2x 1x     2x 1x     2x 2x 1x   2x     2x 1x     2x 1x     2x 1x     2x 1x     2x 1x     2x 10x 10x 10x 10x     10x               2x       2x 2x 2x 2x 2x 2x   2x 2x   2x                                                                                                                                                                 4x 4x 4x 4x 4x   4x   4x 1x   3x   3x           1x     3x 1x 1x           1x       3x 1x   1x 1x 1x         1x     3x   3x      
/**
 * Handles the creation and the behavior of the Toolset Content Template block.
 *
 * @since  2.3
 */
 
/**
 * Block dependencies
 */
import icon from './icon';
import Inspector from './inspector/inspector';
import CREDFormSelect from './inspector/cred-form-select';
import CREDFormPreview from './cred-form-preview';
import './styles/editor.scss';
 
/**
 * Internal block libraries
 */
import { __ } from '@wordpress/i18n';
import { Placeholder } from '@wordpress/components';
import { RawHTML } from '@wordpress/element';
// Defined in webpack externals.
import i18n from '@cred/cred_form_block_strings';
 
export const name = i18n.blockName;
 
/**
 * Forms are grouped by Form type (Post, User and Relationship), and each of them by Editing or Adding.
 * This funcion search a form by its `post_name`
 *
 * @param {Object} forms An object containing the forms: `i18n.publishedForms`
 * @param {String} formName The name of the form: `post_name`
 * @return {Object} An object containing form data
 */
const getFormByFormName = ( forms, formName ) => {
	if ( forms instanceof Array ) {
		const selectedItem = forms.filter( formItem => formItem.post_name === formName );
		return selectedItem.length ? selectedItem[ 0 ] : null;
	}
	let result = null;
	Object.keys( forms ).forEach( key => {
		result = result || getFormByFormName( forms[ key ], formName );
		return result;
	} );
	return result;
};
 
export const settings = {
	title: __( 'Form' ),
	description: __( 'Add a Toolset Form to the editor.' ),
	category: i18n.blockCategory,
	icon: icon.blockIcon,
	keywords: [
		__( 'Toolset' ),
		__( 'Form' ),
		__( 'Shortcode' ),
	],
 
	edit: props => {
		const onChangeCredForm = ( event ) => {
			const formInfo = JSON.parse( event.target.value );
			props.setAttributes( { form: event.target.value } );
			props.setAttributes( { formType: formInfo.formType } );
			props.setAttributes( { formAction: formInfo.formAction } );
		};
 
		const onChangePostToEdit = ( value ) => {
			props.setAttributes( { postToEdit: value } );
		};
 
		const onChangeAnotherPostToEdit = ( value ) => {
			props.setAttributes( { anotherPostToEdit: value } );
		};
 
		const onChangeUserToEdit = ( value ) => {
			props.setAttributes( { userToEdit: value } );
		};
 
		const onChangeAnotherUserToEdit = ( value ) => {
			if ( null === value ) {
				value = '';
			}
			props.setAttributes( { anotherUserToEdit: value } );
		};
 
		const onChangeRelationshipFormType = ( value ) => {
			props.setAttributes( { relationshipFormType: value } );
		};
 
		const onChangeRelationshipParentItem = ( value ) => {
			props.setAttributes( { relationshipParentItem: value } );
		};
 
		const onChangeRelationshipChildItem = ( value ) => {
			props.setAttributes( { relationshipChildItem: value } );
		};
 
		const onChangeRelationshipParentItemSpecific = ( option ) => {
			props.setAttributes( { relationshipParentItemSpecific: option } );
		};
 
		const onChangeRelationshipChildItemSpecific = ( option ) => {
			props.setAttributes( { relationshipChildItemSpecific: option } );
		};
 
		const assignFormTypeAndAction = ( forms, type, action ) => {
			forms.map( ( item ) => {
				item.formType = type;
				item.formAction = action;
				return item;
			} );
 
			return forms;
		};
 
		/**
		 * When selecting a relationship, extra data must be fetched and added for using in the Inspector
		 *
		 * @param {object} data Extra data.
		 */
		const onUpdateRelationshipData = data => {
			props.setAttributes( { relationshipData: data } );
		};
 
		const newPostForms = assignFormTypeAndAction( i18n.publishedForms.postForms.new, 'post', 'new' );
		const editPostForms = assignFormTypeAndAction( i18n.publishedForms.postForms.edit, 'post', 'edit' );
		const newUserForms = assignFormTypeAndAction( i18n.publishedForms.userForms.new, 'user', 'new' );
		const editUserForms = assignFormTypeAndAction( i18n.publishedForms.userForms.edit, 'user', 'edit' );
		const newRelationshipForms = assignFormTypeAndAction( i18n.publishedForms.relationshipForms.new, 'relationship', 'new' );
		const relationshipData = !! props.attributes.relationshipData ? props.attributes.relationshipData.relationship : {};
		// props.attributes.form has not the same value that the select option, so it needs to get it again.
		const formData = !! props.attributes.form ? JSON.parse( props.attributes.form ) : {};
		const formValue = JSON.stringify( getFormByFormName( i18n.publishedForms, formData.post_name || '' ) );
 
		return [
			!! (
				props.focus ||
				props.isSelected
			) && (
				<Inspector
					key="wpv-gutenberg-cred-form-block-render-inspector"
					className={ 'wp-block-toolset-cred-form-inspector' }
					attributes={
						{
							newPostForms: newPostForms,
							editPostForms: editPostForms,
							newUserForms: newUserForms,
							editUserForms: editUserForms,
							newRelationshipForms: newRelationshipForms,
							form: formValue,
							formType: props.attributes.formType,
							formAction: props.attributes.formAction,
							postToEdit: props.attributes.postToEdit,
							anotherPostToEdit: props.attributes.anotherPostToEdit,
							userToEdit: props.attributes.userToEdit,
							anotherUserToEdit: props.attributes.anotherUserToEdit,
							relationshipFormType: props.attributes.relationshipFormType,
							relationshipParentItem: props.attributes.relationshipParentItem,
							relationshipChildItem: props.attributes.relationshipChildItem,
							relationshipParentItemSpecific: props.attributes.relationshipParentItemSpecific,
							relationshipChildItemSpecific: props.attributes.relationshipChildItemSpecific,
							relationshipData: relationshipData,
						}
					}
					onChangeCredForm={ onChangeCredForm }
					onChangePostToEdit={ onChangePostToEdit }
					onChangeUserToEdit={ onChangeUserToEdit }
					onChangeAnotherPostToEdit={ onChangeAnotherPostToEdit }
					onChangeAnotherUserToEdit={ onChangeAnotherUserToEdit }
					onChangeRelationshipFormType={ onChangeRelationshipFormType }
					onChangeRelationshipParentItem={ onChangeRelationshipParentItem }
					onChangeRelationshipChildItem={ onChangeRelationshipChildItem }
					onChangeRelationshipParentItemSpecific={ onChangeRelationshipParentItemSpecific }
					onChangeRelationshipChildItemSpecific={ onChangeRelationshipChildItemSpecific }
				/>
			),
			(
				! props.attributes.form ?
					<Placeholder
						key="cred-form-block-placeholder"
						className={ 'wp-block-toolset-cred-form' }
					>
						<div className="wp-block-toolset-cred-form-placeholder">
							{ icon.blockPlaceholder }
							<h2>{ __( 'Toolset Form' ) }</h2>
							<CREDFormSelect
								attributes={
									{
										newPostForms: newPostForms,
										editPostForms: editPostForms,
										newUserForms: newUserForms,
										editUserForms: editUserForms,
										newRelationshipForms: newRelationshipForms,
										form: '',
									}
								}
								className={ 'components-select-control__input' }
								onChangeCredForm={ onChangeCredForm }
							/>
						</div>
					</Placeholder> :
					<CREDFormPreview
						key="toolset-cred-form-gutenberg-block-preview"
						className={ `${ props.className } wp-block-toolset-cred-form-preview` }
						attributes={
							{
								form: JSON.parse( props.attributes.form ),
							}
						}
						onUpdateRelationshipData={ onUpdateRelationshipData }
					/>
			),
		];
	},
	save: ( props ) => {
		let form = props.attributes.form || '';
		let post = '',
			user = '',
			relationship = '',
			shortcodeStart = '[cred-form';
 
		const shortcodeEnd = ']';
 
		if ( ! form.length ) {
			return null;
		}
		form = JSON.parse( form );
 
		if (
			'post' === props.attributes.formType &&
			'edit' === props.attributes.formAction &&
			'another' === props.attributes.postToEdit &&
			props.attributes.anotherPostToEdit
		) {
			post = `post="${ props.attributes.anotherPostToEdit.value }" `;
		}
 
		if ( 'user' === props.attributes.formType ) {
			shortcodeStart = '[cred_user_form';
			Eif (
				'edit' === props.attributes.formAction &&
				'another' === props.attributes.userToEdit &&
				!! props.attributes.anotherUserToEdit &&
				!! props.attributes.anotherUserToEdit.value
			) {
				user = `user="${ props.attributes.anotherUserToEdit.value }" `;
			}
		}
 
		if ( 'relationship' === props.attributes.formType ) {
			shortcodeStart = '[cred-relationship-form';
 
			Eif ( 'createParent' === props.attributes.relationshipFormType ) {
				const parentItem = 'specific' === props.attributes.relationshipChildItem ? props.attributes.relationshipChildItemSpecific.value : props.attributes.relationshipChildItem;
				relationship += `child_item="${ parentItem }" `;
			} else if ( 'createChild' === props.attributes.relationshipFormType ) {
				const parentItem = 'specific' === props.attributes.relationshipParentItem ? props.attributes.relationshipParentItemSpecific.value : props.attributes.relationshipParentItem;
				relationship += `parent_item="${ parentItem }" `;
			}
			post = '';
		}
 
		form = ' form="' + form.post_name + '" ';
 
		return <RawHTML>{ shortcodeStart + form + post + user + relationship + shortcodeEnd }</RawHTML>;
	},
};