-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cgra-dynamicmeasures - Improvements by github.com/no23reason
- Loading branch information
1 parent
ec5bc67
commit 17f133b
Showing
5 changed files
with
109 additions
and
109 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import React, { useMemo, useState } from "react"; | ||
import { DefaultDashboardInsight } from "@gooddata/sdk-ui-dashboard"; | ||
import { insightSetBuckets } from "@gooddata/sdk-model"; | ||
|
||
import styles from "./MetricSwitcher.module.scss"; | ||
|
||
const MetricSwitcher = props => { | ||
const widgetId = props.widget.identifier; | ||
const originalBuckets = props.insight.insight.buckets; | ||
const originalMeasures = originalBuckets[0].items; | ||
const [measures, setMeasures] = useState(originalMeasures); | ||
|
||
// memoize the altered insight properly to improve performance and prevent weird race conditions | ||
// this makes sure the underlying pluggable visualization is updated only when actually needed | ||
const newInsight = useMemo(() => { | ||
const newMeasuresBucket = { | ||
...originalBuckets[0], | ||
items: measures, | ||
}; | ||
|
||
return insightSetBuckets(props.insight, [ | ||
newMeasuresBucket, | ||
...originalBuckets.filter(b => b.localIdentifier !== "measures"), | ||
]); | ||
}, [measures, props.insight, originalBuckets]); | ||
|
||
return ( | ||
<div className={styles.CustomInsight}> | ||
<div className={styles.MetricSwitcher}> | ||
{originalMeasures.map(defaultMeasure => { | ||
const localIdentifier = defaultMeasure.measure.localIdentifier; | ||
const isChecked = !!measures.find(m => m.measure.localIdentifier === localIdentifier); | ||
|
||
return ( | ||
<label | ||
htmlFor={`${widgetId}-${localIdentifier}`} | ||
// use key to prevent react warnings | ||
key={`${widgetId}-${localIdentifier}`} | ||
> | ||
<input | ||
id={`${widgetId}-${localIdentifier}`} | ||
value={localIdentifier} | ||
type="checkbox" | ||
defaultChecked={isChecked} | ||
disabled={measures.length === 1 && isChecked} | ||
onChange={e => { | ||
if (!e.currentTarget.checked) { | ||
setMeasures( | ||
measures.filter( | ||
m => m.measure.localIdentifier !== localIdentifier, | ||
), | ||
); | ||
} else { | ||
setMeasures([...measures, defaultMeasure]); | ||
} | ||
}} | ||
/> | ||
{defaultMeasure.measure.title} | ||
</label> | ||
); | ||
})} | ||
</div> | ||
<div className={styles.Insight}> | ||
<DefaultDashboardInsight {...props} insight={newInsight} /> | ||
</div> | ||
</div> | ||
); | ||
}; | ||
|
||
export default MetricSwitcher; |
25 changes: 25 additions & 0 deletions
25
cgra-dynamicmeasures/src/components/MetricSwitcher.module.scss
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
.CustomInsight { | ||
// border: 1px solid red; | ||
height: 100%; | ||
display: flex; | ||
|
||
.MetricSwitcher { | ||
padding: 20px; | ||
// border: 1px solid green; | ||
width: 400px; | ||
|
||
label { | ||
display: block; | ||
margin-bottom: 10px; | ||
|
||
input { | ||
margin-right: 10px; | ||
} | ||
} | ||
} | ||
|
||
.Insight { | ||
// border: 1px solid blue; | ||
flex: 1 0 auto; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.