import React from "react"; import { useSelector } from "react-redux"; import { useAppDispatch } from "../../hooks"; import { isEqual } from "lodash"; import { changeMeasurementsCollection, changeMeasurementsDisplay, changeMeasurementsGroupBy, toggleOverallMean, toggleThreshold } from "../../actions/measurements"; import { controlsWidth } from "../../util/globals"; import { SidebarSubtitle, SidebarButton } from "./styles"; import Toggle from "./toggle"; import CustomSelect from "./customSelect"; import { Collection } from "../../reducers/measurements/types"; import { RootState } from "../../store"; interface SelectOption { value: string label: string } /** * React Redux selector function that takes the key and title for the * available collections to create the object expected for the Select library. * The label defaults to the key if a collection does not have a set title. */ const collectionOptionsSelector = ( collections: Collection[] ): SelectOption[] => { return collections.map((collection) => { return { value: collection.key, label: collection.title || collection.key }; }); }; const MeasurementsOptions = (): JSX.Element => { const dispatch = useAppDispatch(); const collection = useSelector((state: RootState) => state.measurements.collectionToDisplay); const collectionOptions = useSelector((state: RootState) => collectionOptionsSelector(state.measurements.collections), isEqual); const groupBy = useSelector((state: RootState) => state.controls.measurementsGroupBy); const display = useSelector((state: RootState) => state.controls.measurementsDisplay); const showOverallMean = useSelector((state: RootState) => state.controls.measurementsShowOverallMean); const showThreshold = useSelector((state: RootState) => state.controls.measurementsShowThreshold); // Create grouping options for the Select library let groupingOptions: SelectOption[] = []; if (collection.groupings) { groupingOptions = [...collection.groupings.keys()].map((grouping) => { return { value: grouping, label: collection.fields.get(grouping).title }; }); } return (
{"Collections"}
value === collection.key)} options={collectionOptions} isClearable={false} isSearchable={false} isMulti={false} onChange={(opt): void => { dispatch(changeMeasurementsCollection(opt.value)); }} />
{"Group By"}
value === groupBy)} options={groupingOptions} isClearable={false} isSearchable={false} isMulti={false} onChange={(opt): void => {dispatch(changeMeasurementsGroupBy(opt.value));}} />
{"Measurements Display"} {dispatch(changeMeasurementsDisplay("mean"));}} > {"Mean ± SD"} {dispatch(changeMeasurementsDisplay("raw"));}} > {"Raw"} dispatch(toggleOverallMean())} /> typeof threshold === "number") } on={showThreshold} label="Show measurement threshold(s)" callback={(): void => dispatch(toggleThreshold())} />
); }; export default MeasurementsOptions;