Skip to content

Commit 747f342

Browse files
committed
refactor: pass btn click handler to empty state component
1 parent c97e5b9 commit 747f342

File tree

6 files changed

+23
-22
lines changed

6 files changed

+23
-22
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: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1+
import { useContext } from 'react';
2+
13
import { useLoadOnScroll } from '../../hooks';
24
import { useSearchContext } from '../../search-manager';
35
import { NoComponents, NoSearchResults } from '../EmptyStates';
46
import CollectionCard from '../components/CollectionCard';
57
import { LIBRARY_SECTION_PREVIEW_LIMIT } from '../components/LibrarySection';
68
import messages from '../messages';
9+
import { LibraryContext } from '../common/context';
710

811
type LibraryCollectionsProps = {
912
variant: 'full' | 'preview',
@@ -26,6 +29,8 @@ const LibraryCollections = ({ variant }: LibraryCollectionsProps) => {
2629
isFiltered,
2730
} = useSearchContext();
2831

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

3136
useLoadOnScroll(
@@ -35,13 +40,13 @@ const LibraryCollections = ({ variant }: LibraryCollectionsProps) => {
3540
variant === 'full',
3641
);
3742

38-
if (totalCollectionHits === 0) {
43+
if (totalCollectionHits === 1) {
3944
return isFiltered ?
4045
<NoSearchResults infoText={messages.noSearchResultsCollections} />
4146
: <NoComponents
4247
infoText={messages.noCollections}
4348
addBtnText={messages.addCollection}
44-
searchType="collection"
49+
handleBtnClick={openCreateCollectionModal}
4550
/>;
4651
}
4752

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,11 +1,12 @@
1-
import React, { useMemo } from 'react';
1+
import React, { useContext, useMemo } from 'react';
22

33
import { useLoadOnScroll } from '../../hooks';
44
import { useSearchContext } from '../../search-manager';
55
import { NoComponents, NoSearchResults } from '../EmptyStates';
66
import { useLibraryBlockTypes } from '../data/apiHooks';
77
import ComponentCard from './ComponentCard';
88
import { LIBRARY_SECTION_PREVIEW_LIMIT } from './LibrarySection';
9+
import { LibraryContext } from '../common/context';
910

1011
type LibraryComponentsProps = {
1112
libraryId: string,
@@ -28,6 +29,7 @@ const LibraryComponents = ({ libraryId, variant }: LibraryComponentsProps) => {
2829
fetchNextPage,
2930
isFiltered,
3031
} = useSearchContext();
32+
const { openAddContentSidebar } = useContext(LibraryContext);
3133

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

@@ -51,7 +53,7 @@ const LibraryComponents = ({ libraryId, variant }: LibraryComponentsProps) => {
5153
);
5254

5355
if (componentCount === 0) {
54-
return isFiltered ? <NoSearchResults /> : <NoComponents />;
56+
return isFiltered ? <NoSearchResults /> : <NoComponents handleBtnClick={openAddContentSidebar} />;
5557
}
5658

5759
return (

0 commit comments

Comments
 (0)