Skip to content

Commit

Permalink
Bug fix by removing all context logic from FormStore as it is handled…
Browse files Browse the repository at this point in the history
… in Metric Settings by MetricConfigStore
  • Loading branch information
Mahmoud authored and Mahmoud committed May 22, 2024
1 parent 5f2373d commit 4df2a75
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 165 deletions.
66 changes: 0 additions & 66 deletions publisher/src/components/Reports/DataEntryFormComponents.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import { Input } from "@justice-counts/common/components/Input";
import {
Metric,
MetricContext,
MetricDisaggregationDimensions,
MetricDisaggregations,
} from "@justice-counts/common/types";
Expand Down Expand Up @@ -158,68 +157,3 @@ export const DisaggregationDimensionTextInput = observer(
);
}
);

interface AdditionalContextInputsProps extends MetricTextInputProps {
context: MetricContext;
contextIndex: number;
}

export const AdditionalContextInput = observer(
({
reportID,
metric,
context,
contextIndex,
disabled,
updateFieldDescription,
clearFieldDescription,
}: AdditionalContextInputsProps) => {
const { formStore } = useStore();
const { contexts, updateContextValue } = formStore;
const getContextValue = () => {
if (
contexts?.[reportID]?.[metric.key]?.[context.key]?.value !== undefined
) {
return context.type === "NUMBER"
? formatNumberInput(
contexts[reportID]?.[metric.key][context.key].value
)
: contexts[reportID]?.[metric.key][context.key].value;
}

return metric.contexts[contextIndex].value?.toString() || "";
};
const contextValue = getContextValue();

const handleContextChange = (
e: React.ChangeEvent<HTMLTextAreaElement | HTMLInputElement>
) =>
updateContextValue(
reportID,
metric.key,
context.key,
e.target.value,
context.required,
context.type,
metric.enabled
);

return (
<Input
type="text"
metricKey={metric.key}
name={context.key}
id={context.key}
label="Type here..."
onChange={handleContextChange}
value={contextValue}
multiline={context.type === "TEXT"}
error={contexts?.[reportID]?.[metric.key]?.[context.key]?.error}
required={context.required}
onFocus={updateFieldDescription}
onBlur={clearFieldDescription}
disabled={disabled}
/>
);
}
);
100 changes: 1 addition & 99 deletions publisher/src/stores/FormStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,20 +82,6 @@ class FormStore {
}
});
});

metric.contexts.forEach((context) => {
if (context.value !== null && context.value !== undefined) {
this.updateContextValue(
reportID,
metric.key,
context.key,
normalizeToString(context.value),
context.required,
context.type,
metric.enabled
);
}
});
});
}

Expand Down Expand Up @@ -141,7 +127,6 @@ class FormStore {
const updatedMetrics = this.reportStore.reportMetrics[reportID]?.map(
(metric) => {
const metricValues = this.metricsValues[reportID]?.[metric.key];
const contexts = this.contexts[reportID]?.[metric.key];
const disaggregationForMetric =
this.disaggregations[reportID]?.[metric.key];
const metricIsEmpty = this.isMetricEmpty(reportID, metric.key);
Expand Down Expand Up @@ -173,38 +158,7 @@ class FormStore {
...metric,
value: sanitizeInputValue(metricValues?.value, metric.value),
error: metricError,
contexts: metric.contexts.map((context) => {
/** Touch & validate context field */
if (metricValues?.value !== "") {
this.updateContextValue(
reportID,
metric.key,
context.key,
normalizeToString(contexts?.[context.key]?.value) ||
normalizeToString(context.value),
context.required,
context.type,
metric.enabled
);
}

const contextError =
this.contexts[reportID]?.[metric.key]?.[context.key]?.error;

if (contextError) {
errorFound = true;
}

return {
...context,
value: sanitizeInputValue(
contexts?.[context.key]?.value,
context.value,
context.type
),
error: contextError,
};
}),
contexts: metric.contexts,
disaggregations: metric.disaggregations.map((disaggregation) => {
return {
...disaggregation,
Expand Down Expand Up @@ -278,19 +232,6 @@ class FormStore {
disaggregations: [],
};

metric.contexts.forEach((context) => {
const contextValue = sanitizeInputValue(
this.contexts[reportID]?.[metric.key]?.[context.key]?.value,
context.value,
context.type
);

combinedMetricValues.contexts.push({
key: context.key,
value: contextValue,
});
});

metric.disaggregations.forEach((disaggregation) => {
combinedMetricValues.disaggregations.push({
key: disaggregation.key,
Expand Down Expand Up @@ -490,45 +431,6 @@ class FormStore {
}
};

updateContextValue = (
reportID: number,
metricKey: string,
contextKey: string,
updatedValue: string,
required: boolean,
contextType: string,
metricEnabled: boolean | undefined | null
): void => {
/**
* Create an empty object within the property if none exist to improve access
* speed and to help with isolating re-renders for each form component.
*/
if (!this.contexts[reportID]) {
this.contexts[reportID] = {};
}

if (!this.contexts[reportID][metricKey]) {
this.contexts[reportID][metricKey] = {};
}

if (!this.contexts[reportID][metricKey][contextKey]) {
this.contexts[reportID][metricKey][contextKey] = {};
}

this.contexts[reportID][metricKey][contextKey].value = updatedValue;

if (metricEnabled) {
this.validate(
contextType,
updatedValue,
required,
reportID,
metricKey,
contextKey
);
}
};

resetBinaryInput = (
reportID: number,
metricKey: string,
Expand Down

0 comments on commit 4df2a75

Please sign in to comment.