Skip to content

Commit

Permalink
controls: refactored isDisabled function
Browse files Browse the repository at this point in the history
* Refactored isDisabled function in PublishButton and SubmitReviewButton components to check if all files are finished uploading before enabling the button.
* Passed filesState instead of just the number of files to the function.
* Added a check for non-finished files.
* Added early returns to avoid unnecessary checks on possible large objects / arrays.
  • Loading branch information
alejandromumo committed Sep 3, 2024
1 parent 0c834f3 commit 7a7adb0
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,29 @@ class PublishButtonComponent extends Component {
this.closeConfirmModal();
};

isDisabled = (values, isSubmitting, numberOfFiles) => {
isDisabled = (values, isSubmitting, filesState) => {
if (isSubmitting) {
return true;
}

const filesEnabled = _get(values, "files.enabled", false);
const filesMissing = filesEnabled && !numberOfFiles;
return isSubmitting || filesMissing;
const filesArray = Object.values(filesState.entries ?? {});
const filesMissing = filesEnabled && filesArray.length === 0;

if (filesMissing) {
return true;
}

// All files must be finished uploading
const allCompleted = filesArray.every(file => file.status === "finished")

return !allCompleted;
};

render() {
const {
actionState,
numberOfFiles,
filesState,
buttonLabel,
publishWithoutCommunity,
formik,
Expand All @@ -64,7 +77,7 @@ class PublishButtonComponent extends Component {
return (
<>
<Button
disabled={this.isDisabled(values, isSubmitting, numberOfFiles)}
disabled={this.isDisabled(values, isSubmitting, filesState)}
name="publish"
onClick={this.openConfirmModal}
positive
Expand Down Expand Up @@ -123,22 +136,23 @@ PublishButtonComponent.propTypes = {
buttonLabel: PropTypes.string,
publishWithoutCommunity: PropTypes.bool,
actionState: PropTypes.string,
numberOfFiles: PropTypes.number.isRequired,
formik: PropTypes.object.isRequired,
publishModalExtraContent: PropTypes.string,
filesState: PropTypes.object,
};

PublishButtonComponent.defaultProps = {
buttonLabel: i18next.t("Publish"),
publishWithoutCommunity: false,
actionState: undefined,
publishModalExtraContent: undefined,
filesState: undefined,
};

const mapStateToProps = (state) => ({
actionState: state.deposit.actionState,
numberOfFiles: Object.values(state.files.entries).length,
publishModalExtraContent: state.deposit.config.publish_modal_extra,
filesState: state.files,
});

export const PublishButton = connect(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,26 @@ class SubmitReviewButtonComponent extends Component {
this.closeConfirmModal();
};

isDisabled = (numberOfFiles, disableSubmitForReviewButton) => {
isDisabled = (disableSubmitForReviewButton, filesState) => {
const { formik } = this.props;
const { values, isSubmitting } = formik;

if (disableSubmitForReviewButton || isSubmitting) {
return true;
}

const filesEnabled = _get(values, "files.enabled", false);
const filesMissing = filesEnabled && !numberOfFiles;
return isSubmitting || filesMissing || disableSubmitForReviewButton;
const filesArray = Object.values(filesState.entries ?? {});
const filesMissing = filesEnabled && filesArray.length === 0;

if (filesMissing) {
return true;
}

// All files must be finished uploading
const allCompleted = filesArray.every(file => file.status === "finished")

return !allCompleted;
};

render() {
Expand All @@ -59,8 +72,8 @@ class SubmitReviewButtonComponent extends Component {
directPublish,
formik,
isRecordSubmittedForReview,
numberOfFiles,
publishModalExtraContent,
filesState,
...ui
} = this.props;

Expand All @@ -80,7 +93,7 @@ class SubmitReviewButtonComponent extends Component {
return (
<>
<Button
disabled={this.isDisabled(numberOfFiles, disableSubmitForReviewButton)}
disabled={this.isDisabled(disableSubmitForReviewButton, filesState)}
name="SubmitReview"
onClick={this.openConfirmModal}
positive={directPublish}
Expand Down Expand Up @@ -112,20 +125,20 @@ SubmitReviewButtonComponent.propTypes = {
actionState: PropTypes.string,
actionStateExtra: PropTypes.object.isRequired,
community: PropTypes.object.isRequired,
numberOfFiles: PropTypes.number,
disableSubmitForReviewButton: PropTypes.bool,
isRecordSubmittedForReview: PropTypes.bool.isRequired,
directPublish: PropTypes.bool,
formik: PropTypes.object.isRequired,
publishModalExtraContent: PropTypes.string,
filesState: PropTypes.object,
};

SubmitReviewButtonComponent.defaultProps = {
actionState: undefined,
numberOfFiles: undefined,
disableSubmitForReviewButton: undefined,
publishModalExtraContent: undefined,
directPublish: false,
filesState: undefined
};

const mapStateToProps = (state) => ({
Expand All @@ -135,8 +148,8 @@ const mapStateToProps = (state) => ({
isRecordSubmittedForReview: state.deposit.record.status === DepositStatus.IN_REVIEW,
disableSubmitForReviewButton:
state.deposit.editorState.ui.disableSubmitForReviewButton,
numberOfFiles: Object.values(state.files.entries).length,
publishModalExtraContent: state.deposit.config.publish_modal_extra,
filesState: state.files,
});

export const SubmitReviewButton = connect(
Expand Down

0 comments on commit 7a7adb0

Please sign in to comment.