Skip to content

Commit 397b6de

Browse files
authored
Merge pull request #1510 from Vizzuality/chore/client/wdpa-jsonapi
[N/A]: parses WDPA endpoint
2 parents 33bc29f + c2f9a45 commit 397b6de

File tree

7 files changed

+20
-31
lines changed

7 files changed

+20
-31
lines changed

app/hooks/wdpa/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { Project } from 'types/api/project';
77
import { Scenario } from 'types/api/scenario';
88
import { WDPA } from 'types/api/wdpa';
99

10-
import { API } from 'services/api';
10+
import { API, JSONAPI } from 'services/api';
1111
import SCENARIOS from 'services/scenarios';
1212
import UPLOADS from 'services/uploads';
1313

@@ -91,7 +91,7 @@ export function useProjectWDPAs<T = WDPA[]>(
9191
return useQuery({
9292
queryKey: ['wdpas', pid],
9393
queryFn: async () =>
94-
API.request<{ data: WDPA[] }>({
94+
JSONAPI.request<{ data: WDPA[] }>({
9595
method: 'GET',
9696
url: `/projects/${pid}/protected-areas`,
9797
headers: {

app/layout/project/sidebar/project/inventory-panel/components/inventory-table/types.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import { ChangeEvent } from 'react';
22

3-
import { WDPAAttributes } from 'types/api/wdpa';
3+
import { WDPA } from 'types/api/wdpa';
44

55
export type DataItem = {
66
id: string;
7-
attributes?: WDPAAttributes;
7+
attributes?: Omit<WDPA, 'id'>;
88
name: string;
99
scenarios: number;
1010
tag?: string;

app/layout/project/sidebar/project/inventory-panel/wdpas/index.tsx

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -47,19 +47,12 @@ const InventoryPanelProtectedAreas = ({
4747
search,
4848
},
4949
{
50-
select: (data) =>
51-
data?.map((wdpa) => ({
52-
id: wdpa.id,
53-
attributes: wdpa.attributes,
54-
})),
5550
keepPreviousData: true,
5651
placeholderData: [],
5752
}
5853
);
5954

60-
const WDPAIds = allProjectWDPAsQuery.data
61-
?.filter((wdpa) => wdpa.attributes.isCustom)
62-
.map((wdpa) => wdpa.id);
55+
const WDPAIds = allProjectWDPAsQuery.data?.filter((wdpa) => wdpa.isCustom).map((wdpa) => wdpa.id);
6356

6457
const handleSelectAll = useCallback(
6558
(evt: ChangeEvent<HTMLInputElement>) => {
@@ -112,9 +105,9 @@ const InventoryPanelProtectedAreas = ({
112105

113106
const data: DataItem[] = allProjectWDPAsQuery.data?.map((wdpa) => ({
114107
...wdpa,
115-
name: wdpa.attributes.isCustom ? wdpa.attributes.fullName : wdpa.attributes.iucnCategory,
116-
scenarios: wdpa.attributes.scenarioUsageCount,
117-
isCustom: wdpa.attributes.isCustom,
108+
name: wdpa.isCustom ? wdpa.fullName : wdpa.iucnCategory,
109+
scenarios: wdpa.scenarioUsageCount,
110+
isCustom: wdpa.isCustom,
118111
isVisibleOnMap: visibleWDPAs?.includes(wdpa.id),
119112
}));
120113

app/layout/project/sidebar/project/inventory-panel/wdpas/modals/delete/index.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,11 @@ const DeleteModal = ({
3636
return allProjectWDPAsQuery.data?.filter(({ id }) => selectedWDPAIds.includes(id));
3737
}, [allProjectWDPAsQuery.data, selectedWDPAIds]);
3838

39-
const WDPAsNames = selectedWDPAs.map(({ attributes }) => attributes.fullName);
39+
const WDPAsNames = selectedWDPAs.map(({ fullName }) => fullName);
4040

4141
// ? the user will be able to delete the protected areas only if they are not being used by any scenario.
42-
const haveScenarioAssociated = selectedWDPAs.some(({ attributes }) =>
43-
Boolean(attributes.scenarioUsageCount)
42+
const haveScenarioAssociated = selectedWDPAs.some(({ scenarioUsageCount }) =>
43+
Boolean(scenarioUsageCount)
4444
);
4545

4646
const handleBulkDelete = useCallback(() => {

app/layout/project/sidebar/project/inventory-panel/wdpas/modals/edit/index.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ import Button from 'components/button';
1212
import Field from 'components/forms/field';
1313
import Label from 'components/forms/label';
1414
import { composeValidators } from 'components/forms/validations';
15-
import { WDPA, WDPAAttributes } from 'types/api/wdpa';
15+
import { WDPA } from 'types/api/wdpa';
1616

17-
export type FormValues = { fullName: WDPAAttributes['fullName'] };
17+
export type FormValues = { fullName: WDPA['fullName'] };
1818

1919
const EditModal = ({
2020
wdpaId,
@@ -79,7 +79,7 @@ const EditModal = ({
7979
return (
8080
<FormRFF<FormValues>
8181
initialValues={{
82-
fullName: allProjectWDPAsQuery.data?.[0]?.attributes.fullName,
82+
fullName: allProjectWDPAsQuery.data?.[0]?.fullName,
8383
}}
8484
ref={formRef}
8585
onSubmit={onEditSubmit}

app/layout/project/sidebar/project/inventory-panel/wdpas/modals/upload/index.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,14 @@ import Loading from 'components/loading';
2323
import Modal from 'components/modal';
2424
import { PROTECTED_AREA_UPLOADER_SHAPEFILE_MAX_SIZE } from 'constants/file-uploader-size-limits';
2525
import UploadWDPAsInfoButtonContent from 'layout/info/upload-wdpas';
26-
import { WDPAAttributes } from 'types/api/wdpa';
26+
import { WDPA } from 'types/api/wdpa';
2727
import { cn } from 'utils/cn';
2828
import { bytesToMegabytes } from 'utils/units';
2929

3030
import CLOSE_SVG from 'svgs/ui/close.svg?sprite';
3131

3232
export type FormValues = {
33-
name: WDPAAttributes['fullName'];
33+
name: WDPA['fullName'];
3434
file: File;
3535
};
3636

@@ -164,7 +164,7 @@ export const WDPAUploadModal = ({
164164

165165
uploadWDPAsShapefileMutation.mutate({ data, id: `${pid}` }, mutationResponse);
166166
},
167-
[pid, addToast, onClose, uploadWDPAsShapefileMutation, successFile]
167+
[pid, addToast, onClose, uploadWDPAsShapefileMutation, successFile, queryClient]
168168
);
169169

170170
const { getRootProps, getInputProps, isDragActive, isDragAccept, isDragReject } = useDropzone({

app/types/api/wdpa.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { Job } from './job';
22

3-
export interface WDPAAttributes {
3+
export interface WDPA {
4+
id: string;
5+
type: 'protected_areas';
46
countryId: string;
57
designation?: string;
68
fullName: string;
@@ -13,12 +15,6 @@ export interface WDPAAttributes {
1315
isCustom?: boolean;
1416
}
1517

16-
export interface WDPA {
17-
id: string;
18-
type: string;
19-
attributes: WDPAAttributes;
20-
}
21-
2218
export interface WDPACategory {
2319
id: string;
2420
kind: 'global' | 'project';

0 commit comments

Comments
 (0)