Skip to content
Merged
Show file tree
Hide file tree
Changes from 7 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 doc/release/RELEASE-NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ This project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html
All other traffic will be redirected to the React application.

### Bugfix
* *Describe bugfix here.*
* [OSDEV-1697](https://opensupplyhub.atlassian.net/browse/OSDEV-1697) - Added a redirect to the main page upon closing the SLC modal window to prevent the creation of multiple moderation events.

### What's new
* [OSDEV-1662](https://opensupplyhub.atlassian.net/browse/OSDEV-1662) - Added a new field, `action_perform_by`, to the moderation event. This data appears on the Contribution Record page when a moderator perform any actions like `APPROVED` or `REJECTED`.
Expand Down
56 changes: 20 additions & 36 deletions src/react/src/__tests__/components/ProductionLocationDialog.test.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
import React from 'react';
import { render, screen, fireEvent } from '@testing-library/react';
import { BrowserRouter as Router, useHistory } from 'react-router-dom';
import { BrowserRouter as Router } from 'react-router-dom';
import ProductionLocationDialog from '../../components/Contribute/ProductionLocationDialog';
import ProductionLocationDialogCloseButton from '../../components/Contribute/ProductionLocationDialogCloseButton';
import history from '../../util/history';

jest.mock('react-router-dom', () => ({
...jest.requireActual('react-router-dom'),
useHistory: jest.fn(),
}));

const mockHistoryPush = jest.fn();

describe('ProductionLocationDialog', () => {
const defaultProps = {
Expand Down Expand Up @@ -57,12 +52,6 @@ describe('ProductionLocationDialog', () => {
}
}

beforeEach(() => {
useHistory.mockReturnValue({
push: mockHistoryPush,
});
});

test('renders dialog content', () => {
render(
<Router>
Expand Down Expand Up @@ -146,33 +135,28 @@ describe('ProductionLocationDialog', () => {
expect(claimButton).toHaveAttribute('href', `/facilities/${defaultProps.osID}/claim`);
})

test('closes ProductionLocationDialog when close button is clicked', () => {
const handleShowMock = jest.fn();
const { unmount } = render(
<ProductionLocationDialog
classes={{}}
data={defaultProps.data}
osID={defaultProps.osID}
moderationStatus={defaultProps.moderationStatus}
isOpen
handleShow={handleShowMock}
>
<ProductionLocationDialogCloseButton
handleShow={handleShowMock}
isMobile={false}
/>
</ProductionLocationDialog>
test('redirect to the main page when clicking close button', () => {
render(
<Router history={history}>
<ProductionLocationDialog
classes={{}}
data={defaultProps.data}
osID={defaultProps.osID}
moderationStatus={defaultProps.moderationStatus}
isOpen
>
<ProductionLocationDialogCloseButton
isMobile={false}
/>
</ProductionLocationDialog>
</Router>
);

expect(screen.getByText(/Continue to Claim/i)).toBeInTheDocument();

const closeButton = screen.getByRole('button', { name: /close/i });
fireEvent.click(closeButton);

expect(handleShowMock).toHaveBeenCalledWith(false);

unmount();
const closeButton = screen.getByRole('button', { name: /close/i });

expect(screen.queryByText(/Continue to Claim/i)).not.toBeInTheDocument();
fireEvent.click(closeButton);
expect(history.location.pathname).toBe('/');
});
});
23 changes: 18 additions & 5 deletions src/react/src/components/Contribute/ProductionLocationDialog.jsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React, { useEffect, useMemo, useState } from 'react';
import { func, number, object, string } from 'prop-types';
import { number, object, string } from 'prop-types';
import { withStyles, withTheme } from '@material-ui/core/styles';
import { useHistory } from 'react-router-dom';
import {
Expand Down Expand Up @@ -27,6 +27,7 @@ import ProductionLocationDialogFields from './ProductionLocationDialogFields';
import {
mainRoute,
searchByNameAndAddressResultRoute,
contributeProductionLocationRoute,
MODERATION_STATUSES_ENUM,
PRODUCTION_LOCATION_CLAIM_STATUSES_ENUM,
EMPTY_PLACEHOLDER,
Expand Down Expand Up @@ -89,7 +90,6 @@ const ProductionLocationDialog = ({
data,
osID,
moderationStatus,
handleShow,
claimStatus,
}) => {
const history = useHistory();
Expand All @@ -98,6 +98,20 @@ const ProductionLocationDialog = ({
const getIsMobileMemoized = useMemo(() => getIsMobile(innerWidth), [
innerWidth,
]);

// Override browser's go back button when modal dialog is open
useEffect(() => {
const cleanupListener = history.listen((location, action) => {
if (action === 'POP') {
history.push(contributeProductionLocationRoute);
}
});

return () => {
cleanupListener();
};
}, [history]);

useEffect(() => {
setIsMobile(getIsMobileMemoized);
}, [getIsMobileMemoized]);
Expand Down Expand Up @@ -147,7 +161,7 @@ const ProductionLocationDialog = ({
<>
{isMobile ? (
<ProductionLocationDialogCloseButton
handleShow={handleShow}
handleGoToMainPage={handleGoToMainPage}
isMobile={isMobile}
/>
) : null}
Expand All @@ -171,7 +185,7 @@ const ProductionLocationDialog = ({
</p>
{!isMobile ? (
<ProductionLocationDialogCloseButton
handleShow={handleShow}
handleGoToMainPage={handleGoToMainPage}
isMobile={isMobile}
/>
) : null}
Expand Down Expand Up @@ -317,7 +331,6 @@ ProductionLocationDialog.propTypes = {
data: object.isRequired,
osID: string,
moderationStatus: string.isRequired,
handleShow: func.isRequired,
classes: object.isRequired,
theme: object.isRequired,
innerWidth: number.isRequired,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,22 @@ import { makeProductionLocationCloseButtonStyles } from '../../util/styles';
const ProductionLocationDialogCloseButton = ({
isMobile,
classes,
handleShow,
handleGoToMainPage,
}) => (
<>
{isMobile ? (
<IconButton
aria-label="Close"
className="mobile-dialog-close-button"
onClick={() => handleShow(false)}
onClick={() => handleGoToMainPage()}
>
<CloseIcon />
</IconButton>
) : (
<IconButton
aria-label="Close"
className={classes.desktopCloseButton}
onClick={() => handleShow(false)}
onClick={() => handleGoToMainPage()}
>
<CloseIcon />
</IconButton>
Expand All @@ -32,7 +32,7 @@ const ProductionLocationDialogCloseButton = ({
);

ProductionLocationDialogCloseButton.propTypes = {
handleShow: func.isRequired,
handleGoToMainPage: func.isRequired,
isMobile: bool.isRequired,
classes: object.isRequired,
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -785,7 +785,6 @@ const ProductionLocationInfo = ({
singleModerationEventItem?.cleaned_data
}
osID={osID || singleModerationEventItem?.os_id}
handleShow={setShowProductionLocationDialog}
innerWidth={innerWidth}
moderationStatus={
pendingModerationEventData?.status ||
Expand Down