woocommerce/plugins/woocommerce-admin/client/settings/content.js

148 lines
3.2 KiB
JavaScript
Raw Normal View History

2024-05-31 04:32:22 +00:00
/**
* External dependencies
*/
import { Button } from '@wordpress/components';
import { __ } from '@wordpress/i18n';
import { useState, useRef, useEffect } from '@wordpress/element';
2024-06-03 10:54:35 +00:00
import { useDispatch } from '@wordpress/data';
2024-05-31 04:32:22 +00:00
2024-05-29 03:41:08 +00:00
/**
* Internal dependencies
*/
2024-06-03 11:15:11 +00:00
import { SettingsCheckbox, SettingsInput } from './components';
2024-05-29 03:41:08 +00:00
export const Content = ( { data } ) => {
const { settings } = data;
2024-06-04 00:56:19 +00:00
console.log( settings );
2024-06-03 10:54:35 +00:00
const [ isBusy, setIsBusy ] = useState( false );
2024-05-31 04:32:22 +00:00
const formRef = useRef( null );
2024-06-03 10:54:35 +00:00
const { createNotice } = useDispatch( 'core/notices' );
2024-05-31 04:32:22 +00:00
const gatherFormInputs = () => {
2024-06-09 23:48:08 +00:00
if ( ! formRef.current ) {
return {};
}
2024-05-31 04:32:22 +00:00
const formElements = formRef.current.querySelectorAll(
'input' // For now. There will be more.
);
2024-06-03 11:26:08 +00:00
2024-05-31 04:32:22 +00:00
const data = {};
formElements.forEach( ( input ) => {
2024-06-03 11:53:03 +00:00
let value;
if ( input.type === 'checkbox' || input.type === 'radio' ) {
value = input.checked ? 'yes' : 'no';
} else {
value = input.value;
}
2024-05-31 04:32:22 +00:00
data[ input.id || input.name ] = value;
} );
2024-05-29 03:41:08 +00:00
2024-06-03 11:53:03 +00:00
return data;
2024-05-31 04:32:22 +00:00
};
2024-06-03 10:54:35 +00:00
const handleSubmit = async ( event ) => {
2024-05-31 04:32:22 +00:00
event.preventDefault();
2024-05-31 05:01:08 +00:00
try {
2024-06-03 10:54:35 +00:00
setIsBusy( true );
const response = await fetch( '/wp-json/wc-admin/options', {
2024-05-31 05:01:08 +00:00
method: 'POST',
2024-06-03 10:37:40 +00:00
headers: { 'content-type': 'application/json' },
2024-06-03 11:53:03 +00:00
body: JSON.stringify( gatherFormInputs() ),
2024-05-31 05:01:08 +00:00
} );
2024-06-03 10:54:35 +00:00
setIsBusy( false );
if ( response.status === 200 ) {
createNotice(
'success',
2024-06-03 11:26:08 +00:00
__( 'Settings saved successfully', 'woocommerce' )
2024-06-03 10:54:35 +00:00
);
} else {
throw new Error();
}
2024-05-31 05:01:08 +00:00
} catch ( error ) {
2024-06-03 10:54:35 +00:00
createNotice(
'fail',
__( 'Error saving settings', 'woocommerce' )
);
2024-05-31 05:01:08 +00:00
console.error( 'Error saving settings', error );
}
2024-05-31 04:32:22 +00:00
};
// Run once to gather inputs on initial render
useEffect( () => {
gatherFormInputs();
}, [] );
2024-05-29 05:24:19 +00:00
2024-05-30 04:32:57 +00:00
if ( ! settings ) {
return null;
}
2024-05-29 03:41:08 +00:00
return (
2024-05-31 04:32:22 +00:00
<form ref={ formRef } id="mainform" onSubmit={ handleSubmit }>
2024-05-30 00:46:53 +00:00
{ settings.map( ( setting, idx ) => {
const key = setting.id || setting.title || idx;
2024-05-29 03:41:08 +00:00
switch ( setting.type ) {
case 'title':
2024-05-29 05:24:19 +00:00
return (
2024-05-29 10:39:02 +00:00
<div
2024-05-30 00:46:53 +00:00
key={ key }
2024-05-29 10:39:02 +00:00
className="woocommerce-settings-element"
>
2024-05-29 05:24:19 +00:00
<h3>{ setting.title }</h3>
</div>
);
2024-05-29 03:41:08 +00:00
case 'checkbox':
return (
2024-06-03 11:53:03 +00:00
<SettingsCheckbox setting={ setting } key={ key } />
2024-05-29 03:41:08 +00:00
);
2024-05-30 02:28:29 +00:00
case 'slotfill_placeholder':
return (
<div
key={ key }
id={ setting.id }
className={ setting.class }
></div>
);
2024-06-03 11:15:11 +00:00
case 'text':
case 'password':
case 'datetime':
case 'datetime-local':
case 'date':
case 'month':
case 'time':
case 'week':
case 'number':
case 'email':
case 'url':
case 'tel':
return (
2024-06-03 11:53:03 +00:00
<SettingsInput setting={ setting } key={ key } />
2024-06-03 11:15:11 +00:00
);
2024-06-04 00:56:19 +00:00
case 'custom':
return (
<div
key={ key }
className="woocommerce-settings-element"
id={ setting.id }
>
<div
dangerouslySetInnerHTML={ {
__html: setting.content,
} }
/>
</div>
);
2024-05-29 03:41:08 +00:00
default:
return null;
}
} ) }
2024-06-03 10:54:35 +00:00
<Button variant="primary" type="submit" isBusy={ isBusy }>
2024-05-31 04:32:22 +00:00
{ __( 'Save changes', 'woocommerce' ) }
</Button>
</form>
2024-05-29 03:41:08 +00:00
);
};