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

Austenem/CAT-754 Refactor my lists #3668

Closed
wants to merge 16 commits into from
Prev Previous commit
Next Next commit
fix typing
  • Loading branch information
austenem committed Jan 13, 2025
commit 72d9e545f42a4cca7fc52681e040041fcf5cb1c5
21 changes: 14 additions & 7 deletions context/app/static/js/components/savedLists/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,23 +34,30 @@ interface SavedListsDataProps {

function useFetchSavedEntitiesAndLists({ urls, groupsToken }: SavedListsDataProps) {
const { data, isLoading } = useSWR([urls.keys, groupsToken], ([url, token]: string[]) =>
fetcher<Record<string, SavedEntitiesList>>({
fetcher<{ key: string; value: SavedEntitiesList }[]>({
url,
requestInit: { headers: { Authorization: `Bearer ${token}` } },
}),
);

let savedEntities: Record<string, SavedEntity> = {};
const savedEntities: Record<string, SavedEntity> = {};
let savedLists: Record<string, SavedEntitiesList> = {};

if (data && !isLoading) {
if (data.savedEntities) {
savedEntities = { savedEntities: data.savedEntities };
const savedEntitiesObject = data.find((item) => item.key === 'savedEntities');

if (savedEntitiesObject) {
savedEntities.savedEntities = savedEntitiesObject.value;
}

savedLists = Object.entries(data)
.filter(([key]) => key !== 'savedEntities')
.reduce((acc, [_, value]) => ({ ...acc, ...value }), {});
savedLists = data
.filter((item) => item.key !== 'savedEntities')
.reduce((acc, item) => {
return {
...acc,
[item.key]: item.value,
};
}, {});
}

return { savedLists, savedEntities, isLoading };
Expand Down