Convert bulk action to await
MAILPOET-5395
This commit is contained in:
committed by
Aschepikov
parent
9a8d6de626
commit
d429f538af
@@ -5,7 +5,7 @@ import { __, _n, sprintf } from '@wordpress/i18n';
|
|||||||
import { DynamicSegment, DynamicSegmentAction } from '../types';
|
import { DynamicSegment, DynamicSegmentAction } from '../types';
|
||||||
import { MailPoet } from '../../../mailpoet';
|
import { MailPoet } from '../../../mailpoet';
|
||||||
import { storeName } from '../store';
|
import { storeName } from '../store';
|
||||||
import { DynamicSegmentResponse } from '../../../ajax';
|
import { DynamicSegmentResponse, isErrorResponse } from '../../../ajax';
|
||||||
|
|
||||||
async function bulkAction(
|
async function bulkAction(
|
||||||
action: DynamicSegmentAction,
|
action: DynamicSegmentAction,
|
||||||
@@ -14,75 +14,71 @@ async function bulkAction(
|
|||||||
if (!action) {
|
if (!action) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
void MailPoet.Ajax.post({
|
try {
|
||||||
api_version: 'v1',
|
const response: DynamicSegmentResponse = await MailPoet.Ajax.post({
|
||||||
endpoint: 'dynamic_segments',
|
api_version: 'v1',
|
||||||
action: 'bulk_action',
|
endpoint: 'dynamic_segments',
|
||||||
data: {
|
action: 'bulk_action',
|
||||||
action,
|
data: {
|
||||||
listing: {
|
action,
|
||||||
selection: segments.map((segment) => segment.id),
|
listing: {
|
||||||
|
selection: segments.map((segment) => segment.id),
|
||||||
|
},
|
||||||
},
|
},
|
||||||
},
|
});
|
||||||
})
|
|
||||||
.then((response: DynamicSegmentResponse) => {
|
if (response.meta.errors && response.meta.errors.length > 0) {
|
||||||
if (response.meta.errors && response.meta.errors.length > 0) {
|
response.meta.errors.forEach(
|
||||||
response.meta.errors.forEach(
|
(error: string) => void dispatch(noticesStore).createErrorNotice(error),
|
||||||
(error: string) =>
|
);
|
||||||
void dispatch(noticesStore).createErrorNotice(error),
|
}
|
||||||
);
|
|
||||||
|
const count = response.meta.count;
|
||||||
|
if (count > 0) {
|
||||||
|
let successMessage = '';
|
||||||
|
switch (action) {
|
||||||
|
case 'trash':
|
||||||
|
successMessage = sprintf(
|
||||||
|
/* translators: %d - number of segments */
|
||||||
|
_n(
|
||||||
|
'Segment moved to trash.',
|
||||||
|
'%d segments moved to trash.',
|
||||||
|
count,
|
||||||
|
'mailpoet',
|
||||||
|
),
|
||||||
|
count,
|
||||||
|
);
|
||||||
|
break;
|
||||||
|
case 'delete':
|
||||||
|
successMessage = sprintf(
|
||||||
|
/* translators: %d - number of segments */
|
||||||
|
_n(
|
||||||
|
'Segment permanently deleted.',
|
||||||
|
'%d segments permanently deleted.',
|
||||||
|
count,
|
||||||
|
'mailpoet',
|
||||||
|
),
|
||||||
|
count,
|
||||||
|
);
|
||||||
|
break;
|
||||||
|
case 'restore':
|
||||||
|
successMessage = sprintf(
|
||||||
|
/* translators: %d - number of segments */
|
||||||
|
_n('Segment restored.', '%d segments restored.', count, 'mailpoet'),
|
||||||
|
count,
|
||||||
|
);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
const count = response.meta.count;
|
void dispatch(noticesStore).createSuccessNotice(successMessage);
|
||||||
if (count > 0) {
|
void dispatch(storeName).loadDynamicSegments();
|
||||||
let successMessage = '';
|
}
|
||||||
switch (action) {
|
} catch (errorResponse: unknown) {
|
||||||
case 'trash':
|
if (isErrorResponse(errorResponse)) {
|
||||||
successMessage = sprintf(
|
|
||||||
/* translators: %d - number of segments */
|
|
||||||
_n(
|
|
||||||
'Segment moved to trash.',
|
|
||||||
'%d segments moved to trash.',
|
|
||||||
count,
|
|
||||||
'mailpoet',
|
|
||||||
),
|
|
||||||
count,
|
|
||||||
);
|
|
||||||
break;
|
|
||||||
case 'delete':
|
|
||||||
successMessage = sprintf(
|
|
||||||
/* translators: %d - number of segments */
|
|
||||||
_n(
|
|
||||||
'Segment permanently deleted.',
|
|
||||||
'%d segments permanently deleted.',
|
|
||||||
count,
|
|
||||||
'mailpoet',
|
|
||||||
),
|
|
||||||
count,
|
|
||||||
);
|
|
||||||
break;
|
|
||||||
case 'restore':
|
|
||||||
successMessage = sprintf(
|
|
||||||
/* translators: %d - number of segments */
|
|
||||||
_n(
|
|
||||||
'Segment restored.',
|
|
||||||
'%d segments restored.',
|
|
||||||
count,
|
|
||||||
'mailpoet',
|
|
||||||
),
|
|
||||||
count,
|
|
||||||
);
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
void dispatch(noticesStore).createSuccessNotice(successMessage);
|
|
||||||
void dispatch(storeName).loadDynamicSegments();
|
|
||||||
}
|
|
||||||
})
|
|
||||||
.fail((response: ErrorResponse) => {
|
|
||||||
let errorMessage = '';
|
let errorMessage = '';
|
||||||
if (response.errors) {
|
if (errorResponse.errors) {
|
||||||
response.errors.forEach(
|
errorResponse.errors.forEach(
|
||||||
(error) =>
|
(error) =>
|
||||||
void dispatch(noticesStore).createErrorNotice(error.message),
|
void dispatch(noticesStore).createErrorNotice(error.message),
|
||||||
{ explicitDismiss: true },
|
{ explicitDismiss: true },
|
||||||
@@ -105,7 +101,8 @@ async function bulkAction(
|
|||||||
explicitDismiss: true,
|
explicitDismiss: true,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
});
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
type ActionConfirmProps = {
|
type ActionConfirmProps = {
|
||||||
@@ -113,6 +110,7 @@ type ActionConfirmProps = {
|
|||||||
selected: DynamicSegment[];
|
selected: DynamicSegment[];
|
||||||
onClose: () => void;
|
onClose: () => void;
|
||||||
};
|
};
|
||||||
|
|
||||||
export function ActionConfirm({
|
export function ActionConfirm({
|
||||||
action,
|
action,
|
||||||
selected,
|
selected,
|
||||||
|
Reference in New Issue
Block a user