Currency Converter App in LWC



 



The Currency Converter Project is a robust application built using cutting-edge technologies such as Lightning Web Components (LWC), Service Components, Styling Hooks, API integration with exchange rate APIs, and the seamless utilization of static resources. This project not only showcases the power of LWC for dynamic and modular user interfaces but also highlights the importance of service components, styling hooks for enhanced presentation, and API integration for real-time exchange rate data. With the added sophistication of static resources, this Currency Converter project delivers a comprehensive and responsive solution. This blog post unveils the synergy of these technologies, offering a glimpse into the seamless convergence of design, functionality, and real-time data. 

1. Service Component: A service component in LWC is basically a lightning component without an HTML template. Service Components or Service Libraries can be used for reusable functions, that can be used in other LWC components. 

In this project, I used a service component to display the drop-down list of currency values. Now, let's take a look at how we can import a service component into our LWC component - 

In our JS file we need to import the service component as per the below code:


import { countryCodeList } from 'c/countryCodeList';


In the above line, countryCodeList is the service component that contains the dropdown list values of the currency types.


2. Styling Hooks: Styling hooks is a concept used in LWC to customize the components using custom CSS properties. In this project, I used styling hooks to customize the convert icon used in the app. Using Styling hooks I changed the background color to yellow color refer to the code below:

.custom-icon{
    --slds-c-icon-color-foreground-default: #f9ba49;
    --slds-c-icon-color-background: #f9ba49;
    --slds-c-icon-color-foreground: #0c2451;
    transform: rotate(45deg);
}

In the above code - --slds-c-icon-color classes are used to change the background color of the standard icon.



3. Static Resources: In our app, we are using a custom background image with the help of static resources. You can either use JS to import your static resource into your lightning web component or you can use it in HTML directly. 

In CSS:


background: url(/sfsites/c/resource/currencyConverterAssets/currencyConverterAssets/cur
rencyAppBackground.png);


In JS:


import currencyConverterAssetsfrom '@salesforce/resourceUrl/currencyConverterAssets'


Utilizing static resources minimizes page load times and ensures smoother interactions, contributing to a seamless experience.


4. LWC Directives: A directive serves as a distinctive attribute imparting dynamic functionality to an HTML page. It can be applied to a root <template> tag, a nested <template> tag, or an HTML element like a <p> tag. 

In this project, we used <template if:true> and <template if:false> directives to conditionally render the currency conversion result. Please refer to the code below:

<template if:true={result}>
    <div class="slds-text-align_center">
        <div class="result">
          {amount} {countryFrom} = <span class="result-to">{result} {countryTo}</span>
        </div>  
    </div>
</template>


5. API Integration:  In this project, I am using exchange rate api which provides the ability to get accurate and up-to-date currency conversion data. 
This facet of the project underscored the importance of asynchronous programming, managing errors, and manipulating data when interacting with external APIs. I followed these steps in this integration:

a. Initially, adding the exchange rate api in a trusted URL and add a remote site setting for this URL.

b. Create a async method in JS that will be fired when the Convert button is pressed.

c. This async method constructs the URL by attaching the required parameters for the conversion endpoint and then makes the API call.

d. Convert the response into JSON and do the required calculation to store the result.


6. Exposing our LWC using Experience Sites: I am using the Salesforce LWR to extend the applicability of Lightning Web Components (LWC) beyond internal applications. In this project, I learned the steps involved in exposing LWC to Experience Site, enabling external users to engage with these components. 



Demo video 👇





Please share your thoughts and suggestions in the comments.






Comments

Popular posts from this blog

Display Maps in LWC

Calling Apex method from LWC

Navigation Service in LWC (Lightning Web Components)