Skip to content

Commit 8f8a0b0

Browse files
committed
refactor: address review comments
1 parent 398c28d commit 8f8a0b0

File tree

8 files changed

+78
-77
lines changed

8 files changed

+78
-77
lines changed

src/hooks.js

Lines changed: 0 additions & 37 deletions
This file was deleted.

src/hooks.ts

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import { useEffect, useState } from 'react';
2+
import { history } from '@edx/frontend-platform';
3+
4+
export const useScrollToHashElement = ({ isLoading }: { isLoading: boolean }) => {
5+
const [elementWithHash, setElementWithHash] = useState<string | null>(null);
6+
7+
useEffect(() => {
8+
const currentHash = window.location.hash.substring(1);
9+
10+
if (currentHash) {
11+
const element = document.getElementById(currentHash);
12+
if (element) {
13+
element.scrollIntoView();
14+
history.replace({ hash: '' });
15+
}
16+
setElementWithHash(currentHash);
17+
}
18+
}, [isLoading]);
19+
20+
return { elementWithHash };
21+
};
22+
23+
export const useEscapeClick = ({ onEscape, dependency }: { onEscape: () => void, dependency: any }) => {
24+
useEffect(() => {
25+
const handleEscapeClick = (event: KeyboardEvent) => {
26+
if (event.key === 'Escape') {
27+
onEscape();
28+
}
29+
};
30+
31+
window.addEventListener('keydown', handleEscapeClick);
32+
33+
return () => {
34+
window.removeEventListener('keydown', handleEscapeClick);
35+
};
36+
}, [dependency]);
37+
};
38+
39+
/**
40+
* Hook which loads next page of items on scroll
41+
*/
42+
export const useLoadOnScroll = (
43+
hasNextPage: boolean | undefined,
44+
isFetchingNextPage: boolean,
45+
fetchNextPage: () => void,
46+
enabled: boolean,
47+
) => {
48+
useEffect(() => {
49+
if (enabled) {
50+
const onscroll = () => {
51+
// Verify the position of the scroll to implementa a infinite scroll.
52+
// Used `loadLimit` to fetch next page before reach the end of the screen.
53+
const loadLimit = 300;
54+
const scrolledTo = window.scrollY + window.innerHeight;
55+
const scrollDiff = document.body.scrollHeight - scrolledTo;
56+
const isNearToBottom = scrollDiff <= loadLimit;
57+
if (isNearToBottom && hasNextPage && !isFetchingNextPage) {
58+
fetchNextPage();
59+
}
60+
};
61+
window.addEventListener('scroll', onscroll);
62+
return () => {
63+
window.removeEventListener('scroll', onscroll);
64+
};
65+
}
66+
return () => { };
67+
}, [hasNextPage, isFetchingNextPage, fetchNextPage]);
68+
};

src/library-authoring/LibraryCollections.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { CardGrid } from '@openedx/paragon';
22

3-
import { useLoadOnScroll, useSearchContext } from '../search-manager';
3+
import { useLoadOnScroll } from '../hooks';
4+
import { useSearchContext } from '../search-manager';
45
import { NoComponents, NoSearchResults } from './EmptyStates';
56
import CollectionCard from './components/CollectionCard';
67
import { LIBRARY_SECTION_PREVIEW_LIMIT } from './components/LibrarySection';

src/library-authoring/components/BaseComponentCard.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import {
55
Icon,
66
Stack,
77
} from '@openedx/paragon';
8-
import { ReactNodeLike } from 'prop-types';
98

109
import { getItemIcon, getComponentStyleColor } from '../../generic/block-type-utils';
1110
import TagCount from '../../generic/tag-count';
@@ -16,7 +15,7 @@ type BaseComponentCardProps = {
1615
displayName: string,
1716
description: string,
1817
tags: ContentHitTags,
19-
actions: ReactNodeLike,
18+
actions: React.ReactNode,
2019
blockTypeDisplayName: string,
2120
openInfoSidebar: () => void
2221
};

src/library-authoring/components/CollectionCard.test.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { AppProvider } from '@edx/frontend-platform/react';
33
import { initializeMockApp } from '@edx/frontend-platform';
44
import { getAuthenticatedHttpClient } from '@edx/frontend-platform/auth';
55
import { IntlProvider } from '@edx/frontend-platform/i18n';
6-
import { render } from '@testing-library/react';
6+
import { render, screen } from '@testing-library/react';
77
import MockAdapter from 'axios-mock-adapter';
88
import type { Store } from 'redux';
99

@@ -66,9 +66,9 @@ describe('<CollectionCard />', () => {
6666
});
6767

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

71-
expect(getByText('Collection Display Formated Name')).toBeInTheDocument();
72-
expect(getByText('Collection description')).toBeInTheDocument();
71+
expect(screen.getByText('Collection Display Formated Name')).toBeInTheDocument();
72+
expect(screen.getByText('Collection description')).toBeInTheDocument();
7373
});
7474
});

src/library-authoring/components/LibraryComponents.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import React, { useMemo } from 'react';
22
import { CardGrid } from '@openedx/paragon';
33

4-
import { useLoadOnScroll, useSearchContext } from '../../search-manager';
4+
import { useLoadOnScroll } from '../../hooks';
5+
import { useSearchContext } from '../../search-manager';
56
import { NoComponents, NoSearchResults } from '../EmptyStates';
67
import { useLibraryBlockTypes } from '../data/apiHooks';
78
import ComponentCard from './ComponentCard';

src/search-manager/SearchManager.ts

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -185,34 +185,3 @@ export const useSearchContext = () => {
185185
}
186186
return ctx;
187187
};
188-
189-
/**
190-
* Hook which loads next page of items on scroll
191-
*/
192-
export const useLoadOnScroll = (
193-
hasNextPage: boolean | undefined,
194-
isFetchingNextPage: boolean,
195-
fetchNextPage: () => void,
196-
enabled: boolean,
197-
) => {
198-
React.useEffect(() => {
199-
if (enabled) {
200-
const onscroll = () => {
201-
// Verify the position of the scroll to implementa a infinite scroll.
202-
// Used `loadLimit` to fetch next page before reach the end of the screen.
203-
const loadLimit = 300;
204-
const scrolledTo = window.scrollY + window.innerHeight;
205-
const scrollDiff = document.body.scrollHeight - scrolledTo;
206-
const isNearToBottom = scrollDiff <= loadLimit;
207-
if (isNearToBottom && hasNextPage && !isFetchingNextPage) {
208-
fetchNextPage();
209-
}
210-
};
211-
window.addEventListener('scroll', onscroll);
212-
return () => {
213-
window.removeEventListener('scroll', onscroll);
214-
};
215-
}
216-
return () => {};
217-
}, [hasNextPage, isFetchingNextPage, fetchNextPage]);
218-
};

src/search-manager/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export { SearchContextProvider, useLoadOnScroll, useSearchContext } from './SearchManager';
1+
export { SearchContextProvider, useSearchContext } from './SearchManager';
22
export { default as ClearFiltersButton } from './ClearFiltersButton';
33
export { default as FilterByBlockType } from './FilterByBlockType';
44
export { default as FilterByTags } from './FilterByTags';

0 commit comments

Comments
 (0)