Skip to content

Commit 40f3a00

Browse files
committed
feat: adapt PlantLocationDetails for single tree registration
- updates API to return correct data for single tree registration - displays only relevant data for single tree registration - add header to display selected plant location hid
1 parent ef207d6 commit 40f3a00

File tree

5 files changed

+90
-31
lines changed

5 files changed

+90
-31
lines changed

pages/api/data-explorer/map/plant-location/[plantLocationId].ts

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,24 +14,32 @@ handler.get(async (req, response) => {
1414
const query = `
1515
SELECT
1616
JSON_OBJECT(
17+
'properties', (
18+
SELECT JSON_OBJECT(
19+
'type', iv.type,
20+
'hid', iv.hid
21+
)
22+
FROM intervention iv
23+
WHERE iv.guid = ?
24+
),
1725
'plantedSpecies', (
1826
SELECT JSON_ARRAYAGG(
1927
JSON_OBJECT(
2028
'scientificName', COALESCE(ss.name, ps.other_species, iv.other_species),
21-
'treeCount', ps.tree_count
29+
'treeCount', COALESCE(ps.tree_count, iv.trees_planted, 0)
2230
)
2331
)
24-
FROM planted_species ps
25-
INNER JOIN intervention iv ON ps.intervention_id = iv.id
26-
LEFT JOIN scientific_species ss ON ps.scientific_species_id = ss.id
32+
FROM intervention iv
33+
LEFT JOIN planted_species ps ON iv.id = ps.intervention_id
34+
LEFT JOIN scientific_species ss ON COALESCE(iv.scientific_species_id, ps.scientific_species_id) = ss.id
2735
WHERE iv.guid = ?
2836
GROUP BY iv.id
2937
),
3038
'totalPlantedTrees', (
31-
SELECT SUM(ps.tree_count)
32-
FROM planted_species ps
33-
INNER JOIN intervention iv ON ps.intervention_id = iv.id
34-
LEFT JOIN scientific_species ss ON ps.scientific_species_id = ss.id
39+
SELECT SUM(COALESCE(ps.tree_count, iv.trees_planted, 0))
40+
FROM intervention iv
41+
LEFT JOIN planted_species ps ON iv.id = ps.intervention_id
42+
LEFT JOIN scientific_species ss ON COALESCE(iv.scientific_species_id, ps.scientific_species_id) = ss.id
3543
WHERE iv.guid = ?
3644
GROUP BY iv.id
3745
),
@@ -58,15 +66,15 @@ handler.get(async (req, response) => {
5866
)
5967
)
6068
FROM intervention iv
61-
LEFT JOIN intervention siv ON iv.id = siv.parent_id
69+
INNER JOIN intervention siv ON iv.id = siv.parent_id
6270
LEFT JOIN scientific_species ss ON iv.scientific_species_id = ss.id
6371
WHERE iv.guid = ?
6472
GROUP BY iv.parent_id
6573
),
6674
'totalSamplePlantLocations', (
6775
SELECT COUNT(*)
6876
FROM intervention iv
69-
LEFT JOIN intervention siv ON iv.id = siv.parent_id
77+
INNER JOIN intervention siv ON iv.id = siv.parent_id
7078
WHERE iv.guid = ?
7179
GROUP BY iv.parent_id
7280
)
@@ -79,6 +87,7 @@ handler.get(async (req, response) => {
7987
plantLocationId,
8088
plantLocationId,
8189
plantLocationId,
90+
plantLocationId,
8291
]);
8392

8493
const plantLocationDetails: PlantLocationDetails = JSON.parse(res[0].result);

public/static/locales/en/treemapperAnalytics.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,10 @@
6969
"sites": "Sites",
7070
"searchDateError": "Search date is not between the selected date range.",
7171
"noProjectsText": "No projects are available.",
72-
"addProjectsButton": "Add A Project"
72+
"addProjectsButton": "Add A Project",
73+
"plantLocationType": {
74+
"single-tree-registration": "Single Tree",
75+
"multi-tree-registration": "Multiple Trees"
76+
}
7377
}
7478
}

src/features/common/types/dataExplorer.d.ts

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -124,36 +124,45 @@ export interface PlantLocationDetailsQueryRes {
124124
result: string;
125125
}
126126

127+
export interface PlantLocationProperties {
128+
hid: string;
129+
type: 'single-tree-registration' | 'multi-tree-registration';
130+
}
131+
127132
export interface PlantLocationDetails {
133+
properties: PlantLocationProperties;
128134
plantedSpecies: PlantedSpecies[];
129135
totalPlantedTrees: number;
130-
samplePlantLocations: SamplePlantLocation[];
131-
totalSamplePlantLocations: number;
136+
samplePlantLocations: null | SamplePlantLocation[];
137+
totalSamplePlantLocations: null | number;
132138
}
133139

134140
// --- types for plantLocationDetailsApi ------
135141

136142
export interface PlantLocationDetailsApiResponse {
137143
res: {
144+
properties: PlantLocationProperties;
138145
plantedSpecies: {
139146
treeCount: number;
140147
scientificName: string;
141148
}[];
142149
totalPlantedTrees: number;
143-
samplePlantLocations: {
144-
tag: string;
145-
guid: string;
146-
species: string;
147-
geometry: {
148-
type: string;
149-
coordinates: number[];
150-
};
151-
measurements: {
152-
width: string;
153-
height: string;
154-
};
155-
}[];
156-
totalSamplePlantLocations: number;
150+
samplePlantLocations:
151+
| null
152+
| {
153+
tag: string;
154+
guid: string;
155+
species: string;
156+
geometry: {
157+
type: string;
158+
coordinates: number[];
159+
};
160+
measurements: {
161+
width: string;
162+
height: string;
163+
};
164+
}[];
165+
totalSamplePlantLocations: null | number;
157166
};
158167
}
159168

src/features/user/TreeMapper/Analytics/components/Map/components/PlantLocationDetails/index.module.scss

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,19 @@
7070
row-gap: 16px;
7171
width: 100%;
7272

73+
.header {
74+
width: 100%;
75+
padding: 0 5px;
76+
font-size: 12px;
77+
display: flex;
78+
justify-content: space-between;
79+
margin-bottom: -8px;
80+
81+
.hid {
82+
font-weight: 500;
83+
}
84+
}
85+
7386
.topContainer {
7487
display: flex;
7588
justify-content: space-between;

src/features/user/TreeMapper/Analytics/components/Map/components/PlantLocationDetails/index.tsx

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import styles from './index.module.scss';
33
import {
44
PlantLocationDetailsApiResponse,
55
PlantLocation,
6+
PlantLocationProperties,
67
} from '../../../../../../../common/types/dataExplorer';
78
import PlantLocationDetailsZeroState from '../PlantLocationDetailsZeroState';
89
import TreeMapperIcon from '../TreeMapperIcon';
@@ -134,16 +135,30 @@ const SampleTreesInfo = ({
134135
);
135136
};
136137

138+
const PlantLocationHeader = ({ type, hid }: PlantLocationProperties) => {
139+
const t = useTranslations('TreemapperAnalytics');
140+
const formattedHid = hid.substring(0, 3) + '-' + hid.substring(3);
141+
142+
return (
143+
<header className={styles.header}>
144+
<div>{t(`plantLocationType.${type}`)}</div>
145+
<div className={styles.hid}>#{formattedHid}</div>
146+
</header>
147+
);
148+
};
149+
137150
const PlantLocationDetails = ({
138151
plantLocationDetails,
139152
selectedLayer,
140153
loading,
141154
}: Props) => {
155+
const plantLocationType = plantLocationDetails?.properties.type;
156+
142157
const hasData =
143158
plantLocationDetails !== null &&
144159
Boolean(
145-
selectedLayer.treeCount ||
146-
selectedLayer?.density ||
160+
(plantLocationType === 'multi-tree-registration' &&
161+
(selectedLayer.treeCount || selectedLayer.density)) ||
147162
(plantLocationDetails?.plantedSpecies?.length || 0) > 0 ||
148163
(plantLocationDetails?.samplePlantLocations?.length || 0) > 0
149164
);
@@ -158,9 +173,18 @@ const PlantLocationDetails = ({
158173
</>
159174
) : hasData ? (
160175
<div className={styles.contentTop}>
161-
<PlantationUnitInfo selectedLayer={selectedLayer} />
176+
<PlantLocationHeader
177+
type={plantLocationDetails.properties.type}
178+
hid={plantLocationDetails.properties.hid}
179+
/>
180+
{plantLocationType === 'multi-tree-registration' && (
181+
<PlantationUnitInfo selectedLayer={selectedLayer} />
182+
)}
162183
<ListOfSpeciesPlanted plantLocationDetails={plantLocationDetails} />
163-
<SampleTreesInfo plantLocationDetails={plantLocationDetails} />
184+
{plantLocationType === 'multi-tree-registration' &&
185+
plantLocationDetails.totalSamplePlantLocations !== null && (
186+
<SampleTreesInfo plantLocationDetails={plantLocationDetails} />
187+
)}
164188
</div>
165189
) : (
166190
<>

0 commit comments

Comments
 (0)