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

142 lines
3.1 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-05-31 04:32:22 +00:00
const [ formData, setFormData ] = useState( {} );
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 = () => {
const formElements = formRef.current.querySelectorAll(
'input' // For now. There will be more.
);
const data = {};
formElements.forEach( ( input ) => {
const value =
// Only looking at checkboxes for now.
2024-05-31 05:01:08 +00:00
input.checked ? 'yes' : 'no';
2024-05-31 04:32:22 +00:00
data[ input.id || input.name ] = value;
} );
2024-05-29 03:41:08 +00:00
2024-05-31 04:32:22 +00:00
setFormData( data );
};
const handleFormChange = ( { id, value } ) => {
setFormData( {
...formData,
[ id ]: value,
} );
};
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
console.log( 'Submitting form', formData );
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' },
body: JSON.stringify( formData ),
2024-05-31 05:01:08 +00:00
} );
2024-06-03 10:54:35 +00:00
setIsBusy( false );
if ( response.status === 200 ) {
createNotice(
'success',
__( 'Options saved successfully', 'woocommerce' )
);
} 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-05-31 04:32:22 +00:00
<SettingsCheckbox
setting={ setting }
key={ key }
handleFormChange={ handleFormChange }
/>
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 (
<SettingsInput
setting={ setting }
key={ key }
handleFormChange={ handleFormChange }
/>
);
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
);
};