Skip to content

Commit

Permalink
fix: use sidebarAction instead of managing separate state
Browse files Browse the repository at this point in the history
when editing component collections.
  • Loading branch information
pomegranited committed Dec 30, 2024
1 parent 71c8cc0 commit 4fb6064
Showing 1 changed file with 15 additions and 30 deletions.
45 changes: 15 additions & 30 deletions src/library-authoring/component-info/ManageCollections.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useContext, useEffect, useState } from 'react';
import { useContext, useState } from 'react';
import { FormattedMessage, useIntl } from '@edx/frontend-platform/i18n';
import {
Button, Icon, Scrollable, SelectableBox, Stack, StatefulButton, useCheckboxSetValues,
Expand Down Expand Up @@ -191,38 +191,23 @@ const ComponentCollections = ({ collections, onManageClick }: {
};

const ManageCollections = ({ usageKey, collections }: ManageCollectionsProps) => {
const { sidebarAction, resetSidebarAction } = useSidebarContext();
const jumpToCollections = sidebarAction === SidebarActions.JumpToAddCollections;
const [editing, setEditing] = useState(jumpToCollections);
const { sidebarAction, resetSidebarAction, setSidebarAction } = useSidebarContext();
const collectionNames = collections.map((collection) => collection.title);

useEffect(() => {
if (jumpToCollections) {
setEditing(true);
}
}, [jumpToCollections]);

useEffect(() => {
// This is required to redo actions.
if (!editing) {
resetSidebarAction();
}
}, [editing]);

if (editing) {
return (
<AddToCollectionsDrawer
usageKey={usageKey}
collections={collections}
onClose={() => setEditing(false)}
/>
);
}
return (
<ComponentCollections
collections={collectionNames}
onManageClick={() => setEditing(true)}
/>
sidebarAction === SidebarActions.JumpToAddCollections
? (
<AddToCollectionsDrawer
usageKey={usageKey}
collections={collections}
onClose={() => resetSidebarAction()}
/>
) : (
<ComponentCollections
collections={collectionNames}
onManageClick={() => setSidebarAction(SidebarActions.JumpToAddCollections)}
/>
)
);
};

Expand Down

0 comments on commit 4fb6064

Please sign in to comment.