Refactor to not define components inside a component
This commit is contained in:
committed by
M. Shull
parent
ab04cbab27
commit
c8c47eb37a
@ -1,19 +1,111 @@
|
|||||||
import React, { useLayoutEffect } from 'react';
|
import React, { useLayoutEffect } from 'react';
|
||||||
import PropTypes from 'prop-types';
|
import PropTypes from 'prop-types';
|
||||||
import MailPoet from 'mailpoet';
|
import MailPoet from 'mailpoet';
|
||||||
import _ from 'underscore';
|
|
||||||
|
|
||||||
import generateColumnSelection from './generate_column_selection.jsx';
|
import generateColumnSelection from './generate_column_selection.jsx';
|
||||||
import matchColumns from './match_columns.jsx';
|
import matchColumns from './match_columns.jsx';
|
||||||
|
|
||||||
const MAX_SUBSCRIBERS_SHOWN = 10;
|
const MAX_SUBSCRIBERS_SHOWN = 10;
|
||||||
|
|
||||||
|
function ColumnDataMatch({ header, subscribers }) {
|
||||||
|
const matchedColumnTypes = matchColumns(subscribers, header);
|
||||||
|
// selectedColumns = _.pluck(matchedColumnTypes, 'column_id');
|
||||||
|
return (
|
||||||
|
<tr>
|
||||||
|
<th>{MailPoet.I18n.t('matchData')}</th>
|
||||||
|
{
|
||||||
|
matchedColumnTypes.map((columnType, i) => (
|
||||||
|
// eslint-disable-next-line react/no-array-index-key
|
||||||
|
<th key={columnType.column_id + i}>
|
||||||
|
<select
|
||||||
|
className="mailpoet_subscribers_column_data_match"
|
||||||
|
data-column-id={columnType.column_id}
|
||||||
|
data-validation-rule="false"
|
||||||
|
data-column-index={i}
|
||||||
|
id={`column_${i}`}
|
||||||
|
/>
|
||||||
|
</th>
|
||||||
|
))
|
||||||
|
}
|
||||||
|
</tr>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
ColumnDataMatch.propTypes = {
|
||||||
|
header: PropTypes.arrayOf(PropTypes.string).isRequired,
|
||||||
|
subscribers: PropTypes.arrayOf(PropTypes.arrayOf(PropTypes.string)).isRequired,
|
||||||
|
};
|
||||||
|
|
||||||
|
function Header({ header }) {
|
||||||
|
return (
|
||||||
|
<tr className="mailpoet_header">
|
||||||
|
<td />
|
||||||
|
{header.map(headerName => <td key={headerName}>{headerName}</td>)}
|
||||||
|
</tr>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
Header.propTypes = {
|
||||||
|
header: PropTypes.arrayOf(PropTypes.string).isRequired,
|
||||||
|
};
|
||||||
|
|
||||||
|
function Subscriber({ subscriber, index }) {
|
||||||
|
return (
|
||||||
|
<>
|
||||||
|
<td>{index}</td>
|
||||||
|
{/* eslint-disable-next-line react/no-array-index-key */}
|
||||||
|
{subscriber.map((field, i) => <td key={field + index + i}>{field}</td>)}
|
||||||
|
</>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
Subscriber.propTypes = {
|
||||||
|
subscriber: PropTypes.arrayOf(PropTypes.string).isRequired,
|
||||||
|
index: PropTypes.node.isRequired,
|
||||||
|
};
|
||||||
|
|
||||||
|
function Subscribers({ subscribers, subscribersCount }) {
|
||||||
|
const filler = '. . .';
|
||||||
|
const fillerArray = Array(subscribers[0].length).fill(filler);
|
||||||
|
return (
|
||||||
|
<>
|
||||||
|
{
|
||||||
|
subscribers
|
||||||
|
.slice(0, MAX_SUBSCRIBERS_SHOWN)
|
||||||
|
.map((subscriber, i) => (
|
||||||
|
<tr key={subscriber[0]}>
|
||||||
|
<Subscriber subscriber={subscriber} index={i + 1} />
|
||||||
|
</tr>
|
||||||
|
))
|
||||||
|
}
|
||||||
|
{
|
||||||
|
subscribersCount > MAX_SUBSCRIBERS_SHOWN + 1
|
||||||
|
? <tr key="filler"><Subscriber subscriber={fillerArray} index={filler} /></tr>
|
||||||
|
: null
|
||||||
|
}
|
||||||
|
{
|
||||||
|
subscribersCount > MAX_SUBSCRIBERS_SHOWN
|
||||||
|
? (
|
||||||
|
<tr key={subscribers[subscribersCount - 1][0]}>
|
||||||
|
<Subscriber
|
||||||
|
subscriber={subscribers[subscribersCount - 1]}
|
||||||
|
index={subscribersCount}
|
||||||
|
/>
|
||||||
|
</tr>
|
||||||
|
)
|
||||||
|
: null
|
||||||
|
}
|
||||||
|
</>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
Subscribers.propTypes = {
|
||||||
|
subscribersCount: PropTypes.number.isRequired,
|
||||||
|
subscribers: PropTypes.arrayOf(PropTypes.arrayOf(PropTypes.string)).isRequired,
|
||||||
|
};
|
||||||
|
|
||||||
function MatchTable({
|
function MatchTable({
|
||||||
subscribersCount,
|
subscribersCount,
|
||||||
subscribers,
|
subscribers,
|
||||||
header,
|
header,
|
||||||
}) {
|
}) {
|
||||||
let selectedColumns = [];
|
const selectedColumns = [];
|
||||||
|
|
||||||
useLayoutEffect(() => {
|
useLayoutEffect(() => {
|
||||||
generateColumnSelection((selectedOptionId, columnIndex) => {
|
generateColumnSelection((selectedOptionId, columnIndex) => {
|
||||||
@ -21,98 +113,15 @@ function MatchTable({
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
function ColumnDataMatch() {
|
|
||||||
const matchedColumnTypes = matchColumns(subscribers, header);
|
|
||||||
selectedColumns = _.pluck(matchedColumnTypes, 'column_id');
|
|
||||||
return (
|
|
||||||
<tr>
|
|
||||||
<th>{MailPoet.I18n.t('matchData')}</th>
|
|
||||||
{
|
|
||||||
matchedColumnTypes.map((columnType, i) => (
|
|
||||||
// eslint-disable-next-line react/no-array-index-key
|
|
||||||
<th key={columnType.column_id + i}>
|
|
||||||
<select
|
|
||||||
className="mailpoet_subscribers_column_data_match"
|
|
||||||
data-column-id={columnType.column_id}
|
|
||||||
data-validation-rule="false"
|
|
||||||
data-column-index={i}
|
|
||||||
id={`column_${i}`}
|
|
||||||
/>
|
|
||||||
</th>
|
|
||||||
))
|
|
||||||
}
|
|
||||||
</tr>
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
function Header() {
|
|
||||||
return (
|
|
||||||
<tr className="mailpoet_header">
|
|
||||||
<td />
|
|
||||||
{header.map(headerName => <td key={headerName}>{headerName}</td>)}
|
|
||||||
</tr>
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
function Subscriber({ subscriber, index }) {
|
|
||||||
return (
|
|
||||||
<>
|
|
||||||
<td>{index}</td>
|
|
||||||
{/* eslint-disable-next-line react/no-array-index-key */}
|
|
||||||
{subscriber.map((field, i) => <td key={field + index + i}>{field}</td>)}
|
|
||||||
</>
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
Subscriber.propTypes = {
|
|
||||||
subscriber: PropTypes.arrayOf(PropTypes.string).isRequired,
|
|
||||||
index: PropTypes.node.isRequired,
|
|
||||||
};
|
|
||||||
|
|
||||||
function Subscribers() {
|
|
||||||
const filler = '. . .';
|
|
||||||
const fillerArray = Array(subscribers[0].length).fill(filler);
|
|
||||||
return (
|
|
||||||
<>
|
|
||||||
{
|
|
||||||
subscribers
|
|
||||||
.slice(0, MAX_SUBSCRIBERS_SHOWN)
|
|
||||||
.map((subscriber, i) => (
|
|
||||||
<tr key={subscriber[0]}>
|
|
||||||
<Subscriber subscriber={subscriber} index={i + 1} />
|
|
||||||
</tr>
|
|
||||||
))
|
|
||||||
}
|
|
||||||
{
|
|
||||||
subscribersCount > MAX_SUBSCRIBERS_SHOWN + 1
|
|
||||||
? <tr key="filler"><Subscriber subscriber={fillerArray} index={filler} /></tr>
|
|
||||||
: null
|
|
||||||
}
|
|
||||||
{
|
|
||||||
subscribersCount > MAX_SUBSCRIBERS_SHOWN
|
|
||||||
? (
|
|
||||||
<tr key={subscribers[subscribersCount - 1][0]}>
|
|
||||||
<Subscriber
|
|
||||||
subscriber={subscribers[subscribersCount - 1]}
|
|
||||||
index={subscribersCount}
|
|
||||||
/>
|
|
||||||
</tr>
|
|
||||||
)
|
|
||||||
: null
|
|
||||||
}
|
|
||||||
</>
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="subscribers_data">
|
<div className="subscribers_data">
|
||||||
<table className="mailpoet_subscribers widefat fixed">
|
<table className="mailpoet_subscribers widefat fixed">
|
||||||
<thead>
|
<thead>
|
||||||
<ColumnDataMatch />
|
<ColumnDataMatch header={header} subscribers={subscribers} />
|
||||||
</thead>
|
</thead>
|
||||||
<tbody>
|
<tbody>
|
||||||
<Header />
|
<Header header={header} />
|
||||||
<Subscribers />
|
<Subscribers subscribers={subscribers} subscribersCount={subscribersCount} />
|
||||||
</tbody>
|
</tbody>
|
||||||
</table>
|
</table>
|
||||||
</div>
|
</div>
|
||||||
|
Reference in New Issue
Block a user