Ionic getting Pokemon cards from an API

I'm a full-stack developer from South Africa 🇿🇦. I love writing about JavaScript, HTML and CSS.
Search for a command to run...

I'm a full-stack developer from South Africa 🇿🇦. I love writing about JavaScript, HTML and CSS.
No comments yet. Be the first to comment.
Most of you know me for my consistency, a golden arrow in my blog series. I've written 1000 articles in 1008 days! Almost an article a day, and my honeymoon was the only holiday I ever took. I'm super proud of this achievement; it has been a fantasti...

It's not the first time I'll be talking about community. I think it's an essential aspect of any successful tool. This shows in my previous explorations of Astro, Medusa, and now Vendure as well. All these products thrive in a super open, welcoming, ...

The cool part about Vendure is how easy it is to set up and how abstract each layer is. Basically, we get the following elements: External database Server Worker Admin UI Frontend While this is amazing, it also brings a bit of complexity when it co...

The previous article looked at customizing Vendure on a data and process level. In this article, we'll look at customizing emails, as they are often a big part of a webshop system. We'll be looking at two different layers of customization for customi...

Even though Vendure is a pretty significant project out of the box, in some cases, we might want to go in and modify some elements to work to our specific use case. In this article, I'll take a high-level look at some elements we can customize within...

Like many apps, we want our app to pull data from a specific API. In this case, it's a Pokemon TCG (Trading Card Game) API.
It's a good practice to create a service that will wrap the API, so we will be looking at that today.
The end result will be this list of Pokemon cards when we search for a specific Pokemon name.

Note: Today's article covers a wrapped API, for your casual API's I refer you to my article on showing Ionic API Results.
The API comes as a typescript variant, so we don't need to wrap our calls in an HTTP wrapper.
We do have to add the package to our project.
npm install --save pokemon-tcg-sdk-typescript
This will install the Pokemon TCG API.
Then we want to create our own service where we can implement all our logic for the API calls.
ng g service services/card
That will generate a service inside the shared folder called card.service.ts.
Open up the card.service.ts file and start by importing the PokemonTCG API.
import { PokemonTCG } from 'pokemon-tcg-sdk-typescript'
Now we want to build a function that can search the API for a specific card name.
searchCard(name:string) {
let params:PokemonTCG.IQuery[] = [{name: 'name', value: name}];
return PokemonTCG.Card.where(params);
}
The API comes with something called an IQuery, which we can use to provide a specific search. Then we call the Card API and pass our search query.
So far, we have the Card Service, but it won't do much if we don't have a component talking to it.
Let's modify the tab2.page component since we previously made sure that it was secured for logged in users.
Let's start by defining an empty array of cards.
cards: Card[];
We load the Card type from the API as such:
import { Card } from 'pokemon-tcg-sdk-typescript/dist/sdk';
Then we want to add our own wrapping service in the constructor.
constructor(private cardService: CardService) {}
This will load the Card service we created. You do need to import it still.
import { CardService } from '../services/card.service';
The last thing we need is a function that can search for a specific name of Pokemon.
searchCard(event) {
this.cardService.searchCard(event.srcElement.value).then(cards => {
this.cards = cards;
})
}
This function will receive an event, at which point it will call the cardService and invoke the searchCard method. It will return a list of cards and set the local reference to these cards as a callback.
Now let's look at how we can call this function from our HTML file.
<ion-searchbar (search)="searchCard($event)"></ion-searchbar>
The ion-searchbar is a great way to accomplish this. I used the (search) callback since I didn't want to flood the API with a partial request.
This means the search event will only fire once we click search or enter, instead of doing it on every tick.
Now we need a grid to showcase all our cards.
<ion-grid>
<ion-row>
<ion-col size="4" *ngFor="let card of cards">
<img [src]="card.imageUrl" [title]="card.name">
</ion-col>
</ion-row>
</ion-grid>
This will loop over our cards and add the image to our output. It will make size four columns, which means 4/12, so basically 1/3 of the width.
And that's it if we now search for a Pokemon and click enter, we should see all the cards associated.

You can find today's code on the following GitHub repo.
Thank you for reading my blog. Feel free to subscribe to my email newsletter and connect on Facebook or Twitter