2021-03-03 06:05:45 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { withDispatch, withSelect } from '@wordpress/data';
|
|
|
|
import { compose } from '@wordpress/compose';
|
2021-03-27 17:03:18 +00:00
|
|
|
import { Modal, Notice } from '@wordpress/components';
|
|
|
|
import { useState } from '@wordpress/element';
|
2021-03-03 06:05:45 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import { STORE_KEY } from './data/constants';
|
2021-03-27 17:03:18 +00:00
|
|
|
import { default as OptionEditor } from './OptionEditor';
|
2021-03-03 06:05:45 +00:00
|
|
|
import './data';
|
|
|
|
|
|
|
|
function Options( {
|
|
|
|
options,
|
|
|
|
getOptions,
|
2021-04-27 01:03:24 +00:00
|
|
|
deleteOption,
|
2021-03-24 05:54:19 +00:00
|
|
|
isLoading,
|
|
|
|
invalidateResolution,
|
2021-03-27 17:03:18 +00:00
|
|
|
getOptionForEditing,
|
|
|
|
editingOption,
|
|
|
|
saveOption,
|
|
|
|
notice,
|
|
|
|
setNotice,
|
2021-03-03 06:05:45 +00:00
|
|
|
} ) {
|
2021-03-27 17:03:18 +00:00
|
|
|
const [ isEditModalOpen, setEditModalOpen ] = useState( false );
|
|
|
|
|
2021-04-27 01:03:24 +00:00
|
|
|
const deleteOptionByName = ( optionName ) => {
|
2021-03-03 06:05:45 +00:00
|
|
|
// eslint-disable-next-line no-alert
|
|
|
|
if ( confirm( 'Are you sure you want to delete?' ) ) {
|
2021-04-27 01:03:24 +00:00
|
|
|
deleteOption( optionName );
|
2021-03-03 06:05:45 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-03-27 17:03:18 +00:00
|
|
|
const openEditModal = ( optionName ) => {
|
|
|
|
invalidateResolution( STORE_KEY, 'getOptionForEditing', [
|
|
|
|
optionName,
|
|
|
|
] );
|
|
|
|
|
|
|
|
getOptionForEditing( optionName );
|
|
|
|
setEditModalOpen( true );
|
|
|
|
};
|
|
|
|
|
|
|
|
const handleSaveOption = ( optionName, newValue ) => {
|
|
|
|
saveOption( optionName, newValue );
|
|
|
|
setEditModalOpen( false );
|
|
|
|
};
|
|
|
|
|
|
|
|
const renderLoading = () => {
|
2021-03-24 05:54:19 +00:00
|
|
|
return (
|
|
|
|
<tr>
|
2021-03-27 17:03:18 +00:00
|
|
|
<td colSpan="5" align="center">
|
2021-03-24 05:54:19 +00:00
|
|
|
Loading...
|
|
|
|
</td>
|
|
|
|
</tr>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2021-03-27 17:03:18 +00:00
|
|
|
const renderTableData = () => {
|
2021-03-24 05:54:19 +00:00
|
|
|
if ( options.length === 0 ) {
|
|
|
|
return (
|
|
|
|
<tr>
|
2021-03-27 17:03:18 +00:00
|
|
|
<td colSpan="5" align="center">
|
2021-03-24 05:54:19 +00:00
|
|
|
No Options Found
|
|
|
|
</td>
|
|
|
|
</tr>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-03-03 06:05:45 +00:00
|
|
|
return options.map( ( option ) => {
|
|
|
|
// eslint-disable-next-line camelcase
|
|
|
|
const { option_id, option_name, autoload } = option;
|
|
|
|
|
|
|
|
// eslint-disable-next-line camelcase
|
|
|
|
const optionId = option_id;
|
|
|
|
// eslint-disable-next-line camelcase
|
|
|
|
const optionName = option_name;
|
|
|
|
|
|
|
|
return (
|
|
|
|
<tr key={ optionId }>
|
|
|
|
<td key={ 0 }>{ optionId }</td>
|
|
|
|
<td key={ 1 }>{ optionName }</td>
|
2021-03-27 17:03:18 +00:00
|
|
|
<td className="align-center" key={ 2 }>
|
|
|
|
{ autoload }
|
|
|
|
</td>
|
|
|
|
<td className="align-center" key={ 3 }>
|
2021-03-03 06:05:45 +00:00
|
|
|
<button
|
|
|
|
className="button btn-danger"
|
2021-04-27 01:03:24 +00:00
|
|
|
onClick={ () => deleteOptionByName( optionName ) }
|
2021-03-03 06:05:45 +00:00
|
|
|
>
|
|
|
|
Delete
|
|
|
|
</button>
|
|
|
|
</td>
|
2021-03-27 17:03:18 +00:00
|
|
|
<td className="align-center" key={ 4 }>
|
|
|
|
<button
|
|
|
|
className="button btn-primary"
|
|
|
|
onClick={ () => openEditModal( optionName ) }
|
|
|
|
>
|
|
|
|
Edit
|
|
|
|
</button>
|
|
|
|
</td>
|
2021-03-03 06:05:45 +00:00
|
|
|
</tr>
|
|
|
|
);
|
|
|
|
} );
|
|
|
|
};
|
|
|
|
|
2021-03-27 17:03:18 +00:00
|
|
|
const searchOption = ( event ) => {
|
2021-03-03 06:05:45 +00:00
|
|
|
event.preventDefault();
|
2021-03-24 05:54:19 +00:00
|
|
|
const keyword = event.target.search.value;
|
|
|
|
|
|
|
|
// Invalidate resolution of the same selector + arg
|
|
|
|
// so that entering the same keyword always works
|
|
|
|
invalidateResolution( STORE_KEY, 'getOptions', [ keyword ] );
|
2021-03-03 06:05:45 +00:00
|
|
|
|
2021-03-24 05:54:19 +00:00
|
|
|
getOptions( keyword );
|
2021-03-03 06:05:45 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
2021-03-27 17:03:18 +00:00
|
|
|
<>
|
|
|
|
{ isEditModalOpen && (
|
|
|
|
<Modal
|
|
|
|
title={ editingOption.name }
|
|
|
|
onRequestClose={ () => {
|
|
|
|
setEditModalOpen( false );
|
|
|
|
} }
|
|
|
|
>
|
|
|
|
<OptionEditor
|
|
|
|
option={ editingOption }
|
|
|
|
onSave={ handleSaveOption }
|
|
|
|
></OptionEditor>
|
|
|
|
</Modal>
|
|
|
|
) }
|
|
|
|
<div id="wc-admin-test-helper-options">
|
|
|
|
{ notice.message.length > 0 && (
|
|
|
|
<Notice
|
|
|
|
status={ notice.status }
|
|
|
|
onRemove={ () => {
|
|
|
|
setNotice( { message: '' } );
|
|
|
|
} }
|
2021-03-03 06:05:45 +00:00
|
|
|
>
|
2021-03-27 17:03:18 +00:00
|
|
|
{ notice.message }
|
|
|
|
</Notice>
|
|
|
|
) }
|
|
|
|
<form onSubmit={ searchOption }>
|
|
|
|
<div className="search-box">
|
|
|
|
<label
|
|
|
|
className="screen-reader-text"
|
|
|
|
htmlFor="post-search-input"
|
|
|
|
>
|
|
|
|
Search products:
|
|
|
|
</label>
|
|
|
|
<input type="search" name="search" />
|
|
|
|
<input
|
|
|
|
type="submit"
|
|
|
|
id="search-submit"
|
|
|
|
className="button"
|
|
|
|
value="Search Option"
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</form>
|
|
|
|
<div className="clear"></div>
|
|
|
|
<table className="wp-list-table striped table-view-list widefat">
|
|
|
|
<thead>
|
|
|
|
<tr>
|
|
|
|
<td
|
|
|
|
className="manage-column column-thumb"
|
|
|
|
key={ 0 }
|
|
|
|
>
|
|
|
|
I.D
|
|
|
|
</td>
|
|
|
|
<td
|
|
|
|
className="manage-column column-thumb"
|
|
|
|
key={ 1 }
|
|
|
|
>
|
|
|
|
Name
|
|
|
|
</td>
|
|
|
|
<td
|
|
|
|
className="manage-column column-thumb align-center"
|
|
|
|
key={ 2 }
|
|
|
|
>
|
|
|
|
Autoload
|
|
|
|
</td>
|
|
|
|
<td
|
|
|
|
className="manage-column column-thumb align-center"
|
|
|
|
key={ 3 }
|
|
|
|
>
|
|
|
|
Delete
|
|
|
|
</td>
|
|
|
|
<td
|
|
|
|
className="manage-column column-thumb align-center"
|
|
|
|
key={ 4 }
|
|
|
|
>
|
|
|
|
Edit
|
|
|
|
</td>
|
|
|
|
</tr>
|
|
|
|
</thead>
|
|
|
|
<tbody>
|
|
|
|
{ isLoading ? renderLoading() : renderTableData() }
|
|
|
|
</tbody>
|
|
|
|
</table>
|
|
|
|
</div>
|
|
|
|
</>
|
2021-03-03 06:05:45 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
export default compose(
|
|
|
|
withSelect( ( select ) => {
|
2021-03-27 17:03:18 +00:00
|
|
|
const {
|
|
|
|
getOptions,
|
|
|
|
getOptionForEditing,
|
|
|
|
getNotice,
|
|
|
|
isLoading,
|
|
|
|
} = select( STORE_KEY );
|
2021-03-03 06:05:45 +00:00
|
|
|
const options = getOptions();
|
2021-03-27 17:03:18 +00:00
|
|
|
const editingOption = getOptionForEditing();
|
|
|
|
const notice = getNotice();
|
2021-03-24 05:54:19 +00:00
|
|
|
|
2021-03-27 17:03:18 +00:00
|
|
|
return {
|
|
|
|
options,
|
|
|
|
getOptions,
|
|
|
|
isLoading: isLoading(),
|
|
|
|
editingOption,
|
|
|
|
getOptionForEditing,
|
|
|
|
notice,
|
|
|
|
};
|
2021-03-03 06:05:45 +00:00
|
|
|
} ),
|
|
|
|
withDispatch( ( dispatch ) => {
|
2021-04-27 01:03:24 +00:00
|
|
|
const { deleteOption, saveOption, setNotice } = dispatch( STORE_KEY );
|
2021-03-24 05:54:19 +00:00
|
|
|
const { invalidateResolution } = dispatch( 'core/data' );
|
|
|
|
|
2021-03-27 17:03:18 +00:00
|
|
|
return {
|
2021-04-27 01:03:24 +00:00
|
|
|
deleteOption,
|
2021-03-27 17:03:18 +00:00
|
|
|
invalidateResolution,
|
|
|
|
saveOption,
|
|
|
|
setNotice,
|
|
|
|
};
|
2021-03-03 06:05:45 +00:00
|
|
|
} )
|
|
|
|
)( Options );
|