<?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: Karan</title>
    <description>The latest articles on DEV Community by Karan (@karanche97).</description>
    <link>https://dev.to/karanche97</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%2F84696%2F31e20dce-42ef-4b73-bee4-568a8da88a1d.jpg</url>
      <title>DEV Community: Karan</title>
      <link>https://dev.to/karanche97</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/karanche97"/>
    <language>en</language>
    <item>
      <title>Dynamic PDF generation made simple</title>
      <dc:creator>Karan</dc:creator>
      <pubDate>Wed, 20 Sep 2023 11:11:30 +0000</pubDate>
      <link>https://dev.to/karanche97/dynamic-pdf-generation-made-simple-5c5c</link>
      <guid>https://dev.to/karanche97/dynamic-pdf-generation-made-simple-5c5c</guid>
      <description>&lt;p&gt;One major challenge every developer faces is generating reports and sending it as emails, coping up with the client's everchanging requirements over the templates, and data configuration. &lt;/p&gt;

&lt;p&gt;The traditional way of doing it, is by creating a HTML Pug template for the requested PDF and place template placeholders and pass the real-time data as dynamic variables. Most probably we'll end up using Puppeteer or PDFKit. Customizing these templates as the requirement changes is a tedious and repetitive work.&lt;/p&gt;

&lt;p&gt;To tackle these issues, why not ask the business people to design their PDF templates and customize it as per their requirements. As a developer our focus is to prepare data for the reports and call a service that will take care of the PDF Generation. We created an service that does exactly this. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--2C_Xo43b--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/htkstiqgbr5hyvairytr.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--2C_Xo43b--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/htkstiqgbr5hyvairytr.png" alt="How exaclty?" width="374" height="590"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.producthunt.com/posts/pdf-hippo"&gt;PDFHippo&lt;/a&gt; a tool that let you business people design their report just using an Excel and map the cells that's going to render dynamic data using the syntax &lt;code&gt;var:customer_name&lt;/code&gt; and define the cell ranges. &lt;/p&gt;

&lt;p&gt;To render array of data like invoice table we can use the formula&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ErMtEqvz--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yyd2os5y0ix6e8g6yafm.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ErMtEqvz--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yyd2os5y0ix6e8g6yafm.png" alt="table syntax" width="800" height="224"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Upload the excel and save it as a template in PDFHippo and it will give you and unique api end point for the template.&lt;/p&gt;

&lt;p&gt;Prepare the JSON data using your favorite programming language and call the template api. Save the blob and boom... Now you can share it with your clients, send it an email or save it in your bucket. &lt;br&gt;
Best part: None of your data is getting saved in PDFHippo.&lt;/p&gt;

&lt;p&gt;Documentation: &lt;a href="https://pdf-hippo.readme.io/reference/getting-started-with-your-api"&gt;https://pdf-hippo.readme.io/reference/getting-started-with-your-api&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.producthunt.com/posts/pdf-hippo"&gt;Give it a try&lt;/a&gt; and let me know what we can do to build up on this!&lt;/p&gt;

</description>
      <category>pdf</category>
      <category>api</category>
      <category>node</category>
      <category>sass</category>
    </item>
    <item>
      <title>When Promise.race() wasn't racing</title>
      <dc:creator>Karan</dc:creator>
      <pubDate>Sat, 15 Oct 2022 17:16:08 +0000</pubDate>
      <link>https://dev.to/karanche97/when-promiserace-wasnt-racing-n0h</link>
      <guid>https://dev.to/karanche97/when-promiserace-wasnt-racing-n0h</guid>
      <description>&lt;h2&gt;
  
  
  The Problem
&lt;/h2&gt;

&lt;p&gt;A week ago we encountered this weird bug in one of our core APIs. The issue was that it was not returning any response whatsoever. &lt;/p&gt;

&lt;p&gt;Luckily we’d found this issue during staging. My senior and I immediately got to work. For almost an hour we scratched our heads checking all the edge cases, connections, environments and queries. Everything looked perfectly fine on the command line, so why wasn’t this working?&lt;/p&gt;

&lt;p&gt;Annoyed beyond repair, we just decided to take the age old route and decipher each piece of code piece by piece on blocks and queries in the API, until the culprit revealed itself.&lt;/p&gt;

&lt;h3&gt;
  
  
  Promise.race()
&lt;/h3&gt;

&lt;p&gt;We had initialised a simple check to return a response as soon as the validation service found at least one error in the data that we were validating. Pretty simple use case. We used &lt;code&gt;Promise.race()&lt;/code&gt; to achieve this. &lt;code&gt;Promise.race()&lt;/code&gt; works perfectly fine when validating lot of data. Again, pretty straightforward answer. &lt;/p&gt;

&lt;p&gt;EXCEPT! The function only works on certain conditions.&lt;/p&gt;

&lt;h3&gt;
  
  
  The BUG
&lt;/h3&gt;

&lt;p&gt;The data that we needed to be validated was being fetched from another query. Apparently when we were passing this data, it was empty and we were unknowingly passing an empty array into the &lt;code&gt;Promise.race([])&lt;/code&gt; function.&lt;/p&gt;

&lt;p&gt;This will result in something called a &lt;code&gt;foreverPendingPromise&lt;/code&gt;. It’s clearly mentioned in the &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/race#examples"&gt;MDN&lt;/a&gt; about this case, and we’d overlooked it.&lt;/p&gt;

&lt;p&gt;To understand this situation better, let's dig a little deeper on how the function actually works.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--2AM9ps3B--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dqs6deyvmfebnosto2tu.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--2AM9ps3B--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dqs6deyvmfebnosto2tu.png" alt="Image description" width="880" height="658"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This results in  &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--DRjSp8RJ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/13ibr7e6mi6yr57u1jur.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--DRjSp8RJ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/13ibr7e6mi6yr57u1jur.png" alt="Image description" width="796" height="356"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As we see &lt;code&gt;callPromise()&lt;/code&gt; took exactly 5 seconds to execute - the fastest &lt;code&gt;getValue()&lt;/code&gt; promise to resolve. Works totally fine.&lt;/p&gt;

&lt;p&gt;The whole fiasco starts when we pass an empty array. &lt;a href="https://codepen.io/karan_fp/pen/bGMZKWq"&gt; Code pen to Cry &lt;/a&gt; - where the console logs `start' and wait forever for the race to finish.&lt;/p&gt;

&lt;h3&gt;
  
  
  The fix
&lt;/h3&gt;

&lt;p&gt;The simple fix we patched is to run the race only if something in the validation array. It’s a simple problem and a simple solution, but it’s the simplest things that are often neglected. The devil is in the details.&lt;/p&gt;




&lt;p&gt;Editing courtesy &lt;a href="https://twitter.com/ah__sho"&gt;ah__sho&lt;/a&gt;&lt;br&gt;
Code Snippets by &lt;a href="https://carbon.now.sh"&gt;Carbon&lt;/a&gt;&lt;br&gt;
Cover pic by &lt;a href="https://unsplash.com/@austris_a?utm_source=unsplash&amp;amp;utm_medium=referral&amp;amp;utm_content=creditCopyText"&gt;Austris Augusts&lt;/a&gt; on &lt;a href="https://unsplash.com/s/photos/race?utm_source=unsplash&amp;amp;utm_medium=referral&amp;amp;utm_content=creditCopyText"&gt;Unsplash&lt;/a&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>programming</category>
    </item>
    <item>
      <title>Use hook functions in class component: React</title>
      <dc:creator>Karan</dc:creator>
      <pubDate>Fri, 22 Oct 2021 13:02:58 +0000</pubDate>
      <link>https://dev.to/karanche97/use-hook-functions-in-class-component-react-25i5</link>
      <guid>https://dev.to/karanche97/use-hook-functions-in-class-component-react-25i5</guid>
      <description>&lt;p&gt;Converting the legacy class components to functional components is not an easy task if the component is dealing with complex logic.&lt;/p&gt;

&lt;p&gt;There are times when we can implement custom hook functions to achieve a shared functionality like triggering the popup with a message on any component. Eg: Popup will be visible on the  component. But we can't consume the hook directly in a class based component. However there's a workaround to achieve it.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fg5pvbf3m464emf1vydx3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fg5pvbf3m464emf1vydx3.png" alt="HOC with Hooks"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  Step 1:
&lt;/h4&gt;

&lt;p&gt;Create a functional Higher Order Component which accepts the initialState for the hook and a react component as the param.&lt;/p&gt;

&lt;h4&gt;
  
  
  Step 2:
&lt;/h4&gt;

&lt;p&gt;Import the hooks here and pass the hook functions as params to the return component.&lt;/p&gt;

&lt;h4&gt;
  
  
  Step 3:
&lt;/h4&gt;

&lt;p&gt;Wrap the class component with our HOC component. Pass the initial states for the hook as params in the HOC.&lt;/p&gt;

&lt;h4&gt;
  
  
  Result:
&lt;/h4&gt;

&lt;p&gt;Now we can call the hook methods from &lt;code&gt;this.props&lt;/code&gt;.&lt;/p&gt;

&lt;h5&gt;
  
  
  &lt;em&gt;Comments are welcomed!&lt;/em&gt;
&lt;/h5&gt;

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