I created a weather app with Lightning Web Components, to avoid looking out the window
Hey! I am a nerd. I enjoy finding new ways of building some new developments, and I have to confess. This hobby is taking me some time. So, to avoid looking out the window and keep focus, I needed this app, and I am really proud of this one. Every time I connect to Salesforce and I am on my Home page, I can see what is the weather like, without moving from my chair, or asking Google(even this one is not really an argument: to get some information about the weather, my LWC has to ask to an API, so it’s the same…the difference is that, with my development, I’ve automated all this process).
So, if you want to see how I did, keep reading!
Step 0: Before we begin this development
Before we begin, we have to choose an API and authorize an endpoint. We’ve chosen an API called weatherapi and created a remote site setting for the occasion(to tell Salesforce that this connexion is legit).
The next thing to do is to create a custom metadata type called Key_Weather_API__mdt. We add a custom field of type text, Key__c. Then, we can create our record on this custom metadata type, which will store our token.
To get a token, we have to go to https://www.weatherapi.com/my/. Normally, you should see a menu asking you to create a new token. When we got the token, you copy it. After that, you can create a new Key_Weather_API__mdt record, and paste the token to it.
Now that everything is in place, we can begin our Lightning Web Component!
Step 1: HTML
Here, the lightning layouts and lightning layout items allow me to get some columns. I need four(Location, Temperature(°C), Weather Conditions, and Weather Icon). I still used slds to improve the render, and I also used two lines of CSS to describe how I want the text to be displayed. It could have been used directly inside the html template, but I find it more convenient to also have an external CSS file when slds is not sufficient. But it’s up to you. Concerning the icons, I called them from Lightning Design System. I will add the links at the end of this article.
Step 2: JavaScript
Two steps here: 1)We get the current location of the user(the user will have to allow the geolocation on his computer if he wants this to work), and store it to two variables(one for longitude, one for latitude), and 2)We call Apex with these two. In return, our Apex method gives us the weather information we want to display on the screen.
Step 3: Apex
When we think about calling an external web service, we got to think about two things: how to authorize the endpoint, and where to store the credentials. For this step, we have the choice between using a named credential to handle the whole operation or using a remote site setting with a custom metadata type(to store the credentials). I chose to use the latter because, in my mind, both are secure. The thing to avoid is having some confidential information on the code, which is not the case here.
I also didn’t use it, but you can use Postman when you need to test API callouts before beginning the developments, it’s pretty useful, knowing that each API has its own specificities.
You also have to know here that, even if we return a Map<String, String> from this method, the received data into the LWC will be an object.