Skip to content

Commit b826fd1

Browse files
committed
refactor: pass btn click handler to empty state component
1 parent eba5baf commit b826fd1

File tree

6 files changed

+21
-21
lines changed

6 files changed

+21
-21
lines changed

src/library-authoring/EmptyStates.tsx

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import React, { useContext, useCallback } from 'react';
21
import { useParams } from 'react-router';
32
import { FormattedMessage } from '@edx/frontend-platform/i18n';
43
import type { MessageDescriptor } from 'react-intl';
@@ -8,36 +7,26 @@ import {
87
import { Add } from '@openedx/paragon/icons';
98
import { ClearFiltersButton } from '../search-manager';
109
import messages from './messages';
11-
import { LibraryContext } from './common/context';
1210
import { useContentLibrary } from './data/apiHooks';
1311

1412
export const NoComponents = ({
1513
infoText = messages.noComponents,
1614
addBtnText = messages.addComponent,
17-
searchType = "component",
15+
handleBtnClick,
1816
}: {
1917
infoText?: MessageDescriptor;
2018
addBtnText?: MessageDescriptor;
21-
searchType?: "collection" | "component";
19+
handleBtnClick: () => void;
2220
}) => {
23-
const { openAddContentSidebar, openCreateCollectionModal } = useContext(LibraryContext);
2421
const { libraryId } = useParams();
2522
const { data: libraryData } = useContentLibrary(libraryId);
2623
const canEditLibrary = libraryData?.canEditLibrary ?? false;
2724

28-
const handleOnClickButton = useCallback(() => {
29-
if (searchType === 'collection') {
30-
openCreateCollectionModal();
31-
} else {
32-
openAddContentSidebar();
33-
}
34-
}, [searchType]);
35-
3625
return (
3726
<Stack direction="horizontal" gap={3} className="mt-6 justify-content-center">
3827
<FormattedMessage {...infoText} />
3928
{canEditLibrary && (
40-
<Button iconBefore={Add} onClick={handleOnClickButton}>
29+
<Button iconBefore={Add} onClick={handleBtnClick}>
4130
<FormattedMessage {...addBtnText} />
4231
</Button>
4332
)}

src/library-authoring/LibraryHome.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React from 'react';
1+
import React, { useContext } from 'react';
22
import { Stack } from '@openedx/paragon';
33
import { useIntl } from '@edx/frontend-platform/i18n';
44

@@ -9,6 +9,7 @@ import { LibraryComponents } from './components';
99
import LibrarySection from './components/LibrarySection';
1010
import LibraryRecentlyModified from './LibraryRecentlyModified';
1111
import messages from './messages';
12+
import { LibraryContext } from './common/context';
1213

1314
type LibraryHomeProps = {
1415
libraryId: string,
@@ -23,10 +24,11 @@ const LibraryHome = ({ libraryId, tabList, handleTabChange } : LibraryHomeProps)
2324
totalCollectionHits: collectionCount,
2425
isFiltered,
2526
} = useSearchContext();
27+
const { openAddContentSidebar } = useContext(LibraryContext);
2628

2729
const renderEmptyState = () => {
2830
if (componentCount === 0 && collectionCount === 0) {
29-
return isFiltered ? <NoSearchResults /> : <NoComponents />;
31+
return isFiltered ? <NoSearchResults /> : <NoComponents handleBtnClick={openAddContentSidebar} />;
3032
}
3133
return null;
3234
};

src/library-authoring/collections/LibraryCollectionComponents.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,22 @@
1+
import { useContext } from 'react';
12
import { Stack } from '@openedx/paragon';
23
import { NoComponents, NoSearchResults } from '../EmptyStates';
34
import { useSearchContext } from '../../search-manager';
45
import { LibraryComponents } from '../components';
56
import messages from './messages';
7+
import { LibraryContext } from '../common/context';
68

79
const LibraryCollectionComponents = ({ libraryId }: { libraryId: string }) => {
810
const { totalHits: componentCount, isFiltered } = useSearchContext();
11+
const { openAddContentSidebar } = useContext(LibraryContext);
912

1013
if (componentCount === 0) {
1114
return isFiltered ?
1215
<NoSearchResults infoText={messages.noSearchResultsInCollection} />
1316
: <NoComponents
1417
infoText={messages.noComponentsInCollection}
1518
addBtnText={messages.addComponentsInCollection}
19+
handleBtnClick={openAddContentSidebar}
1620
/>;
1721
}
1822

src/library-authoring/collections/LibraryCollections.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { useContext } from 'react';
12
import { CardGrid } from '@openedx/paragon';
23

34
import { useLoadOnScroll } from '../../hooks';
@@ -6,6 +7,7 @@ import { NoComponents, NoSearchResults } from '../EmptyStates';
67
import CollectionCard from '../components/CollectionCard';
78
import { LIBRARY_SECTION_PREVIEW_LIMIT } from '../components/LibrarySection';
89
import messages from '../messages';
10+
import { LibraryContext } from '../common/context';
911

1012
type LibraryCollectionsProps = {
1113
variant: 'full' | 'preview',
@@ -28,6 +30,8 @@ const LibraryCollections = ({ variant }: LibraryCollectionsProps) => {
2830
isFiltered,
2931
} = useSearchContext();
3032

33+
const { openCreateCollectionModal } = useContext(LibraryContext);
34+
3135
const collectionList = variant === 'preview' ? collectionHits.slice(0, LIBRARY_SECTION_PREVIEW_LIMIT) : collectionHits;
3236

3337
useLoadOnScroll(
@@ -43,7 +47,7 @@ const LibraryCollections = ({ variant }: LibraryCollectionsProps) => {
4347
: <NoComponents
4448
infoText={messages.noCollections}
4549
addBtnText={messages.addCollection}
46-
searchType="collection"
50+
handleBtnClick={openCreateCollectionModal}
4751
/>;
4852
}
4953

src/library-authoring/common/context.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ export const LibraryProvider = (props: { children?: React.ReactNode }) => {
4040
const [sidebarBodyComponent, setSidebarBodyComponent] = React.useState<SidebarBodyComponentId | null>(null);
4141
const [currentComponentUsageKey, setCurrentComponentUsageKey] = React.useState<string>();
4242
const [isCreateCollectionModalOpen, openCreateCollectionModal, closeCreateCollectionModal] = useToggle(false);
43-
const [currentComponentKey, setCurrentComponentKey] = React.useState<string>();
4443

4544
const closeLibrarySidebar = React.useCallback(() => {
4645
setSidebarBodyComponent(null);
@@ -62,7 +61,7 @@ export const LibraryProvider = (props: { children?: React.ReactNode }) => {
6261
[],
6362
);
6463
const openCollectionInfoSidebar = React.useCallback(() => {
65-
setCurrentComponentKey(undefined);
64+
setCurrentComponentUsageKey(undefined);
6665
setSidebarBodyComponent(SidebarBodyComponentId.CollectionInfo);
6766
}, []);
6867

src/library-authoring/components/LibraryComponents.tsx

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

44
import { useLoadOnScroll } from '../../hooks';
@@ -7,6 +7,7 @@ import { NoComponents, NoSearchResults } from '../EmptyStates';
77
import { useLibraryBlockTypes } from '../data/apiHooks';
88
import ComponentCard from './ComponentCard';
99
import { LIBRARY_SECTION_PREVIEW_LIMIT } from './LibrarySection';
10+
import { LibraryContext } from '../common/context';
1011

1112
type LibraryComponentsProps = {
1213
libraryId: string,
@@ -29,6 +30,7 @@ const LibraryComponents = ({ libraryId, variant }: LibraryComponentsProps) => {
2930
fetchNextPage,
3031
isFiltered,
3132
} = useSearchContext();
33+
const { openAddContentSidebar } = useContext(LibraryContext);
3234

3335
const componentList = variant === 'preview' ? hits.slice(0, LIBRARY_SECTION_PREVIEW_LIMIT) : hits;
3436

@@ -52,7 +54,7 @@ const LibraryComponents = ({ libraryId, variant }: LibraryComponentsProps) => {
5254
);
5355

5456
if (componentCount === 0) {
55-
return isFiltered ? <NoSearchResults /> : <NoComponents />;
57+
return isFiltered ? <NoSearchResults /> : <NoComponents handleBtnClick={openAddContentSidebar} />;
5658
}
5759

5860
return (

0 commit comments

Comments
 (0)