Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Apple-188 - Section Mapping Automations #1196

Merged
merged 24 commits into from
Nov 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
40635bb
Update text domain
efuller Nov 14, 2024
118f041
Set up an admin menu page for section mappings
efuller Nov 14, 2024
9dc0523
Set up an entry point and get React wired up
efuller Nov 14, 2024
097cf63
Add section mappings entry point as a subfolder of admin settings
efuller Nov 14, 2024
dacf48f
Add in initial mappings layout
efuller Nov 15, 2024
5486fd5
Format markup
efuller Nov 15, 2024
afc622c
Add in a prop for hiding fields
efuller Nov 15, 2024
7fa3b21
Allow the value to be passed in when adding a rule
efuller Nov 15, 2024
4e4cbb8
Hide the field and value columns / fields
efuller Nov 15, 2024
fbed40e
Add a data attribute to each role that represents its current index
efuller Nov 18, 2024
f1ba582
Split up rules between section and non-section based rules
efuller Nov 18, 2024
b6cbad5
Add in ability to add new section rule
efuller Nov 18, 2024
a3ae2fe
Tweak height of table nav for sections
efuller Nov 18, 2024
ec45564
Rename row arrays to be more meaningful
efuller Nov 18, 2024
62901dc
Add in a check to make sure rows cannot be dragged into another table
efuller Nov 18, 2024
b3b226d
i18n headings
efuller Nov 18, 2024
37d86ed
Cast to and from to numbers
efuller Nov 18, 2024
fb33523
Remove section mapping config and files
efuller Nov 18, 2024
891b751
Remove hideFields prop
efuller Nov 18, 2024
9df0eb6
Add in the ability to hide field types from the field dropdown
efuller Nov 18, 2024
ca5d820
Refactor to remove forEach as well as param from generateRule function
efuller Nov 18, 2024
40da581
Chores: eslint
efuller Nov 18, 2024
c7208e9
Update docblock
efuller Nov 18, 2024
87b1b8c
Add in ability to hide columns
efuller Nov 19, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
134 changes: 105 additions & 29 deletions assets/js/admin-settings/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,14 @@ function AdminSettings() {
const { apple_news_automation: ruleList } = settings;
const { fields } = AppleNewsAutomationConfig;

if (!ruleList) {
return null;
}

const LINK_SECTIONS = 'links.sections';
const allFieldsButSections = Object.keys(fields).filter((field) => field !== LINK_SECTIONS);
const sectionsOnly = [LINK_SECTIONS];

/**
* Helper function for pushing to in-memory settings inside useSiteOptions.
* @param {array} updatedRules - The new array of rules.
Expand All @@ -33,12 +41,13 @@ function AdminSettings() {

/**
* Adds a new empty rule to the end of the list.
* @param {string} field - The field to add a rule for.
*/
const addRule = () => {
const addRule = (field = '') => {
updateSettings([
...(ruleList ?? []),
{
field: '',
field,
taxonomy: '',
term_id: 0,
value: '',
Expand Down Expand Up @@ -75,11 +84,103 @@ function AdminSettings() {
updateSettings(updatedRules);
};

/**
* Generates a rule component.
* @param {object} item - The rule object.
* @param {number} index - The index of the rule.
* @return {React.JSX.Element}
*/
const generateRule = (item, index) => {
const hideFieldTypes = item.field === LINK_SECTIONS ? allFieldsButSections : sectionsOnly;
const hideColumns = item.field === LINK_SECTIONS ? ['field'] : [];

return (
<Rule
busy={busy}
field={item.field}
key={index} // eslint-disable-line react/no-array-index-key
onDelete={() => updateSettings(deleteAtIndex(ruleList, index))}
onDragEnd={(e) => {
const targetRow = document
.elementFromPoint(e.clientX, e.clientY)
.closest('.apple-news-automation-row');
// Checking for the parent element ensures that the row is in the same table.
if (targetRow && targetRow.parentElement === e.currentTarget.parentElement) {
reorderRule(
Number(e.currentTarget.dataset.index),
Number(targetRow.dataset.index),
);
}
}}
onUpdate={(key, value) => updateRule(index, key, value)}
taxonomy={item.taxonomy}
termId={item.term_id}
value={item.value}
index={index}
hideFieldTypes={hideFieldTypes}
hideColumns={hideColumns}
/>
);
};

// Split the rows into sections and non-sections.
const { sectionAutomationRows, additionalAutomationRows } = ruleList.reduce(
(acc, item, index) => {
const ruleComponent = generateRule(item, index);

if (item.field === LINK_SECTIONS) {
acc.sectionAutomationRows.push(ruleComponent);
} else {
acc.additionalAutomationRows.push(ruleComponent);
}

return acc;
},
{ sectionAutomationRows: [], additionalAutomationRows: [] },
);

return (
<div className="apple-news-options__wrapper">
<h1>{__('Apple News Automation', 'apple-news')}</h1>
<p>{__('Configure automation rules below to automatically apply certain settings based on the taxonomy terms applied to each post.', 'apple-news')}</p>
<p><a target="_blank" rel="noreferrer" href="https://github.com/alleyinteractive/apple-news/wiki/Automation">{__('For more information on how automation works, visit our wiki.', 'apple-news')}</a></p>
<p>
<a target="_blank" rel="noreferrer" href="https://github.com/alleyinteractive/apple-news/wiki/Automation">{__('For more information on how automation works, visit our wiki.', 'apple-news')}</a>
</p>
<h2 className="title">{__('Section Mapping Automation', 'apple-news')}</h2>
<table className="wp-list-table widefat fixed striped">
<thead>
<tr>
<th id="apple-news-automation-column-taxonomy" scope="col">{__('Taxonomy', 'apple-news')}</th>
<th id="apple-news-automation-column-term" scope="col">{__('Term', 'apple-news')}</th>
<th id="apple-news-automation-column-value" scope="col">{__('Section', 'apple-news')}</th>
<th id="apple-news-automation-column-delete" scope="col">{__('Delete?', 'apple-news')}</th>
</tr>
</thead>
<tbody>
{sectionAutomationRows}
</tbody>
</table>
<div className="tablenav bottom" style={{ height: '50px' }}>
<div className="alignleft actions">
<Button
disabled={busy}
isSecondary
onClick={() => addRule(LINK_SECTIONS)}
>
{__('Add Rule', 'apple-news')}
</Button>
{' '}
<Button
disabled={busy}
isPrimary
onClick={saveSettings}
>
{__('Save Settings', 'apple-news')}
</Button>
</div>
</div>
<hr />
<h2 className="title">{__('Additional Automation', 'apple-news')}</h2>
<table className="wp-list-table widefat fixed striped">
<thead>
<tr>
Expand All @@ -91,32 +192,7 @@ function AdminSettings() {
</tr>
</thead>
<tbody>
{!loading && ruleList ? (
ruleList.map((item, index) => (
<Rule
busy={busy}
field={item.field}
key={index} // eslint-disable-line react/no-array-index-key
onDelete={() => updateSettings(deleteAtIndex(ruleList, index))}
onDragEnd={(e) => {
const targetRow = document
.elementFromPoint(e.clientX, e.clientY)
.closest('.apple-news-automation-row');
if (targetRow) {
reorderRule(
index,
Array.from(targetRow.parentElement.querySelectorAll('tr'))
.indexOf(targetRow),
);
}
}}
onUpdate={(key, value) => updateRule(index, key, value)}
taxonomy={item.taxonomy}
termId={item.term_id}
value={item.value}
/>
))
) : null}
{additionalAutomationRows}
</tbody>
</table>
<div className="tablenav bottom">
Expand Down
Loading