Back

0% Statements 0/5
0% Branches 0/8
0% Functions 0/2
0% Lines 0/5

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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                                                                                                                         
/**
 * Reads and writes one integration's settings on a form, mirroring the PHP
 * Integration_Settings resolver.
 *
 * Integrations registered through `jetpack_forms_register_integration()` store their per-form
 * settings in the shared `integrations` block attribute, keyed by slug. The bundled
 * integrations predate that container and still use their own top-level attributes; they read
 * those directly and do not go through here.
 */
 
export type IntegrationSettings = Record< string, unknown >;
 
type FormAttributes = {
	integrations?: Record< string, IntegrationSettings >;
} & Record< string, unknown >;
 
/**
 * Get one integration's settings for the form being edited.
 *
 * @param slug       - Integration slug, matching the PHP registration.
 * @param attributes - The contact form block's attributes.
 * @return The stored settings, or an empty object when the form has none.
 */
export function getIntegrationSettings(
	slug: string,
	attributes: FormAttributes | undefined
): IntegrationSettings {
	const stored = attributes?.integrations?.[ slug ];
 
	return stored && typeof stored === 'object' ? stored : {};
}
 
/**
 * Merge a change into one integration's settings, leaving other integrations untouched.
 *
 * @param slug          - Integration slug.
 * @param attributes    - The contact form block's current attributes.
 * @param setAttributes - The block's attribute setter.
 * @param patch         - The settings to merge in.
 */
export function updateIntegrationSettings(
	slug: string,
	attributes: FormAttributes | undefined,
	setAttributes: ( ( attrs: Record< string, unknown > ) => void ) | undefined,
	patch: IntegrationSettings
): void {
	if ( ! setAttributes ) {
		return;
	}
 
	setAttributes( {
		integrations: {
			...( attributes?.integrations ?? {} ),
			[ slug ]: {
				...getIntegrationSettings( slug, attributes ),
				...patch,
			},
		},
	} );
}