From 7819567cb316b375d8d0b27acac6cde7e7b5d18d Mon Sep 17 00:00:00 2001 From: Jamie Liu Date: Wed, 4 Mar 2020 17:19:04 -0800 Subject: [PATCH 1/2] Fix bug causing code to download incorrectly The bug: Open a sketch in view-only mode. Go to the sketches page and try to download your most recently viewed sketch. The sketch title and extension are as expected, but the code is the code from the view-only sketch. This fixes the bug by saving the original code for the most recently viewed program, and restoring it before the component unmounts. --- src/components/ViewOnly.js | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/components/ViewOnly.js b/src/components/ViewOnly.js index 383161bd..0593c77e 100644 --- a/src/components/ViewOnly.js +++ b/src/components/ViewOnly.js @@ -31,11 +31,25 @@ class ViewOnly extends React.Component { code: "", loaded: false, notfound: false, + originalCode: "", }; } componentDidMount = async () => { + const { ok: okOriginal, sketch: original } = await fetch.getSketch( + this.props.mostRecentProgram, + ); const { ok, sketch } = await fetch.getSketch(this.props.programid); + + if (!okOriginal) { + this.setState({ notfound: true }); + return; + } + + this.setState({ + originalCode: original.code, + }); + if (!ok) { this.setState({ notfound: true }); return; @@ -62,6 +76,10 @@ class ViewOnly extends React.Component { } } + componentWillUnmount = () => { + this.props.setProgramCode(this.props.mostRecentProgram, this.state.originalCode); + }; + onThemeChange = () => { let newTheme = this.props.theme === "dark" ? "light" : "dark"; cookies.setThemeCookie(newTheme); From 5b348a5b66731b8c72dea5c755b47ba601849214 Mon Sep 17 00:00:00 2001 From: Matthew Wang Date: Wed, 25 Mar 2020 15:37:19 -0700 Subject: [PATCH 2/2] only triggers save program if user is logged in this is also a bandaid fix --- src/components/ViewOnly.js | 34 ++++++++++++++++++++++------------ 1 file changed, 22 insertions(+), 12 deletions(-) diff --git a/src/components/ViewOnly.js b/src/components/ViewOnly.js index 0593c77e..01e56d4e 100644 --- a/src/components/ViewOnly.js +++ b/src/components/ViewOnly.js @@ -33,22 +33,15 @@ class ViewOnly extends React.Component { notfound: false, originalCode: "", }; + this.savePrevProgram = (this.props.uid !== ""); } componentDidMount = async () => { - const { ok: okOriginal, sketch: original } = await fetch.getSketch( - this.props.mostRecentProgram, - ); - const { ok, sketch } = await fetch.getSketch(this.props.programid); - - if (!okOriginal) { - this.setState({ notfound: true }); - return; + if (this.savePrevProgram){ + await this.codeSaverHelper(); } - this.setState({ - originalCode: original.code, - }); + const { ok, sketch } = await fetch.getSketch(this.props.programid); if (!ok) { this.setState({ notfound: true }); @@ -77,9 +70,26 @@ class ViewOnly extends React.Component { } componentWillUnmount = () => { - this.props.setProgramCode(this.props.mostRecentProgram, this.state.originalCode); + 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);