Skip to content

Commit

Permalink
Refactored forwardButtonEnabled logic
Browse files Browse the repository at this point in the history
using the existing case statements.
  • Loading branch information
jebeni777 committed Nov 2, 2020
1 parent 61333b9 commit 0383bcb
Showing 1 changed file with 14 additions and 21 deletions.
35 changes: 14 additions & 21 deletions app/javascript/components/ActionItemCreationPage/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,7 @@ class ActionItemCreationPage extends React.Component {
let leftComponentText;
let rightComponentText;
let headerText;
let forwardButtonEnabled;
switch (stepSize) {
case steps.CHOOSE_ASSIGNMENT:
leftComponentText = 'Assignments';
Expand Down Expand Up @@ -418,6 +419,7 @@ class ActionItemCreationPage extends React.Component {
categories={this.props.categories}
/>
);
forwardButtonEnabled = !!this.state.selectedActionItems.length;
break;
case steps.CHOOSE_PARTICIPANT:
leftComponentText = 'Students';
Expand Down Expand Up @@ -445,6 +447,7 @@ class ActionItemCreationPage extends React.Component {
removeAllUsers={this.removeAllUsersFromState}
/>
);
forwardButtonEnabled = !!this.state.selectedParticipants.length;
break;
case steps.CONFIRM_SUBMIT:
leftComponentText = 'Review Students';
Expand All @@ -465,47 +468,33 @@ class ActionItemCreationPage extends React.Component {
handleOpenModal={this.handleOpenModal}
/>
);
forwardButtonEnabled = true;
break;
default:
leftComponent = null;
rightComponent = null;
leftComponentText = null;
rightComponentText = null;
headerText = null;
forwardButtonEnabled = false;
}
return {
leftComponent,
rightComponent,
leftComponentText,
rightComponentText,
headerText,
forwardButtonEnabled,
};
}

getButtons(stepSize) {
getButtons(stepSize, forwardButtonEnabled) {
const { classes } = this.props;
const forwardButtonText =
stepSize === steps.CONFIRM_SUBMIT ? 'ASSIGN' : 'SAVE & CONTINUE';
const handleForwardButtonClick =
stepSize === steps.CONFIRM_SUBMIT ? this.handleSubmit : this.nextStep;
const forwardButtonEnabled = () => {
if (
stepSize === steps.CHOOSE_ASSIGNMENT &&
this.state.selectedActionItems.length
) {
return true;
}
if (
stepSize === steps.CHOOSE_PARTICIPANT &&
this.state.selectedParticipants.length
) {
return true;
}
if (stepSize === steps.CONFIRM_SUBMIT) {
return true;
}
return false;
};

const backButton = (
<Grid item>
<Fab
Expand All @@ -530,7 +519,7 @@ class ActionItemCreationPage extends React.Component {
variant="extended"
size="medium"
aria-label="category"
disabled={!forwardButtonEnabled()}
disabled={!forwardButtonEnabled}
onClick={handleForwardButtonClick}
>
<Typography className={classes.categoryButtonStyle} align="center">
Expand All @@ -557,8 +546,12 @@ class ActionItemCreationPage extends React.Component {
rightComponent,
rightComponentText,
headerText,
forwardButtonEnabled,
} = this.getMainComponents(this.state.step);
const buttonsGrid = this.getButtonsGrid(this.state.step);
const buttonsGrid = this.getButtonsGrid(
this.state.step,
forwardButtonEnabled,
);

return (
<div>
Expand Down

0 comments on commit 0383bcb

Please sign in to comment.