Skip to content

Commit 0b42f0d

Browse files
committed
feat: collections tab with basic cards
1 parent 002143f commit 0b42f0d

File tree

4 files changed

+55
-11
lines changed

4 files changed

+55
-11
lines changed

src/library-authoring/LibraryAuthoringPage.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ const LibraryAuthoringPage = () => {
203203
/>
204204
<Route
205205
path={TabList.collections}
206-
element={<LibraryCollections />}
206+
element={<LibraryCollections libraryId={libraryId} variant="full" />}
207207
/>
208208
<Route
209209
path="*"

src/library-authoring/LibraryCollections.tsx

Lines changed: 50 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
import React, { useEffect, useMemo } from 'react';
22
import { FormattedMessage } from '@edx/frontend-platform/i18n';
3+
import { CardGrid } from '@openedx/paragon';
34

45
import messages from './messages';
56
import { useSearchContext } from '../search-manager';
7+
import { NoComponents, NoSearchResults } from './EmptyStates';
8+
import ComponentCard from './components/ComponentCard';
9+
import { LIBRARY_SECTION_PREVIEW_LIMIT } from './components/LibrarySection';
610

711
type LibraryCollectionsProps = {
812
libraryId: string,
@@ -27,10 +31,52 @@ const LibraryCollections = ({ libraryId, variant }: LibraryCollectionsProps) =>
2731
setExtraFilter,
2832
} = useSearchContext();
2933

30-
// __AUTO_GENERATED_PRINT_VAR_START__
31-
console.log("LibraryCollections collectionHits: ", collectionHits); // __AUTO_GENERATED_PRINT_VAR_END__
32-
return <div className="d-flex my-6 justify-content-center">
33-
</div>
34+
const collectionList = variant === 'preview' ? collectionHits.slice(0, LIBRARY_SECTION_PREVIEW_LIMIT) : collectionHits;
35+
36+
useEffect(() => {
37+
if (variant === 'full') {
38+
const onscroll = () => {
39+
// Verify the position of the scroll to implementa a infinite scroll.
40+
// Used `loadLimit` to fetch next page before reach the end of the screen.
41+
const loadLimit = 300;
42+
const scrolledTo = window.scrollY + window.innerHeight;
43+
const scrollDiff = document.body.scrollHeight - scrolledTo;
44+
const isNearToBottom = scrollDiff <= loadLimit;
45+
if (isNearToBottom && hasNextPage && !isFetchingNextPage) {
46+
fetchNextPage();
47+
}
48+
};
49+
window.addEventListener('scroll', onscroll);
50+
return () => {
51+
window.removeEventListener('scroll', onscroll);
52+
};
53+
}
54+
return () => {};
55+
}, [hasNextPage, isFetchingNextPage, fetchNextPage]);
56+
57+
if (totalCollectionHits === 0) {
58+
return isFiltered ? <NoSearchResults /> : <NoComponents />;
59+
}
60+
61+
return (
62+
<CardGrid
63+
columnSizes={{
64+
sm: 12,
65+
md: 6,
66+
lg: 4,
67+
xl: 3,
68+
}}
69+
hasEqualColumnHeights
70+
>
71+
{ collectionList.map((contentHit) => (
72+
<ComponentCard
73+
key={contentHit.id}
74+
contentHit={contentHit}
75+
blockTypeDisplayName={'Collection'}
76+
/>
77+
)) }
78+
</CardGrid>
79+
);
3480
};
3581

3682
export default LibraryCollections;

src/library-authoring/LibraryHome.tsx

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,12 @@ const LibraryHome = ({ libraryId, tabList, handleTabChange } : LibraryHomeProps)
2020
const intl = useIntl();
2121
const {
2222
totalHits: componentCount,
23+
totalCollectionHits: collectionCount,
2324
isFiltered,
2425
} = useSearchContext();
2526

26-
const collectionCount = 0;
27-
2827
const renderEmptyState = () => {
29-
if (componentCount === 0) {
28+
if (componentCount === 0 && collectionCount === 0) {
3029
return isFiltered ? <NoSearchResults /> : <NoComponents />;
3130
}
3231
return null;
@@ -42,9 +41,9 @@ const LibraryHome = ({ libraryId, tabList, handleTabChange } : LibraryHomeProps)
4241
<LibrarySection
4342
title={intl.formatMessage(messages.collectionsTitle, { collectionCount })}
4443
contentCount={collectionCount}
45-
// TODO: add viewAllAction here once collections implemented
44+
viewAllAction={() => handleTabChange(tabList.collections)}
4645
>
47-
<LibraryCollections />
46+
<LibraryCollections libraryId={libraryId} variant="preview" />
4847
</LibrarySection>
4948
<LibrarySection
5049
title={intl.formatMessage(messages.componentsTitle, { componentCount })}

src/search-manager/data/apiHooks.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,6 @@ export const useContentSearchResults = ({
125125
hasNextPage: query.hasNextPage,
126126
// The last page has the most accurate count of total hits
127127
totalHits: pages?.[pages.length - 1]?.totalHits ?? 0,
128-
// The last page has the most accurate count of total hits
129128
totalCollectionHits: pages?.[pages.length - 1]?.totalCollectionHits ?? 0,
130129
};
131130
};

0 commit comments

Comments
 (0)