Skip to content

Commit

Permalink
fixes many crashes due to disabling pagination
Browse files Browse the repository at this point in the history
  • Loading branch information
agnlez authored and hotzevzl committed Nov 10, 2023
1 parent a2fe7b8 commit 26ef6a2
Show file tree
Hide file tree
Showing 13 changed files with 302 additions and 696 deletions.
155 changes: 58 additions & 97 deletions app/hooks/features/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
import { useMemo } from 'react';

import {
useQuery,
useInfiniteQuery,
useMutation,
useQueryClient,
QueryObserverOptions,
} from 'react-query';
import { useQuery, useMutation, useQueryClient, QueryObserverOptions } from 'react-query';

import { AxiosRequestConfig } from 'axios';
import Fuse from 'fuse.js';
Expand Down Expand Up @@ -48,119 +42,86 @@ export function useAllPaginatedFeatures(projectId, options: UseFeaturesOptionsPr
};
}, {});

const fetchFeatures = ({ pageParam = 1 }) =>
const fetchFeatures = () =>
PROJECTS.request({
method: 'GET',
url: `/${projectId}/features`,
headers: {
Authorization: `Bearer ${session.accessToken}`,
},
params: {
'page[number]': pageParam,
// omitFields: 'properties',
...parsedFilters,
...(search && {
q: search,
}),
...(sort && {
sort,
}),
disablePagination: true,
},
});
}).then((response) => response.data);

const query = useInfiniteQuery(
['all-paginated-features', projectId, JSON.stringify(options)],
fetchFeatures,
{
keepPreviousData: true,
getNextPageParam: (lastPage) => {
return useQuery(['all-paginated-features', projectId, JSON.stringify(options)], fetchFeatures, {
keepPreviousData: true,
select: ({ data }) => {
const parsedData = data.map((d): AllItemProps => {
const {
data: { meta },
} = lastPage;
const { page, totalPages } = meta;

const nextPage = page + 1 > totalPages ? null : page + 1;
return nextPage;
},
}
);

const { data } = query;
const { pages } = data || {};
id,
alias,
featureClassName,
description,
properties = {},
splitSelected,
splitFeaturesSelected,
} = d;

return useMemo(() => {
const parsedData = Array.isArray(pages)
? flatten(
pages.map((p) => {
const {
data: { data: pageData },
} = p;

return pageData.map((d): AllItemProps => {
const {
id,
alias,
featureClassName,
description,
properties = {},
splitSelected,
splitFeaturesSelected,
} = d;

let splitOptions = [];
let splitFeaturesOptions = [];

/**
* @todo Checking whether `properties` is defined here is just a
* workaround to avoid an error when processing `bioregional`
* features, which would prevent progressing through the stop of
* configuring features for a scenario until this code is reviewed.
* Without much knowledge of the flow for feature data, I see that
* short-circuiting the `map()` below and therefore setting
* `splitOptions = []` still results in properties being shown in the
* dropdowns used for splitting features, but since `properties` is
* always undefined (from what I can see), we may need to adapt the
* API payload or how we process it here.
*/
splitOptions = properties
? Object.keys(properties).map((k) => {
return {
key: k,
label: k,
values: properties[k].map((v) => ({ id: v, name: v })),
};
})
: [];

splitFeaturesOptions = splitSelected
? splitOptions
.find((s) => s.key === splitSelected)
.values.map((v) => ({ label: v.name, value: v.id }))
: [];
let splitOptions = [];
let splitFeaturesOptions = [];

/**
* @todo Checking whether `properties` is defined here is just a
* workaround to avoid an error when processing `bioregional`
* features, which would prevent progressing through the stop of
* configuring features for a scenario until this code is reviewed.
* Without much knowledge of the flow for feature data, I see that
* short-circuiting the `map()` below and therefore setting
* `splitOptions = []` still results in properties being shown in the
* dropdowns used for splitting features, but since `properties` is
* always undefined (from what I can see), we may need to adapt the
* API payload or how we process it here.
*/
splitOptions = properties
? Object.keys(properties).map((k) => {
return {
id,
name: alias || featureClassName,
description,

splitSelected,
splitOptions,
splitFeaturesSelected,
splitFeaturesOptions,
key: k,
label: k,
values: properties[k].map((v) => ({ id: v, name: v })),
};
});
})
)
: [];
})
: [];

// We want to return custom features first, but preserve the overall sorting
const sortedByCustomFeature = flatten(partition(parsedData, (feature) => feature.isCustom));
splitFeaturesOptions = splitSelected
? splitOptions
.find((s) => s.key === splitSelected)
.values.map((v) => ({ label: v.name, value: v.id }))
: [];

return {
...query,
data: sortedByCustomFeature,
};
}, [query, pages]);
return {
id,
name: alias || featureClassName,
description,

splitSelected,
splitOptions,
splitFeaturesSelected,
splitFeaturesOptions,
};
});

// We want to return custom features first, but preserve the overall sorting
return flatten(partition(parsedData, (feature) => feature.isCustom));
},
});
}

export function useAllFeatures<T = { data: Feature[] }>(
Expand Down
Loading

1 comment on commit 26ef6a2

@vercel
Copy link

@vercel vercel bot commented on 26ef6a2 Nov 10, 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 – ./

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

Please sign in to comment.