Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Stop series data not saving when max values reached [PT-187949831] #167

Merged
merged 1 commit into from
Jul 18, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions src/shared/components/data-table-field.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ export const DataTableField: React.FC<FieldProps> = props => {
}, [isTimeSeries, formData]);
const [selectableSensorId, setSelectableSensorId] = useState<any>();
const {log} = formContext;
const timeSeriesFormDataRef = useRef<IDataTableRow[]|undefined>(undefined);

// So this is a bit of a hack - if we use the setInputDisabled function passed in the formContext
// the timeseries data saving fails, probably because of a re-render which updates the formData.
Expand Down Expand Up @@ -389,11 +390,15 @@ export const DataTableField: React.FC<FieldProps> = props => {

const timeSeriesMetadata = getTimeSeriesMetadata(timeSeriesCapabilities);

// save a ref to the data so that when we stop the stop handler doesn't keep a closure over the data
timeSeriesFormDataRef.current = [];

stopTimeSeriesFnRef.current = sensor.collectTimeSeries(timeSeriesCapabilities.measurementPeriod, selectableSensorId, (values) => {
const newData = formData.slice();
newData[rowIdx] = {...newData[rowIdx], timeSeries: values, timeSeriesMetadata};
if (values.length <= MaxNumberOfTimeSeriesValues) {
setFormData(newData);
timeSeriesFormDataRef.current = newData;
}
if (values.length === MaxNumberOfTimeSeriesValues) {
onSensorStopTimeSeries();
Expand All @@ -417,17 +422,18 @@ export const DataTableField: React.FC<FieldProps> = props => {
}
};

const onSensorStopTimeSeries = useCallback(() => {
const onSensorStopTimeSeries = () => {
// no check of input disabled here as this handler updates it
if (stopTimeSeriesFnRef.current) {
if (stopTimeSeriesFnRef.current && timeSeriesFormDataRef.current) {
log?.("sensorStopTimeSeries");
stopTimeSeriesFnRef.current?.();
stopTimeSeriesFnRef.current = undefined;
timeSeriesRecordingRowRef.current = undefined;
saveData(formData);
saveData(timeSeriesFormDataRef.current);
timeSeriesFormDataRef.current = undefined;
setInputDisabled(false);
}
}, [formData]);
};

// make sure the time series recording stops if the tab is changed
useEffect(() => {
Expand Down
Loading