fix: Refactor challenge mounting and updating

componentDidMount and componentDidUpdate share a lot of code. This just
DRYs them out.
This commit is contained in:
Oliver Eyton-Williams
2019-06-20 10:49:53 +02:00
committed by mrugesh
parent a8c86f6168
commit 6a2974e55e
2 changed files with 41 additions and 60 deletions

View File

@ -97,23 +97,7 @@ export class BackEnd extends Component {
} }
componentDidMount() { componentDidMount() {
const { this.initializeComponent();
challengeMounted,
initConsole,
initTests,
updateChallengeMeta,
data: {
challengeNode: {
fields: { tests },
challengeType
}
},
pageContext: { challengeMeta }
} = this.props;
initConsole('');
initTests(tests);
updateChallengeMeta({ ...challengeMeta, challengeType });
challengeMounted(challengeMeta.id);
window.addEventListener('resize', this.updateDimensions); window.addEventListener('resize', this.updateDimensions);
} }
@ -131,6 +115,17 @@ export class BackEnd extends Component {
challengeNode: { title: prevTitle } challengeNode: { title: prevTitle }
} }
} = prevProps; } = prevProps;
const {
data: {
challengeNode: { title: currentTitle }
}
} = this.props;
if (prevTitle !== currentTitle) {
this.initializeComponent();
}
}
initializeComponent() {
const { const {
challengeMounted, challengeMounted,
initConsole, initConsole,
@ -138,19 +133,16 @@ export class BackEnd extends Component {
updateChallengeMeta, updateChallengeMeta,
data: { data: {
challengeNode: { challengeNode: {
title: currentTitle,
fields: { tests }, fields: { tests },
challengeType challengeType
} }
}, },
pageContext: { challengeMeta } pageContext: { challengeMeta }
} = this.props; } = this.props;
if (prevTitle !== currentTitle) { initConsole('');
initConsole(''); initTests(tests);
initTests(tests); updateChallengeMeta({ ...challengeMeta, challengeType });
updateChallengeMeta({ ...challengeMeta, challengeType }); challengeMounted(challengeMeta.id);
challengeMounted(challengeMeta.id);
}
} }
handleSubmit(values) { handleSubmit(values) {

View File

@ -104,6 +104,31 @@ class ShowClassic extends Component {
} }
componentDidMount() { componentDidMount() {
const {
data: {
challengeNode: { title }
}
} = this.props;
this.initializeComponent(title);
}
componentDidUpdate(prevProps) {
const {
data: {
challengeNode: { title: prevTitle }
}
} = prevProps;
const {
data: {
challengeNode: { title: currentTitle }
}
} = this.props;
if (prevTitle !== currentTitle) {
this.initializeComponent(currentTitle);
}
}
initializeComponent(title) {
const { const {
challengeMounted, challengeMounted,
createFiles, createFiles,
@ -113,7 +138,6 @@ class ShowClassic extends Component {
data: { data: {
challengeNode: { challengeNode: {
files, files,
title,
fields: { tests }, fields: { tests },
challengeType challengeType
} }
@ -127,41 +151,6 @@ class ShowClassic extends Component {
challengeMounted(challengeMeta.id); challengeMounted(challengeMeta.id);
} }
componentDidUpdate(prevProps) {
const {
data: {
challengeNode: { title: prevTitle }
}
} = prevProps;
const {
challengeMounted,
createFiles,
initConsole,
initTests,
updateChallengeMeta,
data: {
challengeNode: {
files,
title: currentTitle,
fields: { tests },
challengeType
}
},
pageContext: { challengeMeta }
} = this.props;
if (prevTitle !== currentTitle) {
initConsole('');
createFiles(files);
initTests(tests);
updateChallengeMeta({
...challengeMeta,
title: currentTitle,
challengeType
});
challengeMounted(challengeMeta.id);
}
}
componentWillUnmount() { componentWillUnmount() {
const { createFiles } = this.props; const { createFiles } = this.props;
createFiles({}); createFiles({});