Skip to content

Commit 673f0f2

Browse files
authored
Merge pull request #15 from Visual-Regression-Tracker/22-variation-filter
22 variation filter
2 parents dd0d187 + f4467de commit 673f0f2

File tree

3 files changed

+53
-15
lines changed

3 files changed

+53
-15
lines changed

src/components/Filters.tsx

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,20 @@ import {
99
InputLabel,
1010
} from "@material-ui/core";
1111
import { Clear } from "@material-ui/icons";
12-
import { TestRun } from "../types";
12+
import { TestRun, TestVariation } from "../types";
1313

1414
interface IProps {
15-
testRuns: TestRun[];
15+
items: (TestRun | TestVariation)[];
1616
queryState: [string, React.Dispatch<React.SetStateAction<string>>];
1717
osState: [string, React.Dispatch<React.SetStateAction<string>>];
1818
deviceState: [string, React.Dispatch<React.SetStateAction<string>>];
1919
browserState: [string, React.Dispatch<React.SetStateAction<string>>];
2020
viewportState: [string, React.Dispatch<React.SetStateAction<string>>];
21-
testStatusState: [string, React.Dispatch<React.SetStateAction<string>>];
21+
testStatusState?: [string, React.Dispatch<React.SetStateAction<string>>];
2222
}
2323

2424
const Filters: React.FunctionComponent<IProps> = ({
25-
testRuns,
25+
items,
2626
queryState,
2727
osState,
2828
deviceState,
@@ -35,27 +35,31 @@ const Filters: React.FunctionComponent<IProps> = ({
3535
const [device, setDevice] = deviceState;
3636
const [browser, setBrowser] = browserState;
3737
const [viewport, setViewport] = viewportState;
38-
const [testStatus, setTestStatus] = testStatusState;
38+
const [testStatus, setTestStatus] = testStatusState
39+
? testStatusState
40+
: [null, () => {}];
3941

40-
const osList = testRuns
42+
const osList = items
4143
.map((t) => t.os)
4244
.filter((v, i, array) => v && array.indexOf(v) === i);
4345

44-
const deviceList = testRuns
46+
const deviceList = items
4547
.map((t) => t.device)
4648
.filter((v, i, array) => v && array.indexOf(v) === i);
4749

48-
const browserList = testRuns
50+
const browserList = items
4951
.map((t) => t.browser)
5052
.filter((v, i, array) => v && array.indexOf(v) === i);
5153

52-
const viewportList = testRuns
54+
const viewportList = items
5355
.map((t) => t.viewport)
5456
.filter((v, i, array) => v && array.indexOf(v) === i);
5557

56-
const testStatusList = testRuns
57-
.map((t) => t.status)
58-
.filter((v, i, array) => v && array.indexOf(v) === i);
58+
const testStatusList =
59+
items.some((i) => (i as TestRun).status) &&
60+
(items as TestRun[])
61+
.map((t) => t.status)
62+
.filter((v, i, array) => v && array.indexOf(v) === i);
5963

6064
return (
6165
<React.Fragment>
@@ -171,7 +175,7 @@ const Filters: React.FunctionComponent<IProps> = ({
171175
</FormControl>
172176
</Grid>
173177
)}
174-
{testStatusList.length > 0 && (
178+
{testStatusList && testStatusList.length > 0 && (
175179
<Grid item xs>
176180
<FormControl fullWidth>
177181
<InputLabel shrink id="filter_testStatus">

src/pages/ProjectPage.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ const ProjectPage = () => {
147147
<Grid item>
148148
<Box m={2}>
149149
<Filters
150-
testRuns={testRuns}
150+
items={testRuns}
151151
queryState={[query, setQuery]}
152152
osState={[os, setOs]}
153153
deviceState={[device, setDevice]}

src/pages/TestVariationListPage.tsx

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,22 @@ import { TestVariation } from "../types";
55
import { testVariationService } from "../services";
66
import { Container, Box, Grid, Typography } from "@material-ui/core";
77
import ProjectSelect from "../components/ProjectSelect";
8+
import Filters from "../components/Filters";
89

910
const TestVariationListPage: React.FunctionComponent = () => {
1011
const { projectId } = useParams();
1112
const [testVariations, setTestVariations] = React.useState<TestVariation[]>(
1213
[]
1314
);
1415

16+
// filter
17+
const [query, setQuery] = React.useState("");
18+
const [os, setOs] = React.useState("");
19+
const [device, setDevice] = React.useState("");
20+
const [browser, setBrowser] = React.useState("");
21+
const [viewport, setViewport] = React.useState("");
22+
const [filteredItems, setFilteredItems] = React.useState<TestVariation[]>([]);
23+
1524
React.useEffect(() => {
1625
if (projectId) {
1726
testVariationService.getList(projectId).then((testVariations) => {
@@ -20,6 +29,19 @@ const TestVariationListPage: React.FunctionComponent = () => {
2029
}
2130
}, [projectId]);
2231

32+
React.useEffect(() => {
33+
setFilteredItems(
34+
testVariations.filter(
35+
(t) =>
36+
t.name.includes(query) && // by query
37+
(os ? t.os === os : true) && // by OS
38+
(device ? t.device === device : true) && // by device
39+
(viewport ? t.viewport === viewport : true) && // by viewport
40+
(browser ? t.browser === browser : true) // by browser
41+
)
42+
);
43+
}, [query, os, device, browser, viewport, testVariations]);
44+
2345
return (
2446
<React.Fragment>
2547
<Container>
@@ -30,7 +52,19 @@ const TestVariationListPage: React.FunctionComponent = () => {
3052
<ProjectSelect selectedId={projectId} />
3153
</Grid>
3254
<Grid item>
33-
<TestVariationList items={testVariations} />
55+
<Box m={2}>
56+
<Filters
57+
items={testVariations}
58+
queryState={[query, setQuery]}
59+
osState={[os, setOs]}
60+
deviceState={[device, setDevice]}
61+
browserState={[browser, setBrowser]}
62+
viewportState={[viewport, setViewport]}
63+
/>
64+
</Box>
65+
</Grid>
66+
<Grid item>
67+
<TestVariationList items={filteredItems} />
3468
</Grid>
3569
</Grid>
3670
</Box>

0 commit comments

Comments
 (0)