Skip to content

Commit

Permalink
feat: Generate project intervals on frontend
Browse files Browse the repository at this point in the history
  • Loading branch information
David Code Howard committed Jan 3, 2024
1 parent d31f266 commit bc7655d
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 2 deletions.
3 changes: 1 addition & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 23 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
type DepthIntervalBounds = {
start: number;
end: number;
};

export const PRESETS: Record<'LANDPKS' | 'NRCS', DepthIntervalBounds[]> = {
LANDPKS: [
{ start: 0, end: 10 },
{ start: 10, end: 20 },
{ start: 20, end: 50 },
{ start: 50, end: 70 },
{ start: 70, end: 100 },
{ start: 100, end: 200 },
],
NRCS: [
{ start: 0, end: 5 },
{ start: 5, end: 15 },
{ start: 15, end: 30 },
{ start: 30, end: 60 },
{ start: 60, end: 100 },
{ start: 100, end: 200 },
],
};
38 changes: 38 additions & 0 deletions src/selectors.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
import { createSelector } from '@reduxjs/toolkit';
import { User } from 'terraso-client-shared/account/accountSlice';
import { PRESETS } from 'terraso-client-shared/constants';
import { UserRole } from 'terraso-client-shared/graphqlSchema/graphql';
import {
Project,
ProjectMembership,
} from 'terraso-client-shared/project/projectSlice';
import {
ProjectDepthInterval,
ProjectSoilSettings,
} from 'terraso-client-shared/soilId/soilIdSlice';
import { type SharedState } from 'terraso-client-shared/store/store';
import { exists, filterValues, mapValues } from 'terraso-client-shared/utils';

Expand Down Expand Up @@ -144,3 +149,36 @@ export const selectUserRoleSite = createSelector(
return { kind: 'project', role: membership.userRole };
},
);

const generateProjectIntervals = (settings: ProjectSoilSettings) => {
let depthIntervals: ProjectDepthInterval[] | undefined;
switch (settings.depthIntervalPreset) {
case 'LANDPKS':
case 'NRCS':
depthIntervals = PRESETS[settings.depthIntervalPreset].map(
depthInterval => ({ depthInterval, label: '' }),
);
break;
case 'CUSTOM':
depthIntervals = settings.depthIntervals;
break;
case 'NONE':
depthIntervals = undefined;
break;
}
return depthIntervals;
};

export const selectProjectSettings = createSelector(
[
(state: SharedState, projectId: string) =>
state.soilId.projectSettings[projectId],
],
(projectSettings: ProjectSoilSettings) => {
if (!projectSettings) {
return undefined;
}
const depthIntervals = generateProjectIntervals(projectSettings) || [];
return { ...projectSettings, depthIntervals };
},
);

0 comments on commit bc7655d

Please sign in to comment.