From dc1bf73f09c7243e9fc328a054f5e0d6bed27b17 Mon Sep 17 00:00:00 2001 From: Anna Mistele Date: Wed, 14 Aug 2024 15:55:06 -0700 Subject: [PATCH] Fix state and props usage --- packages/perseus/src/widgets/phet-sim.tsx | 90 ++++++++++++----------- 1 file changed, 47 insertions(+), 43 deletions(-) diff --git a/packages/perseus/src/widgets/phet-sim.tsx b/packages/perseus/src/widgets/phet-sim.tsx index cf9aa22679..3652c1372b 100644 --- a/packages/perseus/src/widgets/phet-sim.tsx +++ b/packages/perseus/src/widgets/phet-sim.tsx @@ -8,7 +8,6 @@ import Banner from "@khanacademy/wonder-blocks-banner"; import {View} from "@khanacademy/wonder-blocks-core"; import IconButton from "@khanacademy/wonder-blocks-icon-button"; import cornersOutIcon from "@phosphor-icons/core/regular/corners-out.svg"; -import PropTypes from "prop-types"; import * as React from "react"; import {PerseusI18nContext} from "../components/i18n-context"; @@ -28,12 +27,6 @@ type State = { /* This renders the PhET sim */ class PhetSim extends React.Component { - static propTypes = { - ...Changeable.propTypes, - url: PropTypes.string, - description: PropTypes.string, - }; - static contextType = PerseusI18nContext; declare context: React.ContextType; private readonly iframeRef: React.RefObject; @@ -48,11 +41,6 @@ class PhetSim extends React.Component { super(props); this.locale = this.getPhetCompatibleLocale(getDependencies().kaLocale); this.iframeRef = React.createRef(); - this.state.url = new URL(this.props.url); - this.state.url.searchParams.set("locale", this.locale); - if (this.state.url.origin !== "https://phet.colorado.edu") { - this.state.url = null; - } } getUserInput: () => any = () => { @@ -60,20 +48,13 @@ class PhetSim extends React.Component { }; async componentDidMount() { - // Display an error if we fail to load the resource - if (this.state.url) { - const validLink = await fetch(this.state.url).then( - (response: Response) => response.ok, - ); - if (!validLink) { - this.setState({url: null}); - } - } - // Display a warning if the simulation doesn't have our locale - if (await this.showLocaleWarning(this.locale)) { - this.setState({ - errMessage: this.context.strings.simulationLocaleWarning, - }); + await this.updateSimState(this.props.url); + } + + async componentDidUpdate(prevProps) { + // If the URL has changed, update our state + if (prevProps.url !== this.props.url) { + await this.updateSimState(this.props.url); } } @@ -93,28 +74,20 @@ class PhetSim extends React.Component { text={this.state.errMessage} /> )} - +