Skip to content

Commit

Permalink
refactor: address review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
navinkarkera committed Sep 9, 2024
1 parent 398c28d commit 8f8a0b0
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 77 deletions.
37 changes: 0 additions & 37 deletions src/hooks.js

This file was deleted.

68 changes: 68 additions & 0 deletions src/hooks.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import { useEffect, useState } from 'react';
import { history } from '@edx/frontend-platform';

export const useScrollToHashElement = ({ isLoading }: { isLoading: boolean }) => {
const [elementWithHash, setElementWithHash] = useState<string | null>(null);

useEffect(() => {
const currentHash = window.location.hash.substring(1);

if (currentHash) {
const element = document.getElementById(currentHash);

Check warning on line 11 in src/hooks.ts

View check run for this annotation

Codecov / codecov/patch

src/hooks.ts#L11

Added line #L11 was not covered by tests
if (element) {
element.scrollIntoView();
history.replace({ hash: '' });

Check warning on line 14 in src/hooks.ts

View check run for this annotation

Codecov / codecov/patch

src/hooks.ts#L13-L14

Added lines #L13 - L14 were not covered by tests
}
setElementWithHash(currentHash);

Check warning on line 16 in src/hooks.ts

View check run for this annotation

Codecov / codecov/patch

src/hooks.ts#L16

Added line #L16 was not covered by tests
}
}, [isLoading]);

return { elementWithHash };
};

export const useEscapeClick = ({ onEscape, dependency }: { onEscape: () => void, dependency: any }) => {
useEffect(() => {
const handleEscapeClick = (event: KeyboardEvent) => {
if (event.key === 'Escape') {
onEscape();

Check warning on line 27 in src/hooks.ts

View check run for this annotation

Codecov / codecov/patch

src/hooks.ts#L27

Added line #L27 was not covered by tests
}
};

window.addEventListener('keydown', handleEscapeClick);

return () => {
window.removeEventListener('keydown', handleEscapeClick);
};
}, [dependency]);
};

/**
* Hook which loads next page of items on scroll
*/
export const useLoadOnScroll = (
hasNextPage: boolean | undefined,
isFetchingNextPage: boolean,
fetchNextPage: () => void,
enabled: boolean,
) => {
useEffect(() => {
if (enabled) {
const onscroll = () => {
// Verify the position of the scroll to implementa a infinite scroll.
// Used `loadLimit` to fetch next page before reach the end of the screen.
const loadLimit = 300;
const scrolledTo = window.scrollY + window.innerHeight;
const scrollDiff = document.body.scrollHeight - scrolledTo;
const isNearToBottom = scrollDiff <= loadLimit;
if (isNearToBottom && hasNextPage && !isFetchingNextPage) {
fetchNextPage();
}
};
window.addEventListener('scroll', onscroll);
return () => {
window.removeEventListener('scroll', onscroll);
};
}
return () => { };
}, [hasNextPage, isFetchingNextPage, fetchNextPage]);
};
3 changes: 2 additions & 1 deletion src/library-authoring/LibraryCollections.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { CardGrid } from '@openedx/paragon';

import { useLoadOnScroll, useSearchContext } from '../search-manager';
import { useLoadOnScroll } from '../hooks';
import { useSearchContext } from '../search-manager';
import { NoComponents, NoSearchResults } from './EmptyStates';
import CollectionCard from './components/CollectionCard';
import { LIBRARY_SECTION_PREVIEW_LIMIT } from './components/LibrarySection';
Expand Down
3 changes: 1 addition & 2 deletions src/library-authoring/components/BaseComponentCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import {
Icon,
Stack,
} from '@openedx/paragon';
import { ReactNodeLike } from 'prop-types';

import { getItemIcon, getComponentStyleColor } from '../../generic/block-type-utils';
import TagCount from '../../generic/tag-count';
Expand All @@ -16,7 +15,7 @@ type BaseComponentCardProps = {
displayName: string,
description: string,
tags: ContentHitTags,
actions: ReactNodeLike,
actions: React.ReactNode,
blockTypeDisplayName: string,
openInfoSidebar: () => void
};
Expand Down
8 changes: 4 additions & 4 deletions src/library-authoring/components/CollectionCard.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { AppProvider } from '@edx/frontend-platform/react';
import { initializeMockApp } from '@edx/frontend-platform';
import { getAuthenticatedHttpClient } from '@edx/frontend-platform/auth';
import { IntlProvider } from '@edx/frontend-platform/i18n';
import { render } from '@testing-library/react';
import { render, screen } from '@testing-library/react';
import MockAdapter from 'axios-mock-adapter';
import type { Store } from 'redux';

Expand Down Expand Up @@ -66,9 +66,9 @@ describe('<CollectionCard />', () => {
});

it('should render the card with title and description', () => {
const { getByText } = render(<RootWrapper />);
render(<RootWrapper />);

expect(getByText('Collection Display Formated Name')).toBeInTheDocument();
expect(getByText('Collection description')).toBeInTheDocument();
expect(screen.getByText('Collection Display Formated Name')).toBeInTheDocument();
expect(screen.getByText('Collection description')).toBeInTheDocument();
});
});
3 changes: 2 additions & 1 deletion src/library-authoring/components/LibraryComponents.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import React, { useMemo } from 'react';
import { CardGrid } from '@openedx/paragon';

import { useLoadOnScroll, useSearchContext } from '../../search-manager';
import { useLoadOnScroll } from '../../hooks';
import { useSearchContext } from '../../search-manager';
import { NoComponents, NoSearchResults } from '../EmptyStates';
import { useLibraryBlockTypes } from '../data/apiHooks';
import ComponentCard from './ComponentCard';
Expand Down
31 changes: 0 additions & 31 deletions src/search-manager/SearchManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,34 +185,3 @@ export const useSearchContext = () => {
}
return ctx;
};

/**
* Hook which loads next page of items on scroll
*/
export const useLoadOnScroll = (
hasNextPage: boolean | undefined,
isFetchingNextPage: boolean,
fetchNextPage: () => void,
enabled: boolean,
) => {
React.useEffect(() => {
if (enabled) {
const onscroll = () => {
// Verify the position of the scroll to implementa a infinite scroll.
// Used `loadLimit` to fetch next page before reach the end of the screen.
const loadLimit = 300;
const scrolledTo = window.scrollY + window.innerHeight;
const scrollDiff = document.body.scrollHeight - scrolledTo;
const isNearToBottom = scrollDiff <= loadLimit;
if (isNearToBottom && hasNextPage && !isFetchingNextPage) {
fetchNextPage();
}
};
window.addEventListener('scroll', onscroll);
return () => {
window.removeEventListener('scroll', onscroll);
};
}
return () => {};
}, [hasNextPage, isFetchingNextPage, fetchNextPage]);
};
2 changes: 1 addition & 1 deletion src/search-manager/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export { SearchContextProvider, useLoadOnScroll, useSearchContext } from './SearchManager';
export { SearchContextProvider, useSearchContext } from './SearchManager';
export { default as ClearFiltersButton } from './ClearFiltersButton';
export { default as FilterByBlockType } from './FilterByBlockType';
export { default as FilterByTags } from './FilterByTags';
Expand Down

0 comments on commit 8f8a0b0

Please sign in to comment.