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

Show refresh warning when user switch team project in another tab #1665

Open
wants to merge 3 commits into
base: feat/vadc_sprint24
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import React, { useState, useEffect } from 'react';
import PropTypes from 'prop-types';
import { useHistory } from 'react-router-dom';
import { useQuery } from 'react-query';
import { BroadcastChannel } from 'broadcast-channel';
import EditIcon from './Icons/EditIcon';
import isEnterOrSpace from '../../AccessibilityUtils/IsEnterOrSpace';
import TeamProjectModal from '../TeamProjectModal/TeamProjectModal';
Expand All @@ -28,6 +29,32 @@ const TeamProjectHeader = ({ isEditable }) => {
}
};

const channel = new BroadcastChannel('teamProjectChannel');

const [isWarningModalOpen, setIsWarningModalOpen] = useState(false);
const showWarningModal = () => {
setIsWarningModalOpen(true);
};

useEffect(() => {
const handleMessage = () => {
// Only Show Warning Modal for tabs
// that didn't just submit the team change
if (!isModalOpen) {
showWarningModal();
}
// Close Open Team Change Modal
setIsModalOpen(false);
};

channel.onmessage = handleMessage;

// Clean up the channel when the component unmounts
return () => {
channel.close();
};
}, [channel, showWarningModal]);

const { data, status } = useQuery(
'teamprojects',
fetchArboristTeamProjectRoles,
Expand Down Expand Up @@ -76,11 +103,26 @@ const TeamProjectHeader = ({ isEditable }) => {
</button>
)}
</div>
{isWarningModalOpen && (
<TeamProjectModal
isModalOpen={isModalOpen}
setIsModalOpen={setIsModalOpen}
setBannerText={setBannerText}
isWarningModalOpen={isWarningModalOpen}
setIsWarningModalOpen={setIsWarningModalOpen}
data={data}
status={status}
selectedTeamProject={selectedTeamProject}
setSelectedTeamProject={setSelectedTeamProject}
/>
)}
{isEditable && (
<TeamProjectModal
isModalOpen={isModalOpen}
setIsModalOpen={setIsModalOpen}
setBannerText={setBannerText}
isWarningModalOpen={isWarningModalOpen}
setIsWarningModalOpen={setIsWarningModalOpen}
data={data}
status={status}
selectedTeamProject={selectedTeamProject}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,42 @@ import React from 'react';
import PropTypes from 'prop-types';
import { useHistory } from 'react-router-dom';
import { Button, Modal, Spin } from 'antd';
import { BroadcastChannel } from 'broadcast-channel';
import LoadingErrorMessage from '../../LoadingErrorMessage/LoadingErrorMessage';
import TeamsDropdown from './TeamsDropdown/TeamsDropdown';
import './TeamProjectModal.css';


const TeamProjectModal = ({
isModalOpen,
setIsModalOpen,
isWarningModalOpen,
setIsWarningModalOpen,
setBannerText,
data,
status,
selectedTeamProject,
setSelectedTeamProject,
}) => {
const history = useHistory();

const channel = new BroadcastChannel('teamProjectChannel');
const sendMessage = (msg) => {
channel.postMessage(msg);
};

const closeAndUpdateTeamProject = () => {
setIsModalOpen(false);
// Closing Modal is handled in the header
sendMessage('Team Project Changed!');
setBannerText(selectedTeamProject);
localStorage.setItem('teamProject', selectedTeamProject);
};

const closeWarningAndRefreshPage = () => {
setIsWarningModalOpen(false);
window.location.reload();
};

const redirectToHomepage = () => {
history.push('/');
};
Expand All @@ -42,6 +59,32 @@ const TeamProjectModal = ({
</Modal>
);
}
if (isWarningModalOpen === true) {
return (
<Modal
open={isWarningModalOpen}
className='team-project-modal'
title='Team Projects'
closable
maskClosable={false}
keyboard={false}
footer={[
<Button
key='submit'
type='primary'
disabled={!selectedTeamProject}
onClick={() => closeWarningAndRefreshPage()}
>
Refresh Page
</Button>,
]}
>
<div className='team-project-modal_modal-description'>
Team Project has been updated in another tab. Please click refresh page to prevent errors.
</div>
</Modal>
);
}
if (data) {
if (data.teams.length > 0) {
return (
Expand Down Expand Up @@ -119,6 +162,8 @@ const TeamProjectModal = ({
TeamProjectModal.propTypes = {
isModalOpen: PropTypes.bool.isRequired,
setIsModalOpen: PropTypes.func.isRequired,
isWarningModalOpen: PropTypes.bool.isRequired,
setIsWarningModalOpen: PropTypes.func.isRequired,
setBannerText: PropTypes.func.isRequired,
data: PropTypes.object,
status: PropTypes.string.isRequired,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,16 @@ const testTeamName = TeamProjectTestData.data.teams[0].teamName;
const setIsModalOpen = jest.fn();
const setBannerText = jest.fn();
const setSelectedTeamProject = jest.fn();
const setIsWarningModalOpen = jest.fn();

describe('TeamProjectModal', () => {
test('renders with loading text initially', async () => {
render(
<TeamProjectModal
isModalOpen
setIsModalOpen={setIsModalOpen}
isWarningModalOpen={false}
setIsWarningModalOpen={setIsWarningModalOpen}
setBannerText={setBannerText}
data={undefined}
status='loading'
Expand All @@ -40,6 +43,8 @@ describe('TeamProjectModal', () => {
<TeamProjectModal
isModalOpen
setIsModalOpen={setIsModalOpen}
isWarningModalOpen={false}
setIsWarningModalOpen={setIsWarningModalOpen}
setBannerText={setBannerText}
data={TeamProjectTestData.data}
status='success'
Expand Down Expand Up @@ -67,6 +72,8 @@ describe('TeamProjectModal', () => {
<TeamProjectModal
isModalOpen
setIsModalOpen={setIsModalOpen}
isWarningModalOpen={false}
setIsWarningModalOpen={setIsWarningModalOpen}
setBannerText={setBannerText}
data={TeamProjectTestData.data}
status='success'
Expand All @@ -93,6 +100,8 @@ describe('TeamProjectModal', () => {
isModalOpen
setIsModalOpen={setIsModalOpen}
setBannerText={setBannerText}
isWarningModalOpen={false}
setIsWarningModalOpen={setIsWarningModalOpen}
data={TeamProjectTestData.data}
status='success'
selectedTeamProject={testTeamName}
Expand All @@ -106,7 +115,25 @@ describe('TeamProjectModal', () => {
fireEvent.click(submitButton);

// Assert that the modal closes and sets banner text
expect(setIsModalOpen).toHaveBeenCalledWith(false);
expect(setBannerText).toHaveBeenCalledWith(testTeamName);
});

test('Render warning modal based ', async () => {
render(
<TeamProjectModal
isModalOpen
setIsModalOpen={setIsModalOpen}
setBannerText={setBannerText}
isWarningModalOpen
setIsWarningModalOpen={setIsWarningModalOpen}
data={TeamProjectTestData.data}
status='success'
selectedTeamProject={testTeamName}
setSelectedTeamProject={setSelectedTeamProject}
/>,
);

// Assert that the modal closes and sets banner text
expect(screen.getByText('Refresh Page')).toBeInTheDocument();
});
});
Loading