Skip to content

Commit

Permalink
feat: more/correct v2 url handling. TNL-10742
Browse files Browse the repository at this point in the history
  • Loading branch information
Ken Clary committed Jul 20, 2023
1 parent 4a5eaaf commit f37935c
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 14 deletions.
4 changes: 2 additions & 2 deletions src/editors/containers/EditorContainer/hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export const handleSaveClicked = ({
returnFunction,
}) => {
// eslint-disable-next-line react-hooks/rules-of-hooks
const destination = useSelector(selectors.app.returnUrl);
const destination = returnFunction ? '' : useSelector(selectors.app.returnUrl);
// eslint-disable-next-line react-hooks/rules-of-hooks
const analytics = useSelector(selectors.app.analytics);

Expand Down Expand Up @@ -57,7 +57,7 @@ export const handleCancel = ({ onClose, returnFunction }) => {
return navigateCallback({
returnFunction,
// eslint-disable-next-line react-hooks/rules-of-hooks
destination: useSelector(selectors.app.returnUrl),
destination: returnFunction ? '' : useSelector(selectors.app.returnUrl),
analyticsEvent: analyticsEvt.editorCancelClick,
// eslint-disable-next-line react-hooks/rules-of-hooks
analytics: useSelector(selectors.app.analytics),
Expand Down
2 changes: 1 addition & 1 deletion src/editors/data/services/cms/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ export const apiMethods = {
response = {
data: content.olx,
category: blockType,
couseKey: learningContextId,
courseKey: learningContextId,
has_changes: true,
id: blockId,
metadata: { display_name: title, ...content.settings },
Expand Down
31 changes: 23 additions & 8 deletions src/editors/data/services/cms/urls.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,41 @@ export const unit = ({ studioEndpointUrl, unitUrl }) => (
);

export const returnUrl = ({ studioEndpointUrl, unitUrl, learningContextId }) => {
if (learningContextId && learningContextId.includes('library-v1')) {
if (learningContextId && learningContextId.startsWith('library-v1')) {
// when the learning context is a v1 library, return to the library page
return libraryV1({ studioEndpointUrl, learningContextId });
}
if (learningContextId && learningContextId.startsWith('lib')) {
// when it's a v2 library, there will be no return url (instead a closed popup)
throw new Error('Return url not available (or needed) for V2 libraries');
}
// when the learning context is a course, return to the unit page
return unitUrl ? unit({ studioEndpointUrl, unitUrl }) : '';
if (unitUrl) {
return unit({ studioEndpointUrl, unitUrl });
} else {
throw new Error('No unit url for return url');
}
};

export const block = ({ studioEndpointUrl, blockId }) => (
blockId.includes('block-v1')
blockId.startsWith('block-v1')
? `${studioEndpointUrl}/xblock/${blockId}`
: `${studioEndpointUrl}/api/xblock/v2/xblocks/${blockId}`
: `${studioEndpointUrl}/api/xblock/v2/xblocks/${blockId}/fields/`
);

export const blockAncestor = ({ studioEndpointUrl, blockId }) => (
`${block({ studioEndpointUrl, blockId })}?fields=ancestorInfo`
);
export const blockAncestor = ({ studioEndpointUrl, blockId }) => {
if blockId.startsWith('block-v1') {
return `${block({studioEndpointUrl, blockId})}?fields=ancestorInfo`;
} else {
// this url only need to get info to build the return url, which isn't used by V2 blocks
throw new Error('Block ancestor not available (and not needed) for V2 blocks');
}
};

export const blockStudioView = ({ studioEndpointUrl, blockId }) => (
`${block({ studioEndpointUrl, blockId })}/studio_view`
blockId.startsWith('block-v1')
? `${block({ studioEndpointUrl, blockId })}/studio_view`
: `${studioEndpointUrl}/api/xblock/v2/xblocks/${blockId}/view/studio_view/`
);

export const courseAssets = ({ studioEndpointUrl, learningContextId }) => (
Expand Down
19 changes: 16 additions & 3 deletions src/editors/data/services/cms/urls.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ describe('cms url methods', () => {
const learningContextId = 'lEarnIngCOntextId123';
const courseId = 'course-v1:courseId123';
const libraryV1Id = 'library-v1:libaryId123';
const libraryV2Id = 'lib:libaryId123';
const language = 'la';
const handout = '/aSSet@hANdoUt';
const videoId = '123-SOmeVidEOid-213';
Expand All @@ -41,10 +42,14 @@ describe('cms url methods', () => {
],
},
};
it('returns the library page when given the library', () => {
it('returns the library page when given the v1 library', () => {
expect(returnUrl({ studioEndpointUrl, unitUrl, learningContextId: libraryV1Id }))
.toEqual(`${studioEndpointUrl}/library/${libraryV1Id}`);
});
it('returns empty string when given the v2 library', () => {
expect(returnUrl({ studioEndpointUrl, unitUrl, learningContextId: libraryV2Id }))
.toEqual('');
});
it('returns url with studioEndpointUrl and unitUrl', () => {
expect(returnUrl({ studioEndpointUrl, unitUrl, learningContextId: courseId }))
.toEqual(`${studioEndpointUrl}/container/${unitUrl.data.ancestors[0].id}`);
Expand All @@ -69,20 +74,28 @@ describe('cms url methods', () => {
});
it('returns v2 url with studioEndpointUrl and v2BlockId', () => {
expect(block({ studioEndpointUrl, blockId: v2BlockId }))
.toEqual(`${studioEndpointUrl}/api/xblock/v2/xblocks/${v2BlockId}`);
.toEqual(`${studioEndpointUrl}/api/xblock/v2/xblocks/${v2BlockId}/fields/`);
});
});
describe('blockAncestor', () => {
it('returns url with studioEndpointUrl, blockId and ancestor query', () => {
expect(blockAncestor({ studioEndpointUrl, blockId }))
.toEqual(`${block({ studioEndpointUrl, blockId })}?fields=ancestorInfo`);
});
it('returns blank url with studioEndpointUrl, v2 blockId and ancestor query', () => {
expect(blockAncestor({ studioEndpointUrl, blockId: v2BlockId }))
.toEqual('');
});
});
describe('blockStudioView', () => {
it('returns url with studioEndpointUrl, blockId and studio_view query', () => {
it('returns v1 url with studioEndpointUrl, blockId and studio_view query', () => {
expect(blockStudioView({ studioEndpointUrl, blockId }))
.toEqual(`${block({ studioEndpointUrl, blockId })}/studio_view`);
});
it('returns v2 url with studioEndpointUrl, v2 blockId and studio_view query', () => {
expect(blockStudioView({ studioEndpointUrl, blockId: v2BlockId }))
.toEqual(`${studioEndpointUrl}/api/xblock/v2/xblocks/${v2BlockId}/view/studio_view/`);
});
});

describe('courseAssets', () => {
Expand Down

0 comments on commit f37935c

Please sign in to comment.