Skip to content

Commit

Permalink
feat: implement fist versioning of image upload for creation workflow
Browse files Browse the repository at this point in the history
  • Loading branch information
dcoa committed Jan 16, 2025
1 parent 30c2f11 commit e677ed0
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 5 deletions.
1 change: 1 addition & 0 deletions src/editors/data/redux/requests/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const initialState = {
[RequestKeys.fetchUnit]: { status: RequestStates.inactive },
[RequestKeys.fetchBlock]: { status: RequestStates.inactive },
[RequestKeys.fetchStudioView]: { status: RequestStates.inactive },
[RequestKeys.createBlock]: { status: RequestStates.inactive },
[RequestKeys.saveBlock]: { status: RequestStates.inactive },
[RequestKeys.uploadAsset]: { status: RequestStates.inactive },
[RequestKeys.allowThumbnailUpload]: { status: RequestStates.inactive },
Expand Down
42 changes: 38 additions & 4 deletions src/editors/data/redux/thunkActions/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import * as requests from './requests';
// should be re-thought and cleaned up to avoid this pattern.
// eslint-disable-next-line import/no-self-import
import * as module from './app';
import { actions as appActions } from '../app';
import { actions as appActions, selectors } from '../app';
import { actions as requestsActions } from '../requests';
import { RequestKeys } from '../../constants/requests';

Expand Down Expand Up @@ -90,6 +90,7 @@ export const initialize = (data) => (dispatch) => {
const editorType = data.blockType;
dispatch({ type: 'resetEditor' });
dispatch(actions.app.initialize(data));

if (data.blockId === '' && editorType) {
dispatch(actions.app.initializeEditor());
return;
Expand All @@ -109,7 +110,6 @@ export const initialize = (data) => (dispatch) => {
dispatch(module.fetchCourseDetails());
break;
case 'html':
if (isLibraryKey(data.learningContextId)) { dispatch(actions.app.resetImages()); }
dispatch(module.fetchImages({ pageNumber: 0 }));
break;
default:
Expand All @@ -131,14 +131,30 @@ export const saveBlock = (content, returnToUnit) => (dispatch) => {
}));
};

const imagesNewBlock = [];

/**
* @param {func} onSuccess
*/
export const createBlock = (content, returnToUnit) => (dispatch) => {
dispatch(requests.createBlock({
onSuccess: (response) => {
dispatch(actions.app.setBlockId(response.id));
dispatch(saveBlock(content, returnToUnit));
imagesNewBlock.forEach(({ file, url }) => {

Check failure on line 143 in src/editors/data/redux/thunkActions/app.js

View workflow job for this annotation

GitHub Actions / tests

'url' is defined but never used
dispatch(requests.uploadAsset({
asset: file,
onSuccess: async (resp) => {
const imagePath = `/${resp.data.asset.portableUrl}`;
const reader = new FileReader();
reader.addEventListener('load', () => {
const imageBS64 = reader.result.toString();
content = content.replace(imageBS64, imagePath);

Check failure on line 151 in src/editors/data/redux/thunkActions/app.js

View workflow job for this annotation

GitHub Actions / tests

Assignment to function parameter 'content'
dispatch(saveBlock(content, returnToUnit));
});
reader.readAsDataURL(file);
},
}));
});
},
onFailure: (error) => dispatch(actions.requests.failRequest({
requestKey: RequestKeys.createBlock,
Expand All @@ -147,7 +163,25 @@ export const createBlock = (content, returnToUnit) => (dispatch) => {
}));
};

export const uploadAsset = ({ file, setSelection }) => (dispatch) => {
export const uploadAsset = ({ file, setSelection }) => (dispatch, getState) => {
if (selectors.isCreateBlock(getState())) {
console.debug(file);

Check warning on line 168 in src/editors/data/redux/thunkActions/app.js

View workflow job for this annotation

GitHub Actions / tests

Unexpected console statement
const tempFileURL = URL.createObjectURL(file);
const tempImage = {
displayName: file.name,
url: tempFileURL,
externalUrl: tempFileURL,
portableUrl: tempFileURL,
thumbnail: tempFileURL,
id: file.name,
locked: false,
};

imagesNewBlock.push({ url: tempFileURL, file });
setSelection(tempImage);
return;
}

dispatch(requests.uploadAsset({
asset: file,
onSuccess: (response) => setSelection(camelizeKeys(response.data.asset)),
Expand Down
2 changes: 1 addition & 1 deletion src/editors/sharedComponents/ImageUploadModal/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ ImageUploadModal.propTypes = {
images: PropTypes.shape({}).isRequired,
lmsEndpointUrl: PropTypes.string.isRequired,
editorType: PropTypes.string,
isLibrary: PropTypes.string,
isLibrary: PropTypes.bool,
};

export const ImageUploadModalInternal = ImageUploadModal; // For testing only
Expand Down

0 comments on commit e677ed0

Please sign in to comment.