diff --git a/frontend/src/components/steps/zone/ZoneSelector.js b/frontend/src/components/steps/zone/ZoneSelector.js index 1eef7c1..2510cfc 100644 --- a/frontend/src/components/steps/zone/ZoneSelector.js +++ b/frontend/src/components/steps/zone/ZoneSelector.js @@ -1,5 +1,5 @@ -import { useEffect, useState } from 'react'; -import SampleBackground from '../../../assets/img/sample-bg.jpg'; +import { useEffect, useState, useRef } from 'react'; +import SampleBackground from '../../../assets/img/stepBackgrounds/step1.jpg'; import SiteRepository from '../../../repository/SiteRepository'; @@ -7,14 +7,18 @@ export default function ZoneSelector(props) { const [habitatImageObject, setHabitatImageObject] = useState({}); const [habitatImageFile, setHabitatImageFile] = useState(SampleBackground); + const [imageHeight, setImageHeight] = useState(0) const [selectedZoneSegment, setZoneSegment] = useState(null); + const imageContainer = useRef(null) - const getHabitatImage = () => { + const getHabitatImage = (findAndSetImageHeightFunc) => { SiteRepository.getHabitatImage(props.filters.habitatImage).then(response => { if (response.status === 200) { const imageData = response.data setHabitatImageObject(imageData) - setHabitatImageFile(require(`../../../assets/img/habitats/${imageData.image_filename}`).default) + const imageSrc = require(`../../../assets/img/habitats/${imageData.image_filename}`).default + setHabitatImageFile(imageSrc) + findAndSetImageHeightFunc(imageSrc) } }) } @@ -25,8 +29,19 @@ export default function ZoneSelector(props) { } useEffect(() => { + // Calculates the image ratio and uses this to calculate the height in pixels based on outer container width + const findAndSetImageHeight = (imageSource) => { + let img = new Image(); + img.src = imageSource; + const newHeight = imageContainer.current ? imageContainer.current.clientWidth * (img.height / img.width) : 0; + setImageHeight(newHeight); + } + // Retrieves the habitat image from the api if they are not loaded already - Object.keys(habitatImageObject).length === 0 && getHabitatImage() + Object.keys(habitatImageObject).length === 0 && getHabitatImage(findAndSetImageHeight) + + // Sets the image height such that the full image always displays on page resize + window.addEventListener("resize", () => findAndSetImageHeight(habitatImageFile), false); // Temporarily bypass if there is no image available if (props.filters.zone || !props.filters.habitatImage) { @@ -38,7 +53,8 @@ export default function ZoneSelector(props) { const stepBackground = { backgroundImage: `url(${habitatImageFile})`, backgroundSize: '100% auto', - height: '100%', + backgroundRepeat: 'none', + height: imageHeight, width: '100%', display: 'flex' } @@ -50,7 +66,7 @@ export default function ZoneSelector(props) { }; return ( -
+

{habitatImageObject && habitatImageObject.name}

{Object.keys(habitatImageObject).length === 0 ? ( // Sample segment selector (temporary if no image is available) @@ -62,7 +78,7 @@ export default function ZoneSelector(props) { // Actual zone selector (
{habitatImageObject && habitatImageObject.image_segments && Array.isArray(habitatImageObject.image_segments) && habitatImageObject.image_segments.map(segment => -
{ selectZone(segment) }} className={`selectable-section ${selectedZoneSegment === segment.zone ? 'selected-segment' : ''}`} style={{ width: `${segment.segment_percentage_width}%`, height: '100%' }}>
+
{ selectZone(segment) }} className={`selectable-section ${selectedZoneSegment === segment.zone ? 'selected-segment' : ''}`} style={{ width: `${segment.segment_percentage_width}%`, height: '100%' }}>
)}
)} diff --git a/frontend/src/components/steps/zone/ZoneStep.js b/frontend/src/components/steps/zone/ZoneStep.js index edfbb57..2ac3fe5 100644 --- a/frontend/src/components/steps/zone/ZoneStep.js +++ b/frontend/src/components/steps/zone/ZoneStep.js @@ -13,7 +13,9 @@ export default function ZoneStep(props) { ) const zoneSelectionPanel = ( - +
+ +
) return (