Skip to content

Commit 4dff5b5

Browse files
committed
feat: update germlineReports to use new query hooks
DEVSU-2860
1 parent 959ab4f commit 4dff5b5

File tree

4 files changed

+21
-51
lines changed

4 files changed

+21
-51
lines changed

app/context/GermlineReportContext/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import {
55

66
const GermlineReportContext = React.createContext<GermlineReportContextType>({
77
report: null,
8-
setReport: () => {},
8+
refetchReport: async () => undefined,
99
});
1010

1111
export default GermlineReportContext;

app/context/GermlineReportContext/types.d.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { UseQueryResult } from 'react-query';
12
import { RecordDefaults, UserType, ProjectType } from '@/common';
23

34
type ReviewType = {
@@ -65,9 +66,8 @@ type GermlineReportType = {
6566

6667
type GermlineReportContextType = {
6768
/** Current report that's being viewed */
68-
report: GermlineReportType | null,
69-
/** Set new current report */
70-
setReport: React.Dispatch<React.SetStateAction<GermlineReportType>>;
69+
report: GermlineReportType | null;
70+
refetchReport: UseQueryResult<GermlineReportType>['refetch'];
7171
};
7272

7373
export {

app/views/GermlineView/components/Report/components/Reviews/index.tsx

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import columnDefs from './columnDefs';
2121
import './index.scss';
2222

2323
const Reviews = (): JSX.Element => {
24-
const { report, setReport } = useContext(GermlineReportContext);
24+
const { report, refetchReport } = useContext(GermlineReportContext);
2525

2626
const [isEditing, setIsEditing] = useState(false);
2727
const [newComment, setNewComment] = useState('');
@@ -36,38 +36,32 @@ const Reviews = (): JSX.Element => {
3636

3737
const handleAddReview = useCallback(async () => {
3838
try {
39-
const newReview = await api.post(
39+
await api.post(
4040
`/germline-small-mutation-reports/${report.ident}/reviews`,
4141
{ type: newType, comment: newComment },
4242
{},
4343
).request();
44-
setReport((prevVal) => ({
45-
...prevVal,
46-
reviews: [...prevVal.reviews, newReview],
47-
}));
44+
refetchReport();
4845
setIsEditing(false);
4946
snackbar.success('Review added');
5047
} catch (err) {
5148
snackbar.error(`Error adding review: ${err}`);
5249
}
53-
}, [newComment, newType, report, setReport]);
50+
}, [newComment, newType, report, refetchReport]);
5451

5552
const handleRowDelete = useCallback(async (row) => {
5653
try {
57-
api.del(
54+
await api.del(
5855
`/germline-small-mutation-reports/${report.ident}/reviews/${row.data.ident}`,
5956
{},
6057
{},
6158
).request();
62-
setReport((prevVal) => ({
63-
...prevVal,
64-
reviews: prevVal.reviews.filter((val) => val.ident !== row.data.ident),
65-
}));
59+
refetchReport();
6660
snackbar.success('Review removed');
6761
} catch (err) {
6862
snackbar.error(`Error removing review: ${err}`);
6963
}
70-
}, [report, setReport]);
64+
}, [report, refetchReport]);
7165

7266
const DeleteCellRenderer = useCallback((row) => (
7367
<DeleteCell

app/views/GermlineView/components/Report/index.tsx

Lines changed: 10 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React, {
2-
useEffect, useState, useCallback, useMemo,
2+
useState, useCallback, useMemo,
33
} from 'react';
44
import { useParams, useHistory } from 'react-router-dom';
55
import {
@@ -19,7 +19,7 @@ import { ActionCellRenderer } from '@/components/DataTable/components/ActionCell
1919
import AlertDialog from '@/components/AlertDialog';
2020
import snackbar from '@/services/SnackbarUtils';
2121
import { GermlineReportType, VariantType } from '@/context/GermlineReportContext/types';
22-
import withLoading, { WithLoadingInjectedProps } from '@/hoc/WithLoading';
22+
import { useGermlineSmallMutationReportsGermline } from '@/queries/get';
2323
import { getDate } from '@/utils/date';
2424
import StrikethroughCell from './components/StrikethroughCell';
2525
import EditDialog from './components/EditDialog';
@@ -28,16 +28,13 @@ import columnDefs from './columnDefs';
2828
import './index.scss';
2929
import LinkoutCellRenderer from '../LinkoutCellRenderer';
3030

31-
type GermlineReportProps = WithLoadingInjectedProps;
32-
33-
const GermlineReport = ({
34-
isLoading,
35-
setIsLoading,
36-
}: GermlineReportProps): JSX.Element => {
31+
const GermlineReport = (): JSX.Element => {
3732
const { ident } = useParams<{ ident: string }>();
3833
const { gridApi, colApi, onGridReady } = useGrid();
3934
const history = useHistory();
40-
const [report, setReport] = useState<GermlineReportType>();
35+
const {
36+
data: report, isLoading, refetch: refetchReport,
37+
} = useGermlineSmallMutationReportsGermline<GermlineReportType>(ident);
4138

4239
const [showAllColumns, setShowAllColumns] = useState(false);
4340
const [showEditDialog, setShowEditDialog] = useState(false);
@@ -46,25 +43,7 @@ const GermlineReport = ({
4643

4744
const [menuAnchor, setMenuAnchor] = useState<HTMLElement>();
4845

49-
const germlineReportContextValue = useMemo(() => ({ report, setReport }), [report]);
50-
51-
useEffect(() => {
52-
if (ident) {
53-
const getData = async () => {
54-
try {
55-
const reportResp = await api.get(
56-
`/germline-small-mutation-reports/${ident}`,
57-
).request();
58-
setReport(reportResp);
59-
} catch (err) {
60-
snackbar.error(`Network error: ${err}`);
61-
} finally {
62-
setIsLoading(false);
63-
}
64-
};
65-
getData();
66-
}
67-
}, [ident, setReport, setIsLoading]);
46+
const germlineReportContextValue = useMemo(() => ({ report, refetchReport }), [report, refetchReport]);
6847

6948
const handleGridReady = useCallback((params) => {
7049
const { columnApi } = params;
@@ -92,13 +71,10 @@ const GermlineReport = ({
9271

9372
const handleEditClose = useCallback((newRow) => {
9473
if (newRow) {
95-
const newVariants = [...report.variants];
96-
const index = newVariants.findIndex((variant) => variant.ident === newRow.ident);
97-
newVariants[index] = newRow;
98-
setReport((prevVal) => ({ ...prevVal, variants: newVariants }));
74+
refetchReport();
9975
}
10076
setShowEditDialog(false);
101-
}, [report]);
77+
}, [refetchReport]);
10278

10379
const RowActionCellRenderer = useCallback((row) => (
10480
<ActionCellRenderer
@@ -248,4 +224,4 @@ const GermlineReport = ({
248224
);
249225
};
250226

251-
export default withLoading(GermlineReport);
227+
export default GermlineReport;

0 commit comments

Comments
 (0)