Refactor dynamic/list.jsx to dynamic/list.tsx

[MAILPOET-5407]
This commit is contained in:
Rodrigo Primo
2023-06-15 16:29:35 -03:00
committed by Aschepikov
parent 4583b80fa6
commit 34937e99c0
3 changed files with 58 additions and 29 deletions

View File

@ -1,12 +1,42 @@
import { Link, withRouter } from 'react-router-dom'; import { Link, withRouter } from 'react-router-dom';
import PropTypes from 'prop-types';
import ReactStringReplace from 'react-string-replace'; import ReactStringReplace from 'react-string-replace';
import { MailPoet } from 'mailpoet'; import { MailPoet } from 'mailpoet';
import { Listing } from 'listing/listing.jsx'; import { Listing } from 'listing/listing.jsx';
import { escapeHTML } from '@wordpress/escape-html'; import { escapeHTML } from '@wordpress/escape-html';
import { SegmentResponse } from 'segments/types';
const columns = [ type ColumnType = {
name: string;
label: string;
sortable: boolean;
};
type DynamicSegmentItem = {
id: number;
name: string;
description: string;
count_all: string;
count_subscribed: string;
created_at: string;
is_plugin_missing: boolean;
subscribers_url: string;
missing_plugin_message?: {
message: string;
link?: string;
};
};
type DynamicSegmentListComponentProps = {
location: {
pathname: string;
};
match: {
params;
};
};
const columns: ColumnType[] = [
{ {
name: 'name', name: 'name',
label: MailPoet.I18n.t('nameColumn'), label: MailPoet.I18n.t('nameColumn'),
@ -85,10 +115,10 @@ const itemActions = [
{ {
name: 'edit', name: 'edit',
className: 'mailpoet-hide-on-mobile', className: 'mailpoet-hide-on-mobile',
link: (item) => ( link: (item: DynamicSegmentItem) => (
<Link to={`/edit-segment/${item.id}`}>{MailPoet.I18n.t('edit')}</Link> <Link to={`/edit-segment/${item.id}`}>{MailPoet.I18n.t('edit')}</Link>
), ),
display: (item) => !item.is_plugin_missing, display: (item: DynamicSegmentItem) => !item.is_plugin_missing,
}, },
{ {
name: 'duplicate_segment', name: 'duplicate_segment',
@ -103,13 +133,13 @@ const itemActions = [
id: item.id, id: item.id,
}, },
}) })
.done((response) => { .done((response: SegmentResponse) => {
MailPoet.Notice.success( MailPoet.Notice.success(
MailPoet.I18n.t('segmentDuplicated').replace( MailPoet.I18n.t('segmentDuplicated').replace(
'%1$s', '%1$s',
escapeHTML(response.data.name), escapeHTML(response.data.name),
{ scroll: true },
), ),
{ scroll: true },
); );
refresh(); refresh();
}) })
@ -123,10 +153,10 @@ const itemActions = [
{ {
name: 'edit_disabled', name: 'edit_disabled',
className: 'mailpoet-hide-on-mobile mailpoet-disabled', className: 'mailpoet-hide-on-mobile mailpoet-disabled',
link: (item) => ( link: (item: DynamicSegmentItem) => (
<Link to={`/edit-segment/${item.id}`}>{MailPoet.I18n.t('edit')}</Link> <Link to={`/edit-segment/${item.id}`}>{MailPoet.I18n.t('edit')}</Link>
), ),
display: (item) => item.is_plugin_missing, display: (item: DynamicSegmentItem) => item.is_plugin_missing,
}, },
{ {
name: 'view_subscribers', name: 'view_subscribers',
@ -148,7 +178,7 @@ const bulkActions = [
}, },
]; ];
function renderItem(item, actions) { function renderItem(item: DynamicSegmentItem, actions) {
return ( return (
<> <>
<td <td
@ -163,7 +193,7 @@ function renderItem(item, actions) {
</td> </td>
{item.is_plugin_missing ? ( {item.is_plugin_missing ? (
<td <td
colSpan="2" colSpan={2}
className="column mailpoet-hide-on-mobile" className="column mailpoet-hide-on-mobile"
data-colname={MailPoet.I18n.t('missingPluginMessageColumn')} data-colname={MailPoet.I18n.t('missingPluginMessageColumn')}
> >
@ -185,7 +215,7 @@ function renderItem(item, actions) {
</a> </a>
), ),
) )
: item.missing_plugin_message} : item.missing_plugin_message.message}
</td> </td>
) : ( ) : (
<> <>
@ -215,7 +245,9 @@ function renderItem(item, actions) {
); );
} }
function DynamicSegmentListComponent(props) { function DynamicSegmentListComponent(
props: DynamicSegmentListComponentProps,
): JSX.Element {
return ( return (
<> <>
<Listing <Listing
@ -249,13 +281,4 @@ function DynamicSegmentListComponent(props) {
); );
} }
DynamicSegmentListComponent.propTypes = {
location: PropTypes.object.isRequired, // eslint-disable-line react/forbid-prop-types
match: PropTypes.shape({
params: PropTypes.object, // eslint-disable-line react/forbid-prop-types
}).isRequired,
};
DynamicSegmentListComponent.displayName = 'DynamicSegmentList';
export const DynamicSegmentList = withRouter(DynamicSegmentListComponent); export const DynamicSegmentList = withRouter(DynamicSegmentListComponent);

View File

@ -11,7 +11,7 @@ import { GlobalContext, useGlobalContextValue } from 'context';
import { Notices } from 'notices/notices.jsx'; import { Notices } from 'notices/notices.jsx';
import { registerTranslations, withBoundary } from 'common'; import { registerTranslations, withBoundary } from 'common';
import { Editor } from './dynamic/editor'; import { Editor } from './dynamic/editor';
import { DynamicSegmentList } from './dynamic/list.jsx'; import { DynamicSegmentList } from './dynamic/list.tsx';
const container = document.getElementById('segments_container'); const container = document.getElementById('segments_container');

View File

@ -82,13 +82,19 @@ class DynamicSegmentsResponseBuilder {
if ($missingPlugins) { if ($missingPlugins) {
$missingPlugin = reset($missingPlugins); $missingPlugin = reset($missingPlugins);
$data['is_plugin_missing'] = true; $data['is_plugin_missing'] = true;
$data['missing_plugin_message'] = $this->segmentDependencyValidator->getCustomErrorMessage($missingPlugin)
?: $missingPluginMessage = $this->segmentDependencyValidator->getCustomErrorMessage($missingPlugin);
if ($missingPluginMessage) {
$data['missing_plugin_message'] = $missingPluginMessage;
} else {
$data['missing_plugin_message']['message'] =
sprintf( sprintf(
// translators: %s is the name of the missing plugin. // translators: %s is the name of the missing plugin.
__('Activate the %s plugin to see the number of subscribers and enable the editing of this segment.', 'mailpoet'), __('Activate the %s plugin to see the number of subscribers and enable the editing of this segment.', 'mailpoet'),
$missingPlugin $missingPlugin
); );
}
} else { } else {
$data['is_plugin_missing'] = false; $data['is_plugin_missing'] = false;
$data['missing_plugin_message'] = null; $data['missing_plugin_message'] = null;