| 1 |
/** |
| 2 |
* WordPress dependencies. |
| 3 |
*/ |
| 4 |
import { CheckboxControl } from '@wordpress/components'; |
| 5 |
import { WPElement } from '@wordpress/element'; |
| 6 |
import { __ } from '@wordpress/i18n'; |
| 7 |
|
| 8 |
/** |
| 9 |
* Internal dependencies. |
| 10 |
*/ |
| 11 |
import { useSync } from '../../sync'; |
| 12 |
import { useSyncSettings } from '../provider'; |
| 13 |
|
| 14 |
/** |
| 15 |
* Delete checkbox component. |
| 16 |
* |
| 17 |
* @returns {WPElement} Sync page component. |
| 18 |
*/ |
| 19 |
export default () => { |
| 20 |
const { isSyncing } = useSync(); |
| 21 |
const { args, indexables, setArgs } = useSyncSettings(); |
| 22 |
|
| 23 |
/** |
| 24 |
* Handle changes to Indexable checkboxes. |
| 25 |
* |
| 26 |
* Sets the post types argument as empty if the Posts indexable is not |
| 27 |
* being synced. |
| 28 |
* |
| 29 |
* @param {string} indexable Indexable. |
| 30 |
* @param {boolean} checked Whether the Indexable is checked. |
| 31 |
* @returns {void} |
| 32 |
*/ |
| 33 |
const onChange = (indexable, checked) => { |
| 34 |
const indexables = args.indexables ? [...args.indexables] : []; |
| 35 |
|
| 36 |
if (checked) { |
| 37 |
indexables.push(indexable); |
| 38 |
} else { |
| 39 |
indexables.splice(indexables.indexOf(indexable), 1); |
| 40 |
} |
| 41 |
|
| 42 |
const post_type = !indexables.length || indexables.includes('post') ? args.post_type : []; |
| 43 |
|
| 44 |
setArgs({ ...args, indexables, post_type }); |
| 45 |
}; |
| 46 |
|
| 47 |
return indexables.length > 1 ? ( |
| 48 |
<fieldset className="ep-sync-advanced-control"> |
| 49 |
<legend className="ep-sync-advanced-control__label"> |
| 50 |
{__('Content to sync', 'elasticpress')} |
| 51 |
</legend> |
| 52 |
{indexables.map(([indexable, label]) => ( |
| 53 |
<CheckboxControl |
| 54 |
checked={args.indexables?.includes(indexable)} |
| 55 |
disabled={isSyncing} |
| 56 |
indeterminate={!args.indexables.length} |
| 57 |
key={indexable} |
| 58 |
label={label} |
| 59 |
onChange={(checked) => onChange(indexable, checked)} |
| 60 |
/> |
| 61 |
))} |
| 62 |
</fieldset> |
| 63 |
) : null; |
| 64 |
}; |
| 65 |
|