Skip to content

Commit 2b664f6

Browse files
improve animal page
1 parent b802130 commit 2b664f6

File tree

3 files changed

+20
-4
lines changed

3 files changed

+20
-4
lines changed

src/features/Animal/Animal.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,3 +68,10 @@ export function getAnimalsByCategory(categoryName: string): Animal[] {
6868
(animal) => animal.category.toLowerCase() === categoryName.toLowerCase()
6969
);
7070
}
71+
72+
// Function to get an animal by its slug
73+
export function getAnimalBySlug(slug: string): Animal | null {
74+
const animals = getAllAnimals();
75+
76+
return animals.find((animal) => animal.slug === slug) || null;
77+
}

src/features/AnimalCategory/AnimalCategory.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,9 @@ export function getCategoryBySlug(slug: string): AnimalCategory | null {
4040

4141
return categories.find((category) => category.slug === slug) || null;
4242
}
43+
44+
export function getCategoryByName(name: string): AnimalCategory | null {
45+
const categories = getAllCategories();
46+
47+
return categories.find((category) => category.name === name) || null;
48+
}

src/pages/animalDetailsPage/AnimalDetailsPage.tsx

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import React from "react";
22
import { useParams, Link } from "react-router-dom";
33
import { ArrowLeft, MapPin, Ruler, Utensils, Shield, Info } from "lucide-react";
4-
import { getAnimalById, categories } from "../../data/animals";
4+
5+
import { getAnimalBySlug } from "~features/Animal/Animal";
6+
import { getCategoryByName } from "~features/AnimalCategory/AnimalCategory";
57

68
export default function AnimalDetailPage() {
79
const { animalId } = useParams<{ animalId: string }>();
@@ -10,13 +12,13 @@ export default function AnimalDetailPage() {
1012
return <div>Animal not found</div>;
1113
}
1214

13-
const animal = getAnimalById(animalId);
15+
const animal = getAnimalBySlug(animalId);
1416

1517
if (!animal) {
1618
return <div>Animal not found</div>;
1719
}
1820

19-
const category = categories.find((cat) => cat.id === animal.category);
21+
const category = getCategoryByName(animal.category);
2022

2123
const getStatusColor = (status: string) => {
2224
switch (status.toLowerCase()) {
@@ -35,6 +37,7 @@ export default function AnimalDetailPage() {
3537
}
3638
};
3739

40+
console.log(animal, category);
3841
return (
3942
<div className="min-h-screen bg-gray-50">
4043
<div className="max-w-6xl mx-auto px-4 sm:px-6 lg:px-8 py-8">
@@ -45,7 +48,7 @@ export default function AnimalDetailPage() {
4548
</Link>
4649
<span className="text-gray-400">/</span>
4750
<Link
48-
to={`/category/${animal.category}`}
51+
to={`/category/${category?.slug}`}
4952
className="text-teal-600 hover:text-teal-700"
5053
>
5154
{category?.name}

0 commit comments

Comments
 (0)