Skip to content

Commit

Permalink
Clean code of checkIfImageExists - fixed: #456
Browse files Browse the repository at this point in the history
  • Loading branch information
martinheppner committed Aug 30, 2024
1 parent d5be91b commit 938fc65
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 81 deletions.
53 changes: 3 additions & 50 deletions src/components/Map/PopupCard.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as React from 'react';
import Card from '@mui/material/Card';
import CardContent from '@mui/material/CardContent';
import Typography from '@mui/material/Typography';
import {checkIfImageExists, convertNumToTime, formatNumber} from "../../utils/globals";
import {convertNumToTime} from "../../utils/globals";
import {useEffect, useState} from "react";
import Box from "@mui/material/Box";
import useMediaQuery from "@mui/material/useMediaQuery";
Expand Down Expand Up @@ -35,61 +35,14 @@ export default function PopupCard({tour, city}){
//description
//search tour-related image in folders and set image state to it , otherwise set state to DEFAULT_IMAGE
useEffect(() => {

if(!!tour.image_url && tour.provider==='bahnzumberg'){
checkIfImageExists(tour.image_url).then(exists => {
if(!!exists){
setImage(tour.image_url);
} else if(!!tour.gpx_image_file_small){
checkIfImageExists(tour.gpx_image_file_small).then(gpxExists => {
if(!!gpxExists){
setImage(tour.gpx_image_file_small);
} else {
setImage(DEFAULT_IMAGE);
}
})
}
})
} else if(!!tour.gpx_image_file_small){
checkIfImageExists(tour.gpx_image_file_small).then(gpxExists => {
if(!!gpxExists){
setImage(tour.gpx_image_file_small);
} else {
setImage(DEFAULT_IMAGE);
}
})
if(!!tour.gpx_image_file_small && !!gpxExists){
setImage(tour.gpx_image_file_small);
} else {
setImage(DEFAULT_IMAGE);
}
}, [tour])

const imageOpacity = 1;

const isMobile_600px = useMediaQuery('(max-width:600px)');
if(isMobile_600px){
let iconStyle = { fill: "transparent", width: '25px', height: '25px' }
}


const shortened_url = () => {
let length = 45;
if (!!isMobile_600px) {
length = 35;
}
let red_url = (!!tour.url?tour.url:"").replace("https://www.", "").replace("http://www.", "").split("/"), i;
let final_url = red_url[0];

for (i = 1; i < red_url.length-1; i++) {
if (final_url.length + red_url[i].length <= length) {
final_url = final_url + " > " + red_url[i]
}
else {
return final_url;
}
}
return final_url;
}


let value_best_connection_duration = tour.min_connection_duration;
let value_connection_no_of_transfers = tour.min_connection_no_of_transfers;
Expand Down
14 changes: 1 addition & 13 deletions src/components/RangeCard.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,16 @@ import Typography from "@mui/material/Typography";
import { Paper } from "@mui/material";
import Box from "@mui/material/Box";
import { useEffect, useState } from "react";
import { checkIfImageExists } from "../utils/globals";
import SouthEastIcon from "@mui/icons-material/SouthEast";

// const DEFAULT_IMAGE = `${window.location.origin}/app_static/img/default.jpg`;
const DEFAULT_IMAGE = `${window.location.origin}/public/range-image/default.webp`

export default function RangeCard({ range, onSelectTour }) {
const [image, setImage] = useState(DEFAULT_IMAGE);

useEffect(() => {
if (!!range) {
if (!!range.image_url) {
checkIfImageExists(range.image_url).then((exists) => {
if (!!exists) {
setImage(range.image_url);
} else {
setImage(DEFAULT_IMAGE);
}
});
} else {
setImage(DEFAULT_IMAGE);
}
setImage(range.image_url);
}
}, [range]);

Expand Down
18 changes: 0 additions & 18 deletions src/utils/globals.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,24 +40,6 @@ export function formatNumber(number, postfix = ""){
return parseFloat(number).toLocaleString('de-AT') + postfix;
}

export function checkIfImageExists(url) {
const img = new Image();
img.src = url;

return new Promise(resolve => {
if (img.complete) {
resolve(true);
} else {
img.onload = () => {
resolve(true);
};

img.onerror = () => {
resolve(false);
};
}
})
}

export function updateAllSearchParams(searchParams, values){
for (const [key, value] of Object.entries(values)) {
Expand Down

0 comments on commit 938fc65

Please sign in to comment.