Skip to content

Commit 630bdf3

Browse files
committed
feat: create component under collection
1 parent fd01130 commit 630bdf3

File tree

5 files changed

+28
-13
lines changed

5 files changed

+28
-13
lines changed

src/editors/EditorContainer.tsx

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React from 'react';
2-
import { useParams } from 'react-router-dom';
2+
import { useLocation, useParams } from 'react-router-dom';
33
import { getConfig } from '@edx/frontend-platform';
44

55
import EditorPage from './EditorPage';
@@ -8,9 +8,9 @@ interface Props {
88
/** Course ID or Library ID */
99
learningContextId: string;
1010
/** Event handler for when user cancels out of the editor page */
11-
onClose?: () => void;
11+
onClose?: (prevPath?: string) => void;
1212
/** Event handler called after when user saves their changes using an editor */
13-
afterSave?: () => (newData: Record<string, any>) => void;
13+
afterSave?: (prevPath?: string) => (newData: Record<string, any>) => void;
1414
}
1515

1616
const EditorContainer: React.FC<Props> = ({
@@ -19,6 +19,8 @@ const EditorContainer: React.FC<Props> = ({
1919
afterSave,
2020
}) => {
2121
const { blockType, blockId } = useParams();
22+
const location = useLocation();
23+
2224
if (blockType === undefined || blockId === undefined) {
2325
// istanbul ignore next - This shouldn't be possible; it's just here to satisfy the type checker.
2426
return <div>Error: missing URL parameters</div>;
@@ -41,8 +43,8 @@ const EditorContainer: React.FC<Props> = ({
4143
blockId={blockId}
4244
studioEndpointUrl={getConfig().STUDIO_BASE_URL}
4345
lmsEndpointUrl={getConfig().LMS_BASE_URL}
44-
onClose={onClose}
45-
returnFunction={afterSave}
46+
onClose={onClose ? () => onClose(location.state?.from) : undefined}
47+
returnFunction={afterSave ? () => afterSave(location.state?.from) : undefined}
4648
/>
4749
</div>
4850
);

src/library-authoring/LibraryLayout.tsx

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,14 @@ const LibraryLayout = () => {
2525
}
2626

2727
const navigate = useNavigate();
28-
const goBack = React.useCallback(() => {
29-
// Go back to the library
30-
navigate(`/library/${libraryId}`);
28+
29+
const goBack = React.useCallback((prevPath?: string) => {
30+
if (prevPath) {
31+
// Redirects back to the previous route like collection page or library page
32+
navigate(prevPath);
33+
} else {
34+
navigate(`/library/${libraryId}`);
35+
}
3136
// The following function is called only if changes are saved:
3237
return ({ id: usageKey }) => {
3338
// invalidate any queries that involve this XBlock:

src/library-authoring/add-content/AddContentContainer.tsx

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import {
1616
ContentPaste,
1717
} from '@openedx/paragon/icons';
1818
import { v4 as uuid4 } from 'uuid';
19-
import { useNavigate, useParams } from 'react-router-dom';
19+
import { useLocation, useNavigate, useParams } from 'react-router-dom';
2020

2121
import { ToastContext } from '../../generic/toast-context';
2222
import { useCopyToClipboard } from '../../generic/clipboard';
@@ -62,7 +62,9 @@ const AddContentButton = ({ contentType, onCreateContent } : AddContentButtonPro
6262
const AddContentContainer = () => {
6363
const intl = useIntl();
6464
const navigate = useNavigate();
65-
const { libraryId } = useParams();
65+
const location = useLocation();
66+
const currentPath = location.pathname;
67+
const { libraryId, collectionId } = useParams();
6668
const createBlockMutation = useCreateLibraryBlock();
6769
const pasteClipboardMutation = useLibraryPasteClipboard();
6870
const { showToast } = useContext(ToastContext);
@@ -147,10 +149,13 @@ const AddContentContainer = () => {
147149
libraryId,
148150
blockType,
149151
definitionId: `${uuid4()}`,
152+
collectionId,
150153
}).then((data) => {
151154
const editUrl = getEditUrl(data.id);
152155
if (editUrl) {
153-
navigate(editUrl);
156+
// Pass currentPath in state so that we can come back to
157+
// current page on save or cancel
158+
navigate(editUrl, { state: { from: currentPath } });
154159
} else {
155160
// We can't start editing this right away so just show a toast message:
156161
showToast(intl.formatMessage(messages.successCreateMessage));
@@ -168,7 +173,7 @@ const AddContentContainer = () => {
168173

169174
return (
170175
<Stack direction="vertical">
171-
<AddContentButton contentType={collectionButtonData} onCreateContent={onCreateContent} />
176+
{!collectionId && <AddContentButton contentType={collectionButtonData} onCreateContent={onCreateContent} />}
172177
<hr className="w-100 bg-gray-500" />
173178
{contentTypes.map((contentType) => (
174179
<AddContentButton

src/library-authoring/data/api.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ export interface CreateBlockDataRequest {
134134
libraryId: string;
135135
blockType: string;
136136
definitionId: string;
137+
collectionId?: string;
137138
}
138139

139140
export interface LibraryBlockMetadata {
@@ -197,13 +198,15 @@ export async function createLibraryBlock({
197198
libraryId,
198199
blockType,
199200
definitionId,
201+
collectionId,
200202
}: CreateBlockDataRequest): Promise<LibraryBlockMetadata> {
201203
const client = getAuthenticatedHttpClient();
202204
const { data } = await client.post(
203205
getCreateLibraryBlockUrl(libraryId),
204206
{
205207
block_type: blockType,
206208
definition_id: definitionId,
209+
collection_key: collectionId,
207210
},
208211
);
209212
return camelCaseObject(data);

src/library-authoring/data/apiHooks.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ export const useCreateLibraryBlock = () => {
128128
mutationFn: createLibraryBlock,
129129
onSettled: (_data, _error, variables) => {
130130
queryClient.invalidateQueries({ queryKey: libraryAuthoringQueryKeys.contentLibrary(variables.libraryId) });
131-
queryClient.invalidateQueries({ queryKey: ['content_search'] });
131+
queryClient.invalidateQueries({ predicate: (query) => libraryQueryPredicate(query, variables.libraryId) });
132132
},
133133
});
134134
};

0 commit comments

Comments
 (0)