Skip to content

Commit 9a6b8cb

Browse files
committed
refactor: phase out UserContext
no-ticket
1 parent bd2c828 commit 9a6b8cb

File tree

6 files changed

+90
-150
lines changed

6 files changed

+90
-150
lines changed

app/components/SignatureCard/SignatureCard.stories.tsx

+15-7
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,28 @@
1-
import React from 'react';
1+
import React, { useMemo } from 'react';
22
import { action } from '@storybook/addon-actions';
33
import { Story } from '@storybook/react/types-6-0';
44

5-
import UserContext from '@/context/UserContext';
5+
import ReportContext from '@/context/ReportContext';
66
import SignatureCard, { SignatureCardProps } from '.';
77

88
export default {
99
title: 'components/SignatureCard',
1010
component: SignatureCard,
1111
};
1212

13-
const Template = (args) => (
14-
<UserContext.Provider value={{ canEdit: true }}>
15-
<SignatureCard {...args} />
16-
</UserContext.Provider>
17-
);
13+
const Template = (args) => {
14+
const value = useMemo(() => ({
15+
canEdit: true,
16+
report: null,
17+
setReport: () => { },
18+
}), []);
19+
20+
return (
21+
<ReportContext.Provider value={value}>
22+
<SignatureCard {...args} />
23+
</ReportContext.Provider>
24+
);
25+
};
1826

1927
export const Unsigned: Story<SignatureCardProps> = Template.bind({});
2028

app/components/SignatureCard/__tests__/SignatureCard.test.tsx

+20-20
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,51 @@
11
import React from 'react';
22
import { render, screen, fireEvent } from '@testing-library/react';
33

4-
import UserContext from '@/context/UserContext';
5-
import SignatureCard from '..';
4+
import ReportContext from '@/context/ReportContext';
5+
import SignatureCard, { SignatureType } from '..';
66
import { mockNullData, mockNullObjectData, mockObjectData } from './mockData';
77

88
describe('SignatureCard', () => {
99
test('Author and sign button are visible', async () => {
1010
render(
11-
<UserContext.Provider value={{ canEdit: true, setCanEdit: () => {} }}>
11+
<ReportContext.Provider value={{ canEdit: true, report: null, setReport: () => {} }}>
1212
<SignatureCard
1313
title="Author"
1414
type="author"
1515
signatures={null}
1616
onClick={() => {}}
1717
/>
18-
</UserContext.Provider>,
18+
</ReportContext.Provider>,
1919
);
2020
expect(await screen.findByText('Author')).toBeInTheDocument();
2121
expect(await screen.findByRole('button', { name: 'Sign' })).toBeInTheDocument();
2222
});
2323

2424
test('Sign button is not visible without edit permissions', async () => {
2525
render(
26-
<UserContext.Provider value={{ canEdit: false, setCanEdit: () => {} }}>
26+
<ReportContext.Provider value={{ canEdit: false, report: null, setReport: () => {} }}>
2727
<SignatureCard
2828
title="Author"
2929
type="author"
3030
signatures={null}
3131
onClick={() => {}}
3232
/>
33-
</UserContext.Provider>,
33+
</ReportContext.Provider>,
3434
);
3535
expect(screen.queryByRole('button', { name: 'Sign' })).not.toBeInTheDocument();
3636
});
3737

3838
test('Sign button calls onClick', async () => {
3939
const handleClick = jest.fn();
4040
render(
41-
<UserContext.Provider value={{ canEdit: true, setCanEdit: () => {} }}>
41+
<ReportContext.Provider value={{ canEdit: true, report: null, setReport: () => {} }}>
4242
<SignatureCard
4343
title="Author"
4444
type="author"
4545
signatures={null}
4646
onClick={handleClick}
4747
/>
48-
</UserContext.Provider>,
48+
</ReportContext.Provider>,
4949
);
5050
fireEvent.click(await screen.findByRole('button', { name: 'Sign' }));
5151

@@ -54,44 +54,44 @@ describe('SignatureCard', () => {
5454

5555
test('Sign button is visible when reviewerSignature is null', async () => {
5656
render(
57-
<UserContext.Provider value={{ canEdit: true, setCanEdit: () => {} }}>
57+
<ReportContext.Provider value={{ canEdit: true, report: null, setReport: () => {} }}>
5858
<SignatureCard
5959
title="Reviewer"
6060
type="reviewer"
61-
signatures={mockNullData}
61+
signatures={mockNullData as SignatureType}
6262
onClick={() => {}}
6363
/>
64-
</UserContext.Provider>,
64+
</ReportContext.Provider>,
6565
);
6666

6767
expect(await screen.findByRole('button', { name: 'Sign' })).toBeInTheDocument();
6868
});
6969

7070
test('Sign button is visible when reviewerSignature has null data', async () => {
7171
render(
72-
<UserContext.Provider value={{ canEdit: true, setCanEdit: () => {} }}>
72+
<ReportContext.Provider value={{ canEdit: true, report: null, setReport: () => {} }}>
7373
<SignatureCard
7474
title="Reviewer"
7575
type="reviewer"
76-
signatures={mockNullObjectData}
76+
signatures={mockNullObjectData as SignatureType}
7777
onClick={() => {}}
7878
/>
79-
</UserContext.Provider>,
79+
</ReportContext.Provider>,
8080
);
8181

8282
expect(await screen.findByRole('button', { name: 'Sign' })).toBeInTheDocument();
8383
});
8484

8585
test('Reviewer name & remove signature button are visible', async () => {
8686
render(
87-
<UserContext.Provider value={{ canEdit: true, setCanEdit: () => {} }}>
87+
<ReportContext.Provider value={{ canEdit: true, report: null, setReport: () => {} }}>
8888
<SignatureCard
8989
title="Reviewer"
9090
type="reviewer"
91-
signatures={mockObjectData}
91+
signatures={mockObjectData as SignatureType}
9292
onClick={() => {}}
9393
/>
94-
</UserContext.Provider>,
94+
</ReportContext.Provider>,
9595
);
9696

9797
expect(screen.queryByRole('button', { name: 'Sign' })).not.toBeInTheDocument();
@@ -100,15 +100,15 @@ describe('SignatureCard', () => {
100100

101101
test('No buttons are visible in print view', async () => {
102102
render(
103-
<UserContext.Provider value={{ canEdit: true, setCanEdit: () => {} }}>
103+
<ReportContext.Provider value={{ canEdit: true, report: null, setReport: () => {} }}>
104104
<SignatureCard
105105
title="Reviewer"
106106
type="reviewer"
107-
signatures={mockObjectData}
107+
signatures={mockObjectData as SignatureType}
108108
onClick={() => {}}
109109
isPrint
110110
/>
111-
</UserContext.Provider>,
111+
</ReportContext.Provider>,
112112
);
113113

114114
expect(screen.queryByRole('button')).not.toBeInTheDocument();

app/context/UserContext/index.tsx

-58
This file was deleted.

app/context/UserContext/interfaces.ts

-7
This file was deleted.

app/views/MainView/index.tsx

+46-49
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ import {
1818
import AuthenticatedRoute from '@/components/AuthenticatedRoute';
1919
import SidebarContext from '@/context/SidebarContext';
2020
import { SecurityContext, SecurityContextType } from '@/context/SecurityContext';
21-
import { UserContextProvider } from '@/context/UserContext';
2221
import { ResourceContextProvider } from '@/context/ResourceContext';
2322
import NavBar from '@/components/NavBar';
2423
import Sidebar from '@/components/Sidebar';
@@ -149,55 +148,53 @@ const Main = (): JSX.Element => {
149148

150149
return (
151150
<SecurityContext.Provider value={secContextVal}>
152-
<UserContextProvider>
153-
<ResourceContextProvider>
154-
<SidebarContext.Provider value={sideBarContextVal}>
155-
<div>
156-
<section className={`${isNavVisible ? 'main__content' : ''} ${sidebarMaximized ? 'main__content--maximized' : ''}`}>
157-
{isNavVisible ? (
158-
<>
159-
<NavBar />
160-
<Sidebar />
161-
</>
162-
) : null}
163-
<Suspense fallback={(
164-
<Box
165-
sx={{
166-
position: 'fixed',
167-
top: '50%',
168-
left: '50%',
169-
transform: 'translate(-50%, -50%)',
170-
}}
171-
>
172-
<CircularProgress color="secondary" />
173-
</Box>
174-
)}
151+
<ResourceContextProvider>
152+
<SidebarContext.Provider value={sideBarContextVal}>
153+
<div>
154+
<section className={`${isNavVisible ? 'main__content' : ''} ${sidebarMaximized ? 'main__content--maximized' : ''}`}>
155+
{isNavVisible ? (
156+
<>
157+
<NavBar />
158+
<Sidebar />
159+
</>
160+
) : null}
161+
<Suspense fallback={(
162+
<Box
163+
sx={{
164+
position: 'fixed',
165+
top: '50%',
166+
left: '50%',
167+
transform: 'translate(-50%, -50%)',
168+
}}
175169
>
176-
<TimeoutModal authorizationToken={authorizationToken} setAuthorizationToken={setAuthorizationToken} />
177-
<Switch>
178-
<Route component={LoginView} path="/login" />
179-
<Route component={LinkOutView} path="/graphkb" />
180-
<Route path="/" exact>
181-
<Redirect to={{ pathname: '/reports' }} />
182-
</Route>
183-
<AuthenticatedRoute component={TermsView} path="/terms" />
184-
<AuthenticatedRoute component={ReportsView} path="/reports" />
185-
<AuthenticatedRoute exact component={PatientsView} path="/reports/patients/:patientId" />
186-
<Redirect exact from="/report/:ident/(genomic|probe)/summary" to="/report/:ident/summary" />
187-
<AuthenticatedRoute component={ReportView} path="/report/:ident" />
188-
<AuthenticatedRoute component={PrintView} path="/print/:ident" showNav={false} onToggleNav={setIsNavVisible} />
189-
<AuthenticatedRoute component={BetaPrintView} path="/printBeta/:ident" showNav={false} onToggleNav={setIsNavVisible} />
190-
<AuthenticatedRoute component={GermlineView} path="/germline" />
191-
<AuthenticatedRoute component={ProjectsView} path="/projects" />
192-
<AuthenticatedRoute adminRequired component={AdminView} path="/admin" />
193-
<AuthenticatedRoute adminRequired component={TemplateView} path="/template" />
194-
</Switch>
195-
</Suspense>
196-
</section>
197-
</div>
198-
</SidebarContext.Provider>
199-
</ResourceContextProvider>
200-
</UserContextProvider>
170+
<CircularProgress color="secondary" />
171+
</Box>
172+
)}
173+
>
174+
<TimeoutModal authorizationToken={authorizationToken} setAuthorizationToken={setAuthorizationToken} />
175+
<Switch>
176+
<Route component={LoginView} path="/login" />
177+
<Route component={LinkOutView} path="/graphkb" />
178+
<Route path="/" exact>
179+
<Redirect to={{ pathname: '/reports' }} />
180+
</Route>
181+
<AuthenticatedRoute component={TermsView} path="/terms" />
182+
<AuthenticatedRoute component={ReportsView} path="/reports" />
183+
<AuthenticatedRoute exact component={PatientsView} path="/reports/patients/:patientId" />
184+
<Redirect exact from="/report/:ident/(genomic|probe)/summary" to="/report/:ident/summary" />
185+
<AuthenticatedRoute component={ReportView} path="/report/:ident" />
186+
<AuthenticatedRoute component={PrintView} path="/print/:ident" showNav={false} onToggleNav={setIsNavVisible} />
187+
<AuthenticatedRoute component={BetaPrintView} path="/printBeta/:ident" showNav={false} onToggleNav={setIsNavVisible} />
188+
<AuthenticatedRoute component={GermlineView} path="/germline" />
189+
<AuthenticatedRoute component={ProjectsView} path="/projects" />
190+
<AuthenticatedRoute adminRequired component={AdminView} path="/admin" />
191+
<AuthenticatedRoute adminRequired component={TemplateView} path="/template" />
192+
</Switch>
193+
</Suspense>
194+
</section>
195+
</div>
196+
</SidebarContext.Provider>
197+
</ResourceContextProvider>
201198
</SecurityContext.Provider>
202199
);
203200
};

0 commit comments

Comments
 (0)