Skip to content

Commit

Permalink
Fixes after code analysis
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasOsti committed Mar 19, 2024
1 parent 8abe320 commit 78047b6
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 62 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@ export default class UploadItemComponent extends Component {
constructor(props) {
super(props);

this.handleFileSizeNotAllowed = this.handleFileSizeNotAllowed.bind(this);
this.handleFileTypeNotAllowed = this.handleFileTypeNotAllowed.bind(this);
this.handleContentTypeNotAllowed = this.handleContentTypeNotAllowed.bind(this);
this.handleFileValidationError = this.handleFileValidationError.bind(this);
this.handleEditBtnClick = this.handleEditBtnClick.bind(this);
this.handleUploadAbort = this.handleUploadAbort.bind(this);
this.handleUploadError = this.handleUploadError.bind(this);
Expand Down Expand Up @@ -75,13 +73,8 @@ export default class UploadItemComponent extends Component {
...adminUiConfig.multiFileUpload,
contentCreatePermissionsConfig,
};
const callbacks = {
fileTypeNotAllowedCallback: this.handleFileTypeNotAllowed,
fileSizeNotAllowedCallback: this.handleFileSizeNotAllowed,
contentTypeNotAllowedCallback: this.handleContentTypeNotAllowed,
};

if (!checkCanUpload(item.file, parentInfo, config, callbacks)) {
if (!checkCanUpload(item.file, parentInfo, config, this.handleFileValidationError)) {
this.setState(() => ({
uploading: false,
uploaded: false,
Expand Down Expand Up @@ -137,33 +130,7 @@ export default class UploadItemComponent extends Component {
);
}

handleFileTypeNotAllowed(errorMsg) {
this.setState(
(prevState) => ({
uploading: false,
uploaded: false,
aborted: false,
failed: true,
errorMsgs: [...prevState.errorMsgs, errorMsg],
}),
() => this.props.onCreateError({ ...this.props.item, errorMsgs: this.state.errorMsgs }),
);
}

handleFileSizeNotAllowed(errorMsg) {
this.setState(
(prevState) => ({
uploading: false,
uploaded: false,
aborted: false,
failed: true,
errorMsgs: [...prevState.errorMsgs, errorMsg],
}),
() => this.props.onCreateError({ ...this.props.item, errorMsgs: this.state.errorMsgs }),
);
}

handleContentTypeNotAllowed(errorMsg) {
handleFileValidationError(errorMsg) {
this.setState(
(prevState) => ({
uploading: false,
Expand Down Expand Up @@ -272,7 +239,7 @@ export default class UploadItemComponent extends Component {
getContentTypeIdentifier() {
const { contentTypesMap, item } = this.props;

if (!item.struct || !item.struct.Content) {
if (!item.struct?.Content) {
return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export default class UploadListComponent extends Component {
}

handleAfterDelete(item) {
console.log('After delete');

Check failure on line 38 in src/bundle/ui-dev/src/modules/multi-file-upload/components/upload-list/upload.list.component.js

View workflow job for this annotation

GitHub Actions / Frontend build test

Unexpected console statement
this.setState(
(state) => {
const items = state.items.filter((data) => data.id !== item.id);
Expand All @@ -53,20 +54,6 @@ export default class UploadListComponent extends Component {
}));
}

removeErroredItems(items) {
const itemsIds = items.map((item) => item.id);

this.setState((prevState) => {
const erroredItems = prevState.erroredItems.filter((stateItem) => !itemsIds.includes(stateItem.id));

if (erroredItems.length !== prevState.erroredItems.length) {
return {
erroredItems,
};
}
});
}

renderItemToUpload(item) {
return this.renderItem(item, {
isUploaded: false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ const createDraft = (struct, requestEventHandlers) => {
});
};
const publishDraft = (data) => {
if (!data || !Object.prototype.hasOwnProperty.call(data, 'Content')) {
if (!data?.Content) {
return Promise.reject('Cannot publish content based on an uploaded file');
}

Expand Down Expand Up @@ -241,16 +241,16 @@ const canCreateContent = (file, parentInfo, config) => {
const getMaxFileSize = (file, parentInfo, config) => {
const { maxFileSize: contentMaxFileSize } = detectContentTypeMapping(file, parentInfo, config);

return contentMaxFileSize ? contentMaxFileSize : config.maxFileSize;
return contentMaxFileSize || config.maxFileSize;
};

export const checkCanUpload = (file, parentInfo, config, callbacks) => {
export const checkCanUpload = (file, parentInfo, config, errorCallback) => {
const Translator = getTranslator();
const locationMapping = config.locationMappings.find((item) => item.contentTypeIdentifier === parentInfo.contentTypeIdentifier);
const maxFileSize = getMaxFileSize(file, parentInfo, config);

if (!canCreateContent(file, parentInfo, config)) {
callbacks.contentTypeNotAllowedCallback(
errorCallback(
Translator.trans(
/*@Desc("You do not have permission to create this Content item")*/ 'disallowed_content_type.message',
{},
Expand All @@ -261,17 +261,13 @@ export const checkCanUpload = (file, parentInfo, config, callbacks) => {
return false;
}
if (!checkFileTypeAllowed(file, locationMapping)) {
callbacks.fileTypeNotAllowedCallback(
Translator.trans(/*@Desc("File type is not allowed")*/ 'disallowed_type.message', {}, 'ibexa_multi_file_upload'),
);
errorCallback(Translator.trans(/*@Desc("File type is not allowed")*/ 'disallowed_type.message', {}, 'ibexa_multi_file_upload'));

return false;
}

if (file.size > maxFileSize) {
callbacks.fileSizeNotAllowedCallback(
Translator.trans(/*@Desc("File size is not allowed")*/ 'disallowed_size.message', {}, 'ibexa_multi_file_upload'),
);
errorCallback(Translator.trans(/*@Desc("File size is not allowed")*/ 'disallowed_size.message', {}, 'ibexa_multi_file_upload'));

return false;
}
Expand All @@ -282,7 +278,7 @@ export const createFileStruct = (file, params, contentErrorCallback) => {
return new Promise(readFile.bind(new FileReader(), file)).then((fileData) => prepareStruct(params, fileData, contentErrorCallback));
};
export const publishFile = (data, requestEventHandlers, successCallback, contentErrorCallback) => {
createDraft(data, requestEventHandlers, contentErrorCallback)
createDraft(data, requestEventHandlers)
.then(publishDraft)
.then(successCallback)
.catch(() => {
Expand Down

0 comments on commit 78047b6

Please sign in to comment.