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

Allow 'fake' services to be favorited #2567

Merged
merged 3 commits into from
Jul 13, 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
2 changes: 2 additions & 0 deletions src/hooks/useAllServices.ts
Original file line number Diff line number Diff line change
Expand Up @@ -270,8 +270,10 @@ const useAllServices = () => {
servicesLinks,
error,
ready,
availableSections,
filterValue,
setFilterValue,
findNavItems,
};
};

Expand Down
31 changes: 27 additions & 4 deletions src/hooks/useFavoritedServices.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,50 @@ import { ServiceTileProps } from '../components/FavoriteServices/ServiceTile';
import useAllServices from './useAllServices';
import { useEffect, useMemo, useState } from 'react';
import fetchNavigationFiles, { extractNavItemGroups } from '../utils/fetchNavigationFiles';
import { Navigation } from '../@types/types';
import { NavItem, Navigation } from '../@types/types';
import { findNavLeafPath } from '../utils/common';
import useFavoritePagesWrapper from './useFavoritePagesWrapper';
import { isAllServicesLink } from '../components/AllServices/allServicesLinks';

const useFavoritedServices = () => {
const { favoritePages } = useFavoritePagesWrapper();
const { allLinks } = useAllServices();
const { allLinks, availableSections } = useAllServices();
const [bundles, setBundles] = useState<Navigation[]>([]);

const fakeBundle: NavItem[] = useMemo(() => {
// escape early if we have no services
if (availableSections.length === 0) {
return [];
}

// map services links to nav links
return availableSections.reduce<NavItem[]>((acc, curr) => {
const fakeNavItems: NavItem[] = curr.links.filter(isAllServicesLink);
// no need to recreate the reduce array
acc.push(...fakeNavItems);
return acc;
}, []);
}, [availableSections]);

useEffect(() => {
fetchNavigationFiles()
.then((data) => setBundles(data as Navigation[]))
.catch((error) => {
console.error('Unable to fetch favorite services', error);
});
}, []);

const linksWithFragments = useMemo(() => {
// push items with unique hrefs from our fake bundle for leaf creation
fakeBundle.forEach((item) => {
if (!allLinks.some((link) => link.href === item.href)) {
allLinks.push(item);
}
});
return allLinks.map((link) => {
let linkLeaf: ReturnType<typeof findNavLeafPath> | undefined;
// use every to exit early if match was found
bundles.every((bundle) => {
[...bundles, fakeBundle || []].every((bundle) => {
const leaf = findNavLeafPath(extractNavItemGroups(bundle), (item) => item?.href === link.href);
if (leaf.activeItem) {
linkLeaf = leaf;
Expand All @@ -35,7 +58,7 @@ const useFavoritedServices = () => {
linkLeaf,
};
});
}, [allLinks, bundles]);
}, [allLinks, bundles, fakeBundle]);

// extract human friendly data from the all services data set
const favoriteServices = favoritePages.reduce<ServiceTileProps[]>((acc, curr) => {
Expand Down
Loading