Skip to content

Commit

Permalink
fix: upload and delete files issue (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
johnvente authored Oct 17, 2023
1 parent 24a70da commit b13b810
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 12 deletions.
2 changes: 1 addition & 1 deletion imagesgallery/static/html/bundle.js

Large diffs are not rendered by default.

7 changes: 4 additions & 3 deletions react-app/components/ErrorMessage/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,18 @@ import PropTypes from 'prop-types';

import './styles.css';

const ErrorMessage = ({ message }) => {
const ErrorMessage = ({ message, className = '' }) => {
return (
<div className="error-message">
<div className={`error-message ${className}`}>
<FontAwesomeIcon icon={faExclamationTriangle} className="error-icon" />
<p className="error-text">{message}</p>
</div>
);
};

ErrorMessage.propTypes = {
message: PropTypes.string.isRequired
message: PropTypes.string.isRequired,
className: PropTypes.string
};

export default ErrorMessage;
16 changes: 8 additions & 8 deletions react-app/components/XBlockEditView/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ import ErrorMessage from '@components/ErrorMessage';
import xBlockContext from '@constants/xBlockContext';
import apiConfig from '@config/api';
import useXBlockActionButtons from '@hooks/useXBlockActionButtons';
import useAddErrorMessageToModal from '@hooks/useAddErrorMessageToModal';

import './styles.css';

const itemsPerPage = 10;
const itemsPerPage = 100;

const XBlockEditView = () => {
const [imagesToSave, setImagesToSave] = useState([]);
Expand Down Expand Up @@ -56,19 +57,15 @@ const XBlockEditView = () => {
buttonSaveRef.classList.add('disabled-button');

try {
const requestPromises = [];

if (data.length > 0) {
requestPromises.push(uploadFiles(formData));
await uploadFiles(formData);
}

if (imagesKeysToDelete.length > 0) {
const imagesKeysToDeleteRemoveDuplicates = [...new Set(imagesKeysToDelete)];
requestPromises.push(deleteFiles(imagesKeysToDeleteRemoveDuplicates));
await deleteFiles(imagesKeysToDeleteRemoveDuplicates);
}

await Promise.all(requestPromises);

setImagesToSave([]);
setImagesToDelete([]);
setReloadPage(true);
Expand Down Expand Up @@ -242,6 +239,10 @@ const XBlockEditView = () => {
setImagesToSave(newImagesToSave);
};

useAddErrorMessageToModal(
errorMessage ? <ErrorMessage message={errorMessage} className="error-message-edit" /> : null
);

useEffect(() => {
fetchUploadedFiles();
}, []);
Expand All @@ -256,7 +257,6 @@ const XBlockEditView = () => {
<>
<DropZoneFile onDroppedImages={handleOnDroppedImages} />
<ListImages list={currentImagesList} onDeleteImageList={handleDeleteImage} />
{errorMessage && <ErrorMessage message={errorMessage} />}
</>
);
};
Expand Down
15 changes: 15 additions & 0 deletions react-app/components/XBlockEditView/styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,18 @@
.disabled-button:hover {
cursor: not-allowed;
}

.error-message-edit {
padding: 15px 2% 10px 2% !important;
margin-top: -10px !important;
}

.error-message-edit > .error-icon {
font-size: 16px !important;
margin-right: 10px !important;
}

.error-message-edit > .error-text {
font-size: 14px !important;
margin-top: -5px !important;
}
36 changes: 36 additions & 0 deletions react-app/hooks/useAddErrorMessageToModal.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { useEffect, useMemo } from 'react';
import ReactDOM from 'react-dom';

const useAddErrorMessageToModal = (errorMessage) => {
useEffect(() => {
const modalElement = document.querySelector('.edit-xblock-modal');

if (modalElement && errorMessage) {
const div = document.createElement('div');
modalElement.appendChild(div);

ReactDOM.render(errorMessage, div);

return () => {
ReactDOM.unmountComponentAtNode(div);
modalElement.removeChild(div);
};
}
}, [errorMessage]);

return useMemo(
() => ({
removeErrorMessage: () => {
const modalElement = document.querySelector('.edit-xblock-modal');
const errorMessageDiv = modalElement.querySelector('.error-message');
if (errorMessageDiv) {
ReactDOM.unmountComponentAtNode(errorMessageDiv);
errorMessageDiv.remove();
}
},
}),
[]
);
};

export default useAddErrorMessageToModal;

0 comments on commit b13b810

Please sign in to comment.