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

Maintain collapsed state of Ringdowns list when switching tabs in ER view #274

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
7 changes: 5 additions & 2 deletions client/src/ER/ER.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ import Ringdowns from './Ringdowns';

import notification from '../assets/notification.mp3';
import { useTabPositions } from '../hooks/useTabPositions';
import withUIContext from '../HOC/WithUIContext';

export default function ER() {
const ER = () => {
const { hospitalUser } = useContext(Context);
const socketUrl = `${window.location.origin.replace(/^http/, 'ws')}/wss/hospital?id=${hospitalUser?.hospital.id}`;
const { lastMessage } = useWebSocket(socketUrl, { shouldReconnect: () => true });
Expand Down Expand Up @@ -109,4 +110,6 @@ export default function ER() {
</div>
</div>
);
}
};

export default withUIContext(ER);
21 changes: 16 additions & 5 deletions client/src/ER/RingdownSection.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,32 @@
import React, { useState } from 'react';
import React, { useContext } from 'react';
import PropTypes from 'prop-types';

import Ringdown from '../Models/Ringdown';
import RingdownCard from '../Components/RingdownCard';

import './RingdownSection.scss';
import UIContext from '../UIContext';

function RingdownSection({ title, ringdowns, onStatusChange }) {
const [isExpanded, setExpanded] = useState(true);
function RingdownSection({ title, ringdowns, onStatusChange, id }) {
const { ringdownSections, setRingdownSections } = useContext(UIContext);
const isExpanded = ringdownSections && ringdownSections[id].expanded;

const handleExpanded = () => {
setRingdownSections({
...ringdownSections,
[id]: {
...ringdownSections[id],
expanded: !ringdownSections[id].expanded,
},
});
};
return (
<div className="ringdown-section">
<div
className="usa-accordion__heading ringdown-section__header"
onClick={() => setExpanded(!isExpanded)}
onClick={handleExpanded}
onKeyDown={(event) => {
if (event.key === 'Enter') setExpanded(!isExpanded);
if (event.key === 'Enter') handleExpanded();
}}
role="button"
tabIndex={0}
Expand Down
4 changes: 2 additions & 2 deletions client/src/ER/Ringdowns.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ function Ringdowns({ ringdowns, onStatusChange }) {
return (
<>
<div className="usa-accordion ringdowns">
<RingdownSection title="Waiting" ringdowns={waiting} onStatusChange={onStatusChange} />
<RingdownSection title="Incoming" ringdowns={enroute} onStatusChange={onStatusChange} />
<RingdownSection title="Waiting" id="waiting" ringdowns={waiting} onStatusChange={onStatusChange} />
<RingdownSection title="Incoming" id="enroute" ringdowns={enroute} onStatusChange={onStatusChange} />
</div>
</>
);
Expand Down
16 changes: 16 additions & 0 deletions client/src/HOC/WithUIContext.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import React from 'react';
import { UIContextProvider } from '../UIContext';

const withUIContext = (WrappedComponent) => {
kretzbryan marked this conversation as resolved.
Show resolved Hide resolved
const WithUIContext = ({ ...props }) => {
return (
<UIContextProvider>
<WrappedComponent {...props} />;
</UIContextProvider>
);
};

return WithUIContext;
};

export default withUIContext;
28 changes: 28 additions & 0 deletions client/src/UIContext.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import React, { createContext, useState } from 'react';
kretzbryan marked this conversation as resolved.
Show resolved Hide resolved
import PropTypes from 'prop-types';

const UIContext = createContext();

function UIContextProvider({ children }) {
const [ringdownSections, setRingdownSections] = useState({
waiting: {
expanded: true,
},
enroute: {
expanded: true,
},
});

const value = {
ringdownSections,
setRingdownSections,
};
return <UIContext.Provider value={value}>{children}</UIContext.Provider>;
}
UIContextProvider.propTypes = {
children: PropTypes.oneOfType([PropTypes.arrayOf(PropTypes.node), PropTypes.node]).isRequired,
};

export { UIContextProvider };

export default UIContext;