Introduction
In a world where weather information is a crucial part of our daily lives, the integration of cutting-edge technologies becomes paramount. My latest blog delves into the realm of web development, unveiling a state-of-the-art weather website meticulously crafted using Lightning Web Components (LWC). This responsive website not only provides real-time weather updates using Open Weather API but also revolutionizes the way you interact with temperature data by offering seamless conversion between Fahrenheit and Celsius.
But that's not all. this website also offers a comprehensive dashboard that includes essential weather metrics. From atmospheric pressure and wind speed to the 'feels like' temperature, this website ensures you are equipped with all the information you need. Dive into the details with minimum and maximum temperatures, gaining insights into the day's temperature fluctuations.
Let's dive into the insights gained during the implementation of this exciting project.
Key Concepts
In this project, I have used CSS to design a minimal and responsive design that displays all the essential weather metrics like - Current Temperature, current weather conditions, Minimum Temperature, Maximum Temperature, Feels like, Humidity, Wind, and Pressure.
Weather Header section:
The header section has a form that consists of the search input along with two spans for °C, and °F.
This section also includes a message that pops up when we hit search. If the city entered isn't valid then a message is displayed.
The main challenge in this section was to implement the pop message and temp conversion functionality upon click of C and F.
To display this component we are using conditional rendering, while making the API call to Open Weather API we see the response code and then display a success or error message accordingly.
HTML Code:
.png)
JS Code:
For temperature conversion from Fahrenheit to Celsius and vice versa, I have implemented an On-click method that calls onclickhandler which further calls our fetchData() method that makes the API call.
HTML Code:
JS Code:
Weather Body section:
The body section contains the City name, Country Name, weather condition, weather icon, temperature, minimum, and maximum temperature. By default, the weather info shows weather details of New Delhi.
The main learning point for me in this section was dynamically constructing the URL to show the weather image on the page. For this, I used a property weatherIcon in which I am dynamically constructing the URL using the icon property which I received in the response while making the API call.
HTML Code:
Weather Info section:
In the info section, I am displaying the other key weather metrics like - Feels, humidity, and wind pressure along with the icons.
The main challenge for me was to import the font awesome icons and display them on the UI. I tried to download the zipped file from the
Font Awesome website, upload two CSS files as a static resource, and further import those files into my lightning web component but, it didn't work. I explored more but didn't find a solution then I decided to post my question in the community -
Link to the Question
Further, I decided to download only the icons that I needed for this project, upload them as a static resource, and then use them in my lightning web component. Please find the sample code for one component below:
HTML Code:
API Integration - Open Weather API
In this project, we are integrating with the
Open Weather API to fetch the real-time weather metrics and handle the response to display on our page.
I have added a remote site setting for the weather API and created a trusted URL so that we don't see any issues while making the API call.
In this project, we are making server-side API calls to Open Weather API endpoints. For this purpose, we have developed an Apex controller class which has a method that takes the city and unit as a parameter to make the API call and further returns the response received. I have included another method that fetches the API key value stores in a custom label.
In this integration, I also learned how to use apex methods in our LWC and pass parameters from the LWC to the Apex method.
Apex Class:
JS Code:
The main reason for not using client-side API calling was to make our secret key secure and not expose it to the people who are visiting this page.
Exposing the component using Experience Sites
I have created a website in Salesforce and added a page to it in which I have exposed this component to enable external users to interact with my application without having to log in to Salesforce.
After exposing the website when I tried to fetch the weather I was not able to fetch the details after debugging I noticed that I had missed a major step where I needed to give access to the Apex Class to the custom profile that is created for my website.
Follow the steps below to give access to any apex class for the custom profile:
- Go to builder -> Click on Gear Icon
- In the General tab, you'll find the link for the custom profile created for your website.
- Open it and Click on Enabled Apex class.
- Search and add your apex class.
See this short demo on how to give access.
Weather Info Website - https://neeraj-portfolio-dev-ed.develop.my.site.com/weather-info
Demo 👇
Thank you for giving this a read, Please do share your feedback on the website and let me know if I can add any more functionality to this website.
Comments
Post a Comment