Skip to content

Commit

Permalink
Merge pull request #245 from uclaacm/view-only-fix-dl
Browse files Browse the repository at this point in the history
View-Only: Fixes Redux State Corruption
  • Loading branch information
mattxwang authored Mar 29, 2020
2 parents 228b8c5 + 5b348a5 commit 55cd132
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions src/components/ViewOnly.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,18 @@ class ViewOnly extends React.Component {
code: "",
loaded: false,
notfound: false,
originalCode: "",
};
this.savePrevProgram = (this.props.uid !== "");
}

componentDidMount = async () => {
if (this.savePrevProgram){
await this.codeSaverHelper();
}

const { ok, sketch } = await fetch.getSketch(this.props.programid);

if (!ok) {
this.setState({ notfound: true });
return;
Expand All @@ -62,6 +69,27 @@ class ViewOnly extends React.Component {
}
}

componentWillUnmount = () => {
if (this.savePrevProgram){
this.props.setProgramCode(this.props.mostRecentProgram, this.state.originalCode);
}
};

codeSaverHelper = async () => {
const { ok: okOriginal, sketch: original } = await fetch.getSketch(
this.props.mostRecentProgram,
);

if (!okOriginal) {
this.setState({ notfound: true });
return;
}

this.setState({
originalCode: original.code,
});
}

onThemeChange = () => {
let newTheme = this.props.theme === "dark" ? "light" : "dark";
cookies.setThemeCookie(newTheme);
Expand Down

0 comments on commit 55cd132

Please sign in to comment.