Skip to content

Commit

Permalink
put into util
Browse files Browse the repository at this point in the history
  • Loading branch information
Brandon McArthur authored and Brandon McArthur committed Feb 15, 2024
1 parent 2f01a86 commit fa42a8d
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 10 deletions.
12 changes: 2 additions & 10 deletions src/components/Panel/PanelGraphicWrapper.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-disable react/jsx-no-useless-fragment */
import React, { useState, useEffect } from 'react';
import PanelGraphic from './PanelGraphic';
import { getLabelFromFactor, panelSrcGetter, snakeCase } from '../../utils';
import { panelSrcGetter, replaceDatumFactorsWithLabels, snakeCase } from '../../utils';
import { useDisplayInfo } from '../../slices/displayInfoAPI';

interface PanelGraphicProps {
Expand All @@ -28,15 +28,7 @@ const PanelGraphicWrapper: React.FC<PanelGraphicProps> = ({
const { data: displayInfo } = useDisplayInfo();
const [panelSrc, setPanelSrc] = useState('');
const sourceFunc = async (func: PanelFunction) => {
const dataWithFactorLabels = Object.keys(data).reduce((acc, key) => {
const foundFactor = displayInfo?.metas?.find((curMeta) => curMeta.varname === key && curMeta.type === 'factor');
if (foundFactor) {
const factorLabel = getLabelFromFactor(data[key] as number, foundFactor.levels as string[]);
return { ...acc, [key]: factorLabel };
}
return { ...acc, [key]: data[key] };
}, {});

const dataWithFactorLabels = replaceDatumFactorsWithLabels(data, displayInfo?.metas as IMeta[]);
const res = await func(dataWithFactorLabels);
setPanelSrc(res);
return res;
Expand Down
10 changes: 10 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,13 @@ export const getFactorFromLabel = (values: string[], levels: string[]) =>

export const panelSrcGetter = (basePath: string, fileName: string, displayName: string) =>
`${basePath}/displays/${displayName}/${fileName}`;

export const replaceDatumFactorsWithLabels = (data: Datum, displayMetas: IMeta[]) =>
Object.keys(data).reduce((acc, key) => {
const foundFactor = displayMetas?.find((curMeta) => curMeta.varname === key && curMeta.type === 'factor');
if (foundFactor) {
const factorLabel = getLabelFromFactor(data[key] as number, foundFactor.levels as string[]);
return { ...acc, [key]: factorLabel };
}
return { ...acc, [key]: data[key] };
}, {});

0 comments on commit fa42a8d

Please sign in to comment.