Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extract and refactor clear queue dialog #1419

Merged
merged 1 commit into from
Sep 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion ui/cypress/support.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,5 +57,5 @@ Cypress.Commands.add('mountSample', (sample = 'test', protein = 'test') => {
Cypress.Commands.add('clearSamples', () => {
cy.findByText('Samples').click();
cy.findByRole('button', { name: /Clear sample list/u }).click('left');
cy.findByRole('button', { name: 'Ok' }).click();
cy.findByRole('button', { name: 'Clear' }).click();
});
20 changes: 11 additions & 9 deletions ui/src/components/DraggableModal.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,27 @@ import React from 'react';
import { Modal } from 'react-bootstrap';
import Draggable from 'react-draggable';

class DraggableModalDialog extends React.Component {
render() {
return (
<Draggable handle=".modal-header" defaultPosition={this.props.defaultpos}>
<Modal.Dialog {...this.props} />
</Draggable>
);
}
function DraggableModalDialog(props) {
const { defaultpos } = props;

return (
<Draggable handle=".modal-header" defaultPosition={defaultpos}>
<Modal.Dialog {...props} />
</Draggable>
);
}

export function DraggableModal(props) {
const { children } = props;

return (
<Modal
dialogAs={DraggableModalDialog}
enforceFocus={false}
backdrop="static"
{...props}
>
{props.children}
{children}
</Modal>
);
}
90 changes: 40 additions & 50 deletions ui/src/components/GenericDialog/ConfirmActionDialog.jsx
Original file line number Diff line number Diff line change
@@ -1,56 +1,46 @@
/* eslint-disable react/jsx-handler-names */
import React from 'react';
import { Modal, Button } from 'react-bootstrap';

export default class ConfirmActionDialog extends React.Component {
constructor(props) {
super(props);
this.onOkClick = this.onOkClick.bind(this);
this.onCancelClick = this.onCancelClick.bind(this);
}
function ConfirmActionDialog(props) {
const {
title,
okBtnLabel = 'OK',
cancelBtnLabel = 'Cancel',
show = false,
children,
onHide,
onOk,
onCancel,
} = props;

onOkClick() {
if (this.props.onOk) {
this.props.onOk();
}

this.props.hide();
}

onCancelClick() {
if (this.props.onCancel) {
this.props.onCancel();
}

this.props.hide();
}

render() {
return (
<Modal show={this.props.show}>
<Modal.Header>
<Modal.Title>{this.props.title}</Modal.Title>
</Modal.Header>
<Modal.Body>{this.props.message}</Modal.Body>
<Modal.Footer>
<Button variant="outline-secondary" onClick={this.onCancelClick}>
{this.props.cancelButtonText}
</Button>
<Button variant="outline-secondary" onClick={this.onOkClick}>
{this.props.okButtonText}
</Button>
</Modal.Footer>
</Modal>
);
}
return (
<Modal show={show} data-default-styles onHide={onHide}>
<Modal.Header>
<Modal.Title>{title}</Modal.Title>
</Modal.Header>
<Modal.Body>{children}</Modal.Body>
<Modal.Footer>
<Button
variant="warning"
onClick={() => {
onOk?.();
onHide();
}}
>
{okBtnLabel}
</Button>
<Button
variant="outline-secondary"
onClick={() => {
onCancel?.();
onHide();
}}
>
{cancelBtnLabel}
</Button>
</Modal.Footer>
</Modal>
);
}

ConfirmActionDialog.defaultProps = {
show: false,
title: '',
message: '',
okButtonText: 'Ok',
cancelButtonText: 'Cancel',
onOk: false,
onCancel: false,
};
export default ConfirmActionDialog;
2 changes: 2 additions & 0 deletions ui/src/components/Main.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import './rachat.css';
import { getInitialState } from '../actions/login';
import MXNavbar from './MXNavbar/MXNavbar';
import ChatWidget from './ChatWidget';
import ClearQueueDialog from './SampleGrid/ClearQueueDialog';

function Main() {
const dispatch = useDispatch();
Expand Down Expand Up @@ -54,6 +55,7 @@ function Main() {
)}

<SelectProposalContainer />
<ClearQueueDialog />
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, this dialog only makes sense in the context of the Samples page, but it doesn't really matter since its state is managed globally. I prefer to have all the dialogs in one place. It might uncover opportunities to simplify the global state and combine a few dialogs together into more general abstractions.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it would be nice to only have, if possible, one main dialog component

<TaskContainer />
<PleaseWaitDialog />
<ErrorNotificationPanel />
Expand Down
26 changes: 26 additions & 0 deletions ui/src/components/SampleGrid/ClearQueueDialog.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import React from 'react';
import ConfirmActionDialog from '../GenericDialog/ConfirmActionDialog';
import { useDispatch, useSelector } from 'react-redux';
import { clearQueue } from '../../actions/queue';
import { showConfirmClearQueueDialog } from '../../actions/general';

function ClearQueueDialog() {
const dispatch = useDispatch();
const show = useSelector(
(state) => state.general.showConfirmClearQueueDialog,
);

return (
<ConfirmActionDialog
title="Clear sample list?"
okBtnLabel="Clear"
show={show}
onOk={() => dispatch(clearQueue())}
onHide={() => dispatch(showConfirmClearQueueDialog(false))}
>
This will also <strong>clear the queue</strong>.
</ConfirmActionDialog>
);
}

export default ClearQueueDialog;
19 changes: 2 additions & 17 deletions ui/src/containers/SampleListViewContainer.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ import {
} from '../actions/sampleGrid';

import {
clearQueue,
deleteSamplesFromQueue,
setEnabledSample,
addSamplesToQueue,
Expand All @@ -54,7 +53,6 @@ import { showTaskForm } from '../actions/taskForm';

import SampleGridTableContainer from './SampleGridTableContainer';

import ConfirmActionDialog from '../components/GenericDialog/ConfirmActionDialog';
import QueueSettings from './QueueSettings.jsx';

import '../components/SampleGrid/SampleGridTable.css';
Expand Down Expand Up @@ -789,14 +787,6 @@ class SampleListViewContainer extends React.Component {
id="sampleGridContainer"
className="samples-grid-table-container mt-4"
>
<ConfirmActionDialog
title="Clear sample grid ?"
message="This will remove all samples (and collections) from the grid,
are you sure you would like to continue ?"
onOk={this.props.clearQueue}
show={this.props.general.showConfirmClearQueueDialog}
hide={this.props.confirmClearQueueHide}
/>
{this.props.loading ? (
<div
className="center-in-box"
Expand Down Expand Up @@ -863,7 +853,7 @@ class SampleListViewContainer extends React.Component {
<Button
className="nowrap-style"
variant="outline-secondary"
onClick={this.props.confirmClearQueueShow}
onClick={this.props.showConfirmClearQueueDialog}
disabled={this.props.queue.queueStatus === QUEUE_RUNNING}
>
<i
Expand Down Expand Up @@ -1016,17 +1006,12 @@ function mapDispatchToProps(dispatch) {
dispatch(setEnabledSample(qidList, value)),
deleteTask: (qid, taskIndex) => dispatch(deleteTask(qid, taskIndex)),
deleteTaskList: (sampleIDList) => dispatch(deleteTaskList(sampleIDList)),
clearQueue: () => dispatch(clearQueue()),
addSamplesToQueue: (sampleData) => dispatch(addSamplesToQueue(sampleData)),
stopQueue: () => dispatch(stopQueue()),
confirmClearQueueShow: bindActionCreators(
showConfirmClearQueueDialog: bindActionCreators(
showConfirmClearQueueDialog,
dispatch,
),
confirmClearQueueHide: bindActionCreators(
showConfirmClearQueueDialog.bind(null, false),
dispatch,
),
showConfirmCollectDialog: bindActionCreators(
showConfirmCollectDialog,
dispatch,
Expand Down
Loading