<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Nitin Singh</title>
    <description>The latest articles on DEV Community by Nitin Singh (@nitintwt27).</description>
    <link>https://dev.to/nitintwt27</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1219205%2F8ee47ebc-726b-4cee-b86e-277bc4ca0768.jpg</url>
      <title>DEV Community: Nitin Singh</title>
      <link>https://dev.to/nitintwt27</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/nitintwt27"/>
    <language>en</language>
    <item>
      <title>Bookmyflight : A web app that helps you find cheap flights and book them based on your budget.</title>
      <dc:creator>Nitin Singh</dc:creator>
      <pubDate>Thu, 23 May 2024 20:59:47 +0000</pubDate>
      <link>https://dev.to/nitintwt27/bookmyflight-a-web-app-that-helps-you-find-cheap-flights-and-book-them-based-on-your-budget-9b3</link>
      <guid>https://dev.to/nitintwt27/bookmyflight-a-web-app-that-helps-you-find-cheap-flights-and-book-them-based-on-your-budget-9b3</guid>
      <description>&lt;p&gt;Currently working on a full fledged Flight booking website using Amadeus API. &lt;br&gt;
Progress so far :-&lt;br&gt;
Done with user register , login and logout part. The backend is written in nodejs and express. I have used mongodb for the database. &lt;br&gt;
All the toast messages you see are coming from the server. I have also used JWT , generated access token and refresh token using it, and saved it in user browser cookie. But I don't know how to use this access token or refresh token , like now after login I don't have to call backend again , so how i will get to know when the access token expires??&lt;br&gt;
Will figure it out , after moving deep in the project ( first time using jwt). &lt;br&gt;
On logout , user cookies get deleted.&lt;br&gt;
I have also added Flight search component , I don't know this full white bg looks good or not , will soon improve the bg. First tried gray , but it looked ugly.&lt;br&gt;
I have never used such a large and complicated API before. Every request requires headers and parameters, and I have to generate a new access token whenever the old one expires. It's been a real challenge!&lt;/p&gt;

&lt;p&gt;I want to share some of the problems I faced and how I solved them:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The API doesn’t accept city names directly, instead it takes the city airport code, known as the IATA code. For example, for Delhi, it is DEL. So, when a user types a city name like Ranchi, behind the scenes I had to convert it to the IATA code, IXR. To achieve this, I used another Amadeus API endpoint that fetches this data and stores it in a state variable, which is then passed in the params.
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  const handleFromInputSearch = async (value)=&amp;gt;{
    setFrom(value)
    const headers = {'Authorization' :`Bearer ${accessToken}`}
    const params = {'subType':'CITY,AIRPORT' , 'keyword':`${value}`}
    try {
      const airportData = await axios.get('https://test.api.amadeus.com/v1/reference-data/locations', {headers , params})
      setAirportData(airportData?.data?.data)
      setDepartureAirport(airportData?.data?.data[0]?.iataCode)
      console.log(airportData?.data?.data[0]?.iataCode)
    } catch (error) {
      console.log('Error fetching airport data' , error)
    } 
  }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;The date format required by the API is year-month-day, but the component from which the user selects the date is in month-date-year format. Therefore, I reformatted the date to match the API expectations.
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  const formatDate = (date) =&amp;gt; {
    const year = date?.year
    const month = date?.month &amp;lt; 10 ? `0${date?.month}` : date?.month
    const day = date?.day &amp;lt; 10 ? `0${date?.day}` : date?.day

    return `${year}-${month}-${day}`
  }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;When flight data is fetched from the API, the format of the total travel duration is different, like PT20H55M. This means that the total travel duration is 20 hours and 55 minutes. As you can see, this is not a human readable format. So, I wrote a function to convert this data into a human readable format. I took help from Stackoverflow and Chatgpt because this was a problem I faced for the first time.
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  const formatTotalTravelDuration = (duration) =&amp;gt; {
    // Match the duration string against the regular expression pattern
    const match = duration.match(/PT(\d+H)?(\d+M)?/)

    // Extract hours and minutes from the matched groups
    const hours = match[1] ? match[1].replace('H', '') : '0'
    const minutes = match[2] ? match[2].replace('M', '') : '0'

    // Construct the human-readable format by combining hours and minutes
    return `${hours}h ${minutes}m`.trim()
  }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;There is a similar problem with the flight timing, which is in this format: 2024-05-30T22:15:00. A bit uncomfortable to read, right? I wrote a function to transform this into a human-readable format.
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  const formatTiming = (dateTime) =&amp;gt; {
    // Extract the time part from the datetime string
    const timePart = dateTime.split('T')[1]

    // Match the time part against the regular expression pattern
    const match = timePart.match(/(\d+):(\d+):(\d+)/)

    // Extract hours and minutes from the matched groups
    const hours = match &amp;amp;&amp;amp; match[1] ? match[1] : '0'
    const minutes = match &amp;amp;&amp;amp; match[2] ? match[2] : '0'

    // Construct the human-readable format by combining hours and minutes
    return `${hours}:${minutes}`.trim()
  }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;The airline name you see on the card component is not the way it was sent from the API. I received an airline code from the API, like for Air India, which is AI. So, how will the user know which airline it is? To resolve this issue, I used another Amadeus API endpoint that provides the airline name according to the airline code sent in the parameter.
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  useEffect(() =&amp;gt; {
    const fetchData = async () =&amp;gt; {
      try {
        const headers = { 'Authorization': `Bearer ${accessToken}` };
        const params = { 'airlineCodes': `${airLine}` };
        const response = await axios.get('https://test.api.amadeus.com/v1/reference-data/airlines?airlineCodes', { params, headers });
        setAirlineName(response?.data?.data[0]?.businessName);
      } catch (error) {
        console.log("error fetching airlines name ", error);
      }
    };

    fetchData();
  }, [airLine]);

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So many more features to add, optimization is needed, improvement in UI, and many more things. My main goal in choosing this project was to get good hands on experience with a large and complex API, and luckily, there is not a single tutorial, YouTube video, or blog for this API. So, everything you see I have done by reading docs and using the hit and trial method. You wouldn't believe how many many many times I tried to generate an access token perfectly.&lt;/p&gt;

&lt;p&gt;Another challenging thing I can think of now is the payment for booking, like how the payment will be executed and confirmed.&lt;/p&gt;

&lt;p&gt;Github repo : &lt;a href="https://github.com/nitintwt/bookmyflight"&gt;https://github.com/nitintwt/bookmyflight&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Let's see how it goes.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>react</category>
      <category>node</category>
      <category>express</category>
    </item>
    <item>
      <title>Client Side Rendering VS Server Side Rendering</title>
      <dc:creator>Nitin Singh</dc:creator>
      <pubDate>Thu, 08 Feb 2024 19:50:03 +0000</pubDate>
      <link>https://dev.to/nitintwt27/client-side-rendering-vs-server-side-rendering-5b4j</link>
      <guid>https://dev.to/nitintwt27/client-side-rendering-vs-server-side-rendering-5b4j</guid>
      <description>&lt;p&gt;SSR vs CSR :-&lt;/p&gt;

&lt;p&gt;Our browser only understands HTML, CSS, and JavaScript. All the code we write, all the frameworks, libraries, and everything else, are ultimately converted into HTML, CSS, and JavaScript because that is what our browser can understand.&lt;/p&gt;

&lt;p&gt;Now, let's examine client-side rendering. When we deploy our website code, it is saved on a server. Whenever we request a web page, the server sends the entire code bundle, which includes HTML, CSS, and JavaScript.&lt;br&gt;
This code bundle is then rendered in our own browser, and the page is shown to us. The gap between the request and the rendered page is a few milliseconds or sometimes even seconds. This delay is not great for SEO.&lt;/p&gt;

&lt;p&gt;On the other hand, in Server Side Rendering, when we request a web page, the entire code is first rendered on the server, and then , only HTML and sometimes a few JavaScript codes are sent to our browser. This HTML code is instantly shown on our screen,eliminating the delay between the request and the rendered page.&lt;/p&gt;

&lt;p&gt;What's cool with Next.js is that you choose which part of your app renders on the server side and which on the client side. By default, everything is server rendered.If you want something on the client side, just write "use client" at the top of that file.&lt;/p&gt;

&lt;p&gt;Here's a quick breakdown of server and client-side components:&lt;br&gt;
Server:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Fetching data&lt;/li&gt;
&lt;li&gt;Accessing the backend&lt;/li&gt;
&lt;li&gt;API access, etc.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Client:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;In actions like onClick, onChange, useState, useReducer, useEffect.&lt;/li&gt;
&lt;li&gt;Involves browser-only APIs, hooks, React class components.&lt;/li&gt;
&lt;/ol&gt;

</description>
    </item>
    <item>
      <title>Explore React Navigation: A Guide to React Router</title>
      <dc:creator>Nitin Singh</dc:creator>
      <pubDate>Mon, 01 Jan 2024 18:48:58 +0000</pubDate>
      <link>https://dev.to/nitintwt27/explore-react-navigation-a-guide-to-react-router-1p2g</link>
      <guid>https://dev.to/nitintwt27/explore-react-navigation-a-guide-to-react-router-1p2g</guid>
      <description>&lt;p&gt;Why should you use React Router?&lt;br&gt;
Check it out!&lt;/p&gt;

&lt;p&gt;First, let's understand some terminology...&lt;/p&gt;

&lt;p&gt;Routes: These are defined paths (URLs) that lead us to the components of your app, such as "/home," "/about," and "/login."&lt;/p&gt;

&lt;p&gt;Router: It helps in matching the requested URL with the defined routes and renders the appropriate contents of the web page.&lt;/p&gt;

&lt;p&gt;React Router helps users navigate through different pages of the web without reloading the entire page.&lt;/p&gt;

&lt;p&gt;Your app is like home, components are your rooms, and React Router is like a map of your house, helping you navigate your house without moving you out of it.&lt;/p&gt;

&lt;p&gt;React Router has two special components for navigation.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Link: The &lt;code&gt;Link&lt;/code&gt; component is a basic navigation component that creates a hyperlink to navigate to a specific route.&lt;/li&gt;
&lt;li&gt;NavLink: It is an extension of &lt;code&gt;Link&lt;/code&gt; that provides a special feature called "active," allowing us to check whether the page is active or not using the &lt;code&gt;isActive&lt;/code&gt; prop. You can style it accordingly.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;For example, isActive will check if you are on the "/about” page. If you are on that page, it will turn orange; otherwise, it will change to gray.&lt;br&gt;
&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3otjdq08ddu3g7t58zu6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3otjdq08ddu3g7t58zu6.png" alt="Image description" width="800" height="163"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>react</category>
      <category>learning</category>
      <category>frontend</category>
    </item>
    <item>
      <title>Difference between Scripting and Programming Language.</title>
      <dc:creator>Nitin Singh</dc:creator>
      <pubDate>Sat, 30 Dec 2023 19:57:24 +0000</pubDate>
      <link>https://dev.to/nitintwt27/difference-between-scripting-and-programming-language-1d4k</link>
      <guid>https://dev.to/nitintwt27/difference-between-scripting-and-programming-language-1d4k</guid>
      <description>&lt;p&gt;I was going through important JS interview questions, and came across a topic , Difference between scripting and programming language.&lt;/p&gt;

&lt;p&gt;Let's have a look into it :-&lt;/p&gt;

&lt;p&gt;Before comparing we have to know about how your code gets executed:-&lt;br&gt;
Our source code is written using  human language that computers cannot understand, so this source code is converted into bytecode, which the computer can understand.&lt;/p&gt;

&lt;p&gt;In between this conversion, there comes either of the two things: a compiler or an interpreter.&lt;br&gt;
A compiler first optimize the entire code(source code to bytecode), and then it gets executed.&lt;br&gt;
But in an interpreter, the code will run line by line, and at the same time, the lines will be converted into bytecode.&lt;/p&gt;

&lt;p&gt;Now, the main difference between a programming and a scripting language lies in the use of a compiler or an interpreter.&lt;/p&gt;

&lt;p&gt;A scripting language uses an interpreter for source code conversion, whereas a programming language uses a compiler. Every scripting language is a programming language, but not all programming languages are scripting languages.&lt;/p&gt;

&lt;p&gt;Example:-&lt;br&gt;
Scripting language: JavaScript, Python&lt;br&gt;
Programming language: C, C++ , Java&lt;/p&gt;

&lt;p&gt;But,but,but , here comes JIT compilation into play.&lt;br&gt;
What is JIT??&lt;/p&gt;

&lt;p&gt;Just in time compilation  was added to JavaScript to improve code execution. The V8 JavaScript engine introduced it for the first time. So how does this work, and why was there a need for a it? &lt;br&gt;
The use of interpreters in JavaScript made code execution slow because the code is executed line by line while converting it into bytecode.&lt;br&gt;
However, a JIT compiler converts the JavaScript code into bytecode just before it is executed, this makes the execution faster. In simple words:- JIT compilation=Interpreter + Compiler.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>programming</category>
      <category>learning</category>
    </item>
    <item>
      <title>Basic of useState hook in react.</title>
      <dc:creator>Nitin Singh</dc:creator>
      <pubDate>Sat, 30 Dec 2023 19:56:37 +0000</pubDate>
      <link>https://dev.to/nitintwt27/basic-of-usestate-hook-in-react-1j5g</link>
      <guid>https://dev.to/nitintwt27/basic-of-usestate-hook-in-react-1j5g</guid>
      <description>&lt;p&gt;The useState hook is used to update and display data on the web page. If a component has a value that is changeable , we use the useState hook to handle the change.&lt;/p&gt;

&lt;p&gt;useState has a state variable and a function to update that variable.&lt;br&gt;
The state variable holds some data of the component that can be changed.&lt;br&gt;
The function is used to update the variable.&lt;/p&gt;

&lt;p&gt;Example: const [count , setCount] = useState(1);&lt;br&gt;
Here count is a variable that holds some data and setCount is a function that will update the variable(count) when needed. 1 is the default value we have given to the count.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>react</category>
      <category>learning</category>
    </item>
    <item>
      <title>Why we use useCallback hook in react ?</title>
      <dc:creator>Nitin Singh</dc:creator>
      <pubDate>Sat, 30 Dec 2023 19:55:49 +0000</pubDate>
      <link>https://dev.to/nitintwt27/why-we-use-usecallback-hook-in-react--5d76</link>
      <guid>https://dev.to/nitintwt27/why-we-use-usecallback-hook-in-react--5d76</guid>
      <description>&lt;p&gt;It took me a while to understand the purpose of the useCallback hook.&lt;/p&gt;

&lt;p&gt;Let's take an example: imagine you're making your favorite dish, and you're using your favorite recipe for it. Now, whenever you make your favorite dish, you don't want to think about and rewrite the entire recipe each time. Instead, you make a photocopy of your favorite recipe and use it whenever needed. You only update the copy when you want to improve your recipe.&lt;/p&gt;

&lt;p&gt;In React, components have functions, which are similar to the recipe for your food. These functions get recreated whenever the component is re-rendered, much like writing your recipe every time you make your favorite dish. This can make the process time-consuming.&lt;/p&gt;

&lt;p&gt;useCallback(callback, [dependencies])&lt;/p&gt;

&lt;p&gt;Here, the callback is a function that only gets recreated when there is a change in the dependencies. Otherwise, it memoize the function data if the function dependencies are not changed.&lt;/p&gt;

&lt;p&gt;The main benefit of the useCallback hook is that it helps in avoiding unnecessary re-renders. This, in turn, aids in optimizing the performance of the webpage.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>react</category>
      <category>learning</category>
    </item>
    <item>
      <title>Context API: What and Why?</title>
      <dc:creator>Nitin Singh</dc:creator>
      <pubDate>Sat, 30 Dec 2023 19:54:36 +0000</pubDate>
      <link>https://dev.to/nitintwt27/context-api-what-and-why-544l</link>
      <guid>https://dev.to/nitintwt27/context-api-what-and-why-544l</guid>
      <description>&lt;p&gt;Wanted to share my learning related to Context API.&lt;/p&gt;

&lt;p&gt;Before going into what  Context API is, we need to understand why there was a need for it.&lt;/p&gt;

&lt;p&gt;Let's take an example:&lt;/p&gt;

&lt;p&gt;Imagine an web app with a homepage featuring a dashboard. The dashboard is divided into two parts: the left and right sides. The right side further divides into an upper and lower part, with a card component  in the upper part.&lt;/p&gt;

&lt;p&gt;In the card component, we require a title. Let's assume we are fetching data from an API, and the title comes from this API. The title, initially received from the API, first pass to the App component, then moves to the dashboard component, followed by the right side, and then progresses to the upper side component. Finally, it reaches the card component.&lt;/p&gt;

&lt;p&gt;In this process, the data is passed through different components to reach its original destination. All these data pass-on are inefficient and not an optimized way of handling this.&lt;/p&gt;

&lt;p&gt;To address this issue, we can create a global object that stores all the necessary data or the fetched data required by different components of the app.&lt;/p&gt;

&lt;p&gt;This is where the Context API comes into play.&lt;/p&gt;

&lt;p&gt;Think of the Context API as a global object where different data or Fetched data can be stored, and this data can then be directly accessed by specific components.&lt;/p&gt;

&lt;p&gt;Using the Context API can make handling data and managing states in React apps simpler. This is especially helpful when different parts of your app need to use the same data.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>react</category>
      <category>webdev</category>
      <category>learning</category>
    </item>
  </channel>
</rss>
