Skip to content

Commit

Permalink
ui: connect delete button in new holdingpen
Browse files Browse the repository at this point in the history
  • Loading branch information
karolina-siemieniuk-morawska committed Aug 28, 2024
1 parent 2db006d commit a3a5bb2
Show file tree
Hide file tree
Showing 9 changed files with 487 additions and 274 deletions.
3 changes: 3 additions & 0 deletions ui/src/actions/actionTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,3 +151,6 @@ export const HOLDINGPEN_RESOLVE_ACTION_SUCCESS =
'HOLDINGPEN_RESOLVE_ACTION_SUCCESS';
export const HOLDINGPEN_RESOLVE_ACTION_ERROR =
'HOLDINGPEN_RESOLVE_ACTION_ERROR';
export const HOLDINGPEN_DELETE_REQUEST = 'HOLDINGPEN_DELETE_REQUEST';
export const HOLDINGPEN_DELETE_SUCCESS = 'HOLDINGPEN_DELETE_SUCCESS';
export const HOLDINGPEN_DELETE_ERROR = 'HOLDINGPEN_DELETE_ERROR';
49 changes: 49 additions & 0 deletions ui/src/actions/holdingpen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ import {
HOLDINGPEN_RESOLVE_ACTION_SUCCESS,
HOLDINGPEN_RESOLVE_ACTION_ERROR,
HOLDINGPEN_LOGIN_REQUEST,
HOLDINGPEN_DELETE_SUCCESS,
HOLDINGPEN_DELETE_ERROR,
HOLDINGPEN_DELETE_REQUEST,
} from './actionTypes';
import {
BACKOFFICE_API,
Expand All @@ -35,6 +38,8 @@ import {
notifyLoginError,
notifyActionError,
notifyActionSuccess,
notifyDeleteSuccess,
notifyDeleteError,
} from '../holdingpen-new/notifications';
import { refreshToken } from '../holdingpen-new/utils/utils';

Expand Down Expand Up @@ -318,3 +323,47 @@ export function resolveAction(
}
};
}

// DELETE ACTIONS

export const deletingWorkflow = () => {
return {
type: HOLDINGPEN_DELETE_REQUEST,
};
};

export const deleteWorkflowSuccess = () => {
return {
type: HOLDINGPEN_DELETE_SUCCESS,
};
};

export const deleteWorkflowError = (errorPayload: { error: Error }) => {
return {
type: HOLDINGPEN_DELETE_ERROR,
payload: { ...errorPayload },
};
};

export function deleteWorkflow(
id: string
): (dispatch: ActionCreator<Action>) => Promise<void> {
return async (dispatch) => {
dispatch(deletingWorkflow());
try {
await httpClient.delete(`${BACKOFFICE_API}/${id}/`);

dispatch(deleteWorkflowSuccess());
notifyDeleteSuccess();
} catch (err) {
const { error } = httpErrorToActionPayload(err);

dispatch(deleteWorkflowError(error));
notifyDeleteError(
(typeof error?.error === 'string'
? error?.error
: error?.error?.detail) || 'An error occurred'
);
}
};
}
2 changes: 1 addition & 1 deletion ui/src/common/components/EmptyOrChildren.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const EmptyOrChildren = ({
}: {
data: any;
children: JSX.Element;
title: string;
title: string | JSX.Element;
description?: string;
}) => {
return isEmptyCollection(data) ? (
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.delete-modal {
.ant-modal-footer {
border-top: none;
}
}
48 changes: 48 additions & 0 deletions ui/src/holdingpen-new/components/DeleteWorkflow/DeleteWorkflow.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import React, { useState } from 'react';
import { Button, Modal } from 'antd';
import { Action, ActionCreator } from 'redux';

import './DeleteWorkflow.less';
import { deleteWorkflow } from '../../../actions/holdingpen';

const DeleteWorkflow: React.FC<{
dispatch: ActionCreator<Action>;
id: string;
}> = ({ dispatch, id }) => {
const [open, setOpen] = useState(false);

const showModal = () => {
setOpen(true);
};

const hideModal = () => {
setOpen(false);
};

return (
<>
<Button className="font-white bg-error" onClick={showModal}>
Delete
</Button>
<Modal
title="Modal"
open={open}
onOk={() => {
dispatch(deleteWorkflow(id));
hideModal();
}}
onCancel={hideModal}
okText="Confirm"
cancelText="Cancel"
className="delete-modal"
>
<p>
Are you sure you want to delete workflow? This operation is
unreversable.
</p>
</Modal>
</>
);
};

export default DeleteWorkflow;
56 changes: 56 additions & 0 deletions ui/src/holdingpen-new/components/__tests__/DeleteWorkflow.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import React from 'react';
import { fireEvent, render, screen, waitFor } from '@testing-library/react';
import '@testing-library/jest-dom/extend-expect';
import userEvent from '@testing-library/user-event';

import DeleteWorkflow from '../DeleteWorkflow/DeleteWorkflow';

jest.mock('../../../actions/holdingpen', () => ({
deleteWorkflow: jest.fn((id) => ({
type: 'HOLDINGPEN_DELETE_REQUEST',
payload: id,
})),
}));

describe('DeleteWorkflow component', () => {
const mockDispatch = jest.fn();
const mockId = 'test-id';

beforeEach(() => {
mockDispatch.mockClear();
});

it('should render delete button', () => {
render(<DeleteWorkflow dispatch={mockDispatch} id={mockId} />);
expect(screen.getByText('Delete')).toBeInTheDocument();
});

it('should open the modal when the delete button is clicked', async () => {
render(<DeleteWorkflow dispatch={mockDispatch} id={mockId} />);

await waitFor(() => userEvent.click(screen.getByText('Delete')));

await waitFor(() =>
expect(
screen.getByText(
'Are you sure you want to delete workflow? This operation is unreversable.'
)
).toBeVisible()
);
});

it('should call dispatch with deleteWorkflow and close the modal on confirm', async () => {
render(<DeleteWorkflow dispatch={mockDispatch} id={mockId} />);

await waitFor(() => userEvent.click(screen.getByText('Delete')));

await waitFor(() => userEvent.click(screen.getByText('Confirm')));

await waitFor(() =>
expect(mockDispatch).toHaveBeenCalledWith({
type: 'HOLDINGPEN_DELETE_REQUEST',
payload: mockId,
})
);
});
});
Loading

0 comments on commit a3a5bb2

Please sign in to comment.