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

feat: select soil #2514

Open
wants to merge 14 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ const appState = (): AppState => {
soilData: {},
status: 'ready',
},
soilMetadata: {
soilMetadata: {},
},
};
};

Expand Down
13 changes: 11 additions & 2 deletions dev-client/locales/po/en.po
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ msgstr ""
"Content-Type: text/plain; charset=utf-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Plural-Forms: nplurals=2; plural=(n != 1)\n"
"POT-Creation-Date: 2024-11-20T19:30:15.462Z\n"
"PO-Revision-Date: 2024-11-20T19:30:15.462Z\n"
"POT-Creation-Date: 2024-11-21T22:46:04.456Z\n"
"PO-Revision-Date: 2024-11-21T22:46:04.457Z\n"

msgid "welcome##title"
msgstr "Welcome to LandPKS Soil ID"
Expand Down Expand Up @@ -274,6 +274,9 @@ msgstr ""
"\n"
"Create a site here and enter the recommended data to improve accuracy of the match scores."

msgid "site##soil_id##matches##match_score"
msgstr "{{score}}"

msgid "site##soil_id##matches##match"
msgstr "match"

Expand Down Expand Up @@ -304,6 +307,12 @@ msgstr "You are offline"
msgid "site##soil_id##matches##offline_body"
msgstr "Soil matches will update here when you are online."

msgid "site##soil_id##matches##selector"
msgstr "This is the correct soil"

msgid "site##soil_id##matches##selected"
msgstr "Selected Soil"

msgid "site##soil_id##site_data##title"
msgstr "Site Data"

Expand Down
20 changes: 16 additions & 4 deletions dev-client/package-lock.json

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

2 changes: 1 addition & 1 deletion dev-client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@
"react-native-svg": "^15.9.0",
"react-native-tab-view": "^4.0.2",
"reduce-reducers": "^1.0.4",
"terraso-client-shared": "github:techmatters/terraso-client-shared#cdf2161",
"terraso-client-shared": "github:techmatters/terraso-client-shared#230fdf0",
"use-debounce": "^10.0.4",
"uuid": "^10.0.0",
"yup": "^1.4.0"
Expand Down
2 changes: 2 additions & 0 deletions dev-client/src/model/site/siteGlobalReducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import {
updateSites,
} from 'terraso-mobile-client/model/site/siteSlice';
import {deleteSoilData} from 'terraso-mobile-client/model/soilId/soilIdSlice';
import {deleteSoilMetadata} from 'terraso-mobile-client/model/soilId/soilMetadataSlice';
import {createGlobalReducer} from 'terraso-mobile-client/store/reducers';

export const addSite = createAsyncThunk('site/addSite', siteService.addSite);
Expand Down Expand Up @@ -74,6 +75,7 @@ export const siteGlobalReducer = createGlobalReducer(builder => {
removeSiteFromAllProjects(state.project, payload);
deleteSites(state.site, [payload]);
deleteSoilData(state.soilId, [payload]);
deleteSoilMetadata(state.soilMetadata, [payload]);
});

builder.addCase(transferSites.fulfilled, (state, {payload}) => {
Expand Down
2 changes: 2 additions & 0 deletions dev-client/src/model/soilId/soilIdGlobalReducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {
setSoilData,
updateSoilIdStatus,
} from 'terraso-mobile-client/model/soilId/soilIdSlice';
import {setSoilMetadata} from 'terraso-mobile-client/model/soilId/soilMetadataSlice';
import {createGlobalReducer} from 'terraso-mobile-client/store/reducers';

export const fetchSoilDataForUser = createAsyncThunk(
Expand All @@ -40,6 +41,7 @@ export const soilIdGlobalReducer = createGlobalReducer(builder => {
setUsers(state.account, payload.users);
setProjectSettings(state.soilId, payload.projectSoilSettings);
setSoilData(state.soilId, payload.soilData);
setSoilMetadata(state.soilMetadata, payload.soilMetadata);
updateSoilIdStatus(state.soilId, 'ready');
});

Expand Down
84 changes: 84 additions & 0 deletions dev-client/src/model/soilId/soilMetadataFunctions.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*
* Copyright © 2021-2023 Technology Matters
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see https://www.gnu.org/licenses/.
*/

import {
findSelectedMatch,
getMatchSelectionId,
} from 'terraso-mobile-client/model/soilId/soilMetadataFunctions';

const dataBasedMatchWithName = (name: string) => {
return {
soilInfo: {
landCapabilityClass: {capabilityClass: '', subClass: ''},
soilData: {depthDependentData: []},
soilSeries: {
name: name,
description: '',
fullDescriptionUrl: '',
taxonomySubgroup: '',
},
},
combinedMatch: {
rank: 0,
score: 0,
},
dataMatch: {
rank: 0,
score: 0,
},
distanceToNearestMapUnitM: 0,
locationMatch: {
rank: 0,
score: 0,
},
dataSource: '',
};
};

describe('getMatchSelectionId', () => {
test('returns the soil series name', () => {
expect(getMatchSelectionId(dataBasedMatchWithName('hello'))).toEqual(
'hello',
);
});
});

describe('findSelectedMatch', () => {
test('returns the match with the matching name', () => {
const a = dataBasedMatchWithName('a');
const b = dataBasedMatchWithName('b');
const c = dataBasedMatchWithName('c');

expect(findSelectedMatch([a, b, c], 'b')).toBe(b);
});

test('returns undefined for no match', () => {
const a = dataBasedMatchWithName('a');
const b = dataBasedMatchWithName('b');
const c = dataBasedMatchWithName('c');

expect(findSelectedMatch([a, b, c], 'z')).toBeUndefined();
});

test('returns undefined for no selection', () => {
const a = dataBasedMatchWithName('a');
const b = dataBasedMatchWithName('b');
const c = dataBasedMatchWithName('c');

expect(findSelectedMatch([a, b, c], undefined)).toBeUndefined();
});
});
32 changes: 32 additions & 0 deletions dev-client/src/model/soilId/soilMetadataFunctions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Copyright © 2024 Technology Matters
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see https://www.gnu.org/licenses/.
*/

import {DataBasedSoilMatch} from 'terraso-client-shared/graphqlSchema/graphql';

export const getMatchSelectionId = (match: DataBasedSoilMatch) => {
return match.soilInfo.soilSeries.name;
};

export const findSelectedMatch = (
matches: DataBasedSoilMatch[],
selectedSoilId: string | undefined | null,
): DataBasedSoilMatch | undefined => {
if (!selectedSoilId) {
return undefined;
}
return matches.find(match => selectedSoilId === getMatchSelectionId(match));
};
50 changes: 50 additions & 0 deletions dev-client/src/model/soilId/soilMetadataHooks.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* Copyright © 2024 Technology Matters
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see https://www.gnu.org/licenses/.
*/

import {useCallback} from 'react';
import {useDispatch, useSelector} from 'react-redux';

import type {SharedDispatch} from 'terraso-client-shared/store/store';

import {selectSoilMetadata} from 'terraso-mobile-client/model/soilId/soilMetadataSelectors';
import {updateSoilMetadata} from 'terraso-mobile-client/model/soilId/soilMetadataSlice';

export const useSoilIdSelection = (
siteId: string,
): {
selectedSoilId?: string;
selectSoilId: (selectedSoilId: string | null) => Promise<void>;
} => {
const dispatch = useDispatch<SharedDispatch>();

const soilMetadata = useSelector(selectSoilMetadata(siteId));
const selectedSoilId =
soilMetadata.selectedSoilId === null ||
soilMetadata.selectedSoilId === undefined
? undefined
: soilMetadata.selectedSoilId;

const selectSoilId = useCallback(
(newSelection: string | null) =>
dispatch(
updateSoilMetadata({siteId: siteId, selectedSoilId: newSelection}),
).then(() => {}),
[dispatch, siteId],
);

return {selectedSoilId, selectSoilId};
};
26 changes: 26 additions & 0 deletions dev-client/src/model/soilId/soilMetadataSelectors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Copyright © 2024 Technology Matters
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see https://www.gnu.org/licenses/.
*/

import {createSelector} from '@reduxjs/toolkit';

import {AppState} from 'terraso-mobile-client/store';

export const selectSoilMetadata = (siteId: string) =>
createSelector(
(state: AppState) => state.soilMetadata.soilMetadata[siteId],
metaData => metaData ?? {},
);
67 changes: 67 additions & 0 deletions dev-client/src/model/soilId/soilMetadataSlice.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* Copyright © 2024 Technology Matters
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see https://www.gnu.org/licenses/.
*/

import {createSlice, Draft} from '@reduxjs/toolkit';

import {SoilMetadata} from 'terraso-client-shared/soilId/soilIdTypes';
import * as soilMetadataService from 'terraso-client-shared/soilId/soilMetadataService';
import {createAsyncThunk} from 'terraso-client-shared/store/utils';

export * from 'terraso-client-shared/soilId/soilIdTypes';
export * from 'terraso-mobile-client/model/soilId/soilIdFunctions';

export type SoilState = {
soilMetadata: Record<string, SoilMetadata | undefined>;
};

export const initialState: SoilState = {
soilMetadata: {},
};

export const setSoilMetadata = (
state: Draft<SoilState>,
soilMetadata: Record<string, SoilMetadata>,
) => {
state.soilMetadata = soilMetadata;
};

export const deleteSoilMetadata = (
state: Draft<SoilState>,
siteIds: string[],
) => {
for (const siteId of siteIds) {
delete state.soilMetadata[siteId];
}
};

const soilMetadataSlice = createSlice({
name: 'soilMetadata',
initialState,
reducers: {},
extraReducers: builder => {
builder.addCase(updateSoilMetadata.fulfilled, (state, action) => {
state.soilMetadata[action.meta.arg.siteId] = action.payload;
});
},
});

export const updateSoilMetadata = createAsyncThunk(
'soilId/updateSoilMetadata',
soilMetadataService.updateSoilMetadata,
);

export default soilMetadataSlice.reducer;
Loading
Loading