<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Anjali Verma on Medium]]></title>
        <description><![CDATA[Stories by Anjali Verma on Medium]]></description>
        <link>https://medium.com/@anjaliverma471?source=rss-c1885a01df62------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*ZQbIeJamTa822ZNe4z0HNw.jpeg</url>
            <title>Stories by Anjali Verma on Medium</title>
            <link>https://medium.com/@anjaliverma471?source=rss-c1885a01df62------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 29 Nov 2026 00:06:16 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@anjaliverma471/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Guide to custom Angular Schematics]]></title>
            <link>https://anjaliverma471.medium.com/guide-to-custom-angular-schematics-c10f9541c289?source=rss-c1885a01df62------2</link>
            <guid isPermaLink="false">https://medium.com/p/c10f9541c289</guid>
            <category><![CDATA[angular-schematics]]></category>
            <category><![CDATA[typescript]]></category>
            <category><![CDATA[angular]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Anjali Verma]]></dc:creator>
            <pubDate>Wed, 24 Mar 2021 06:03:03 GMT</pubDate>
            <atom:updated>2021-03-24T09:40:09.978Z</atom:updated>
            <content:encoded><![CDATA[<p>When creating a new Angular project, how many times have you thought of copying files and folders for an initial setup of your Angular project? I’ve done it a million times (okay, not million, but you get the point, right?).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/318/1*_ITishvhzVE6zB317qE6-g.png" /></figure><h4>Rationale behind using Schematics</h4><p>Be it a personal project or an enterprise project, I always wish I could use my own template project to get a great kick off. I wanted to have my core module, graphQL module, all the Apollo dependencies automatically installed, ESLint, prettier and husky hooks configured and etc etc.. every time I started my project. BUTT, I always had to copy paste the exact folders and files every time. And I recently stumbled upon the concept of Schematics and it has made the start of the new project 2x faster.</p><h4>What the heck is Schematics, after all?</h4><blockquote>A schematic is a template-based code generator that supports complex logic. It is a set of instructions for transforming a software project by generating or modifying code. Schematics are packaged into <a href="https://angular.io/guide/glossary#collection">collections</a> and installed with npm.</blockquote><p>Basically, we all know about ng new project-name command, right? This is a schematics which helps us in creating our Angular project, which adds all the files required to build an Angular project. Now, suppose you want to create your own command so that helps you add all the folders and dependencies that are required for you in every new project you create.</p><p>The schematic collection can be a powerful tool for creating, modifying, and maintaining any software project, but is particularly useful for customizing Angular projects to suit the particular needs of your own organization.</p><h4>Creating a new Schematic</h4><p>To create a new schematics project we’re going to use an NPM package called schematics. You’ll need to install it globally, just like the Angular CLI.</p><pre>npm install -g @angular-devkit/schematics-cli</pre><p>Now, we can use it to create a new schematic project, just like we use the Angular CLI to create a new app.</p><pre>schematics blank --name=angular-schematics</pre><p>The blank parameter is used to create a project with minimal files.</p><p>Main files that we need to know about:-</p><ul><li>collection.json is like an index of the schematics in this project. It links the name of each schematic with the code that runs it.</li><li>sample-schematics-project/index.ts is the code run by our schematic.</li></ul><p>Because everything is in TypeScript, we need to build it before we run it. We can do that by running `npm run build`. Running the build script manually can get quite tedious. Instead, we can get the TypeScript compiler to watch for changes and build automatically, by adding a watch script to package.json</p><pre>&quot;scripts&quot;: {<br>    &quot;build&quot;: &quot;tsc -p tsconfig.json&quot;,<br>    &quot;watch&quot;: &quot;tsc -p tsconfig.json --watch&quot;,<br>   <br>  }</pre><h4>Getting User Input</h4><p>We’ve talked about how to create a dummy schematic, which doesn’t really do anything, let’s see how can e get user inputs and how can we create a dummy project.</p><p>We’ve to tell schema.json that our schematic is expecting an input, for example in the below code we’ve added name as one of the properties and it’s data type as string.</p><p><strong><em>Allowed Data Types :-</em></strong></p><ol><li><em>String</em></li><li><em>Number</em></li><li><em>Boolean</em></li><li><em>enum</em></li></ol><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/57df7889cb3a4db11f8955815b410306/href">https://medium.com/media/57df7889cb3a4db11f8955815b410306/href</a></iframe><p>The x-prompt value is the prompt that the schematic will use to ask the user for a value, if they don’t supply one. The default object allows you to provide a default value. In this case, the default value will be the first argument the user passes in on the command line.</p><h4>Calling “ng new” Schematic</h4><p>Now, let’s talk about how can we create an Angular project using a schematic. We’re aware of ng new command, we’ll see how can we make our schematic call an external schematic.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/583677b01a78e68dc763ab62d62f626b/href">https://medium.com/media/583677b01a78e68dc763ab62d62f626b/href</a></iframe><p>The first thing we want to do is call the ng new schematic, passing in our name option, and a few other default options. To do that, we’re finally going to write some TypeScript!</p><p>We have a function, angularSchematic, which is going to get called when the schematic runs. It’s going to get passed in _options, which we can use to get the name. It’s going to return a function, which takes a Tree and a SchematicContext, and returns a new Tree. A function like this is referred to as a Rule. A function which returns a Rule is a rule factory.</p><p>A Tree is a fundamental concept in Schematics. It refers to the schematic’s internal representation of the file system. For those familiar with React, it’s kind of like a virtual DOM, but for the file system. When we make changes, they’re applied to the Tree, rather than the actual files. Once we’ve finished with all our changes, the Tree is written to the file system (or not, if we’re in debug mode).</p><h4>Calling a custom Schematic and creating custom rules</h4><p>Now let’s suppose we want that whenever we use this schematic we add certain files. For that, firstly we need to put all the files and folders in our project and you can also add placeholders <em>inside</em> files, using &lt;%= optionName %&gt;.</p><p>Ok, once we’ve got our files, we need a way to add them to the Tree. To do this, we can use another Rule factory, called mergeWith. To use mergeWith, we need to pass in a templateSource and a MergeStrategy.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/60481406d07576cc378698dcb00978a8/href">https://medium.com/media/60481406d07576cc378698dcb00978a8/href</a></iframe><p>&#39;./files&#39; is the location of the files we want to include, _options is the options passed into our schematic (which will be used by the placeholders), and strings is a collection of utility functions (like camelize and classify) provided by schematics. You can import string from @angular-devkit/core.</p><p>Once we’ve got our templateSource, we can pass it into the mergeWith factory along with a MergeStrategy.</p><p>const merged = mergeWith(templateSource, MergeStrategy.Overwrite)</p><p>MergeStrategy.Overwrite means that if the Tree contains a file that’s also in our templateSource, then use the one in our templateSource.</p><p>Ok, so now we have two rules — one returned by mergeWith, and one from externalSchematic. We need to apply both of them to our tree, but we can only return a single rule. So we need some way to combine the two.</p><p>Chain takes in a list of Rule factories, and returns a factory that combines them all.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d2bf4b5afc5d741955639763ab5722d1/href">https://medium.com/media/d2bf4b5afc5d741955639763ab5722d1/href</a></iframe><h4>Update package.json</h4><p>Now if we want to add a few dependencies to our project upfront we can easily do that with our schematics.</p><p>To do this, we’re going to write our own Rule factory, called updatePackageJson. This factory will need to be passed in the name of the project, and will return a Rule. The next thing we need to do is read in the current package.json file. We can use tree.read(path) to fetch the contents of the file as a buffer, then, because we’re dealing with a JSON file, we can use JSON.parse() to parse it.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9fd01c15e3c05f60de68715df4261850/href">https://medium.com/media/9fd01c15e3c05f60de68715df4261850/href</a></iframe><p>Now that we’ve parsed our package.json file, we can manipulate it just like any other object.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e4517d4d83a1865e5964354f9a5f055f/href">https://medium.com/media/e4517d4d83a1865e5964354f9a5f055f/href</a></iframe><h4>Run the Schematic</h4><pre>schematics ./new-project/src/collection.json:angular-schematic new-app</pre><p>This will create a new Angular application with name new-app.</p><h4>Publish our Schematic</h4><p>Now, once we have created our schematic we can publish it using npm publish and everyone can start using it.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c10f9541c289" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Reusable Directive For Authorisation in Angular, no more *ngIf!!]]></title>
            <link>https://anjaliverma471.medium.com/reusable-directive-for-authorisation-in-angular-no-more-ngif-ea487e8c68e4?source=rss-c1885a01df62------2</link>
            <guid isPermaLink="false">https://medium.com/p/ea487e8c68e4</guid>
            <category><![CDATA[angular]]></category>
            <category><![CDATA[directives]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Anjali Verma]]></dc:creator>
            <pubDate>Wed, 10 Feb 2021 06:51:09 GMT</pubDate>
            <atom:updated>2021-02-10T08:49:07.210Z</atom:updated>
            <content:encoded><![CDATA[<h3>Reusable Directive For Authorisation in Angular, avoid *ngIf!</h3><p>In this article I’d be talking about how to create a Reusable Directive in Angular for Authorisation purposes.</p><p>One of the most common requirements that I see in UI designs is the role based access control. Controlling the display of HTML components on the basis of roles and permissions fetched from the backend.</p><p>How many times have we used *ngIf directive with a really long conditional statement on a HTML Element to show/hide it from the view?</p><p>I’d be showing you why and how you can avoid using *ngIf directive.</p><h4><strong>User Permissions</strong></h4><p>First step is to fetch user Permission from a backend API, for now we can declare a static object in our service. Permission is basically an array of strings.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/043f06c042c46758e56e28b65cf1483b/href">https://medium.com/media/043f06c042c46758e56e28b65cf1483b/href</a></iframe><p>Now, create a permission-service.ts which allows us to set a new user and to subscribe to changes on our authenticated application user.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ef26335ba6d95fc4f537f38a60fbea65/href">https://medium.com/media/ef26335ba6d95fc4f537f38a60fbea65/href</a></iframe><h4>We need *ngIf, right?</h4><p>We can now create a directive that takes the permissions of the currently authenticated user and shows/hides them appropriately. In practice, a custom<strong> </strong><strong>*ngIf</strong> directive, *ngIf has a * in front, which is a special syntax within Angular, denoting a so-called <strong>structural directive</strong>.</p><p>Normally, what we do is apply *ngIf on the HTML component to show/hide it on the basis of a certain condition, for example —</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f428ba830c4814f9472599c1fa29b507/href">https://medium.com/media/f428ba830c4814f9472599c1fa29b507/href</a></iframe><p>But now let’s suppose you have 100 other HTML components with completely different conditions, you’d have to take care of the putting this long conditional for each of the component, which becomes tedious eventually.</p><h4><strong>What is a Directive?</strong></h4><blockquote>An Attribute directive changes the appearance or behavior of a DOM element.</blockquote><p>In layman terms, it’s a a piece of code that you add on your HTML components to run a reusable piece of code</p><p>There are three kinds of directives in Angular:</p><ol><li>Components — directives with a template.</li><li>Structural directives — change the DOM layout by adding and removing DOM elements.</li><li>Attribute directives — change the appearance or behavior of an element, component, or another directive.</li></ol><h4>How custom directive helps?</h4><p>We can create a custom directive which would basically handle all the conditional and business logic instead of putting logic in *ngIf , it definitely improves the readability and maintainability of the code.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/56da78754b4ad1ed0a249c95ec83b742/href">https://medium.com/media/56da78754b4ad1ed0a249c95ec83b742/href</a></iframe><p>In this directive we are passing the accepted values from the HTML component to the directive as an @Input. variable which then gets passed to the decideView() function to put decide business logic.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/68d5597a294ddf78ffbd40e2a4c72ebe/href">https://medium.com/media/68d5597a294ddf78ffbd40e2a4c72ebe/href</a></iframe><p>See, how we shifted responsibility of the business logic from *ngIf to a custom directive. Every time we just have to pass a list of accepted permissions on HTML component.</p><p><strong>To Disable the component</strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0922e41aa1ce1534b6951a3faabe00e2/href">https://medium.com/media/0922e41aa1ce1534b6951a3faabe00e2/href</a></iframe><p>Here, we are using <a href="https://angular.io/api/core/ViewContainerRef">ViewContainerRef</a> to create a new Embedded View by using the <a href="https://angular.io/api/core/TemplateRef">templateRef</a> instance that the directive gets and we are adding to the current view. After that, we can change attributes and properties of the HTML Element and all the changes will be visible to you. Here, we are setting the disabled property to true to disable the component.</p><p><strong>To Enable the component</strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/18c103eb4aa1a57ea5e012da4f4da7f3/href">https://medium.com/media/18c103eb4aa1a57ea5e012da4f4da7f3/href</a></iframe><p>Here, we are doing the same steps but just setting the disabled property to false . Also, make sure to always clear the Container using this.viewContainerRef.clear() before creating the new element to avoid having to HTML elements.</p><p>You can find all the code here,</p><p><a href="https://github.com/anjy-07/rbac-directives.git">anjy-07/rbac-directives</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ea487e8c68e4" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Subjects in RxJS]]></title>
            <link>https://anjaliverma471.medium.com/subjects-in-rxjs-2762a87fa10?source=rss-c1885a01df62------2</link>
            <guid isPermaLink="false">https://medium.com/p/2762a87fa10</guid>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[angular]]></category>
            <category><![CDATA[reactive-programming]]></category>
            <category><![CDATA[observer-pattern]]></category>
            <category><![CDATA[rxjs]]></category>
            <dc:creator><![CDATA[Anjali Verma]]></dc:creator>
            <pubDate>Sun, 26 Jul 2020 18:39:39 GMT</pubDate>
            <atom:updated>2020-07-28T04:20:03.026Z</atom:updated>
            <content:encoded><![CDATA[<p>What are they, after all? When to use what!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*BP3lWPnH8MlWUqp9Qnc_cQ.jpeg" /></figure><p>I have been using Subjects in reactive programming. But, I have always faced difficulty in understanding when to use what type of subject. In this article I’d be going through the types of subjects.</p><p>First of all, <a href="https://rxjs-dev.firebaseapp.com/">RxJS</a> is a framework for reactive programming that makes use of <a href="https://rxjs-dev.firebaseapp.com/guide/observable">Observables</a>, making it really easy to write asynchronous code.</p><h4>What is an Observable?</h4><p>In a nutshell, Observable is a function which returns a stream of values to an observer over time. Observer reacts to whatever values an Observable emits. It implements the <a href="https://medium.com/datadriveninvestor/design-patterns-a-quick-guide-to-observer-pattern-d0622145d6c2">Observer Design Pattern</a>.</p><p><strong><em>Observer Design Pattern:</em></strong><em> </em>A publisher publishes data and the consumer subscribes to the publisher to receive the data and unsubscribes to stop receiving that data. Publisher doesn’t know who you are and how you are subscribing, it just delivers the value to you because you are a subscriber (loose coupling).</p><h4>What is an Observer?</h4><p>An Observer is the consumer of the values emitted by the Observable. It is an object with 3 callback methods next(value), complete()and error(e). Observable will call Observer’s next(value)method to emit data.</p><h4>What is a Subject?</h4><p>Subjects are a special kind of Observables. They can be Observables and Observers both. If we’re using Subject as Observable, then to emit values we can use next(value). Subjects are similar to EventEmitters; they just emit the same data to all its subscribers and maintain a registry of many listeners.</p><p>Basic usage of a Subject is to get the value from the publisher and give it to all of its consumers.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/df55ef0d5462eb341800251f78e66f24/href">https://medium.com/media/df55ef0d5462eb341800251f78e66f24/href</a></iframe><p>Here, we created a Subject and attached two observers to it and we are feeding some values to the Subject. On emitting the first value both the subscribers receive at the same time.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7ce1106673b1f18669d919db21a58ba1/href">https://medium.com/media/7ce1106673b1f18669d919db21a58ba1/href</a></iframe><p>Since, Subjects can also be an Observer, we can pass a subject to the subscribe method of an Observable. Here, if you could notice all of the subscribers are getting the same value, we just converted the unicast behaviour of an Observable to multicast. Later, in this article we’ll see what multicast means.</p><h4>Subjects are not reusable</h4><p>Subjects can’t be reused once they are completed or throw an error. If you’d try to call next(value) on a Subject it would silently ignore the notification. If you want a specific error instead of an ignored notification you can unsubscribe to the Subject.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e44f463fc58aece1b9650dfee532b69c/href">https://medium.com/media/e44f463fc58aece1b9650dfee532b69c/href</a></iframe><h4>When to use Subjects and Observables?</h4><blockquote><strong><em>Observable is generally unicast or cold, but Subject is multicast.</em></strong></blockquote><p>Subjects are basically multicast Observables. They make sure all of the subscribers get the same value. But Observables are unicast because each observer has its own instance of the data producer. Internally to the Subject, subscribe does not invoke a new execution that delivers values.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a69c3ae522016b33e27075e50baa8aa9/href">https://medium.com/media/a69c3ae522016b33e27075e50baa8aa9/href</a></iframe><p>In the above example, we created a currentUserSubject which would notify us whenever our current logged in user changes. So, if we are subscribing to currentUserSubject in 10 different places in our application, this would notify and give all the subscribers the same value.</p><p>When you want to observe the incoming data only once somewhere in your application, but you want to use that data simultaneously around your application when the data arrives. Whenever the subject emits some value all of its subscribers would get the same value. For example, you’ve to perform one function in one component which would affect 10 different components, then you should create subjects and subscribe to it in 10 components. This type of behaviour is a multicast behaviour.</p><p>With Observables, every consumer would get a different data. For example, if we are fetching data using http.get() and calling a url, every time a consumer would subscribe to that Observable, it’d make a new network request and would fetch you a different data every time. Hence, Observables work in an unicast way. But we’ve seen above how to make them multicast using Subjects.</p><p>Now, we’ve established the basics of Observable, Observer and Subject. Let’s talk about the types of Subjects and how they are useful.</p><h3>Types Of Subjects</h3><h4>Behavior Subject</h4><p>BehaviorSubject stores the last emitted value. Whenever a new subscriber comes, it will get the last emitted value, even if it comes much later than the subject emitted value. We create a BehaviorSubject by passing an initial value. The real world usage of BehaviorSubject is to share data between components.</p><blockquote>BehaviorSubjects are useful for representing “values over time”. For instance, an event stream of birthdays is a Subject, but the stream of a person’s age would be a BehaviorSubject</blockquote><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/398b41e8b9712fa02930eb8edd600b1f/href">https://medium.com/media/398b41e8b9712fa02930eb8edd600b1f/href</a></iframe><p>In the above example, we created a Subject and two subscribers, they are getting the initial value emitted by the Subject.</p><p>Then, later we created a new subscriber much after the subject has emitted some values. And this new subscriber received the last emitted value, which is ‘b’.</p><p><strong>Replay Subject</strong></p><p>ReplaySubject works the same as BehaviorSubject. But it has the capability of storing the last emitted n values. It stores the values from the Subject and replays whenever a subscriber arrives.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b5d4081c9e34a35252462d84b8fda7ad/href">https://medium.com/media/b5d4081c9e34a35252462d84b8fda7ad/href</a></iframe><p>In the above example, we are creating a Subject by passing the number of old values we want our subscribers to get every time they subscribe or get notified. You can think of this something like, the magazine publisher would give you the latest 3 published magazines every time you subscribe for a type of magazine.</p><h4>Async Subject</h4><p>AsyncSubject emits its last value only on COMPLETION. The last value of the Observable execution is sent to its observers, only when the execution completes. This is the only Subject that would emit a value even after it completes.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/855d33b14725fb16052a07766e3d732a/href">https://medium.com/media/855d33b14725fb16052a07766e3d732a/href</a></iframe><p>Here, both the subscribers are getting only one (last emitted) value when the Subject calls the complete() .</p><p><strong>Summary</strong></p><ol><li>Subjects emit data.</li><li>Subjects can be used as an Observable and an Observer.</li><li>Subjects can’t be reused.</li><li>Subjects work in a multicast manner.</li></ol><p>Hope you got the basic understanding of the Subjects in RxJS. Next time you’d be able to use them in your code with proper understanding (unlike me 😉).</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2762a87fa10" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Refactoring code in JavaScript]]></title>
            <link>https://javascript.plainenglish.io/refactoring-code-in-javascript-707cf5f18bf6?source=rss-c1885a01df62------2</link>
            <guid isPermaLink="false">https://medium.com/p/707cf5f18bf6</guid>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[coding]]></category>
            <dc:creator><![CDATA[Anjali Verma]]></dc:creator>
            <pubDate>Sat, 11 Jul 2020 15:59:19 GMT</pubDate>
            <atom:updated>2020-07-14T06:09:12.945Z</atom:updated>
            <content:encoded><![CDATA[<h3>How to mercilessly refactor your JavaScript code</h3><h4>Several times to take your JS to the next level</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*_2XY3hclQZ1-_rVa.jpg" /></figure><p>In this article, I’d be going through some of the practices that I’ve picked up over the years to write good and maintainable code.</p><h3>Use DRY</h3><p><strong>Don’t repeat yourself</strong> is a principle aimed at reducing repetition of code, replacing it with abstractions to avoid redundancy. Duplicating code always ends with a mess. If you fixed a bug somewhere you need to re-patch it multiple times. Having less and non-redundant code saves time as well as increases maintainability.</p><p><em>Every piece of knowledge or logic must have a single, unambiguous representation within a system.</em></p><p>In the example below, I’m getting material of different type of tutorials and duplicating the code for every type of tutorial. Now think what would happen if I would want 20 different types of tutorials. Would I write this function 20 times? NO! So, how can we refactor this code now!?</p><p>This happens when we are not considerate about the scalability of the code.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/43dc4e307f6def57a67916b3810bf667/href">https://medium.com/media/43dc4e307f6def57a67916b3810bf667/href</a></iframe><p>Answer is simple, just by taking out the business logic in a separate function and then calling that function by passing the type attribute.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d3c3945b3c1baf2da316ed7a78d62b49/href">https://medium.com/media/d3c3945b3c1baf2da316ed7a78d62b49/href</a></iframe><p><strong>BONUS — </strong>Make the code more readable with ES6 Array functions. <strong>Just 4 lines of the code. </strong>A simple refactoring can reduce your codebase drastically.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ea90ce3d78412771f0f8d65810935bdf/href">https://medium.com/media/ea90ce3d78412771f0f8d65810935bdf/href</a></iframe><h3><strong>Use Functional Binding</strong></h3><p>Some objects in real world have the same functionality, such as printName function is same for a company or a person or any other thing. So, instead of creating separate methods attached to an object, we can use bind()and can bind our objects with this function.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6baed021ad76b8099cf1f8337ab567a5/href">https://medium.com/media/6baed021ad76b8099cf1f8337ab567a5/href</a></iframe><p>After using bind() we are just creating printName() function and calling it multiple times with different objects.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/db7f1807276eb68e5d5aa1ce1942e11b/href">https://medium.com/media/db7f1807276eb68e5d5aa1ce1942e11b/href</a></iframe><h3><strong>Avoid Switch Statements</strong></h3><p>We use Switch statements to avoid using large if-else statements. However, Switch statements are very hard to debug and not very concise and I feel, they have very cluttered syntax. When adding more cases, we have to add them manually and put Break statements.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f8884b2d82c1ce0cf8656f96d9633eef/href">https://medium.com/media/f8884b2d82c1ce0cf8656f96d9633eef/href</a></iframe><p>But we can refactor our code using Object Literal. We all are aware of objects in JavaScript and the syntax is a lot maintainable and scalable. We do not need to create cases manually this time.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2d8181b38162e7c7b54d7d057948ce6a/href">https://medium.com/media/2d8181b38162e7c7b54d7d057948ce6a/href</a></iframe><p>As you can see, how much lines of code we’ve reduced. And it’s super concise and readable now. We can also use enums, interfaces or maps here.</p><h3><strong>Avoid Deep Nesting using Guard Clauses</strong></h3><p>This is one of the things that I’m guilty of doing many times. I used to have deep nested if-else statements and unnecessary logging statements. Sometimes, we do not need to log everything, I learned this the hard way.</p><p>Deeply <strong>nested</strong> conditionals make it just about impossible to tell what code will run, or <strong>when</strong>. In the example, I’ve created a nested if-else of depth-<strong>4.</strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9c095abf9db0ec9e80be4c1633b28b1d/href">https://medium.com/media/9c095abf9db0ec9e80be4c1633b28b1d/href</a></iframe><p>You need a couple of minutes to go read the code and get the flow of the function. Better way of writing this code is to reverse the code by using guards to throw the errors if the reverse conditions fail and don’t move ahead with the flow. For example, we can put a check that if the user is not logged in, we should stop the execution and return from the function.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7a96c431efef9f206ba31275237a11c1/href">https://medium.com/media/7a96c431efef9f206ba31275237a11c1/href</a></iframe><h3>Open-Closed Principle</h3><p><em>Open-Closed Principle means classes should be open to extension, but closed to modification.</em></p><p>If someone wants to extend my module’s behaviour, they shouldn’t feel the need to modify existing code.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0af5a2742f14cbac596ec4c1fc545c2e/href">https://medium.com/media/0af5a2742f14cbac596ec4c1fc545c2e/href</a></iframe><p>In this example, if I want to wash a new type of car, I can’t do it because I don’t have the permission to use it on the objects that I want to use it on. If I want to wash a new type of car, then I’d have to change the code of this file and hence, it would violate the OpenClosed principle.</p><p>Now consider the below example, we can add a new function addCar() which would make this open for extension.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/45131799cdfb1f02f38ab16bb615cc5e/href">https://medium.com/media/45131799cdfb1f02f38ab16bb615cc5e/href</a></iframe><h3>Hide how with what</h3><p><strong>Single Responsibility Principle states that <em>Functions should do one thing and one thing only.</em></strong></p><p><em>Functions that do more than one thing are very difficult to debug and to test.</em></p><p>Everyone is aware of single responsibility principle in software development. But when writing code we sometimes forget to follow it and end up writing a non-modularised, cluttered code.</p><p>Instead of writing all the code in one function, we should always remember to break it in different modules.</p><p>Hope, you learnt something which would help you write good quality code and would reduce your code base. Although, the purpose of refactoring is not to write code in less number lines but to make it readable, maintainable and scalable.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=707cf5f18bf6" width="1" height="1" alt=""><hr><p><a href="https://javascript.plainenglish.io/refactoring-code-in-javascript-707cf5f18bf6">Refactoring code in JavaScript</a> was originally published in <a href="https://javascript.plainenglish.io/">JavaScript in Plain English</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Deploy using Github  Actions on AWS Elastic BeanStalk]]></title>
            <link>https://javascript.plainenglish.io/deploy-using-github-actions-on-aws-elastic-beanstalk-c23ecd35776d?source=rss-c1885a01df62------2</link>
            <guid isPermaLink="false">https://medium.com/p/c23ecd35776d</guid>
            <category><![CDATA[github-actions]]></category>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[aws]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[node]]></category>
            <dc:creator><![CDATA[Anjali Verma]]></dc:creator>
            <pubDate>Sat, 04 Jul 2020 10:03:31 GMT</pubDate>
            <atom:updated>2020-07-10T16:17:24.103Z</atom:updated>
            <content:encoded><![CDATA[<h3>Deploy using Github Actions on AWS Elastic BeanStalk</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*2Zhoq20rqfXzZp1fGSzJfQ.jpeg" /></figure><p>In this article we’d be going through the process of deploying Node application on AWS Elastic BeanStalk using Github Actions.</p><p>All this while I’ve been using TravisCI for my deployments, but when I got introduced to Github Actions, I feel there is no turning back. Github Actions make deployment of your web application super simple ❤.</p><h4>What is Elastic BeanStalk?</h4><p>AWS Elastic Beanstalk is an orchestration service that abstracts away some of hardware resources and details (e.g. setting up AWS components and containers), while still allowing the developer a range of choices when it comes to OS and programming language. Elastic Beanstalk employs Auto Scaling and Elastic Load Balancing to scale and balance workloads. It provides tools in the form of Amazon <a href="https://aws.amazon.com/cloudwatch/">CloudWatch</a> to monitor the health of deployed applications. It also provides capacity provisioning due to its reliance on AWS S3 and EC2.</p><p>All you have to do is upload your application code, and Elastic Beanstalk takes care of the deployment, load balancing, and capacity provisioning. With a single click, you can start all the necessary application servers running. There is no charge for the Elastic Beanstalk service itself, just for the AWS resources you actually use.</p><h4>What are Github Actions?</h4><p>GitHub Actions give you a possibility to automate your software development workflows directly from the GitHub repository. In other words, you will be able to initiate the deployment of your application from the same place where your code is stored.</p><p>Another great feature of Github Actions is that, workflows can run in Linux, macOS, Windows, and containers on GitHub-hosted machines. Elastic Beanstalk web apps are fully supported by Github Actions, so you can deploy your app directly from the repository.</p><p>The pipeline that we’re trying to achieve in this article is:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/778/1*a4gteAJboafqjoQvlNV-4w.png" /><figcaption>CI/CD Pipeline</figcaption></figure><p>With Actions in place, you can initiate functions such as seamlessly building and deploying Docker containers. When you combine Actions into workflows, events can trigger a complex series of processes to run in parallel sequence, enabling more responsive architectures.</p><blockquote>Automate your workflow from idea to production.</blockquote><h3>Getting Started</h3><p><a href="https://aws.amazon.com/elasticbeanstalk/">Elastic Beanstalk</a> will automatically create an <a href="https://aws.amazon.com/ec2/">EC2</a> instance for us when we create our app, this is where our Node.js server will be hosted.</p><h4><strong>Steps to setup Elastic BeanStalk environment:</strong></h4><ol><li>For setting up Elastic BeanStalk environment, I’d recommend you to head over <a href="http://bit.ly/deploy-angular-application-on-aws-beanstalk">the step by step guide to configure the EB environment</a> till step #9, you can skip Load balancer step or watch this <a href="https://www.youtube.com/watch?v=xhc1boyBkJw">video</a> by AWS.</li><li>To improve performance, you can configure the proxy server to serve static files (for example, HTML or images) from a set of directories inside your web application.To statistically deploy your Node application on EB, you need to add <em>.ebextensions/deploy.config file:</em></li></ol><p>Follow <a href="https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/environment-cfg-staticfiles.html">this</a> for more information to serve your files statically.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e46cee69744c8d2ab189148142367efc/href">https://medium.com/media/e46cee69744c8d2ab189148142367efc/href</a></iframe><p>Once you setup your Beanstalk environment, you could also monitor the health of your environment after every build in the EB console:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*NB7-vk3Fh8AZy-t2LDXMWw.png" /></figure><h4><strong>Steps to build your project:</strong></h4><ol><li>Install the dependency to serve your Node application statically.</li></ol><pre>npm i serve --save</pre><p>2. In the production folder of your Node application, make sure to change startscript in package.jsonfile.</p><pre>&quot;scripts&quot;: {<br>    &quot;start&quot;: &quot;serve -s build&quot;,<br>}</pre><p>Replace server.js by your main server file if you named it something else. This is so Elastic Beanstalk knows which file to use to launch your server.</p><h4><strong>Steps to create your Github Actions workflow:</strong></h4><ol><li>Go to your GitHub repository and click the <strong>Actions </strong>tab button.</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*5gjr1U4NklP7tuYEvPE9Pg.png" /></figure><p>2. Click on the <strong>Set up a workflow yourself</strong> button, on the right corner, as shown in the screenshot below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*LpIpn4aGN1a_V6ZCCakCGQ.png" /></figure><p>3. In the editor paste the code snippet below and click on the <strong>Start commit </strong>button on the right corner. Add some commit message and click on <strong>Commit new file button. </strong>I’ve added comments to explain each step of this workflow.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f5ff31154f7f0a14f8cc65c2c6224827/href">https://medium.com/media/f5ff31154f7f0a14f8cc65c2c6224827/href</a></iframe><p>Here, push and pull request eventsare triggering this workflow, but we can have it triggered on various <a href="https://docs.github.com/en/actions/reference/events-that-trigger-workflows#webhook-events">other events</a> too.</p><p>4. Open your project in the IDE on your machine and pull the updated code from the master branch.</p><p>5. Now, go to the <strong>Settings </strong>tab of your repository and on the side menu, select <strong>Secrets. </strong>Secrets are your private values like AWS secret key and access key which should not be directly present in your code.</p><p>6. Click on <strong>Add a new secret </strong>button and add your AWS secret keys and access keys with recommended name as AWS_ACCESS_KEY and AWS_SECRET_KEY.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*qWOTSTLWReZiYYfr8v4CVw.png" /></figure><p>If you configured everything successfully, your workflow should look like:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ykneJ3qNZrg5iMfMLgdlRw.png" /><figcaption>Output of the pipeline</figcaption></figure><p>And that’s it, it’s this simple to create your own CI/CD pipeline with Github Actions for your Node projects. All you need to create is one workflow file to deploy your Node projects on cloud.</p><p>You can find the workflow and EB config files here:</p><p><a href="https://github.com/anjy-07/micro-frontend/tree/master/react-charts">anjy-07/micro-frontend</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c23ecd35776d" width="1" height="1" alt=""><hr><p><a href="https://javascript.plainenglish.io/deploy-using-github-actions-on-aws-elastic-beanstalk-c23ecd35776d">Deploy using Github  Actions on AWS Elastic BeanStalk</a> was originally published in <a href="https://javascript.plainenglish.io/">JavaScript in Plain English</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Micro-Frontend using Web Components]]></title>
            <link>https://medium.com/swlh/micro-frontend-using-web-components-e9faacfc101b?source=rss-c1885a01df62------2</link>
            <guid isPermaLink="false">https://medium.com/p/e9faacfc101b</guid>
            <category><![CDATA[angular]]></category>
            <category><![CDATA[github]]></category>
            <category><![CDATA[micro-frontends]]></category>
            <category><![CDATA[web-components]]></category>
            <category><![CDATA[react]]></category>
            <dc:creator><![CDATA[Anjali Verma]]></dc:creator>
            <pubDate>Sun, 28 Jun 2020 06:04:51 GMT</pubDate>
            <atom:updated>2020-06-29T11:52:21.107Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*rDToqSMaO3VYClHC6aoBcw.png" /><figcaption>Dan Abramov’s Github Dashboard</figcaption></figure><blockquote><em>Good frontend architecture is hard. Scaling frontend development so that many teams can work simultaneously on a large and complex product is even harder. In this article, we’ll talk about a recent trend of breaking up frontend monoliths into many smaller, more manageable components using </em><strong><em>Web Components.</em></strong></blockquote><h4>First of all, what is Micro Frontend?</h4><p><strong>Micro Frontend</strong> is a micro service approach to front end web development. The concept of <strong>Micro Frontend</strong> is to think about a web application as a <strong>composition of features</strong> owned by different <strong>independent teams</strong>. Each team has a <strong>distinct area of business</strong> or <strong>mission</strong> it cares about and specialises in. A team is <strong>cross functional</strong> and develops its features <strong>end-to-end.</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/639/0*XQpx_ZbtXMpy8Ify" /><figcaption>Micro front end architecture</figcaption></figure><p>We’ve established a good micro service architecture for our backends. It’s time to apply the same principles to our front ends. A lot of firms are moving towards micro frontend from classic monolith architecture like Google, Amazon, Salesforce, Zalando, ThoughtWorks etc.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*0uiNQqCuq_kR99xC" /><figcaption>Amazon’s architecture for micro frontend</figcaption></figure><p>There are various implementations for micro frontend approach. But, in this article, we’d be talking about micro frontend implementation using Web Components only.</p><h4>What are Web Components?</h4><p>In modern web development, everyone is using components to encapsulate UI functionalities. Web components are low-level browser API that helps us to extend the browser with new components, they provide us a standard interface for defining new components. Web components have a standard way of creating components that use HTML and DOM API that every other front end framework uses and a common way of receiving and sending data using props and events.</p><blockquote>Web component simply tells the browser WHEN and WHERE to create a component, not HOW to create a component.</blockquote><p>Web Components comprises of 3 concepts:</p><ol><li><strong>Custom Elements:</strong> Set of JavaScript APIs that allow you to create your own HTML elements and define the way they behave.</li><li><strong>Shadow DOM: A p</strong>rivate DOM that is scoped to your component only and capable of isolating CSS and JavaScript.</li><li><strong>HTML templates.</strong> New HTML tags that allows us to create templates for your components.</li></ol><p>In simpler terms, you can create your own HTML selector, like &lt;select&gt;, &lt;h1&gt; etc.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*T_4AeIpTENuW7puzJ14EUQ.png" /></figure><p><em>EVERYONE is using Web components, according to Google, 5% of page loads today, use Web components.</em></p><h4>What’s driving this adoption?</h4><p>As a developer, we have full freedom to use the stack to create our own components and still maintain interoperability. That’s the key difference between web components and proprietary component libraries.</p><p>Once you have invested your time in creating a web component, they can be used widely in a more diverse range of applications because they work anywhere the web works.</p><h4>Custom Elements</h4><p>The Custom Elements specification lays the foundation for designing and using new types of DOM elements. Autonomous custom elements are new HTML tags, defined entirely by you. They have none of the semantics of existing HTML elements, so all the behaviour needs to be defined by you.</p><p>You can define your own custom element by using:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a53ee75a865c1c298700ada462b817e1/href">https://medium.com/media/a53ee75a865c1c298700ada462b817e1/href</a></iframe><p>Custom Elements follow a lifecycle to mount and unmount the component on the DOM. To learn more about custom elements and shadow DOM in depth, refer <a href="https://www.webcomponents.org/introduction"><em>WebComponents</em>.</a></p><p>Till here, I hope you got a basic understanding of micro frontends and web components. Let’s dive into the implementation part, and build something cool using <em>Web Components</em> and <em>Custom HTML Elements.</em></p><h3>Implementation</h3><p>For demonstrating the use of web components and its compatibility with different frameworks I have built an application that fetches Github statistics of a user.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*nkPzeCuZkaFopEiZHPx-xQ.jpeg" /></figure><p>This Application has one parent component developed in Angular which includes the search bar and the two child components, the profile section and statistics section are developed using Angular and React respectively.</p><p><em>All the components follow material design pattern thus ensuring consistent styling</em>. For communication between the parent component, we need to pass Github username typed in the search bar and maintain the global state of the entire application. Child components have their own isolated states.</p><p><strong>Steps to create React Web Component:</strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4a4a38ead1e37ff9b8397deeb9454e1e/href">https://medium.com/media/4a4a38ead1e37ff9b8397deeb9454e1e/href</a></iframe><ol><li>We will start by creating the custom element class and by defining it in the <a href="https://medium.com/@gilfink/understanding-the-customelementregistry-object-74408a25a3d4">CustomElementRegistry</a>, here we’ve registered our component having ‘react-el’ selector. The customElements global is used for defining a custom element and teaching the browser about a new tag. Use <em>CustomElements.define()</em> with the tag name you want to create and a JavaScript class that extends the base HTMLElement.</li><li>To establish the communication between child component and parent component, we will observe the attributes passed to our React component so that we can re-render our component once the attributes change.</li><li>In the connectedCallBack function, we will mount and render our React component. It is invoked each time the custom element is appended into the DOM. In our case, we’ll be rendering the Graphs component that we’ve created in React by passing the global attributes while calling <em>React.render()</em> method. Graph component has its own isolated state, no relation with other components.</li><li>Last step is to unmount the React component and unsubscribe the observer. The disconnectedCallback() callback logs simple messages to the console to inform us when the element is either removed from the DOM, or moved to a different page.</li></ol><p><strong>Now you can just include <em>&lt;react-el&gt;</em> selector to your parent apps.</strong></p><h4>Building Angular Web Component</h4><p>Unlike the defined way of extending to HTMLElement in any JavaScript library, Angular provides the support to create custom elements using <em>Angular</em> <em>Elements</em>.</p><blockquote><strong>Angular Elements</strong> are Angular components packaged as custom elements (also called Web Components), a web standard for defining new HTML elements in a framework-agnostic way.</blockquote><p><strong>Steps to create Angular Web Component:</strong></p><ol><li>To convert and existing app to custom element we need to import <em>createCustomElement() </em>function from ‘@angular/elements’</li></ol><p><em>npm i @angular/elements — save</em></p><p>2. Change package.json to build Angular project and serve the project as custom elements:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/79fd930afe272cd0697465a4d1e54554/href">https://medium.com/media/79fd930afe272cd0697465a4d1e54554/href</a></iframe><p>3. Instead of loading AppComponent on bootstrap, we are going to wrap into a custom element. Move it to the <em>entryPoint</em> list for dynamic loading of the component. Although this step can be skipped if root micro component is the AppComponent. You can read more about entry component vs bootstrap here: <a href="https://angular.io/guide/entry-components">https://angular.io/guide/entry-components</a></p><p>Angular provides the createCustomElement() function for converting an Angular component, together with its dependencies, to a custom element. The function collects the component’s observable properties, along with the Angular functionality the browser needs to create and destroy instances, and to detect and respond to changes.</p><p>The conversion process implements the NgElementConstructor interface, and creates a constructor class that is configured to produce a self-bootstrapping instance of your component.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c26d47d4d74a365e3e81ebd27514b520/href">https://medium.com/media/c26d47d4d74a365e3e81ebd27514b520/href</a></iframe><p>Use a JavaScript function, customElements.define(), to register the configured constructor and its associated custom-element tag with the browser’s CustomElementRegistry. When the browser encounters the tag for the registered element, it uses the constructor to create a custom element instance.</p><p><strong>Now you can just include <em>&lt;ng-profile&gt;</em> selector to your parent apps.</strong></p><h4>Building the Parent App and integrating the Micro components</h4><ol><li>In the parent Angular app to use the web components defined, we need to add CUSTOM_ELEMENTS_SCHEMA to the module.</li><li>We now just need to import the built .js served by the micro components into the parent app and create the elements in parent app with the tag name defined. We include the url for served .js file in parent app’s index.html. In addition to these zone.js and custom-element-es5-adapter.js needs to be included for the web components to work</li><li>The parent app will add the child component to the DOM when username is entered by the user. Then child component will check for attribute change by the parent app which is used as communication mechanism.</li><li>The Angular component will fetch data and update the component once username attribute is set. Similarly React component uses mutation observer api to handle fetch data and update the micro component.</li></ol><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a9116f164ef5830fdf1f556031d58100/href">https://medium.com/media/a9116f164ef5830fdf1f556031d58100/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f6bd4bca872863e1d18243e0c43e2acf/href">https://medium.com/media/f6bd4bca872863e1d18243e0c43e2acf/href</a></iframe><h3>Deployment</h3><p>Now let’s talk about how to deploy these separate web components to efficiently utilise the concept of micro frontends.</p><p>I used <em>AWS Elastic Beanstalk</em> service to deploy graphs component and used <em>S3</em> to deploy my profile component statically on AWS using Github Actions to have a separate CI/CD pipeline for each component. I did this just to ensure and prove the point that I’m trying to make in this article is that we can have isolated teams working on different components irrespective of their deployment architecture.</p><p>Github workflow files and Elastic Beanstalk config files can be found in the repository.</p><p><em>Having said all of the above things, I strongly believe that micro frontend is the approach for large teams only, and this approach is still under consideration by a lot of companies. Try and figure out if this is the architecture your team needs.</em></p><p>Finally, we come to an end of this article, hope you got a bit inspired to move your monolith architecture to micro frontend.</p><p><a href="http://microgithubdashboard-env.eba-x5bzpamu.ap-south-1.elasticbeanstalk.com/">Demo of the application</a></p><p>The complete code for this sample can be found here:</p><p><a href="https://github.com/anjy-07/micro-frontend">anjy-07/micro-frontend</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e9faacfc101b" width="1" height="1" alt=""><hr><p><a href="https://medium.com/swlh/micro-frontend-using-web-components-e9faacfc101b">Micro-Frontend using Web Components</a> was originally published in <a href="https://medium.com/swlh">The Startup</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Flutter Google SignIn Firebase Authentication]]></title>
            <link>https://anjaliverma471.medium.com/flutter-google-signin-firebase-authentication-37b101d4be31?source=rss-c1885a01df62------2</link>
            <guid isPermaLink="false">https://medium.com/p/37b101d4be31</guid>
            <category><![CDATA[firebase]]></category>
            <category><![CDATA[flutter]]></category>
            <category><![CDATA[firebaseauthentication]]></category>
            <category><![CDATA[google]]></category>
            <dc:creator><![CDATA[Anjali Verma]]></dc:creator>
            <pubDate>Sun, 17 Mar 2019 17:15:39 GMT</pubDate>
            <atom:updated>2019-05-14T02:45:09.200Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/913/1*ATmyAWl1tX_D07cBoctsfQ.png" /></figure><p>In this tutorial, I’ll walk you through adding Google authentication to Flutter applications using the Firebase Authentication plugin and display user’s profile picture.</p><p><strong>Step 1:</strong> We need to begin by creating a new Flutter project and adding the <a href="https://pub.dartlang.org/packages/firebase_auth">Firebase Auth package</a> and <a href="https://pub.dartlang.org/packages/google_sign_in">Google SignIn </a>to the pubspec.yaml file, make sure that you use the latest version of the package.</p><pre>dependencies:<br>  flutter:<br>    sdk: flutter<br>  <strong>firebase_auth: ^0.6.2+1<br>  google_sign_in: ^3.2.2</strong></pre><p><strong>Step 2:</strong> Now, register your project on <a href="https://console.firebase.google.com/">https://console.firebase.google.com</a>, follow the steps <strong>1 to 4</strong> to add Firebase to your android app, and don’t forget to enable Google sign-in method for your app under Authentication tab.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*8Yb8cFfIPV4LilB2wtQ3AA.png" /><figcaption>Enable Google sign-in method</figcaption></figure><p><strong>Step 3: </strong>Create a login screen which will have the sign-in button and on button pressed signInWithGoogle(…) will be called which we will define in the state_widget.dart file.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6aa86a5e9267e7ec6f97d644abe239de/href">https://medium.com/media/6aa86a5e9267e7ec6f97d644abe239de/href</a></iframe><p><strong>Step 4: </strong>To set the state of the app when the user logs in successfully, use <strong>setState(). </strong>In this tutorial, we will use <a href="https://docs.flutter.io/flutter/widgets/InheritedWidget-class.html">InheritedWidget</a> to update the state and build content as the state of the app changes.</p><blockquote>The <strong>InheritedWidget</strong> is a special Widget, that you put in the Widgets tree as a parent of another sub-tree.</blockquote><p><em>If user is not logged-in already, </em><em>_buildContent() method in HomeScreen will be called and LoginScreen will be displayed as user property is set to null initially. When the user logs in successfully the state of the app will change and user property will store the Firebase user instance. Hence, home screen will be displayed.</em></p><p><em>signIn() will trigger a user account selection prompt. When the user confirms their account selection a </em><em>GoogleSignInAccount instance will be returned that will be used to authenticate the user with Firebase.</em></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6cab15a460912058ac00d371f031a86a/href">https://medium.com/media/6cab15a460912058ac00d371f031a86a/href</a></iframe><p><strong>Step 5: </strong>Create a function called signIntoFirebase() in auth.dart file that handles Firebase authentication and import:</p><pre><strong>import</strong> &#39;package:google_sign_in/google_sign_in.dart&#39;;<br><strong>import</strong> &#39;package:firebase_auth/firebase_auth.dart&#39;;</pre><p>In signIntoFirebase(…)we first created the instance of the GoogleSignInAuthentication that holds a reference to the authenticated user and access token. After getting the information we call<a href="https://github.com/flutter/plugins/blob/master/packages/firebase_auth/lib/firebase_auth.dart#L241">signInWithGoogle()</a> from our FirebaseAuth instance by passing accessToken and idToken from the instance of GoogleSignInAuthentication. This is an asynchronous function and will return us a Future of the type FirebaseUser which we will use to set the state of the app in state_widget.dart file.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d18f1ed650315cd87d7b7e4fce843005/href">https://medium.com/media/d18f1ed650315cd87d7b7e4fce843005/href</a></iframe><p><strong>Step 6: </strong>The _buildContent(...) method in home.dart file is just used to update the UI when the user logs in successfully.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/cb13826d994668d8f4330fb109c6a24f/href">https://medium.com/media/cb13826d994668d8f4330fb109c6a24f/href</a></iframe><p><strong>Step 7: </strong>Create UI for HomeScreen to show user’s profile data.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/28fe4e146b5c4f59ed5fcb30479f516e/href">https://medium.com/media/28fe4e146b5c4f59ed5fcb30479f516e/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/316/1*sgLIrwtgHVcGI453RvCI6Q.gif" /></figure><h4><strong>For iOS,</strong></h4><p><strong>Step 1: </strong>Add iOS app to your project on Firebase and follow steps 1 and 2 to add Firebase to your iOS app.</p><p>The app will crash on iOS, just add the following code to the end your info.plist file in /ios/Runnerfolder and copy REVERSE_CLIENT_ID from GoogleServices-Info.plist file.</p><p>and …. just do nothing, you have already setup your Flutter code.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5f8a5cdc5c83a5484c9cd99d678bcea8/href">https://medium.com/media/5f8a5cdc5c83a5484c9cd99d678bcea8/href</a></iframe><p><strong>Step 2:</strong> Run your iOS code in Xcode once.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/388/1*C09Cjd6EjneerlwxQVE3xw.gif" /><figcaption>iOS Google Login</figcaption></figure><p>And, there you go, adding social authentication to your Android and iOS app is just a game of <strong>7 </strong>basic<strong> </strong>steps :)</p><p>The complete code for this sample can be found here:</p><p><a href="https://github.com/anjy-07/GoogleSignIn_Flutter">anjy-07/GoogleSignIn_Flutter</a></p><figure><a href="https://usejournal.com/?utm_source=medium.com&amp;utm_medium=noteworthy_blog&amp;utm_campaign=guest_post_image"><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*f2IVAl0TbsfES9cFGYr40g.png" /></a></figure><p>📝 Read this story later in <a href="https://usejournal.com/?utm_source=medium.com&amp;utm_medium=noteworthy_blog&amp;utm_campaign=guest_post_read_later_text">Journal</a>.</p><p>🗞 Wake up every Sunday morning to the week’s most noteworthy Tech stories, opinions, and news waiting in your inbox: <a href="https://usejournal.com/newsletter/?utm_source=medium.com&amp;utm_medium=noteworthy_blog&amp;utm_campaign=guest_post_text">Get the noteworthy newsletter &gt;</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=37b101d4be31" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[To-Do list using Drag and Drop in Angular 7]]></title>
            <link>https://anjaliverma471.medium.com/to-do-list-using-drag-and-drop-in-angular-7-613f516113f1?source=rss-c1885a01df62------2</link>
            <guid isPermaLink="false">https://medium.com/p/613f516113f1</guid>
            <category><![CDATA[cdk]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[angular]]></category>
            <category><![CDATA[to-do-list-app]]></category>
            <category><![CDATA[angular-material]]></category>
            <dc:creator><![CDATA[Anjali Verma]]></dc:creator>
            <pubDate>Mon, 03 Dec 2018 09:46:05 GMT</pubDate>
            <atom:updated>2019-03-17T19:21:31.458Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*zfBoKBYPfjya8zqtRk4mww.jpeg" /><figcaption>Image courtesy: <a href="https://www.youtube.com/channel/UCYFd7Qy93YP7gPERnxP545A">Demos with Angular</a></figcaption></figure><p>The new version of Angular came out with many new features and one of them is Drag and Drop Module. In this article we would be exploring the new Drag and Drop feature that is included in Angular Material by creating a To-Do list.</p><p><strong>Getting Started</strong></p><p>To begin, add or update the @angular/cdk dependency in package file of your project or run one of the following commands:</p><p>npm install @angular/cdk@7.0.0-beta.0<br>OR<br>yarn add @angular/cdk@7.0.0-beta.0</p><p>Now, include package in your app.module.ts</p><pre>import { DragDropModule } from &#39;@angular/cdk/drag-drop&#39;;<br>@NgModule({<br> imports: [BrowserModule, FormsModule, DragDropModule],<br> declarations: [AppComponent],<br> bootstrap: [AppComponent]<br>})<br>export class AppModule { }</pre><p>Now, to make an element drag-able, all you have to do is to add the cdkDrag directive on the element. And there you go! So simple, isn’t it ?</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*zSq4DCpcWQkLlj2zbyZB7w.gif" /></figure><h3>Sort-able List</h3><p>One common use-case of Drag &amp; Drop is sorting or reordering lists which is useful to create a to-do list. Just apply cdkDrag directive to the list items and enclose the list within the cdk-drop element and you’re good to go!</p><p>You can create cdk-drop elements that will tell the CDK that the draggable item is only allowed to drop inside this drop-zone. Also, the best part is you can customize drag and drop animation as you want.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9e378cc2015c421aa7e930e0adc97944/href">https://medium.com/media/9e378cc2015c421aa7e930e0adc97944/href</a></iframe><p>After dropping the item, the list remains unchanged. This is because Angular CDK just drops the item and the list remains unchanged until you handle the drop event and use Angular CDK’s moveItemInArray method which on drop rearrange the items array according to the indexes.</p><p>If the user is dragging an element within the same container, we perform a sortable action; otherwise, we transform the value from one collection to the other using the built-in transferArrayItem() method.</p><p>The cdk-drop component supports transferring dragged items between connected drop zones. You can connect one or more cdk-drop instances by setting the connectedTo property.</p><p><strong>CdkDragPreview</strong></p><p>When a cdkDrag element is picked up, it will create a preview element visible while dragging. By default, this will be a clone of the original element positioned next to the user&#39;s cursor. This preview can be customized, though, by providing a custom template via *cdkDragPreview.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/900f4ec843802f48aafefe211dd33512/href">https://medium.com/media/900f4ec843802f48aafefe211dd33512/href</a></iframe><p>And that’s it, finally your To-Do list looks like this.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*paQNu3K81ajXWkRYhFCeyA.gif" /></figure><p>The complete code for this sample can be found here:</p><p><a href="https://github.com/anjy-07/To-Do-List-in-Angular7">anjy-07/To-Do-List-in-Angular7</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=613f516113f1" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>