summaryrefslogtreecommitdiff
path: root/opendc-web/opendc-web-ui/src/components/app/map/elements/ImageComponent.js
diff options
context:
space:
mode:
authorFabian Mastenbroek <mail.fabianm@gmail.com>2021-05-13 13:00:00 +0200
committerFabian Mastenbroek <mail.fabianm@gmail.com>2021-05-17 17:06:49 +0200
commit24147cba0f5723be3525e8f40d1954144841629b (patch)
treed44727504f7a9c579219616cbdba5e965543561a /opendc-web/opendc-web-ui/src/components/app/map/elements/ImageComponent.js
parent4397a959e806bf476be4c81bc804616adf58b969 (diff)
ui: Address technical dept in frontend
Diffstat (limited to 'opendc-web/opendc-web-ui/src/components/app/map/elements/ImageComponent.js')
-rw-r--r--opendc-web/opendc-web-ui/src/components/app/map/elements/ImageComponent.js54
1 files changed, 21 insertions, 33 deletions
diff --git a/opendc-web/opendc-web-ui/src/components/app/map/elements/ImageComponent.js b/opendc-web/opendc-web-ui/src/components/app/map/elements/ImageComponent.js
index 2b5c569f..7d304b6b 100644
--- a/opendc-web/opendc-web-ui/src/components/app/map/elements/ImageComponent.js
+++ b/opendc-web/opendc-web-ui/src/components/app/map/elements/ImageComponent.js
@@ -1,48 +1,36 @@
import PropTypes from 'prop-types'
-import React from 'react'
+import React, { useEffect, useState } from 'react'
import { Image } from 'react-konva'
-class ImageComponent extends React.Component {
- static imageCaches = {}
- static propTypes = {
- src: PropTypes.string.isRequired,
- x: PropTypes.number.isRequired,
- y: PropTypes.number.isRequired,
- width: PropTypes.number.isRequired,
- height: PropTypes.number.isRequired,
- opacity: PropTypes.number.isRequired,
- }
+const imageCaches = {}
- state = {
- image: null,
- }
+function ImageComponent({ src, x, y, width, height, opacity }) {
+ const [image, setImage] = useState(null)
- componentDidMount() {
- if (ImageComponent.imageCaches[this.props.src]) {
- this.setState({ image: ImageComponent.imageCaches[this.props.src] })
+ useEffect(() => {
+ if (imageCaches[src]) {
+ setImage(imageCaches[src])
return
}
const image = new window.Image()
- image.src = this.props.src
+ image.src = src
image.onload = () => {
- this.setState({ image })
- ImageComponent.imageCaches[this.props.src] = image
+ setImage(image)
+ imageCaches[src] = image
}
- }
+ }, [src])
- render() {
- return (
- <Image
- image={this.state.image}
- x={this.props.x}
- y={this.props.y}
- width={this.props.width}
- height={this.props.height}
- opacity={this.props.opacity}
- />
- )
- }
+ return <Image image={image} x={x} y={y} width={width} height={height} opacity={opacity} />
+}
+
+ImageComponent.propTypes = {
+ src: PropTypes.string.isRequired,
+ x: PropTypes.number.isRequired,
+ y: PropTypes.number.isRequired,
+ width: PropTypes.number.isRequired,
+ height: PropTypes.number.isRequired,
+ opacity: PropTypes.number.isRequired,
}
export default ImageComponent