Skip to content

Commit f9b2e52

Browse files
committed
feat: create component under collection
1 parent d510554 commit f9b2e52

File tree

5 files changed

+31
-15
lines changed

5 files changed

+31
-15
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,7 +8,7 @@ interface Props {
88
/** Course ID or Library ID */
99
learningContextId: string;
1010
/** Event handler sometimes called when user cancels out of the editor page */
11-
onClose?: () => void;
11+
onClose?: (prevPath?: string) => void;
1212
/**
1313
* Event handler called after when user saves their changes using an editor
1414
* and sometimes called when user cancels the editor, instead of onClose.
@@ -17,7 +17,7 @@ interface Props {
1717
* TODO: clean this up so there are separate onCancel and onSave callbacks,
1818
* and they are used consistently instead of this mess.
1919
*/
20-
returnFunction?: () => (newData: Record<string, any> | undefined) => void;
20+
returnFunction?: (prevPath?: string) => (newData: Record<string, any> | undefined) => void;
2121
}
2222

2323
const EditorContainer: React.FC<Props> = ({
@@ -26,6 +26,8 @@ const EditorContainer: React.FC<Props> = ({
2626
returnFunction,
2727
}) => {
2828
const { blockType, blockId } = useParams();
29+
const location = useLocation();
30+
2931
if (blockType === undefined || blockId === undefined) {
3032
// istanbul ignore next - This shouldn't be possible; it's just here to satisfy the type checker.
3133
return <div>Error: missing URL parameters</div>;
@@ -38,8 +40,8 @@ const EditorContainer: React.FC<Props> = ({
3840
blockId={blockId}
3941
studioEndpointUrl={getConfig().STUDIO_BASE_URL}
4042
lmsEndpointUrl={getConfig().LMS_BASE_URL}
41-
onClose={onClose}
42-
returnFunction={returnFunction}
43+
onClose={onClose ? () => onClose(location.state?.from) : undefined}
44+
returnFunction={returnFunction ? () => returnFunction(location.state?.from) : undefined}
4345
/>
4446
</div>
4547
);

src/library-authoring/LibraryLayout.tsx

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

2727
const navigate = useNavigate();
28-
const goBack = React.useCallback(() => {
29-
// Go back to the library
30-
navigate(`/library/${libraryId}`);
28+
const goBack = React.useCallback((prevPath?: string) => {
29+
if (prevPath) {
30+
// Redirects back to the previous route like collection page or library page
31+
navigate(prevPath);
32+
} else {
33+
// Go back to the library
34+
navigate(`/library/${libraryId}`);
35+
}
3136
}, []);
32-
const returnFunction = React.useCallback(() => {
37+
38+
const returnFunction = React.useCallback((prevPath?: string) => {
3339
// When changes are cancelled, either onClose (goBack) or this returnFunction will be called.
3440
// When changes are saved, this returnFunction is called.
35-
goBack();
41+
goBack(prevPath);
3642
return (args) => {
3743
if (args === undefined) {
3844
return; // Do nothing - the user cancelled the changes

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)