2023-03-14 16:16:19 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { addCustomerEffortScoreExitPageListener } from '@woocommerce/customer-effort-score';
|
|
|
|
|
2022-12-15 19:50:34 +00:00
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import { staticFormDataToObject } from '~/utils/static-form-helper';
|
|
|
|
|
|
|
|
type FormElements = {
|
|
|
|
post?: HTMLFormElement;
|
2023-12-29 08:46:59 +00:00
|
|
|
order?: HTMLFormElement;
|
2022-12-15 19:50:34 +00:00
|
|
|
} & HTMLCollectionOf< HTMLFormElement >;
|
|
|
|
const forms: FormElements = document.forms;
|
2023-12-29 08:46:59 +00:00
|
|
|
if ( forms?.post || forms?.order ) {
|
2022-12-15 19:50:34 +00:00
|
|
|
let triggeredSaveOrDeleteButton = false;
|
|
|
|
const saveButton = document.querySelector( '.save_order' );
|
|
|
|
const deleteButton = document.querySelector( '.submitdelete' );
|
|
|
|
|
|
|
|
if ( saveButton ) {
|
|
|
|
saveButton.addEventListener( 'click', () => {
|
|
|
|
triggeredSaveOrDeleteButton = true;
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
if ( deleteButton ) {
|
|
|
|
deleteButton.addEventListener( 'click', () => {
|
|
|
|
triggeredSaveOrDeleteButton = true;
|
|
|
|
} );
|
|
|
|
}
|
2023-12-29 08:46:59 +00:00
|
|
|
const formData = staticFormDataToObject(
|
|
|
|
( forms?.post || forms?.order ) as HTMLFormElement
|
|
|
|
);
|
2022-12-15 19:50:34 +00:00
|
|
|
addCustomerEffortScoreExitPageListener( 'shop_order_update', () => {
|
|
|
|
if ( triggeredSaveOrDeleteButton ) {
|
|
|
|
return false;
|
|
|
|
}
|
2023-12-29 08:46:59 +00:00
|
|
|
const newFormData =
|
|
|
|
forms?.post || forms?.order
|
|
|
|
? staticFormDataToObject(
|
|
|
|
( forms?.post || forms?.order ) as HTMLFormElement
|
|
|
|
)
|
|
|
|
: {};
|
2022-12-15 19:50:34 +00:00
|
|
|
for ( const key of Object.keys( formData ) ) {
|
|
|
|
const value =
|
|
|
|
typeof formData[ key ] === 'object'
|
|
|
|
? JSON.stringify( formData[ key ] )
|
|
|
|
: formData[ key ];
|
|
|
|
const newValue =
|
|
|
|
typeof newFormData[ key ] === 'object'
|
|
|
|
? JSON.stringify( newFormData[ key ] )
|
|
|
|
: newFormData[ key ];
|
|
|
|
if ( value !== newValue ) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
} );
|
|
|
|
}
|