Skip to content

Commit a9c4278

Browse files
WIP making strongly-typed classes/interfaces for the data received from the Pet APIs
1 parent 5154a71 commit a9c4278

File tree

4 files changed

+57
-24
lines changed

4 files changed

+57
-24
lines changed

CatApiData.png

337 KB
Loading

DogApiData.png

137 KB
Loading

src/others/Globals.ts

Lines changed: 50 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,54 @@ export const ROUTE_URL = {
99

1010
export enum PET_API_TYPE { DOG, CAT }
1111

12-
export interface PetApiData {
12+
export abstract class BreedData { }
13+
14+
export class DogBreedData extends BreedData {
15+
breed: string
16+
// bredFor: string[]
17+
heightRangeCM: number[]
18+
lifespanRange: number[]
19+
// temperament: string[]
20+
// weightKG: number[]
21+
22+
constructor(breedsObj: any) {
23+
super()
24+
this.breed = breedsObj.breed
25+
// this.bredFor = breedsObj.bred_for.split(",").map((s: string) => s.trim())
26+
this.heightRangeCM = breedsObj.height.metric.split("-").map((h: string) => parseInt(h))
27+
this.lifespanRange = breedsObj.life_span.split("-").map((ls: string) => parseInt(ls))
28+
}
29+
}
30+
31+
export class CatBreedData extends BreedData {
32+
constructor(breedsObj: any) {
33+
super()
34+
}
35+
}
36+
37+
export class PetApiData {
1338
id: number
1439
imgURL: string
15-
breeds: any
16-
// breeds: {
17-
// name: string
18-
// heightCM: number
19-
// temperament: string[]
20-
// weightKG: number
21-
// }
22-
40+
breedData: BreedData | null
2341
height: number
2442
width: number
2543
apiType: PET_API_TYPE
44+
45+
constructor(id: number, imgURL: string, breedData: any, height: number, width: number, apiType: PET_API_TYPE) {
46+
this.id = id
47+
this.imgURL = imgURL
48+
this.breedData = null
49+
this.height = height
50+
this.width = width
51+
this.apiType = apiType
52+
53+
if (apiType === PET_API_TYPE.CAT) {
54+
this.breedData = new CatBreedData(breedData)
55+
}
56+
else if (apiType === PET_API_TYPE.DOG) {
57+
this.breedData = new DogBreedData(breedData)
58+
}
59+
}
2660
}
2761

2862
export const Utils = {
@@ -51,14 +85,13 @@ export const Utils = {
5185
return new Date(randomTimestamp);
5286
},
5387
convertToPetApiData(obj: any, apiType: PET_API_TYPE): PetApiData {
54-
return {
55-
id: obj.id,
56-
imgURL: obj.url,
57-
breeds: obj.breeds,
58-
height: obj.height,
59-
width: obj.width,
60-
apiType: apiType
61-
} as PetApiData
88+
return new PetApiData(
89+
obj.id, obj.url,
90+
apiType === PET_API_TYPE.DOG ?
91+
new DogBreedData(obj.breeds[0]) :
92+
new CatBreedData(obj.breeds[0]),
93+
obj.height, obj.width, apiType
94+
)
6295
},
6396
durstenfeldShuffle(arr: any[]): void {
6497
for (var i = arr.length - 1; i >= 0; i--) {

src/pages/AdoptionGalleryPage.tsx

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,14 @@ const AdoptionGalleryPage: React.FC = () => {
3333
.filter((petData: PetApiData) => !petData.imgURL.endsWith('.gif'))
3434
]
3535
Utils.durstenfeldShuffle(allPetData)
36-
// console.log("AdoptionGalleryPage - All pet data: ", allPetData)
36+
console.log("All pet data: ", allPetData)
3737
setPetData(allPetData)
3838
}
3939
catch (err: any) {
4040
console.error("Error fetching from TheCatAPI: ", err.message)
4141
}
4242
}
43-
fetchPetApiData(15)
43+
fetchPetApiData(6)
4444
}, [])
4545

4646
return (
@@ -55,11 +55,11 @@ const AdoptionGalleryPage: React.FC = () => {
5555
<div className='text-center rounded-full px-6 py-3 bg-gray-300'>Le Option</div>
5656
</section>
5757
<section className='min-h-96 flex justify-center items-center'>
58-
<div className='max-w-3xl columns-2 sm:columns-3 2xl:columns-4'>
59-
{
60-
m_petData.map(data => <PetInfoCard key={data.id} petApiData={data} />)
61-
}
62-
</div>
58+
<div className='max-w-3xl columns-2 sm:columns-3 2xl:columns-4'>{
59+
m_petData.map(data => {
60+
return <PetInfoCard key={data.id} petApiData={data} />
61+
})
62+
}</div>
6363
</section>
6464
<FooterSection />
6565
</main>

0 commit comments

Comments
 (0)