Skip to content

Commit

Permalink
fix: plotting
Browse files Browse the repository at this point in the history
  • Loading branch information
henrikmv committed Mar 21, 2024
1 parent 244105b commit 1c2d165
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 35 deletions.
2 changes: 1 addition & 1 deletion src/utils/DataFetching/Hooks/useEvents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ export const useEventsByProgramStage = ({
},
}), { staleTime: 5000 });

const events = useMemo(() => data?.eventsByProgramStage?.events?.map((event: ServerEvent) => {
const events = useMemo(() => data?.eventsByProgramStage?.instances?.map((event: ServerEvent) => {
const dataValues = convertDataElementToValues(event?.dataValues);
return {
...event,
Expand Down
64 changes: 30 additions & 34 deletions src/utils/Hooks/ChartDataVisualization/useMeasurementPlotting.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { useEffect, useState } from 'react'; // Import useState
import { useCalculateDecimalDate } from '../Calculations/useCalculateDecimalDate';
import { DataSetLabels } from '../../../types/chartDataTypes';

Expand All @@ -13,49 +12,46 @@ export const useMeasurementPlotting = (
measurementData: MeasurementDataEntry[] | undefined,
fieldName: string,
category: string,
dataset: string | number,
dataset: string,
dateOfBirth: Date,
startIndex: number,
) => {
const [measurementDataValues, setMeasurementDataValues] = useState<{ x: Date | number | string; y: number; eventDate?: Date }[]>([]);
const measurementDataValues: { x: Date | number | string; y: number; eventDate?: Date }[] = [];

useEffect(() => {
const processEntry = (entry: MeasurementDataEntry) => {
let xValue: Date | number | string;
let yValue: number;
if (!measurementData) {
return [];
}

if (category === 'wflh_b' || category === 'wflh_g') {
xValue = parseFloat(entry.dataValues.height as string);
yValue = parseFloat(entry.dataValues.weight as string);
} else {
const dateString: string = typeof entry.eventDate === 'string' ? entry.eventDate : entry.eventDate.toISOString();
const xValueDecimalDate: string = useCalculateDecimalDate(dateString, dataset, dateOfBirth);
xValue = xValueDecimalDate;
yValue = parseFloat(entry.dataValues[fieldName] as string);
}
const processEntry = (entry: MeasurementDataEntry) => {
let xValue: Date | number | string;
let yValue: number;

const eventDateValue = new Date(entry.eventDate);
setMeasurementDataValues((prevValues) => [...prevValues, {
x: xValue,
y: yValue,
eventDate: eventDateValue,
}]);
};

if (!measurementData) {
return;
if (category === 'wflh_b' || category === 'wflh_g') {
xValue = parseFloat(entry.dataValues.height as string);
yValue = parseFloat(entry.dataValues.weight as string);
} else {
const dateString: string = typeof entry.eventDate === 'string' ? entry.eventDate : entry.eventDate.toISOString();
const xValueDecimalDate: string = useCalculateDecimalDate(dateString, dataset, dateOfBirth);
xValue = xValueDecimalDate;
yValue = parseFloat(entry.dataValues[fieldName] as string);
}

const validDatasets = Object.values(DataSetLabels);
if (validDatasets.includes(dataset)) {
measurementData.forEach(processEntry);
const eventDateValue = new Date(entry.eventDate);
measurementDataValues.push({
x: xValue,
y: yValue,
eventDate: eventDateValue,
});
};

const validDatasets = Object.values(DataSetLabels);
if (validDatasets.includes(dataset)) {
measurementData.forEach(processEntry);

if (dataset !== DataSetLabels.y_2_5) {
setMeasurementDataValues((prevValues) => prevValues.filter((data) =>
typeof data.x === 'number' && data.x >= startIndex));
}
if (dataset !== DataSetLabels.y_2_5) {
measurementDataValues.filter((data) => typeof data.x === 'number' && data.x >= startIndex);
}
}, [measurementData, fieldName, category, dataset, dateOfBirth, startIndex]);
}

return [
{
Expand Down

0 comments on commit 1c2d165

Please sign in to comment.