Skip to content

Commit 07bfcf0

Browse files
committed
modularize functions
1 parent cf7237b commit 07bfcf0

File tree

1 file changed

+10
-13
lines changed

1 file changed

+10
-13
lines changed

src/App.js

+10-13
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ class App extends React.Component {
3535
this.generateBars(this.state.barCount);
3636
}
3737

38-
handleStart = () => {
38+
generateSteps = () => {
3939
let array = this.state.array.slice();
4040
let steps = this.state.arraySteps.slice();
4141
let colorSteps = this.state.colorSteps.slice();
@@ -46,10 +46,12 @@ class App extends React.Component {
4646
arraySteps: steps,
4747
colorSteps: colorSteps,
4848
});
49-
this.run(steps, colorSteps);
5049
}
5150

52-
run(steps, colorSteps) {
51+
setTimeouts() {
52+
let steps = this.state.arraySteps;
53+
let colorSteps = this.state.colorSteps;
54+
5355
this.clearTimeouts();
5456
let timeouts = [];
5557
let i = 0;
@@ -85,7 +87,7 @@ class App extends React.Component {
8587
}
8688

8789
stepForward = () => {
88-
if (this.state.currentStep === this.state.arraySteps.length - 1) return;
90+
if (this.state.currentStep === this.state.arraySteps.length) return;
8991
this.clearTimeouts();
9092

9193
let currentStep = this.state.currentStep + 1;
@@ -100,8 +102,8 @@ class App extends React.Component {
100102
this.setState({
101103
algorithm: event.target.value,
102104
currentStep: 0,
103-
arraySteps: [this.state.arraySteps[this.state.arraySteps.length - 1]],
104-
});
105+
arraySteps: [this.state.arraySteps[this.state.currentStep === 0 ? 0 : this.state.currentStep - 1]],
106+
}, () => this.generateSteps());
105107
this.clearTimeouts();
106108
this.clearColorKey();
107109
};
@@ -144,7 +146,7 @@ class App extends React.Component {
144146
arraySteps: [barsTemp],
145147
barCount: barCount,
146148
currentStep: 0,
147-
});
149+
}, () => this.generateSteps());
148150
}
149151

150152
render() {
@@ -162,14 +164,9 @@ class App extends React.Component {
162164
<Pause />
163165
</IconButton>
164166
);
165-
} else if (this.state.arraySteps.length > 2) {
166-
playButton = (
167-
<IconButton color="secondary" onClick={() => this.run(this.state.arraySteps, this.state.colorSteps)} >
168-
<PlayArrow />
169-
</IconButton>);
170167
} else {
171168
playButton = (
172-
<IconButton color="secondary" onClick={() => this.handleStart()} >
169+
<IconButton color="secondary" onClick={() => this.setTimeouts()} >
173170
<PlayArrow />
174171
</IconButton>);
175172
}

0 commit comments

Comments
 (0)