Add Trash button

[MAILPOET-4417]
This commit is contained in:
David Remer
2022-08-11 11:13:34 +03:00
committed by Veljko V
parent ba58022259
commit deb2c74a3a
2 changed files with 70 additions and 0 deletions

View File

@ -0,0 +1,66 @@
import apiFetch from '@wordpress/api-fetch';
import { Button } from '@wordpress/components';
import { StoreDescriptor, useDispatch, useSelect } from '@wordpress/data';
import { store as noticesStore } from '@wordpress/notices';
import { confirmAlert } from '../../../../common/confirm_alert';
import { store } from '../../store';
import { Workflow } from '../workflow/types';
import { MailPoet } from '../../../../mailpoet';
export function TrashButton(): JSX.Element {
const { createErrorNotice } = useDispatch(noticesStore as StoreDescriptor);
const { workflow } = useSelect(
(select) => ({
workflow: select(store).getWorkflowData(),
}),
[],
);
const trash = () => {
apiFetch({
path: `/workflows/${workflow.id}`,
method: 'PUT',
data: {
...workflow,
status: 'trash',
},
})
.then(({ data }: { data: Workflow }) => {
if (data.status !== 'trash') {
void createErrorNotice('An error occurred!', {
explicitDismiss: true,
});
return;
}
window.location.href = MailPoet.urls.automationListing;
})
.catch((): void => {
void createErrorNotice('An error occurred!', {
explicitDismiss: true,
});
});
return true;
};
return (
<Button
isSecondary
isDestructive
onClick={() => {
confirmAlert({
title: 'Delete workflow',
message: `You are about to delete the “${workflow.name}” workflow`,
cancelLabel: 'Cancel',
confirmLabel: 'Yes, delete',
onConfirm: () => {
trash();
},
});
}}
>
Move to Trash
</Button>
);
}

View File

@ -2,6 +2,7 @@ import { PanelBody, PanelRow } from '@wordpress/components';
import { useSelect } from '@wordpress/data';
import { store } from '../../../store';
import { PlainBodyTitle } from '../../panel';
import { TrashButton } from '../../actions/trash-button';
export function WorkflowSidebar(): JSX.Element {
const { workflowData } = useSelect(
@ -47,6 +48,9 @@ export function WorkflowSidebar(): JSX.Element {
<PanelRow>
<strong>Author</strong> {workflowData.author.name}
</PanelRow>
<PanelRow>
<TrashButton />
</PanelRow>
</PanelBody>
);
}