Skip to content

Commit ca6f216

Browse files
committed
refactor: Nits on the code & move utils to key-utils
1 parent 2c15130 commit ca6f216

7 files changed

Lines changed: 44 additions & 28 deletions

File tree

src/generic/key-utils.test.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import {
44
getLibraryId,
55
isLibraryKey,
66
isLibraryV1Key,
7+
getContainerTypeFromId,
8+
ContainerType,
79
} from './key-utils';
810

911
describe('component utils', () => {
@@ -97,4 +99,16 @@ describe('component utils', () => {
9799
});
98100
}
99101
});
102+
103+
describe('getContainerTypeFromId', () => {
104+
for (const [input, expected] of [
105+
['lct:org:lib:unit:my-unit-9284e2', ContainerType.Unit],
106+
['lct:OpenCraftX:ALPHA:my-unit-a3223f', undefined],
107+
['', undefined],
108+
]) {
109+
it(`returns '${expected}' for container key '${input}'`, () => {
110+
expect(getContainerTypeFromId(input!)).toStrictEqual(expected);
111+
});
112+
}
113+
});
100114
});

src/generic/key-utils.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,26 @@ export const buildCollectionUsageKey = (learningContextKey: string, collectionId
4949
const orgLib = learningContextKey.replace('lib:', '');
5050
return `lib-collection:${orgLib}:${collectionId}`;
5151
};
52+
53+
export enum ContainerType {
54+
Unit = 'unit',
55+
}
56+
57+
/**
58+
* Given a container key like `ltc:org:lib:unit:id`
59+
* get the container type
60+
*/
61+
export function getContainerTypeFromId(containerId: string): ContainerType | undefined {
62+
const parts = containerId.split(':');
63+
if (parts.length < 2) {
64+
return undefined;
65+
}
66+
67+
const maybeType = parts[parts.length - 2];
68+
69+
if (Object.values(ContainerType).includes(maybeType as ContainerType)) {
70+
return maybeType as ContainerType;
71+
}
72+
73+
return undefined;
74+
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ import { blockTypes } from '../../editors/data/constants/app';
3131

3232
import messages from './messages';
3333
import type { BlockTypeMetadata } from '../data/api';
34-
import { getContainerTypeFromId, ContainerType } from '../utils';
34+
import { getContainerTypeFromId, ContainerType } from '../../generic/key-utils';
3535

3636
type ContentType = {
3737
name: string,
@@ -208,7 +208,7 @@ const AddContent = () => {
208208
unitId,
209209
} = useLibraryContext();
210210
const addComponentsToCollectionMutation = useAddComponentsToCollection(libraryId, collectionId);
211-
const addComponentsToContainerMutation = useAddComponentsToContainer(unitId);
211+
const addComponentsToContainerMutation = useAddComponentsToContainer(libraryId, unitId);
212212
const createBlockMutation = useCreateLibraryBlock();
213213
const pasteClipboardMutation = useLibraryPasteClipboard();
214214
const { showToast } = useContext(ToastContext);

src/library-authoring/create-unit/CreateUnitModal.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import messages from './messages';
1313
import { useCreateLibraryContainer } from '../data/apiHooks';
1414
import { ToastContext } from '../../generic/toast-context';
1515
import LoadingButton from '../../generic/loading-button';
16-
import { ContainerType } from '../utils';
16+
import { ContainerType } from '../../generic/key-utils';
1717

1818
const CreateUnitModal = () => {
1919
const intl = useIntl();

src/library-authoring/data/api.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { camelCaseObject, getConfig, snakeCaseObject } from '@edx/frontend-platform';
22
import { getAuthenticatedHttpClient } from '@edx/frontend-platform/auth';
33
import { VersionSpec } from '../LibraryBlock';
4-
import { type ContainerType } from '../utils';
4+
import { type ContainerType } from '../../generic/key-utils';
55

66
const getApiBaseUrl = () => getConfig().STUDIO_BASE_URL;
77

src/library-authoring/data/apiHooks.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -672,9 +672,9 @@ export const useContainerChildren = (libraryId?: string, containerId?: string) =
672672
);
673673

674674
/**
675-
* Use this mutatio to add components to a container
675+
* Use this mutation to add components to a container
676676
*/
677-
export const useAddComponentsToContainer = (containerId?: string) => {
677+
export const useAddComponentsToContainer = (libraryId?: string, containerId?: string) => {
678678
const queryClient = useQueryClient();
679679
return useMutation({
680680
mutationFn: async (componentIds: string[]) => {
@@ -684,7 +684,7 @@ export const useAddComponentsToContainer = (containerId?: string) => {
684684
return undefined;
685685
},
686686
onSettled: () => {
687-
queryClient.invalidateQueries({ queryKey: containerQueryKeys.children(containerId) });
687+
queryClient.invalidateQueries({ queryKey: libraryAuthoringQueryKeys.containerChildren(libraryId, containerId) });
688688
},
689689
});
690690
};

src/library-authoring/utils.ts

Lines changed: 0 additions & 21 deletions
This file was deleted.

0 commit comments

Comments
 (0)