| 1 |
/** |
| 2 |
* WordPress dependencies. |
| 3 |
*/ |
| 4 |
import { useContext, useEffect, WPElement } from '@wordpress/element'; |
| 5 |
|
| 6 |
/** |
| 7 |
* Internal dependencies. |
| 8 |
*/ |
| 9 |
import { State, Dispatch } from '../context'; |
| 10 |
import AlterativesEditor from './editors/AlternativesEditor'; |
| 11 |
import SetsEditor from './editors/SetsEditor'; |
| 12 |
import SolrEditor from './editors/SolrEditor'; |
| 13 |
|
| 14 |
/** |
| 15 |
* Synonyms editor component. |
| 16 |
* |
| 17 |
* @returns {WPElement} Synonyms component |
| 18 |
*/ |
| 19 |
const SynonymsEditor = () => { |
| 20 |
const state = useContext(State); |
| 21 |
const dispatch = useContext(Dispatch); |
| 22 |
const { alternatives, sets, isSolrEditable, isSolrVisible, dirty, submit } = state; |
| 23 |
const { |
| 24 |
pageHeading, |
| 25 |
pageDescription, |
| 26 |
pageToggleAdvanceText, |
| 27 |
pageToggleSimpleText, |
| 28 |
alternativesTitle, |
| 29 |
alternativesDescription, |
| 30 |
setsTitle, |
| 31 |
setsDescription, |
| 32 |
solrTitle, |
| 33 |
solrDescription, |
| 34 |
submitText, |
| 35 |
} = window.epSynonyms.i18n; |
| 36 |
|
| 37 |
/** |
| 38 |
* Checks if the form is valid. |
| 39 |
* |
| 40 |
* @param {object} _state Current state. |
| 41 |
* @returns {boolean} If the form is valid |
| 42 |
*/ |
| 43 |
const isValid = (_state) => { |
| 44 |
return [..._state.sets, ..._state.alternatives].reduce((valid, item) => { |
| 45 |
return !valid ? valid : item.valid; |
| 46 |
}, true); |
| 47 |
}; |
| 48 |
|
| 49 |
/** |
| 50 |
* Handles submitting the form. |
| 51 |
*/ |
| 52 |
const handleSubmit = () => { |
| 53 |
if (isSolrEditable) { |
| 54 |
dispatch({ type: 'REDUCE_SOLR_TO_STATE' }); |
| 55 |
} |
| 56 |
|
| 57 |
dispatch({ type: 'VALIDATE_ALL' }); |
| 58 |
dispatch({ type: 'REDUCE_STATE_TO_SOLR' }); |
| 59 |
dispatch({ type: 'SUBMIT' }); |
| 60 |
}; |
| 61 |
|
| 62 |
/** |
| 63 |
* Handle toggling the editor type. |
| 64 |
*/ |
| 65 |
const handleToggleAdvance = () => { |
| 66 |
if (isSolrEditable) { |
| 67 |
dispatch({ type: 'REDUCE_SOLR_TO_STATE' }); |
| 68 |
} else { |
| 69 |
dispatch({ type: 'REDUCE_STATE_TO_SOLR' }); |
| 70 |
} |
| 71 |
|
| 72 |
dispatch({ type: 'SET_SOLR_EDITABLE', data: !isSolrEditable }); |
| 73 |
}; |
| 74 |
|
| 75 |
useEffect(() => { |
| 76 |
if (submit && !dirty && isValid(state)) { |
| 77 |
document.querySelector('.wrap form').submit(); |
| 78 |
} |
| 79 |
}, [submit, dirty, state]); |
| 80 |
|
| 81 |
return ( |
| 82 |
<> |
| 83 |
<h1 className="wp-heading-inline"> |
| 84 |
{pageHeading}{' '} |
| 85 |
<button onClick={handleToggleAdvance} type="button" className="page-title-action"> |
| 86 |
{isSolrEditable ? pageToggleSimpleText : pageToggleAdvanceText} |
| 87 |
</button> |
| 88 |
</h1> |
| 89 |
<p>{pageDescription}</p> |
| 90 |
|
| 91 |
{!isSolrEditable && ( |
| 92 |
<> |
| 93 |
<div className="synonym-editor synonym-editor__sets"> |
| 94 |
<h2>{`${setsTitle} (${sets.length})`}</h2> |
| 95 |
<p>{setsDescription}</p> |
| 96 |
<SetsEditor sets={sets} /> |
| 97 |
</div> |
| 98 |
<div className="synonym-editor synonym-editor__alteratives"> |
| 99 |
<h2>{`${alternativesTitle} (${alternatives.length})`}</h2> |
| 100 |
<p>{alternativesDescription}</p> |
| 101 |
<AlterativesEditor alternatives={alternatives} /> |
| 102 |
</div> |
| 103 |
</> |
| 104 |
)} |
| 105 |
|
| 106 |
<div className="synonym-editor synonym-editor__solr"> |
| 107 |
{isSolrVisible && <h2>{solrTitle}</h2>} |
| 108 |
{isSolrVisible && <p>{solrDescription}</p>} |
| 109 |
<SolrEditor /> |
| 110 |
</div> |
| 111 |
|
| 112 |
<input |
| 113 |
type="hidden" |
| 114 |
name="synonyms_editor_mode" |
| 115 |
value={isSolrEditable ? 'advanced' : 'simple'} |
| 116 |
/> |
| 117 |
|
| 118 |
<div className="synonym-btn-group"> |
| 119 |
<button onClick={handleSubmit} type="button" className="button button-primary"> |
| 120 |
{submitText} |
| 121 |
</button> |
| 122 |
</div> |
| 123 |
</> |
| 124 |
); |
| 125 |
}; |
| 126 |
|
| 127 |
export default SynonymsEditor; |
| 128 |
|