diff --git a/client/src/components/recipe/RecipeCard.tsx b/client/src/components/recipe/RecipeCard.tsx index 2ed1314..30fd3ee 100644 --- a/client/src/components/recipe/RecipeCard.tsx +++ b/client/src/components/recipe/RecipeCard.tsx @@ -1,6 +1,7 @@ import type { Recipe } from '../../types/recipe'; import { Link } from 'react-router'; import { Rating } from '@mui/material'; +import { RecipeImage } from '../ui/RecipeImage'; // Sier hva komponenten får inn. type RecipeCardProps = { @@ -11,7 +12,7 @@ export function RecipeCard({ recipe }: RecipeCardProps) { return (
- {recipe.title}/ +

{recipe.title}

{recipe.prepTimeMinutes} minutter

diff --git a/client/src/components/recipe/RecipeDetail.tsx b/client/src/components/recipe/RecipeDetail.tsx index 4f6f4ab..2a96c2d 100644 --- a/client/src/components/recipe/RecipeDetail.tsx +++ b/client/src/components/recipe/RecipeDetail.tsx @@ -3,6 +3,7 @@ import RestaurantIcon from '@mui/icons-material/Restaurant'; import AccessTimeIcon from '@mui/icons-material/AccessTime'; import { useState } from 'react'; import { Rating } from '@mui/material'; +import { RecipeImage } from '../ui/RecipeImage'; import Checkbox from '@mui/material/Checkbox'; import FormControlLabel from '@mui/material/FormControlLabel'; @@ -62,7 +63,7 @@ export function RecipeDetail({ recipe }: RecipeDetailProps) {
- {recipe.title}/ +

Ingredienser

diff --git a/client/src/components/slideshow/RecipeSlideshow.tsx b/client/src/components/slideshow/RecipeSlideshow.tsx index 14d0b9a..f22a70d 100644 --- a/client/src/components/slideshow/RecipeSlideshow.tsx +++ b/client/src/components/slideshow/RecipeSlideshow.tsx @@ -2,6 +2,7 @@ import type { Recipe } from '../../types/recipe'; import useEmblaCarousel from 'embla-carousel-react'; import { Link } from 'react-router'; import { useEffect, useState } from 'react'; +import { RecipeImage } from '../ui/RecipeImage'; import Autoplay from 'embla-carousel-autoplay'; import AccessTimeIcon from '@mui/icons-material/AccessTime'; import RestaurantIcon from '@mui/icons-material/Restaurant'; @@ -91,7 +92,7 @@ export function RecipeSlideshow({ recipes }: RecipeSlideshowProps) { Se oppskrift {/* Tomt alternativ for bildet, ettersom tittelen allerede står rett ved siden av bildet. */} - + ))} diff --git a/client/src/components/ui/RecipeImage.tsx b/client/src/components/ui/RecipeImage.tsx new file mode 100644 index 0000000..a052cff --- /dev/null +++ b/client/src/components/ui/RecipeImage.tsx @@ -0,0 +1,43 @@ +import { useState } from 'react'; +import RestaurantIcon from '@mui/icons-material/Restaurant'; + +type RecipeImageProps = { + imageUrl: string | null | undefined; + alt: string; + className?: string; +}; + +// viser bildet til oppskriften eller en tegning hvis bildet mangler +export function RecipeImage({ + imageUrl, + alt, + className = '', +}: RecipeImageProps) { + // bytter til tegning hvis bilde ikke finnes + const [failed, setFailed] = useState(false); + + if (imageUrl && !failed) { + return ( + {alt} setFailed(true)} + className={`object-cover ${className}`} + /> + ); + } + + return ( + + ); +}