<?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 Fake Dev on Medium]]></title>
        <description><![CDATA[Stories by Fake Dev on Medium]]></description>
        <link>https://medium.com/@fake.dev00?source=rss-adbd77e15a8b------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*JTvag8Vxq7ESfSetodYcjA.png</url>
            <title>Stories by Fake Dev on Medium</title>
            <link>https://medium.com/@fake.dev00?source=rss-adbd77e15a8b------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 28 Nov 2026 05:04:23 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@fake.dev00/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[Implementation of Pagination in Angular 16 (3/4)]]></title>
            <link>https://medium.com/@fake.dev00/implementation-of-pagination-in-angular-16-3-4-7e8408d71521?source=rss-adbd77e15a8b------2</link>
            <guid isPermaLink="false">https://medium.com/p/7e8408d71521</guid>
            <dc:creator><![CDATA[Fake Dev]]></dc:creator>
            <pubDate>Sat, 30 Dec 2023 01:01:14 GMT</pubDate>
            <atom:updated>2023-12-30T01:01:14.821Z</atom:updated>
            <content:encoded><![CDATA[<p>So we are now able to present a solution that does not delegate responsabilities on the parent component and can be reused anywhere.</p><p>Unfortunately I could not use the same API as the previous examples because although the previous one does have some pagination interaction it does not enough to be considered a real world example, we need responses with pagination information, i.e., number of items, does it have a next or a previous.</p><p>With this in mind and after searching for a while I decided to use <a href="https://swapi.dev/">https://swapi.dev/</a>. It has enough of pagination features that allows me to wrap up something and present you a real worldish example of a pagination component.</p><p><strong>NOTE:</strong> I am not going into full detail into the concepts I am going to present here so I do apologize for that, the purpose here is just to give you some hints and let you explore on your own. However feel free to reach out to me should you wish to discuss something in more depth.</p><p>To introduce the concept of datasource there is nothing better than to read what is means from the Angular Material documentation, <a href="https://material.angular.io/components/table/overview">https://material.angular.io/components/table/overview</a></p><blockquote><strong>Observable stream of data arrays</strong></blockquote><blockquote>An alternative approach to providing data to the table is by passing an Observable stream that emits the data array to be rendered each time it is changed. The table will listen to this stream and automatically trigger an update to the rows each time a new data array is emitted.</blockquote><blockquote><strong>DataSource</strong></blockquote><blockquote>For most real-world applications, providing the table a DataSource instance will be the best way to manage data. The DataSource is meant to serve as a place to encapsulate any sorting, filtering, pagination, and data retrieval logic specific to the application.</blockquote><blockquote>A DataSource is simply a class that has at a minimum the following methods: connect and disconnect. The connect method will be called by the table to provide an Observable that emits the data array that should be rendered. The table will call disconnect when the table is destroyed, which may be the right time to clean up any subscriptions that may have been registered in the connect method.</blockquote><p>What we will implement shares the same purpose. Our custom datasource will have different methods and properties but it is simply a class meant to encapsulate pagination logic.</p><h3>DataSource</h3><p>In our example, to be able to have an aproach that is abstracted away from the application but extensible and robust I decided to start by creating an abstract class with the methods needed for pagination logic,</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/934/1*-9hJypHb4SiSBC4ja1acrg.png" /><figcaption>Abstract Datasource class</figcaption></figure><p>Here we layout what we absolute need to have defined in any datasource related to pagination. We have three methods that allow us to get the response from the backend, the actual items we will want to feed to our template and the total number of pages available, from now on it will be customizations.</p><p>So now we have only to create our custom datasource extended by this previous one,</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*hTGoiVt6uxvRqtLmZN7cCw.png" /><figcaption>Custom Datasource</figcaption></figure><p>Here I defined a response stream where I could combine with our pagination stream. Bear in mind that in the last method, the total pages one we are calculating exactly what the name implies, but the magic number 10 that shows up there is by default and also the limit of items per page that the API shows, therefore I added it there, although in a real world example this number could have come from a global config object or something similar. Here you also see some types you don’t know what they are, like <em>RemoteRequest</em>, <em>RemoteRequestResponse</em> or <em>RemoteRequestOptions </em>so let me show you what they are.</p><h3>Remote Request</h3><p>In order to make this extensible we need to make sure that the APIs different nomenclature and approach does not affect our outcome. For that we create a set of interfaces we use. <em>RemoteRequestResponse</em> interface is customized for this example because we want this to be extensible but not completely generic. That is a huge difference.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*NHf4-dDS0EeKnju5v-7Mpg.png" /><figcaption>Remote Request interfaces file</figcaption></figure><p>Having said this we do have a signature of our method <em>makeRequest </em>should look like but we do not have a definition for it. We do need to create a customized remote request class for the Star Wars API example.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*_d3T_OcVIIGzCEX5MoCGig.png" /><figcaption>Star Wars Remote request class</figcaption></figure><p>Here we do provide a clear definition on the request method and of the types we want. In the end, and if you were observing carefully this method will be called on the response data stream in our custom data source.</p><h3>Services</h3><p>For the purpose of this example I decided to use two seperate services, but I am sure it could have been done differently and using only one instead.</p><p>So we now need to in fact get our information from the API itself and start passing the info for the star wars data list component.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/908/1*OCRbvrsUvnza7aCqJVaHWw.png" /><figcaption>Service to the data from the API and return it as a Datasource</figcaption></figure><p>In this article I am not worried about the hierarchy of the dependency injection and where my services are being provided, but I do alert you that it is a matter that needs some consideration, don’t just provided them all in root if not necessary.</p><p>In our second service we get the data from the datasource method, already manipulated accordingly to our needs.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*P99jfM5_vME-u2p2h_Fzjg.png" /><figcaption>Service to initialize the pagination properties and get the entries from the Datasource</figcaption></figure><p>Take care that in this case the API did not work with pages below 1, so I initialized it to 1, but there might be a case where there is a page 0 to start, so you will need to adjust this part and some bits on the pagination component itself.</p><h3>Data List Component</h3><p>Our data list component will look much smoother than our example on our previous article and with no responsabilities delegated over by the pagination component.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/924/1*gj9ckv76U2-eyrpGsDlMBg.png" /><figcaption>HTML template for the data list component</figcaption></figure><p>The class itself of the component also gets much cleaner and leaner with a clear division of responsabilities.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*mSpgG4Ne6T6_2r-uSARksw.png" /><figcaption>Data list component class</figcaption></figure><h3>Pagination Component</h3><p>After all this, we are finally ready to make our pagination component and it will be a walk in the park after all this.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/873/1*B8_1z0UG0zkRASxWmEEr8g.png" /></figure><p>The methods here are simple to understand and they define a pretty standard logic for this type of component, I just want to add the decorator <em>@ObserveInput </em>that is custom made, and in a nutshell it just transforms an <em>@input</em> value into an observable of a given type.</p><p>The HTML looks like this,</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/809/1*qBV7tuM7gZzjDrbAeew5og.png" /><figcaption>Pagination Component Template</figcaption></figure><p>And so in the end the result will look like the following, much cleaner and reusable. The small delay between pages could be mitigated by implementing a loading for instance.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Nph6VugA0LfUTvURBNA8AQ.gif" /></figure><p>Hope you enjoyed this article, in the last part we shall tackle unit testing and end 2 end testing.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7e8408d71521" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Implementation of Pagination in Angular 16 (2/4)]]></title>
            <link>https://medium.com/@fake.dev00/implementation-of-pagination-in-angular-16-2-4-35ad305f2777?source=rss-adbd77e15a8b------2</link>
            <guid isPermaLink="false">https://medium.com/p/35ad305f2777</guid>
            <dc:creator><![CDATA[Fake Dev]]></dc:creator>
            <pubDate>Mon, 18 Dec 2023 23:05:19 GMT</pubDate>
            <atom:updated>2023-12-18T23:05:19.717Z</atom:updated>
            <content:encoded><![CDATA[<p>In the second part of this article I will build upon the setup of that beers data list component and add a simple pagination. In the end you will understand why the usual standard way might not be what you want if reusability, extendability and clear responsabilities division are first in your mind.</p><p>So now we can create the component itself with a couple of properties as inputs and an event emitter to check to changes in pages. In this component you might ask why i have an exclamation mark after the prop name, it is simple due to the fact that for the purpose of this examples I don’t want to deal with typing and nullables, so I enforce it by saying I am sure these props will have values, hence the exclamation mark.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/860/1*97ehKztqnGD_T2xfK2zoog.png" /></figure><p>The HTML of this component will be of little importance since this is going to be a pagination with a selection box (this is example is heavily based on most of what I found around the web so it is really just so you can compare afterwards with what we shall have on part 3.)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/757/1*TzbcXmZn4NLJFKNxmszOEQ.png" /></figure><p>Now comes the big change, on the very same beer data list component where our pagination component shall live we will have to add all the logic of the pagination as well as having the properties ready to pass them on down.</p><p>First I would suggest we tackle the method in our service. The one we had previously will no longer work. Now we need to have another one that accepts params that will work as pagination query params.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/811/1*_2X4X67SKyyjhgXo41MbXw.png" /><figcaption>New getter for paginated data</figcaption></figure><p>NOTE: the page and per_page params betweem quotes are specific the API we are working on, they are not random.</p><p>In the beer data list component we shall need the login for going forward and backwards on pagination, also a go to a specific page in the selection box. I will not go into much details about the implementation as I said it is not relevant here.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*iBRocUwmwrDUc8bt6F7rOg.png" /></figure><p>In my opinion this is not correct from the point of view of responsabilities delegation. Also, should you wish to reuse the pagination component somewhere else on your project you can’t unless you delegate and redo the logic on the parent.</p><p>In the end we get this result, it works it does indeed paginate your content but in terms of software development architecture it is poorly designed (this approach).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*dJOJyC7YUg9bp-qYTWNrPA.gif" /><figcaption>End result</figcaption></figure><p>In Part 3 we will adopt an approach that is very much used in Angular material, which is the concept of Datasource. However we will not be stuck to Angular material components and will be using our own.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=35ad305f2777" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Implementation of Pagination in Angular 16 (1/4)]]></title>
            <link>https://medium.com/@fake.dev00/implementation-of-pagination-in-angular-16-1-4-5d4c0762b57f?source=rss-adbd77e15a8b------2</link>
            <guid isPermaLink="false">https://medium.com/p/5d4c0762b57f</guid>
            <category><![CDATA[typescript]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[pagination]]></category>
            <category><![CDATA[angular-16]]></category>
            <dc:creator><![CDATA[Fake Dev]]></dc:creator>
            <pubDate>Sun, 17 Dec 2023 01:33:43 GMT</pubDate>
            <atom:updated>2023-12-17T01:33:43.750Z</atom:updated>
            <content:encoded><![CDATA[<p>This article was born out of the necessity of how to evolve your typical solution of a simple pagination component where you delegate responsability and logic to the parent component and lose reusability, to a version where you make it not generic per say but extensible enough to be abstracted away and reused somewhere else in your project should you need to.</p><p>Every time we work with an API we might end up having to deal with pagination, specially if we do have some kind of element or component that has responsabilities to work as a data table of some sort. Having this in mind I have decided to split this article in three parts:</p><p>1 -&gt; I will introduce the setup project. It will be simple and constrained to be completely directed at what we are trying to share.</p><p>2 -&gt; Secondly, I will add a simple pagination solution on it just so you can later see and check the difference yourself.</p><p>3 -&gt; Then I will adopt a mor extensible approach where the solution can then be reused anywhere else should you wish to.</p><p>4 -&gt; As a bonus I will show you an approach on how to implement unit and e2e tests by using jasmine and playwright respectively.</p><p>NOTE: Please do bear in mind that the styling of every example that will be shown here is not the purpose of this article and therefore is just styled enough so it is not horrendous to see.</p><p>For starters, let me introduce you to my Beer data list application. It is a page where you can check information about beers, fully based on this free API (used this one because it looked cool and had paginated resources)</p><p><a href="https://punkapi.com/documentation/v2">Punk API: Brewdog&#39;s DIY Dog as an API</a></p><p>So I want a Beer data list component that will present the info I will get from this API and someone else will do this communication for me.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/521/1*8wKr8tLkmUCIC1teyEA66g.png" /><figcaption>Small simple example of delegation of responsabilities</figcaption></figure><p>To start getting into the fun part we shall start with our component html, since it will give us a better notion of what we want structurally in terms of DOM elements.</p><p>Create your beers-data-list component as you wish, by using CLI, manually, using Angular Schematics…</p><p>A first draft of our beers-data-list.component.html should look liked this,</p><pre>&lt;div&gt;<br>  &lt;div&gt;&lt;/div&gt;<br>  &lt;table&gt;<br>    &lt;thead&gt;<br>      &lt;tr&gt;<br>        &lt;th&gt;&lt;/th&gt;<br>        &lt;th&gt;&lt;/th&gt;<br>        &lt;th&gt;&lt;/th&gt;<br>        &lt;th&gt;&lt;/th&gt;<br>      &lt;/tr&gt;<br>    &lt;/thead&gt;<br>    &lt;tbody&gt;<br>        &lt;tr&gt;<br>          &lt;td&gt;&lt;/td&gt;<br>          &lt;td&gt;&lt;/td&gt;<br>          &lt;td&gt;&lt;/td&gt;<br>          &lt;td&gt;&lt;/td&gt;<br>        &lt;/tr&gt;<br>        &lt;tr&gt;<br>          &lt;td&gt;&lt;/td&gt;<br>          &lt;td&gt;&lt;/td&gt;<br>          &lt;td&gt;&lt;/td&gt;<br>          &lt;td&gt;&lt;/td&gt;<br>        &lt;/tr&gt;<br>        &lt;tr&gt;<br>          &lt;td&gt;&lt;/td&gt;<br>          &lt;td&gt;&lt;/td&gt;<br>          &lt;td&gt;&lt;/td&gt;<br>          &lt;td&gt;&lt;/td&gt;<br>      &lt;/tr&gt;<br>      .<br>      .<br>      .<br>    &lt;/tbody&gt;<br>  &lt;/table&gt;<br>&lt;/div&gt;</pre><p>Now after consulting the forementioned API documentation I decided only a couple of properties I was really interested in for this example and so I proceeded to make a second draft of our html including already some static content as well as some css classes,</p><pre>&lt;div class=&quot;data-list-container&quot;&gt;<br>  &lt;caption&gt;Beers Pagination Implementation Example&lt;/caption&gt;<br>  &lt;table&gt;<br>    &lt;thead&gt;<br>      &lt;tr&gt;<br>        &lt;th&gt;Name&lt;/th&gt;<br>        &lt;th&gt;First Brewed&lt;/th&gt;<br>        &lt;th&gt;Description&lt;/th&gt;<br>        &lt;th&gt;Image&lt;/th&gt;<br>      &lt;/tr&gt;<br>    &lt;/thead&gt;<br>    &lt;tbody&gt;<br>      &lt;ng-container&gt;<br>      &lt;/ng-container&gt;<br>    &lt;/tbody&gt;<br>  &lt;/table&gt;<br>&lt;/div&gt;</pre><p>Html is done for now, if you notice there is a not native tag in the table body called ng-container, in a nutshell that tag is just a placeholder, it won’t ever exist a &lt;ng-container&gt; … on your DOM tree.</p><p>Moving on to our beer-data-list component per say, it will be a very simple component for this setup project.</p><pre>@Component({<br>  selector: &#39;app-beers-data-list&#39;,<br>  templateUrl: &#39;./beers-data-list.component.html&#39;,<br>  styleUrls: [&#39;./beers-data-list.component.css&#39;]<br>})<br>export class BeersDataListComponent {<br><br>  beers$: Observable&lt;any[]&gt;;<br><br>  constructor(private readonly beersService: BeersService) {<br>    this.beers$ = this.beersService.getBeers();<br>  }<br><br>}</pre><p>It will have a property called beers, it will be an observable since it will be the stream definition that will deal with the response coming out of the API. As a side note typing the observable with any might not be the most correct here but again this is definitely not the focus of these four articles.</p><p>Now to make the communication with the API and to define our stream of data or observable we shall use a service. In this case, I called it BeersService. A very simple service that makes a resquest to an API by using the HttpClient framework of Angular.</p><pre>@Injectable({<br>  providedIn: &#39;root&#39;<br>})<br>export class BeersService {<br><br>  constructor(private readonly httpClient: HttpClient) { }<br><br>  getBeers(): Observable&lt;any&gt; {<br>    const url = &quot;https://api.punkapi.com/v2/beers&quot;;<br>    return this.httpClient.get(url);<br>  }<br>}</pre><p>Again you should take very care with your services and where you do provide them. Here leaving it provided in root is insignificant, but in a more complex project the hierarquical logic of dependency injection should be taken into account.</p><p>Now to finish the implementation we have everything to complete the HTML with the correct information coming out straight of the API into the beers$ observable in the component.</p><pre>&lt;div class=&quot;data-list-container&quot;&gt;<br>  &lt;caption&gt;Beers Pagination Implementation Example&lt;/caption&gt;<br>  &lt;table&gt;<br>    &lt;thead&gt;<br>      &lt;tr&gt;<br>        &lt;th&gt;Name&lt;/th&gt;<br>        &lt;th&gt;First Brewed&lt;/th&gt;<br>        &lt;th&gt;Description&lt;/th&gt;<br>        &lt;th&gt;Image&lt;/th&gt;<br>      &lt;/tr&gt;<br>    &lt;/thead&gt;<br>    &lt;tbody&gt;<br>      &lt;ng-container *ngFor=&quot;let beer of beers$ | async&quot;&gt;<br>        &lt;tr&gt;<br>          &lt;td&gt;{{ beer.name }}&lt;/td&gt;<br>          &lt;td&gt;{{ beer.first_brewed }}&lt;/td&gt;<br>          &lt;td&gt;{{ beer.description }}&lt;/td&gt;<br>          &lt;td class=&quot;data-list-item__image&quot;&gt;&lt;img src={{beer.image_url}}/&gt;&lt;/td&gt;<br>      &lt;/tr&gt;<br>      &lt;/ng-container&gt;<br>    &lt;/tbody&gt;<br>  &lt;/table&gt;<br>&lt;/div&gt;</pre><p>Since beers$ is an observable don’t forget to use the pipe async with it since by doing this the pipe will take care of the unsubscription of the observable for you.</p><p>Just for the sake of completion I leave you here the styles I used in this component.</p><pre>:host {<br>  display: flex;<br>  justify-content: center;<br>}<br><br>.data-list-container {<br>  width: 60%;<br>}<br><br>caption {<br>  font-weight: bold;<br>  font-size: 24px;<br>  color: #333;<br>  margin-bottom: 16px;<br>  text-align: center;<br>  width: 100%;<br>}<br><br>table {<br>  border-collapse: collapse;<br>  border: 1px solid black;<br>  text-align: center;<br> vertical-align: middle;<br>  margin-bottom: 20%;<br>}<br><br>thead {<br>  color: white;<br>  font-size: 0.875rem;<br> text-transform: uppercase;<br> letter-spacing: 2%;<br>}<br><br>thead th {<br>  width: 25%;<br>  background-color: #333;<br>}<br><br>th, td {<br>  border: 1px solid black;<br>  padding: 8px;<br>}<br><br>tbody tr:nth-child(odd) {<br>  background-color: #fff;<br>}<br><br>tbody tr:nth-child(even) {<br>  background-color: #eee;<br>}<br><br>tbody th {<br>  background-color: #36c;<br>  color: #fff;<br>  text-align: left;<br>}<br><br>.data-list-item__image img {<br>  width: 100px;<br>}</pre><p>The final result of the project that will serve as a setup for the pagination looks like this,</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*w9c2TwWwiBYsdOwr6puSfQ.png" /></figure><p>Stay tuned for the next part where we will add upon this a pagination component that looks very similar to almost every example out there.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=5d4c0762b57f" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>