Skip to content

Commit

Permalink
refactor: modify create unique block key
Browse files Browse the repository at this point in the history
  • Loading branch information
dcoa committed Feb 3, 2025
1 parent cfae82d commit 2a96990
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 14 deletions.
4 changes: 4 additions & 0 deletions src/editors/data/constants/problem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -239,3 +239,7 @@ export const ignoredOlxAttributes = [
'@_url_name',
'@_x-is-pointer-node',
] as const;

// Useful for the block creation workflow.
export const problemTitles = new Set([...Object.values(ProblemTypes).map((problem) => problem.title),
...Object.values(AdvanceProblems).map((problem) => problem.title)]);
26 changes: 13 additions & 13 deletions src/editors/data/redux/thunkActions/requests.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { isLibraryKey } from '../../../../generic/key-utils';
import { createLibraryBlock } from '../../../../library-authoring/data/api';
import { acceptedImgKeys } from '../../../sharedComponents/ImageUploadModal/SelectImageModal/utils';
import { blockTypes } from '../../constants/app';
import { AdvanceProblems, ProblemTypes } from '../../constants/problem';
import { problemTitles } from '../../constants/problem';

// Similar to `import { actions, selectors } from '..';` but avoid circular imports:
const actions = { requests: requestsActions };
Expand Down Expand Up @@ -133,24 +133,24 @@ export const saveBlock = ({ content, ...rest }) => (dispatch, getState) => {
* @param {[func]} onFailure - onFailure method ((error) => { ... })
*/
export const createBlock = ({ ...rest }) => (dispatch, getState) => {
const blockType = selectors.app.blockType(getState());
const blockTitle = selectors.app.blockTitle(getState());
let definitionId = blockTitle
? blockTitle.toLowerCase().replaceAll(' ', '-')
: `${uuid4()}`;

if (blockType === blockTypes.problem) {
const problemTitles = new Set([...Object.values(ProblemTypes).map((problem) => problem.title),
...Object.values(AdvanceProblems).map((problem) => problem.title)]);

definitionId = problemTitles.has(blockTitle) ? `${uuid4()}` : definitionId;
const blockType = selectors.app.blockType(getState());
// Remove any special character, a slug should be created with unicode letters, numbers, underscores or hyphens.
const cleanTitle = blockTitle?.toLowerCase().replace(/[^a-zA-Z0-9_\s-]/g, '').trim();
let definitionId;
// Validates if the title has been assigned by the user, if not a UUID is returned as the key.
if (!cleanTitle || (blockType === blockTypes.problem && problemTitles.has(blockTitle))) {
definitionId = `${uuid4()}`;
} else {
// add a hash to prevent duplication.
const hash = uuid4().split('-')[4];
definitionId = `${cleanTitle.replaceAll(/\s+/g, '-')}-${hash}`;
}

dispatch(module.networkRequest({
requestKey: RequestKeys.createBlock,
promise: createLibraryBlock({
libraryId: selectors.app.learningContextId(getState()),
blockType: selectors.app.blockType(getState()),
blockType,
definitionId,
}),
...rest,
Expand Down
2 changes: 1 addition & 1 deletion src/editors/data/redux/thunkActions/requests.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jest.mock('../app/selectors', () => ({
blockId: (state) => ({ blockId: state }),
blockType: (state) => ({ blockType: state }),
learningContextId: (state) => ({ learningContextId: state }),
blockTitle: (state) => state.data,
blockTitle: (state) => state.some,
}));

jest.mock('../../services/cms/api', () => ({
Expand Down

0 comments on commit 2a96990

Please sign in to comment.