Skip to content

Commit

Permalink
refactor: better type safety for soilDataDiff
Browse files Browse the repository at this point in the history
  • Loading branch information
tm-ruxandra committed Nov 13, 2024
1 parent d694f44 commit c216683
Showing 1 changed file with 9 additions and 12 deletions.
21 changes: 9 additions & 12 deletions dev-client/src/model/soilId/actions/soilDataDiff.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,23 +25,20 @@ import {
import {
DEPTH_DEPENDENT_SOIL_DATA_UPDATE_FIELDS,
DEPTH_INTERVAL_UPDATE_FIELDS,
DepthDependentUpdateField,
DepthIntervalUpdateField,
SOIL_DATA_UPDATE_FIELDS,
SoilDataUpdateField,
} from 'terraso-mobile-client/model/soilId/actions/soilDataActionFields';
import {depthIntervalKey} from 'terraso-mobile-client/model/soilId/soilIdFunctions';

export const getChangedSoilDataFields = (
curr: SoilData,
prev?: SoilData,
): Record<SoilDataUpdateField, any> => {
): Partial<SoilData> => {
return diffFields(SOIL_DATA_UPDATE_FIELDS, curr, prev);
};

export type DepthIntervalChanges<F extends string> = {
export type DepthIntervalChanges<T> = {
depthInterval: DepthInterval;
changedFields: Record<F, any>;
changedFields: Partial<T>;
};

export const getDeletedDepthIntervals = (
Expand All @@ -63,7 +60,7 @@ export const getDeletedDepthIntervals = (
export const getChangedDepthIntervals = (
curr: SoilData,
prev?: SoilData,
): DepthIntervalChanges<DepthIntervalUpdateField>[] => {
): DepthIntervalChanges<SoilDataDepthInterval>[] => {
const prevIntervals = indexDepthIntervals(prev?.depthIntervals ?? []);
const diffs = curr.depthIntervals.map(di => {
return {
Expand All @@ -81,14 +78,14 @@ export const getChangedDepthIntervals = (
export const getChangedDepthIntervalFields = (
curr: SoilDataDepthInterval,
prev?: SoilDataDepthInterval,
): Record<DepthIntervalUpdateField, any> => {
): Partial<SoilDataDepthInterval> => {
return diffFields(DEPTH_INTERVAL_UPDATE_FIELDS, curr, prev);
};

export const getChangedDepthDependentData = (
curr: SoilData,
prev?: SoilData,
): DepthIntervalChanges<DepthDependentUpdateField>[] => {
): DepthIntervalChanges<DepthDependentSoilData>[] => {
const prevData = indexDepthIntervals(prev?.depthDependentData ?? []);
const diffs = curr.depthDependentData.map(dd => {
return {
Expand All @@ -105,15 +102,15 @@ export const getChangedDepthDependentData = (
export const getChangedDepthDependentFields = (
curr: DepthDependentSoilData,
prev?: DepthDependentSoilData,
): Record<DepthDependentUpdateField, any> => {
): Partial<DepthDependentSoilData> => {
return diffFields(DEPTH_DEPENDENT_SOIL_DATA_UPDATE_FIELDS, curr, prev);
};

export const diffFields = <F extends keyof T, T>(
fields: F[],
curr: T,
prev?: T,
): Record<F, any> => {
): Partial<T> => {
let changedFields: (keyof T)[];
if (!prev) {
changedFields = fields;
Expand All @@ -123,7 +120,7 @@ export const diffFields = <F extends keyof T, T>(

return Object.fromEntries(
changedFields.map(field => [field, curr[field]]),
) as Record<F, any>;
) as Partial<T>;
};

export const indexDepthIntervals = <T>(
Expand Down

0 comments on commit c216683

Please sign in to comment.