Skip to content

Commit

Permalink
Merge pull request #1510 from Vizzuality/chore/client/wdpa-jsonapi
Browse files Browse the repository at this point in the history
[N/A]: parses WDPA endpoint
  • Loading branch information
andresgnlez authored Sep 19, 2023
2 parents 33bc29f + c2f9a45 commit 397b6de
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 31 deletions.
4 changes: 2 additions & 2 deletions app/hooks/wdpa/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { Project } from 'types/api/project';
import { Scenario } from 'types/api/scenario';
import { WDPA } from 'types/api/wdpa';

import { API } from 'services/api';
import { API, JSONAPI } from 'services/api';
import SCENARIOS from 'services/scenarios';
import UPLOADS from 'services/uploads';

Expand Down Expand Up @@ -91,7 +91,7 @@ export function useProjectWDPAs<T = WDPA[]>(
return useQuery({
queryKey: ['wdpas', pid],
queryFn: async () =>
API.request<{ data: WDPA[] }>({
JSONAPI.request<{ data: WDPA[] }>({
method: 'GET',
url: `/projects/${pid}/protected-areas`,
headers: {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { ChangeEvent } from 'react';

import { WDPAAttributes } from 'types/api/wdpa';
import { WDPA } from 'types/api/wdpa';

export type DataItem = {
id: string;
attributes?: WDPAAttributes;
attributes?: Omit<WDPA, 'id'>;
name: string;
scenarios: number;
tag?: string;
Expand Down
15 changes: 4 additions & 11 deletions app/layout/project/sidebar/project/inventory-panel/wdpas/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,19 +47,12 @@ const InventoryPanelProtectedAreas = ({
search,
},
{
select: (data) =>
data?.map((wdpa) => ({
id: wdpa.id,
attributes: wdpa.attributes,
})),
keepPreviousData: true,
placeholderData: [],
}
);

const WDPAIds = allProjectWDPAsQuery.data
?.filter((wdpa) => wdpa.attributes.isCustom)
.map((wdpa) => wdpa.id);
const WDPAIds = allProjectWDPAsQuery.data?.filter((wdpa) => wdpa.isCustom).map((wdpa) => wdpa.id);

const handleSelectAll = useCallback(
(evt: ChangeEvent<HTMLInputElement>) => {
Expand Down Expand Up @@ -112,9 +105,9 @@ const InventoryPanelProtectedAreas = ({

const data: DataItem[] = allProjectWDPAsQuery.data?.map((wdpa) => ({
...wdpa,
name: wdpa.attributes.isCustom ? wdpa.attributes.fullName : wdpa.attributes.iucnCategory,
scenarios: wdpa.attributes.scenarioUsageCount,
isCustom: wdpa.attributes.isCustom,
name: wdpa.isCustom ? wdpa.fullName : wdpa.iucnCategory,
scenarios: wdpa.scenarioUsageCount,
isCustom: wdpa.isCustom,
isVisibleOnMap: visibleWDPAs?.includes(wdpa.id),
}));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@ const DeleteModal = ({
return allProjectWDPAsQuery.data?.filter(({ id }) => selectedWDPAIds.includes(id));
}, [allProjectWDPAsQuery.data, selectedWDPAIds]);

const WDPAsNames = selectedWDPAs.map(({ attributes }) => attributes.fullName);
const WDPAsNames = selectedWDPAs.map(({ fullName }) => fullName);

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

const handleBulkDelete = useCallback(() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ import Button from 'components/button';
import Field from 'components/forms/field';
import Label from 'components/forms/label';
import { composeValidators } from 'components/forms/validations';
import { WDPA, WDPAAttributes } from 'types/api/wdpa';
import { WDPA } from 'types/api/wdpa';

export type FormValues = { fullName: WDPAAttributes['fullName'] };
export type FormValues = { fullName: WDPA['fullName'] };

const EditModal = ({
wdpaId,
Expand Down Expand Up @@ -79,7 +79,7 @@ const EditModal = ({
return (
<FormRFF<FormValues>
initialValues={{
fullName: allProjectWDPAsQuery.data?.[0]?.attributes.fullName,
fullName: allProjectWDPAsQuery.data?.[0]?.fullName,
}}
ref={formRef}
onSubmit={onEditSubmit}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ import Loading from 'components/loading';
import Modal from 'components/modal';
import { PROTECTED_AREA_UPLOADER_SHAPEFILE_MAX_SIZE } from 'constants/file-uploader-size-limits';
import UploadWDPAsInfoButtonContent from 'layout/info/upload-wdpas';
import { WDPAAttributes } from 'types/api/wdpa';
import { WDPA } from 'types/api/wdpa';
import { cn } from 'utils/cn';
import { bytesToMegabytes } from 'utils/units';

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

export type FormValues = {
name: WDPAAttributes['fullName'];
name: WDPA['fullName'];
file: File;
};

Expand Down Expand Up @@ -164,7 +164,7 @@ export const WDPAUploadModal = ({

uploadWDPAsShapefileMutation.mutate({ data, id: `${pid}` }, mutationResponse);
},
[pid, addToast, onClose, uploadWDPAsShapefileMutation, successFile]
[pid, addToast, onClose, uploadWDPAsShapefileMutation, successFile, queryClient]
);

const { getRootProps, getInputProps, isDragActive, isDragAccept, isDragReject } = useDropzone({
Expand Down
10 changes: 3 additions & 7 deletions app/types/api/wdpa.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { Job } from './job';

export interface WDPAAttributes {
export interface WDPA {
id: string;
type: 'protected_areas';
countryId: string;
designation?: string;
fullName: string;
Expand All @@ -13,12 +15,6 @@ export interface WDPAAttributes {
isCustom?: boolean;
}

export interface WDPA {
id: string;
type: string;
attributes: WDPAAttributes;
}

export interface WDPACategory {
id: string;
kind: 'global' | 'project';
Expand Down

1 comment on commit 397b6de

@vercel
Copy link

@vercel vercel bot commented on 397b6de Sep 19, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

marxan – ./

marxan-vizzuality1.vercel.app
marxan-git-develop-vizzuality1.vercel.app
marxan23.vercel.app

Please sign in to comment.