Skip to content

Commit fabebc5

Browse files
authored
Fix navigation after filter/sort (#328)
1 parent 183a11f commit fabebc5

File tree

3 files changed

+58
-60
lines changed

3 files changed

+58
-60
lines changed

src/components/TestDetailsDialog/index.tsx

Lines changed: 18 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,43 @@
11
import { Dialog, Typography } from "@mui/material";
2-
import { styled } from '@mui/material/styles';
32
import React from "react";
43
import { useNavigate } from "react-router";
54
import { useBuildState, useTestRunState } from "../../contexts";
65
import { buildTestRunLocation } from "../../_helpers/route.helpers";
76
import { BaseModal } from "../BaseModal";
87
import TestDetailsModal from "./TestDetailsModal";
98
import { TestRun } from "../../types";
9+
import { makeStyles } from "@mui/styles";
1010

11-
const PREFIX = 'TestDetailsDialog';
12-
13-
const classes = {
14-
modal: `${PREFIX}-modal`
15-
};
16-
17-
const StyledDialog = styled(Dialog)(() => ({
18-
[`&.${classes.modal}`]: {
11+
const useStyles = makeStyles(() => ({
12+
modal: {
1913
margin: "20px 10px 10px 10px",
20-
}
14+
},
2115
}));
2216

2317
export const TestDetailsDialog: React.FunctionComponent = () => {
24-
18+
const classes = useStyles();
2519
const {
2620
selectedTestRun,
2721
touched,
2822
testRuns: allTestRuns,
29-
filteredTestRunIds,
30-
sortedTestRunIds,
23+
filteredSortedTestRunIds,
3124
} = useTestRunState();
3225
const { selectedBuild } = useBuildState();
3326
const navigate = useNavigate();
3427
const [notSavedChangesModal, setNotSavedChangesModal] = React.useState(false);
3528
const [navigationTargetId, setNavigationTargetId] = React.useState<string>();
3629

3730
const testRuns = React.useMemo(() => {
38-
const filtered = filteredTestRunIds
39-
? allTestRuns.filter((tr) => filteredTestRunIds.includes(tr.id))
40-
: allTestRuns;
41-
42-
const sorted = sortedTestRunIds
43-
? filtered
44-
.slice()
45-
.sort(
46-
(a, b) =>
47-
sortedTestRunIds.indexOf(a.id) - sortedTestRunIds.indexOf(b.id),
48-
)
49-
: filtered;
50-
return sorted;
51-
}, [allTestRuns, filteredTestRunIds, sortedTestRunIds]);
31+
if(filteredSortedTestRunIds) {
32+
return allTestRuns
33+
.filter((tr) => filteredSortedTestRunIds.includes(tr.id))
34+
.sort(
35+
(a, b) =>
36+
filteredSortedTestRunIds.indexOf(a.id) - filteredSortedTestRunIds.indexOf(b.id),
37+
)
38+
}
39+
return allTestRuns;
40+
}, [allTestRuns, filteredSortedTestRunIds]);
5241

5342
const selectedTestRunIndex = React.useMemo(
5443
() => testRuns.findIndex((t) => t.id === selectedTestRun?.id),
@@ -82,7 +71,7 @@ export const TestDetailsDialog: React.FunctionComponent = () => {
8271
}
8372

8473
return (
85-
<StyledDialog open={true} fullScreen className={classes.modal}>
74+
<Dialog open={true} fullScreen className={classes.modal}>
8675
<TestDetailsModal
8776
testRun={selectedTestRun}
8877
currentRunIndex={testRuns.findIndex(
@@ -107,6 +96,6 @@ export const TestDetailsDialog: React.FunctionComponent = () => {
10796
setNotSavedChangesModal(false);
10897
}}
10998
/>
110-
</StyledDialog>
99+
</Dialog>
111100
);
112101
};

src/components/TestRunList/index.tsx

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import {
1717
GridRenderCellParams,
1818
GridSortDirection,
1919
GridSortModel,
20+
gridFilteredSortedRowIdsSelector,
2021
} from "@mui/x-data-grid";
2122
import { DataGridCustomToolbar } from "./DataGridCustomToolbar";
2223
import { StatusFilterOperators } from "./StatusFilterOperators";
@@ -52,7 +53,7 @@ const columnsDef: GridColDef[] = [
5253

5354
return tags.reduce(
5455
(prev, curr) => prev.concat(curr ? `${curr};` : ""),
55-
"",
56+
""
5657
);
5758
},
5859
renderCell: (params: GridCellParams) => (
@@ -71,7 +72,7 @@ const columnsDef: GridColDef[] = [
7172
margin: "1px",
7273
}}
7374
/>
74-
),
75+
)
7576
)}
7677
</React.Fragment>
7778
),
@@ -96,7 +97,7 @@ const TestRunList: React.FunctionComponent = () => {
9697
const apiRef = useGridApiRef();
9798
const { enqueueSnackbar } = useSnackbar();
9899
const navigate = useNavigate();
99-
const { testRuns, loading } = useTestRunState();
100+
const { selectedTestRun, testRuns, loading } = useTestRunState();
100101
const { selectedBuild } = useBuildState();
101102
const testRunDispatch = useTestRunDispatch();
102103

@@ -123,12 +124,12 @@ const TestRunList: React.FunctionComponent = () => {
123124
testRunDispatch({
124125
type: "get",
125126
payload,
126-
}),
127+
})
127128
)
128129
.catch((err: string) =>
129130
enqueueSnackbar(err, {
130131
variant: "error",
131-
}),
132+
})
132133
);
133134
} else {
134135
testRunDispatch({
@@ -142,6 +143,29 @@ const TestRunList: React.FunctionComponent = () => {
142143
getTestRunListCallback();
143144
}, [getTestRunListCallback]);
144145

146+
// workaround https://github.com/mui/mui-x/issues/1106
147+
React.useEffect(() => {
148+
let unsubscribe: () => void;
149+
const handleStateChange = () => {
150+
unsubscribe?.();
151+
if (!selectedTestRun) {
152+
testRunDispatch({
153+
type: "filterSort",
154+
payload: gridFilteredSortedRowIdsSelector(apiRef),
155+
});
156+
}
157+
unsubscribe?.();
158+
};
159+
return apiRef.current.subscribeEvent?.(
160+
"stateChange",
161+
() =>
162+
(unsubscribe = apiRef.current.subscribeEvent(
163+
"stateChange",
164+
handleStateChange
165+
))
166+
);
167+
}, [apiRef, apiRef.current.instanceId]);
168+
145169
if (selectedBuild) {
146170
return (
147171
<React.Fragment>
@@ -168,10 +192,7 @@ const TestRunList: React.FunctionComponent = () => {
168192
onSortModelChange={(model) => setSortModel(model)}
169193
onRowClick={(param: GridRowParams) => {
170194
navigate(
171-
buildTestRunLocation(
172-
selectedBuild.id,
173-
param.row["id"].toString(),
174-
),
195+
buildTestRunLocation(selectedBuild.id, param.row["id"].toString())
175196
);
176197
}}
177198
/>

src/contexts/testRun.context.tsx

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { TestRun } from "../types";
33
import { useLocation } from "react-router-dom";
44
import { getQueryParams } from "../_helpers/route.helpers";
55
import { testRunService } from "../services";
6+
import { GridRowId } from "@mui/x-data-grid";
67

78
interface IRequestAction {
89
type: "request";
@@ -43,14 +44,9 @@ interface IRejectAction {
4344
payload: TestRun;
4445
}
4546

46-
interface IFilterAction {
47-
type: "filter";
48-
payload?: string[] | number[];
49-
}
50-
51-
interface ISortAction {
52-
type: "sort";
53-
payload?: string[] | number[];
47+
interface IFilterSortAction {
48+
type: "filterSort";
49+
payload?: GridRowId[];
5450
}
5551

5652
interface ITouchedAction {
@@ -66,15 +62,13 @@ type IAction =
6662
| IUpdateAction
6763
| IApproveAction
6864
| IRejectAction
69-
| IFilterAction
70-
| ISortAction
65+
| IFilterSortAction
7166
| ITouchedAction
7267
| ISelectAction;
7368
type Dispatch = (action: IAction) => void;
7469
type State = {
7570
selectedTestRun?: TestRun;
76-
sortedTestRunIds?: string[] | number[];
77-
filteredTestRunIds?: string[] | number[];
71+
filteredSortedTestRunIds?: GridRowId[];
7872
testRuns: TestRun[];
7973
touched: boolean;
8074
loading: boolean;
@@ -85,7 +79,7 @@ type TestRunProviderProps = {
8579

8680
const TestRunStateContext = React.createContext<State | undefined>(undefined);
8781
const TestRunDispatchContext = React.createContext<Dispatch | undefined>(
88-
undefined,
82+
undefined
8983
);
9084

9185
const initialState: State = {
@@ -103,16 +97,10 @@ function testRunReducer(state: State, action: IAction): State {
10397
selectedTestRun: action.payload,
10498
};
10599

106-
case "filter":
107-
return {
108-
...state,
109-
filteredTestRunIds: action.payload,
110-
};
111-
112-
case "sort":
100+
case "filterSort":
113101
return {
114102
...state,
115-
sortedTestRunIds: action.payload,
103+
filteredSortedTestRunIds: action.payload,
116104
};
117105

118106
case "request":
@@ -142,7 +130,7 @@ function testRunReducer(state: State, action: IAction): State {
142130
...state.testRuns,
143131
...action.payload.filter(
144132
// remove duplicates
145-
(i) => !state.testRuns.find((tr) => tr.id === i.id),
133+
(i) => !state.testRuns.find((tr) => tr.id === i.id)
146134
),
147135
],
148136
};

0 commit comments

Comments
 (0)