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

[MRXN23-498]: fixes many crashes due to disabling pagination #1573

Merged
merged 1 commit into from
Nov 10, 2023
Merged
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
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
Loading