Skip to content

Commit a40fdac

Browse files
committed
examples: web-analytics add measures switcher to custom report
1 parent 197cf52 commit a40fdac

File tree

3 files changed

+73
-10
lines changed

3 files changed

+73
-10
lines changed

examples/web-analytics/dashboard-app/src/components/DotsMenu.js

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,6 @@ import Menu from '@material-ui/core/Menu';
44
import MenuItem from '@material-ui/core/MenuItem';
55
import MoreVertIcon from '@material-ui/icons/MoreVert';
66

7-
const options = [
8-
'Edit',
9-
'Delete'
10-
];
11-
127
const ITEM_HEIGHT = 48;
138

149
export default function DotsMenu({ options }) {
@@ -21,7 +16,7 @@ export default function DotsMenu({ options }) {
2116

2217
const handleClose = (callback) => {
2318
setAnchorEl(null);
24-
callback();
19+
callback && callback();
2520
};
2621

2722
return (
@@ -39,7 +34,7 @@ export default function DotsMenu({ options }) {
3934
anchorEl={anchorEl}
4035
keepMounted
4136
open={open}
42-
onClose={handleClose}
37+
onClose={() => handleClose() }
4338
PaperProps={{
4439
style: {
4540
maxHeight: ITEM_HEIGHT * 4.5,
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import React from 'react';
2+
import Button from '@material-ui/core/Button';
3+
import Menu from '@material-ui/core/Menu';
4+
import MenuItem from '@material-ui/core/MenuItem';
5+
import MoreVertIcon from '@material-ui/icons/MoreVert';
6+
import ExpandMoreIcon from '@material-ui/icons/ExpandMore';
7+
8+
const ITEM_HEIGHT = 48;
9+
10+
export default function Dropdown({ value, options }) {
11+
const [anchorEl, setAnchorEl] = React.useState(null);
12+
const open = Boolean(anchorEl);
13+
14+
const handleClick = event => {
15+
setAnchorEl(event.currentTarget);
16+
};
17+
18+
const handleClose = (callback) => {
19+
setAnchorEl(null);
20+
callback && callback();
21+
};
22+
23+
return (
24+
<div>
25+
<Button
26+
color="inherit"
27+
aria-haspopup="true"
28+
onClick={handleClick}
29+
>
30+
{ value }
31+
<ExpandMoreIcon fontSize="small" />
32+
</Button>
33+
<Menu
34+
id="long-menu"
35+
anchorEl={anchorEl}
36+
keepMounted
37+
open={open}
38+
onClose={() => handleClose() }
39+
PaperProps={{
40+
style: {
41+
maxHeight: ITEM_HEIGHT * 4.5,
42+
width: 200,
43+
},
44+
}}
45+
>
46+
{Object.keys(options).map(option => (
47+
<MenuItem key={option} onClick={() => handleClose(options[option])}>
48+
{option}
49+
</MenuItem>
50+
))}
51+
</Menu>
52+
</div>
53+
);
54+
}

examples/web-analytics/dashboard-app/src/pages/CustomReportPage.js

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@ import Grid from "@material-ui/core/Grid";
66
import { GET_CUSTOM_REPORT } from "../graphql/queries";
77
import OverTimeChart from "../components/OverTimeChart";
88
import DataTable from "../components/DataTable";
9+
import Dropdown from "../components/Dropdown";
910
import { Link } from "react-router-dom";
1011

1112
const CustomReportPage = ({ withTime }) => {
13+
const [activeMeasure, setActiveMeasure] = useState(null);
1214
const { id } = useParams();
1315
const { loading, error, data } = useQuery(GET_CUSTOM_REPORT, {
1416
variables: {
@@ -19,9 +21,10 @@ const CustomReportPage = ({ withTime }) => {
1921
return "Loading";
2022
}
2123

22-
const query = JSON.parse(data.customReport.query);
24+
const { measures, ...query } = JSON.parse(data.customReport.query);
25+
const finalActiveMeasure = activeMeasure || measures[0];
2326
const overTimeChartQuery = {
24-
measures: [query.measures[0]],
27+
measures: [finalActiveMeasure],
2528
timeDimensions: [{
2629
dimension: query.timeDimensions[0].dimension,
2730
granularity: 'day'
@@ -44,7 +47,18 @@ const CustomReportPage = ({ withTime }) => {
4447
</Grid>
4548
<Grid item xs={12}>
4649
<OverTimeChart
47-
title=""
50+
title={
51+
measures.length > 1 ?
52+
<Dropdown
53+
value={finalActiveMeasure}
54+
options={
55+
measures.reduce((out, measure) => {
56+
out[measure] = () => setActiveMeasure(measure)
57+
return out;
58+
}, {})
59+
}
60+
/> : finalActiveMeasure
61+
}
4862
vizState={withTime({ query: overTimeChartQuery, chartType: 'line' })}
4963
/>
5064
</Grid>

0 commit comments

Comments
 (0)