<?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[Station Five - Medium]]></title>
        <description><![CDATA[We consult businesses implementing the latest technologies, and develop scalable, sustainable systems to enable long term growth. - Medium]]></description>
        <link>https://medium.com/stationfive?source=rss----93af788b21cc---4</link>
        <image>
            <url>https://cdn-images-1.medium.com/proxy/1*TGH72Nnw24QL3iV9IOm4VA.png</url>
            <title>Station Five - Medium</title>
            <link>https://medium.com/stationfive?source=rss----93af788b21cc---4</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 17 Nov 2026 15:23:34 GMT</lastBuildDate>
        <atom:link href="https://medium.com/feed/stationfive" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[How to Share Sensitive Files with your Colleagues]]></title>
            <link>https://medium.com/stationfive/how-to-share-sensitive-files-with-your-colleagues-7457ee424f55?source=rss----93af788b21cc---4</link>
            <guid isPermaLink="false">https://medium.com/p/7457ee424f55</guid>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[security]]></category>
            <category><![CDATA[encryption]]></category>
            <category><![CDATA[cybersecurity]]></category>
            <category><![CDATA[hacking]]></category>
            <dc:creator><![CDATA[Matthew Marcos]]></dc:creator>
            <pubDate>Sun, 24 Feb 2019 22:29:29 GMT</pubDate>
            <atom:updated>2019-02-24T22:29:28.886Z</atom:updated>
            <content:encoded><![CDATA[<h3>How to Share Sensitive Files With Your Colleagues</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/960/1*M-njA0f_x4MNgfK8NjOI6A.jpeg" /></figure><p>If you work in a tech company, chances are you will come across the problem of sharing files that you want only one person to have access to. These files could contain secrets, such as passwords, credentials, API keys, or other sensitive information that if viewed by an unauthorised individual, could introduce a cataclysmic risk to your infrastructure.</p><p>In this article, I will introduce <em>GPG (Gnu Privacy Guard)</em> and how it can be used to encrypt a file so that only its intended viewer can access its contents. Then we will go through two hypothetical situations where this can be applied. Finally, I will touch on the basics of public key cryptography, and discuss some of its common use cases and the other problems it solves. I hope that after reading this article, you will appreciate how a few simple steps can mitigate one of the major security vulnerabilities you are possibly exposing your company to.</p><p>This is a technical article, so if you are a non-developer and struggle with the below, please feel free to leave a question in the comments.</p><h4><strong>What is GPG?</strong></h4><p>GPG is a command line tool that comes pre-installed in most linux distributions. It is used by a lot of programs — such as your package manager — behind the scenes. It allows you to generate <em>keys</em>, which are files that contain a seemingly random string of bytes, and enables you to use those keys to encrypt and decrypt files.</p><h4><strong>What do I need to start using GPG</strong></h4><p>To make use of GPG, you need to generate a key pair. By doing so, GPG will generate two files — the <em>public key</em> and a <em>private key</em>. These two files are important, so make sure you do not lose either of them. It is also important that nobody should have access your private key. I will explain why shortly, but for now let us generate our key pairs.</p><h4><strong>How to generate a public key</strong></h4><p>To generate a key pair, you must first open your terminal. Then type in the following command: gpg --full-gen-key. This is a one-off process and once you have a key pair, you do not need to make a new one unless you lose either one of them or they expire (which you’ll have the option for setting later).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/487/0*v0gJDGhcMjoyu3Aa" /></figure><p>You want to select the default (1). If you’re wondering what the other key types do, <a href="https://security.stackexchange.com/questions/72581/new-pgp-key-rsa-rsa-or-dsa-elgamal">here is a brief explanation</a>.</p><p>After this, you will be asked to select the key length you want to generate. The longer the key length, the less likely your file can be brute-forced. The trade-off is that it will take a longer time for GPG to generate your keys. Ideally, since this just a one-off process anyway, you might as well generate the longest possible key (4096).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/351/0*sJ5MIYVE6kdRaQ0S" /></figure><p>You will be prompted to select the expiration date of your key.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/343/0*DYuldJADqgkjkxKd" /></figure><p>This is something you have to decide on your own. The reason you are given the option to set an expiration date is so when your computer gets compromised (or your key does — regardless if you’re aware of it or not), your key pair will become useless after that specified amount of time, and you don’t have to worry about it anymore. I believe that one year is pretty acceptable unless you change computers more frequently.</p><p>After specifying the expiration date, you will be prompted for some of your details. Since this key is something I will be using for work, I entered my company email. It was generated by my laptop, so I will add that to the comments so it can be differentiated from my desktop computer’s key. You will then be asked to enter a password for that key. You need this password every time you want to decrypt something, so make sure you do not forget it. <a href="https://www.youtube.com/watch?v=Pe_3cFuSw1E">Here’s a good guide on making secure passwords you won’t forget</a>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/320/0*u_tT4-KHkKE-0YyC" /></figure><h4><strong>Sharing your public key</strong></h4><p>At this point, GPG should have generated your key pair. If you want people to send you files that only you can read, you need to give them your <strong>public key</strong>. They will use your public key to encrypt the file so that it is converted into a string of seemingly random bytes that will be useless to everybody but you. It does not matter if the whole world sees the encrypted file — they won’t be able to decipher its contents — unless they have the <strong>private key</strong> that is matched to the public key used to encrypt the file. This is why you should not share your private key (ah I could not stress this enough).</p><p>Ideally you want people to have access to your public key. It is a good idea to attach it to your company email signature. However, there are trusted public registries that can make your public key accessible to everybody. It is like an online phone book and it maps your public key’s id (more on this later) to your public key. If someone wants to send you a file, they can just search for your public key id in that registry then they can encrypt files for you.</p><p>To get your public key, type in gpg --armor --output public.gpg --export &lt;email&gt; in your terminal. In your working directory, you should see <em>public.gpg</em>, which is your public key. This is the file you want to share with your officemates. Your public key should look something like this:</p><pre>-----BEGIN PGP PUBLIC KEY BLOCK-----</pre><pre>mQINBFxqeqwBEADltw2GDw3j+QYdT3hpSb1xyYao9GvqF8D1zxQ09LJTrlOpZ9YR<br>qVjIBGagcEwhHbEfKwTcwCLRDdK0gsKkfVnUEo16vPVvRP5BwFkLsQ5V2PkUqZqo<br>aB3rdGiYDfBngf2SRp9H0LWKdElE3splRh5PbgpIm4B1E6pm6Vw9egj5qf0cBnTI<br>Eti3+xjDDmibsn1o2q8XqHiO4iDRYWt6BGAfXBO59vOmKfRALh56YCPdgPWqTyf3<br>... &lt;more text&gt;<br>-----END PGP PUBLIC KEY BLOCK-----</pre><p>To get your public key id, type gpg --list-secret-keys. You will get an output similar to the screenshot below. Your id is the one encircled in red.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/671/0*EN6gAaIuSOuiQxmj" /></figure><p>One of the popular registries is <a href="https://pgp.mit.edu/">https://pgp.mit.edu</a>. You can submit your public key there. You then distribute your public key id among your colleagues — you can make an internal catalog for your company — and whenever they need to send you a secret, they just search for that id in the registry. Pretty neat, eh?</p><p>When someone wants to send you a file, they will have to encrypt it using your public key. Regardless of how they obtain your public key, once it is in their computer, they can import it with the command gpg --import &lt;public key filename&gt;.</p><p>They should get something like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/767/0*8g1-HxHWomknrk7C" /></figure><h4><strong>How to encrypt a file with a public key</strong></h4><p>Once the sender has imported the recipient’s public key, they can encrypt a file with the command gpg --output &lt;encrypted file&gt; --encrypt --recipient &lt;receivers_email&gt; &lt;secret file you want to send&gt;</p><p>In our case let’s make a super secret file.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/382/0*dnqLeq4K2Pr0XicQ" /></figure><p>Then we encrypt it.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/935/0*waNUm5TJqmroCJ5B" /></figure><p>At this point, it should have generated an encrypted version of your secret file. This is the file they will want to send you because it won’t be useful to anybody else.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/385/0*Tn_XUc52imqqm5Nr" /></figure><p>Just for comparison, let’s take a look at their file contents.</p><p>They can email the encrypted file to you and only you can decrypt it.</p><h4><strong>How to decrypt a file</strong></h4><p>Once you have received the encrypted file, viewing its contents is easy. You could run gpg --decrypt &lt; nuclear_launch_codes.txt.gpg. GPG will prompt you for your password and display the decrypted contents to your screen. If it is a larger file, you can use the --output option to output the contents to a file.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/541/0*R4Re4WFlI5aflX-D" /></figure><p>With the --output option:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/543/0*bCB_xw4PWU5sCxBz" /></figure><p>Checking for the contents:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/174/0*Fd4tTLcYQXNqS-wk" /></figure><p>There are many reasons to use encryption like these. First, there is the possibility of your email or whatever medium you use to send files being hijacked. Second, if the file was mistakenly sent to the wrong person, then you’re screwed. Finally, god forbid, you forget to lock your computer when you use the washroom in a coffee shop and someone decides to clone your hard drive (how overly paranoid can security engineers be?). At least they cannot decrypt the secret file even with your private key if they don’t have your password.</p><h4><strong>How will these risks be minimized when encryption has been used?</strong></h4><p>Let’s go through two hypothetical situations where we can use GPG to share secrets.</p><p>The first is a System Administrator giving a Software Engineer the credentials to the admin account of a client’s website in production. Ideally you want to control who has access to these credentials. You cannot just type sensitive information like these into your company’s slack chat. When any member of the team backscrolls a little too much, there’s the possibility that it could catch the eye of a nearby individual with malicious intent.</p><p>The second is a lead software engineer sending a performance evaluation report of his team members to his immediate manager. As the lead software engineer, you are in constant communication with your team, and there’s the possibility of sending this report to the wrong person (who is directly involved with its contents). You want to minimize the consequences of human errors like these when they happen.</p><h4><strong>What is public key cryptography?</strong></h4><p>What we used here is under what is called <em>public key cryptography</em>, the crux of which is a public and private key are mathematically generated such that you can use the public key to encrypt something and only the person with the private key can decrypt it. There’s no real issue with your public key being disseminated across the interwebz but the problem that this poses is ensuring the validity of the public key (Who really owns this key I am about to use?) — see <a href="https://en.wikipedia.org/wiki/Man-in-the-middle_attack">man in the middle attack</a>. Therefore, third party registries like the aforementioned <a href="https://pgp.mit.edu/">pgp.mit.edu</a> were made to combat this issue.</p><h4><strong>Where else has public key cryptography been used?</strong></h4><p>Public key cryptography is used in the widely popular SSH and is a basis for TLS and other things we take for granted these days.</p><p>At any rate, I hope that you appreciate how you can save yourself (and your company) from a lot of the risks that sending in clear-text poses.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7457ee424f55" width="1" height="1" alt=""><hr><p><a href="https://medium.com/stationfive/how-to-share-sensitive-files-with-your-colleagues-7457ee424f55">How to Share Sensitive Files with your Colleagues</a> was originally published in <a href="https://medium.com/stationfive">Station Five</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to create a pie chart with D3.js and React Hooks (Part 1)]]></title>
            <link>https://medium.com/stationfive/how-to-create-a-pie-chart-with-d3-js-and-react-hooks-part-1-81bcd7f39b32?source=rss----93af788b21cc---4</link>
            <guid isPermaLink="false">https://medium.com/p/81bcd7f39b32</guid>
            <category><![CDATA[react]]></category>
            <category><![CDATA[d3js]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[data-visualization]]></category>
            <category><![CDATA[hooks]]></category>
            <dc:creator><![CDATA[Kihwan Cho]]></dc:creator>
            <pubDate>Wed, 30 Jan 2019 06:17:58 GMT</pubDate>
            <atom:updated>2019-01-30T06:17:58.404Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*VbCODhoUK8ADdeXWeiXHMQ.png" /></figure><p><a href="https://d3js.org/">D3.js</a> is one of the most popular JavaScript libraries for data visualization and is used widely with React. When creating these visualizations using D3 and React, the component often needs to have internal state and as a result are Class components. It is very difficult to have these graphs as functional components.</p><p>However, with the introduction of Hooks and supporting SVC elements, we now have a way to create these visualizations as functional components and no longer have to deal with Class components (down with classes)!</p><p><strong>The purpose of this blog post is to show you how much simpler (and easier) it is to create graphs with D3 using React Hooks instead of Class Components.</strong></p><p><em>*** Spoiler Alert ***</em></p><p>The code is reduced by more than half!</p><p><strong>In this article</strong>, we are going to describe how to</p><ul><li>Install React 16.8.0 alpha 1, d3 and react-spring</li><li>Generate a function which can generate random data</li><li>Create a pie chart with an update function using React class</li><li>Create a pie chart with an update function using Hooks</li><li>Replace d3 functions with SVG elements</li><li>Improve a chart with attributes</li></ul><p><strong>In the next article</strong>, the following items will be done.</p><ul><li>Cache the previous value to add animations using useEffect</li><li>Add animations using d3 transition functions</li><li>Add animations using react-spring</li></ul><h3>What we will be building</h3><p>If you want to have a play with the pie graphs before reading, here’s the live example. Please do not eat :)</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcodesandbox.io%2Fembed%2Fr5wp0v08xq&amp;url=https%3A%2F%2Fcodesandbox.io%2Fs%2Fr5wp0v08xq&amp;image=https%3A%2F%2Fcodesandbox.io%2Fapi%2Fv1%2Fsandboxes%2Fr5wp0v08xq%2Fscreenshot.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=codesandbox" width="1000" height="500" frameborder="0" scrolling="no"><a href="https://medium.com/media/5e8778265a51ccff530fa022ebf5545f/href">https://medium.com/media/5e8778265a51ccff530fa022ebf5545f/href</a></iframe><h3>Getting started</h3><p>First of all, let’s install the <a href="https://github.com/facebook/create-react-app"><strong>Create React App 2.0</strong></a>, simply running the command below.</p><pre>npx create-react-app my-app</pre><pre>cd my-app</pre><p>As we want to use Hooks which haven’t been officially released at this moment, we need to update <strong><em>react</em></strong> and <strong><em>react-dom</em></strong> to <strong>16.8.0-alpha.1</strong></p><pre>yarn add react@next react-dom@next</pre><p>And we also need <strong><em>d3</em></strong> and <strong><em>react-spring</em></strong>.</p><pre>yarn add d3 react-spring</pre><h3><strong>index.js</strong></h3><p>We are going to create a function that can generate random data in <strong>index.js</strong>. generateData function will randomise data every time we click a button. Let’s take a look at the code below.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9ea382864d9df93eb2a737aeffb0b111/href">https://medium.com/media/9ea382864d9df93eb2a737aeffb0b111/href</a></iframe><p>Oh look, our first hook! We use the useState hook and generateData function to generate the random data and store it as the state variable data. The useState hook is used to manage local state in function components.</p><p>Then every time the button is clicked, it will run the changeData function where it will change the value of data through the setData function.</p><p>If you are still unsure of the useState hook please refer to the <a href="https://reactjs.org/docs/hooks-intro.html">React docs</a> or <a href="https://medium.com/stationfive/how-to-migrate-from-recompose-to-react-hooks-89b2981c03d">this article</a> which has a more in depth explanation.</p><p>This data variable is passed to the 3 components below.</p><ol><li>PieClass → A class component that will render a Pie Graph</li><li>PieHooks → A functional component using hooks that will render a Pie Graph</li><li>PieSVG → A Pie Graph using just SVG elements (supported since React 16.6)</li></ol><h3><strong>Pie chart using React Class</strong></h3><p>Let’s have a look at the Pie Chart using Class Components.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1ac1ae54c649886dab8e7d9d50df12bd/href">https://medium.com/media/1ac1ae54c649886dab8e7d9d50df12bd/href</a></iframe><p>If you looked at the code above and thought “well the componentDidMount and componentWillUpdate(or componentDidUpdate) look awfully similar.” Then 10 points for you! The code is almost identical and I don’t know about you but I’d rather not have to repeat my code.</p><p>Let’s look at what each of them are doing. The code in componentDidMount will render an initial state for the pie graph, and the code in componentWillUpdate will update the nodes instead of appending the nodes again.</p><p>In both lifecycle methods we take advantage of the d3 methods assigned in the constructor.</p><ul><li><a href="https://github.com/d3/d3-shape/blob/master/README.md#pie">d3.pie()</a> takes an array of data and will generate an array of objects which contains startAngle, endAngle, data, index and so on.</li><li><a href="https://github.com/d3/d3-shape/blob/master/README.md#arc">d3.arc()</a> takes innerRadius, outerRadius, startAngle and endAngle and will return a string value of coordinates of each item which can be used in &lt;path&gt; object. So, this can consume data generated from d3.pie().</li><li><a href="https://github.com/d3/d3-shape/blob/master/README.md#arc_centroid">arc.centroid()</a> function will return x, y values which mean the centre of each arc.</li></ul><p>Although this renders the pie graph, to me it is annoying that we need to have a componentDidMount to handle the initial state and a componentWillUpdate to handle any data that has updated. The code is almost identical in both lifecycles and if you are not aware of the <a href="https://github.com/d3/d3-selection/blob/master/README.md#selection_exit">exit</a>, <a href="https://github.com/d3/d3-selection/blob/master/README.md#selection_exit">remove</a> and <a href="https://github.com/d3/d3-selection/blob/master/README.md#selection_merge">merge</a> pattern it is also quite confusing as to how we handle updated data for the graph.</p><h3><strong>Pie chart using React Hooks</strong></h3><blockquote>In this step, we will avoid code duplication using Hooks.</blockquote><p>So how do we solve the problem above? Well with hooks of course! Using hooks we can replace the class component with a functional component.</p><p>To avoid code duplication, we will look at using the <strong>useEffect </strong>hook. The <strong>useEffect</strong> hook comes into play for lifecycle methods / side-effects which are usually used for interactions with the Browser/DOM API.</p><p>The <strong>useEffect </strong>hook will take responsibility for both componentDidMount and componentDidUpdate so we can avoid code duplication as codes below.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e295d34c57d70e09ed9549d7f722f81a/href">https://medium.com/media/e295d34c57d70e09ed9549d7f722f81a/href</a></iframe><p>So how does it encapsulate both componentDidMount and componentDidUpdate?</p><p>When you use the <strong>useEffect</strong> hook you are telling React that your component needs to do something <strong>after render</strong>. React will remember the function you passed and will call it <strong>after the DOM updates.</strong></p><p>You can see that in the second argument of useEffect we pass an array with the value [props.data] . We are saying that every time the value of [props.data] changes run the function we passed. If it does not change, do nothing!</p><p>The other hook is the useRef this hook replicates what React.CreateRef() does and provides us a way to access DOM nodes. This allows us to target the &lt;g&gt; in our useEffect.</p><p>You can see compared to the class component, the code is more concise, easier to understand and no duplication!</p><h3><strong>The simplest way using SVG elements</strong></h3><blockquote>In this step, to reduce mutations, we try to use D3 as sparingly as possible.</blockquote><p>Instead of using append() and attr() functions from D3, we can simply map over the data and return SVG elements with its attributes as &lt;path d=“…” … /&gt;. In this case, readability can be increased with less state mutations. We don’t even need Hooks for now. (Please note that Hooks will be used when we apply animations later on)</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8b76506a4331b60b265f55212761d45e/href">https://medium.com/media/8b76506a4331b60b265f55212761d45e/href</a></iframe><p>You can see that we just pass the functions and data to the &lt;Arc&gt; component which is just SVG elements. We’ve eliminated the use of append() and attr() functions from D3.</p><h3>Class vs Hooks vs SVG Elements Difference</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*O9NrQKXikErOQ2cBBqqktQ.png" /><figcaption>Class Component vs Functional Component (using hooks) vs SVG Elements</figcaption></figure><p>As you can see from the images above, the functional components using Hooks and the functional component just using SVG elements is significantly less than the class component. Not only that, it is a lot nicer to read and understand. Please note that for Part 2 we will be using the functional component using Hooks since we want to introduce animations!</p><h3><strong>Conclusion</strong></h3><p>Thank you for reading this post. We hope this article helps you to understand how D3.js can be implemented in the React world. <strong>Next time</strong>, we will add some animations with and without using d3.transition based on what we made in this article. Watch this space!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=81bcd7f39b32" width="1" height="1" alt=""><hr><p><a href="https://medium.com/stationfive/how-to-create-a-pie-chart-with-d3-js-and-react-hooks-part-1-81bcd7f39b32">How to create a pie chart with D3.js and React Hooks (Part 1)</a> was originally published in <a href="https://medium.com/stationfive">Station Five</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How To Create a Paragraph Template within Drupal 8]]></title>
            <link>https://medium.com/stationfive/how-to-create-a-paragraph-template-within-drupal-8-2203f6ed6c3b?source=rss----93af788b21cc---4</link>
            <guid isPermaLink="false">https://medium.com/p/2203f6ed6c3b</guid>
            <category><![CDATA[php]]></category>
            <category><![CDATA[drupal-8]]></category>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[drupal]]></category>
            <category><![CDATA[software-development]]></category>
            <dc:creator><![CDATA[Matthew Marcos]]></dc:creator>
            <pubDate>Wed, 23 Jan 2019 21:25:14 GMT</pubDate>
            <atom:updated>2019-01-23T21:27:22.366Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*FMB2RuL-Dx2y4vtps4c39A.jpeg" /></figure><p>When designing a Drupal website, it’s not uncommon to see pages that share similar components. From the content creator’s perspective, it might be tedious to reproduce the same layout in a WYSIWYG editor each time a new page has to be made. The process of manually creating something that should be consistent across the website also introduces the chance of human error.</p><p><a href="https://www.drupal.org/project/paragraphs">Paragraphs</a> is a Drupal module that allows you to break down any content type into reusable components. It allows us to group relevant data fields into <strong><em>paragraphs</em></strong> and associate an HTML template to it so the content creators do not need to worry about the layout.</p><h3>Prerequisites</h3><p>For this tutorial, you will need the following on hand:</p><ol><li><a href="https://drupalconsole.com/">Drupal Console</a></li><li>A fresh Drupal installation</li><li>Some HTML and CSS know-how</li><li>Around 1 hour of your time</li></ol><h3>Breaking down your page into paragraphs</h3><p>In this tutorial we will create two pages with the following layouts:</p><p><a href="https://drive.google.com/open?id=1KlH8KU1ycEf-cfEGbu1o_Gq_G0p48f9V">Page 1</a>:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/0*M0TUWus-NwVstq8t" /></figure><p><a href="https://drive.google.com/open?id=1-Q6Afmf9jjsUOJOjG4T3dL7eDELGIJix">Page 2</a>:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/0*Jz3pzjIlwtGpmQ44" /></figure><p>Before we start making the actual paragraphs, we need to identify the sections of the pages that can be reused. Those will be made into paragraphs.</p><p>In our two-page example, there are <strong>three</strong> unique components that occasionally appear.</p><p>They are:</p><ul><li>The hero image (Hero Image)</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/570/0*OKHpA-ras6HS97T2" /></figure><ul><li>The image pair (Image Pair)</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/564/0*AV3OunVYYXcg6IFd" /></figure><ul><li>The image with a text article to its right (Image with text)</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/574/0*vbGhdMSgMoEiPLCw" /></figure><h3>Create the paragraphs in your admin panel</h3><p>As I mentioned earlier, paragraphs are essentially groupings of fields that we can assign a template to. We need to determine which fields belong to each paragraph that we will make.</p><p>If you’re not familiar with the concept of fields (<a href="https://www.drupal.org/docs/user_guide/en/planning-data-types.html">documentation here</a>), it is imperative that you spend some time studying them because they play a big role when managing content in Drupal 8.</p><p>The hero image is pretty simple. We only need an image field to upload what we want to display. The Image pair consists of two image fields, and the image with a text article consists of an image field and a long text field.</p><p>To create a paragraph, log into the admin panel and go to Structure &gt; Paragraph Types.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/527/0*azUtCJzFkbEHvazH" /></figure><p>You will have to repeat the following process for each of the three paragraph types</p><ol><li>Click <strong>Add paragraph type </strong>and give it a label. Labels should be self-descriptive because you’ll use them to reference that certain template for the perpetuity of the project. For the hero image, I used “Hero Image” and so forth.</li><li>Click <strong>Save and manage fields</strong> to select the fields you want to bundle together in that paragraph. You’ll have to add each field one-by one.</li><li>First, they ask you the type (similar to data type in other programming languages) and a unique label which we will use later.</li><li>Second, they ask you the number of values of that field type that paragraph will have. For all of the paragraphs we will make, you need to limit this to <strong>one</strong>. The way it works is when creating a new paragraph, if a field is set to unlimited, the content creator can add an unlimited number of those fields. I recommend checking it out in your free time.</li></ol><p>After making all three paragraphs, we end up with something like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*3kRAYRnN7Aq0LCGx" /></figure><p>The fields I made were as follows:</p><p><strong>Hero Image</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*M1SIpOWW2OSjUa72" /></figure><p><strong>Image Pair</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*tiiNhASFMChVdR9Mm8aoLQ.png" /></figure><p><strong>Image with text fields</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*js3WYs3wi22RaLy9JgXiwA.png" /></figure><p>Aside: <a href="https://youtu.be/Fy9s01T30uo">Here</a>’s an excellent tutorial that includes more details about using paragraphs as the admin.</p><h3>Create the pages using the paragraphs we made</h3><h4>Attaching our new paragraphs to the Basic Page</h4><p>In order to use your new paragraphs, you have to attach it to a content-type. Since we want to make pages with our paragraphs, we have to attach them to the <em>Basic Page </em>content-type, which was already made for you when you installed Drupal.</p><p>To do this, navigate to Structure &gt; Content Types &gt; Basic page in the admin panel and add a field of type Paragraph.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*tcmJK3Xrdw_rEJDp" /></figure><p>Since you want your user to add paragraphs without any constraints, make sure that the <em>allowed number of values</em> is set to Unlimited. Select <em>Save field settings</em>.</p><p>You will be shown a screen that will allow you to set the <em>reference type</em> of the paragraph you have newly created. This allows you to select which paragraphs a content creator can add to the <em>Basic page</em>. In our case, we want our content creators to use all the paragraphs we had just created.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/639/0*8zCkOJsBaveZC15A" /></figure><p>At this point, we are ready to use the paragraphs we had just made. First, let’s tidy up the basic page.</p><p>Go to <em>Manage form display</em> and set it up like so:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*vzFpVzJ2SV1iR4a0" /></figure><p>We are disabling the body tag because we don’t need it in any of our pages (kindly refer to our wire-frames).</p><p>Go to <em>Manage Display</em> and disable the <em>Body</em> and <em>Links</em> fields like so:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*X4jSg7LryHJzFqWt" /></figure><h3>Creating the actual pages</h3><p>To create a new page, click Content &gt; Add Content in the admin navigation bar and select Basic Page. Add the paragraphs needed to make page 1 and page 2.</p><p>For page 1 it should look something like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/921/0*K2NeeGFQbSc4TibF" /></figure><p>Regardless of your base theme, when you’re done, your pages will look something like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/895/0*ycNmFsvEQlFZLf4y" /></figure><p>All the elements are stacked on top of each other because the default template that drupal uses simply dumps everything to your screen. To fix this, we need to override the default template.</p><p><em>Note: At this point, it is probably a good idea to </em><a href="https://www.drupal.org/docs/8/theming/twig/debugging-twig-templates"><em>activate developers mode</em></a><em> to make twig debugging easier.</em></p><h4><strong>Creating the HTML templates</strong></h4><p>It is a good idea to identify which template renders each section on your page. We do this by opening the developer’s console in your browser after activating developers mode.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/421/0*wGFRrYRcIlgLgQwe" /><figcaption>Look at the “BEGIN OUTPUT from&quot; comment</figcaption></figure><p>We can observe a couple things from this snippet:</p><ol><li>Each paragraph gets rendered in a default template (<em>paragraph.html.twig</em>)</li><li>Drupal is kind enough to recommend the names we need to use when overriding the default template. The naming conventions used are <a href="https://www.drupal.org/docs/8/modules/paragraphs/theming-in-paragraphs-for-drupal-8">documented here</a>.</li></ol><p>In your theme, make a sub-folder called <em>paragraphs</em> (the name of our module) and within it, a folder called <em>templates. </em>If you do not have a theme yet, read <a href="https://www.drupal.org/docs/8/theming-drupal-8/creating-a-drupal-8-sub-theme-or-sub-theme-of-sub-theme">this article</a> about how it works. The easiest way to make a sub-theme is to <a href="https://youtu.be/Si8azuZig78">generate one with Drupal Console</a>! In this example, I <a href="https://www.drupal.org/docs/8/theming-drupal-8/creating-a-drupal-8-sub-theme-or-sub-theme-of-sub-theme">made a sub-theme of</a> the <a href="https://www.drupal.org/project/bootstrap">bootstrap theme</a>.</p><p>The file naming convention for paragraph templates are <em>paragraph — &lt;paragraph label&gt;[…mode].html.twig.</em> I find that replacing the hyphen in the <em>paragraph label</em> portion with an underscore makes it work.</p><p>For each paragraph, copy the default template (from modules/contrib/paragraphs/templates/paragraph.html.twig), put it in the <em>templates</em> folder and rename them to their recommended filenames. Our file structure should look like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/324/0*jXrXDA0Pn8q3TIhG" /></figure><p>After this, clear your Drupal cache and reload your page. Check your developer’s console to determine which template generates your code. Right now, it should be the new templates you just made.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/971/0*zzF5I_ZAcHdt2gLM" /></figure><p><em>Aside: It does not actually matter which sub-folder you put your .html.twig files in your theme — as long as they are inside the templates sub-folder. Drupal will always find your templates because it looks for them by name.</em></p><h4><strong>Modifying the HTML templates</strong></h4><p>At this point, you can edit your html templates. You also want to <a href="https://www.drupal.org/node/2216195">add custom css and js</a> to your sub-theme. Here’s <a href="https://www.youtube.com/watch?v=0hIfdPc6BOw">another guide</a> for doing so.</p><p>It is obviously important to have access to the data you’ve set up in the admin panel earlier so you can use them. The idea is you are given a <em>content</em> object in the template and you can access the attributes using the <em>machine name</em> of the property you want. If you want to review the properties available to you, go to the admin panel and click <em>manage fields. </em>Here’s the documentation for that.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/214/1*1XI9vxx_fcRgrTtoIwhONA.png" /></figure><p>In this paragraph, you can use {{ content.field_image }} and {{ content.field_image_text }} in your template</p><p>I’ve done some crude theme-ing just to get the layout correctly and hid the blocks with css for this example (since we are focused on making templates for the paragraphs module itself). In an actual site, you might want to do this the <a href="https://www.drupal.org/docs/8/api/layout-api/how-to-register-layouts">proper way</a>.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1f743913e6debd49f4426323db03b0b2/href">https://medium.com/media/1f743913e6debd49f4426323db03b0b2/href</a></iframe><h3><strong>Comparing the end result</strong></h3><p>Here’s the original side-by-side layout for the pages we wanted to make:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*Tyru7opDCpuldrtS" /></figure><p>And here’s the side-by side result of our code:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*nFPnbgBz7fq3IdW0" /></figure><p>It’s not 100% exact, but you can tweak the css more (or change the image dimensions) to get the layout exactly as you wish.</p><h3><strong>Next steps</strong></h3><p>Since a paragraph is just a collection of fields that we can attach to a content type as another <em>field</em>, it makes sense that we can also add a <em>paragraph</em> to a <em>paragraph</em>.</p><p>Can you imagine use cases for these?</p><h3><strong>Conclusion</strong></h3><p>Once properly implemented in your site, paragraphs will save you and your team countless templating hours in the long term.</p><p>I hope that this article helped you realize how a purportedly complex CMS like Drupal can be made simple for content creators and developers alike.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2203f6ed6c3b" width="1" height="1" alt=""><hr><p><a href="https://medium.com/stationfive/how-to-create-a-paragraph-template-within-drupal-8-2203f6ed6c3b">How To Create a Paragraph Template within Drupal 8</a> was originally published in <a href="https://medium.com/stationfive">Station Five</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Becoming a better JavaScript developer with  Functional Programming]]></title>
            <link>https://medium.com/stationfive/becoming-a-better-javascript-developer-with-functional-programming-3cfe26a392fe?source=rss----93af788b21cc---4</link>
            <guid isPermaLink="false">https://medium.com/p/3cfe26a392fe</guid>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[functional-programming]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[development]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[I’m only here now]]></dc:creator>
            <pubDate>Tue, 22 Jan 2019 06:26:46 GMT</pubDate>
            <atom:updated>2019-01-22T06:26:46.536Z</atom:updated>
            <content:encoded><![CDATA[<h3>Becoming a better JavaScript developer with Functional Programming</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*WRs0ucpR7BWoFXvwZT-AHg.jpeg" /></figure><p>There’s been a lot of buzz around functional programming over the last few years, and there’s a lot of sanity to be gained by jumping onboard, but only if you keep yourself in check.</p><h3>What is Functional Programming?</h3><p>I see Functional Programming (FP) as a philosophy. A set of principles that form an overarching approach. We’ll explore these principles below, in brief:</p><ul><li>Abstracting procedures and logic to smaller <em>deterministic, pure </em>functions.</li><li>Maintaining <em>immutability </em>of data.</li><li>Passing functions as values. Since functions are on the same level as variables in javascript, they can be passed around and called elsewhere.</li><li><em>Composition </em>and <em>partial application </em>of functions. This becomes almost a benefit from writing deterministically and immutably, and thus the style unfolds from here.</li></ul><p><em>(See the end of the article for an explanation of terms, if you are unfamiliar.)</em></p><h3>JavaScript and how FP helps</h3><h4><strong>Variable mutability vs immutability</strong></h4><p><strong><em>The problem:</em></strong><br>Like many traditional languages, JavaScript variables can be mutated (even ones defined with const). This can seem like a freedom at first, but freedom quickly devolves into chaos.</p><blockquote>Dealing with the possibility that variable values could be changed impacts not only the code you write, but the way you think while developing.</blockquote><p>It demands wider cognitive focus, maintaining awareness of the entire application architecture, rather than simply focusing on the code within your scope.</p><p><strong><em>The solution:<br></em></strong>Introduce a ‘no mutation’ policy in your project.</p><p>This can be assisted by using an immutable data structure library such as ImmutableJS, ramda or mori, or simply deep-freezing native javascript objects and arrays.</p><p>We employ eslint rules to throw errors if code mutates variables (see <a href="https://github.com/jhusain/eslint-plugin-immutable">eslint-plugin-immutable</a>). This suits us great as it provides a first line of defence, and if one of the devs thinks a procedure would be accomplished more easily by mutation, it draws attention to this and we can discuss as a team as to whether it is safe and if there are benefits.</p><p>The <a href="https://github.com/mweststrate/immer">immer</a> library also provides an interesting angle to safely constrain mutation within a closure.</p><h4><strong><em>Lexical scoping </em></strong>and <strong><em>global variables</em></strong></h4><p><strong><em>The problem:<br></em></strong>Scope inheritance in javascript poses the risk of namespace collisions, and further exacerbates unexpected mutation of data (if there aren’t strict measures to prevent mutation across the project).</p><p><strong><em>The solution:<br></em></strong>Using a module system helps a lot with namespace collisions, but within a module file you still have to ensure your code plays nicely.</p><p>Wherever possible, break functionality into smaller, stand-alone, pure functions that only reference their own arguments.</p><p>Don’t get too hung up on this and start rejecting any PR that references variables outside of block scope, nor do you need to aim for every one of your module files to be 3 lines long.</p><p>Strike a balance and stay productive.</p><p>As a rule of thumb, if your files gets over 100 lines long, or you’re nesting functions that are more complex than a fat-arrow one-liner, try abstracting something to another function or file/component.</p><h4>Working asynchronously / Promises</h4><p><strong><em>The problem:</em></strong><br>Having to be aware of when a function call is going to return a Promise is not always easy; maybe it started out synchronous, and at some point, requirements added a need to make an asynchronous call. Now any code calling that function has to also change to accommodate the Promise.</p><p><strong><em>The solution:<br></em></strong>Promises are still a significant step towards functional programming, as they are <em>monads</em> (or monad-like, depending on who you ask), which are a data structure that carries metadata about its own state.</p><p>The async/await pattern is a breath of fresh air to avoid callbacks, however you’re still stuck returning a Promise, when only a portion of your return data might be asynchronous. It also still requires a catch block for rejections.</p><p>RxJS provides an alternative paradigm for having synchronous and asynchronous code written in a more consistent way, as well as a stash of tools to pimp your code.</p><p>We’re working on a major update to our in-house framework that will blur the line between Promises and monads, hopefully soothing some headaches and providing a consistent approach.</p><h4>Dynamic typing</h4><p><strong><em>The problem:<br></em></strong>Types in JavaScript are out of sight and out of mind, until you hit an Uncaught TypeError: somePrototypeMethod is not a function. Whoops, I thought that string had already been cast to a number…</p><p>Using false or null as an exception state is a hacky quick fix we’ve all made before, which saves time on the spot, but can introduce bugs down the line.</p><p><strong><em>The solution:<br></em></strong>Straight up, you need to be using TypeScript, Flow typed or an alternative typing system. If you think that sounds too hard to work with, it just proves that either you already don’t know what type your variables are, meaning you’re going to introduce bugs, or your variables can be one of many types, meaning you’re going to introduce bugs.</p><p>Using <em>monads</em> is a great way to keep track of exception states without fuzzy types or throwing errors.</p><h3>Let’s write some functional code</h3><p>Using a <strong>naive, front-end pseudo-framework</strong>, we’re going to render a list of users.</p><p><strong>The old, imperative tangle:</strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f0cd4263d09db32598e7ccbfd9fd0257/href">https://medium.com/media/f0cd4263d09db32598e7ccbfd9fd0257/href</a></iframe><p>Although it’s a small function, your eyes have to dart up and down to mentally execute the code and figure out what is in scope, and what values variables will have at the time of execution.</p><p>If you caught the hasAFriend undefined bug, then well done. Your eyes darted correctly.</p><p><strong>Converted to a Functional + Typed approach:</strong></p><p>The approach below is a realistic example of techniques we use, though usually on more complex problems.</p><p>We’re going to use <em>currying, passing functions as values</em> a <em>Maybe monad </em>and<em> partial application </em>(through calling a curried function),</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/72847a91772469f4250f9caa9575e91f/href">https://medium.com/media/72847a91772469f4250f9caa9575e91f/href</a></iframe><p><strong><em>Currying and partial application<br></em></strong>On line 6, we <em>curry</em> our function renderUser, and on line 16 we call it to partially apply the configuration path to the user profile page. <em>(This results in a function which takes </em><em>(friendId: Maybe&lt;string&gt;, user: User) as arguments)</em>. Doing this avoids passing it through functionalRender just so renderUser has access it, but also means that renderUser is pure.</p><p><strong><em>Passing functions as values</em></strong><br>On line 19, we call _.get (another curried function) from lodash/fp with [&#39;isFriend&#39;] to create a new function, which we pass straight to users.includes to check if any users are friends.</p><p><strong><em>Maybe monad</em></strong><br>On line 24 we construct a Maybe <em>monad</em>, which will contain the value if it exists.<br>On lines 8–9 we map the monad value, and provide a fallback through orElse.</p><p>This should all be taken as an analogy for your broader architecture and more complex problems, where each piece of the puzzle is self-contained and a black box to other components. Functions are ignorant of the inner workings of other functions, and are pure and deterministic.</p><p>External dependencies like reading from config / data, and side-effectful actions like API calls can be injected in a thin layer above the meatier code, whether that is business or display logic. This opens up the possibility of code optimisation such as memoization, and makes testing much easier.</p><h3>Lastly, just take it easy</h3><p>Functional Programming needn’t be void of curly braces, and it shouldn’t be a badge of honour if your code is.</p><p>In our example, wended up with a longer file, introducing two libraries and required transpiling (for type-checking). These facts are valid arguments to keep things simple, however as your project scales to non-trivial size, increased structure does translate to simplicity and will gain you momentum.</p><p>FP should focus on:</p><ul><li>Readability</li><li>Maintainability</li><li>Reducing bugs (and alongside this, improving debuggability)</li></ul><p>Avoid the trap of playing code golf. If your FP code is too intricately trimmed and reduced to be able to refactor easily, you’re FP-ing wrong.</p><blockquote><strong><em>Terminology</em></strong></blockquote><blockquote>A <strong>Deterministic</strong> function will always return the same result given the same arguments.</blockquote><blockquote><strong>Pure</strong> — Function purity includes determinism, and also means there are no side-effects — nothing happens outside of the function (e.g. http requests, modifying html, global variables).</blockquote><blockquote><strong>Immutability</strong> means not changing the value of existing variables.</blockquote><blockquote><strong>Curried Functions </strong>are functions that accept only one argument and return a function which would otherwise be the second argument (etc). It allows for easy and immediate partial application.</blockquote><blockquote><strong>Partial Application<em> </em></strong>is ‘baking in’ or setting one of a function’s parameters, to create a new function which takes fewer arguments.</blockquote><blockquote><strong>Composing</strong> functions means chaining them together to create one function that calls each of them in sequence, passing the return value of one to the next.</blockquote><blockquote><strong>Monads</strong> are a container data structure, keeping track of a value, metadata about the value such as whether it represents a success or failure, and provides an interface to access the data in a consistent and safe way.</blockquote><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3cfe26a392fe" width="1" height="1" alt=""><hr><p><a href="https://medium.com/stationfive/becoming-a-better-javascript-developer-with-functional-programming-3cfe26a392fe">Becoming a better JavaScript developer with  Functional Programming</a> was originally published in <a href="https://medium.com/stationfive">Station Five</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to Integrate Your Application With Salesforce]]></title>
            <link>https://medium.com/stationfive/how-to-integrate-your-application-with-salesforce-cd0636894373?source=rss----93af788b21cc---4</link>
            <guid isPermaLink="false">https://medium.com/p/cd0636894373</guid>
            <category><![CDATA[salesforce-integration]]></category>
            <category><![CDATA[salesforce]]></category>
            <category><![CDATA[salesforce-development]]></category>
            <category><![CDATA[software-integration]]></category>
            <category><![CDATA[vscode]]></category>
            <dc:creator><![CDATA[Reggie Pangilinan]]></dc:creator>
            <pubDate>Fri, 11 Jan 2019 04:37:09 GMT</pubDate>
            <atom:updated>2019-01-11T04:37:09.536Z</atom:updated>
            <content:encoded><![CDATA[<p>In a project I’m currently working on, one of the requirements is to integrate our application to a Salesforce organisation instance. Basically, I’ll need to pass some data from Salesforce to our application through API calls and vice-versa. I’ve been a javascript and .net developer my whole life and this will be my first time working with Salesforce.</p><p>Salesforce is a huge platform and it’s very easy to get lost in the mountain of outdated articles and documentation out there, so this guide is for anyone who is planning or doing any integration project with Salesforce. At the time of this writing, I am working with <strong>Salesforce Spring ’19 release</strong>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/882/1*HdbhNPmjolceMdQeBR3B5g.png" /><figcaption>Salesforce Spring ’19 Loading Screen</figcaption></figure><h3>Apex</h3><p>If you are doing Salesforce (SF) development you definitely need to learn Apex. In a nutshell, Apex is like Java or C# for Salesforce. It is the SF programming language.</p><blockquote><strong>Apex</strong> is a development platform for building software as a service (SaaS) applications on top of <strong>Salesforce</strong>.com’s customer relationship management (CRM) functionality. <br><strong>Apex</strong> allows developers to access <strong>Salesforce</strong>.com’s back-end database and client-server interfaces to create third-party SaaS applications.</blockquote><blockquote><a href="https://searchsalesforce.techtarget.com/definition/Apex">https://searchsalesforce.techtarget.com/definition/Apex</a></blockquote><p>Here are just some of the things that Apex can be used for:</p><ul><li>It can be used to create custom SF objects,</li><li>Intercept object lifecycles with triggers</li><li>Implement your own business logic</li><li>Call external SOAP or REST Apis</li><li>Expose custom endpoints that your applications can integrate to</li><li>Unit test your code</li></ul><p>To read more check out the <a href="https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_dev_guide.htm">Apex Developer Guide</a>.</p><h3>Salesforce Environments</h3><p>I think we can all agree that it’s pretty dangerous to develop, test and deploy directly in production. This isn’t just for SF, but for development in general. Hence, you will need to understand how the SF environment works. It’s a plus if you’re familiar with how git branches work — it’s very similar to that.</p><p>A sandbox environment can be created based on the existing production organisation. Once created, feel free to test your ideas there. Once tested and validated, create a changeset and promote that into the production org.</p><p>To learn how to set up a sandbox environment check this <a href="https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_intro_get_dev_account.htm">link</a>.</p><h3>Tooling</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/256/0*f84N9DWm0etWfK1M.png" /></figure><p>I personally love vscode. For anyone else who feels the same I have good news — you can use it for SF development! The same editor you use for your Javascript, Java or .Net development can also handle Apex, how awesome is that?!</p><p>Just add the <a href="https://developer.salesforce.com/tools/extension_vscode">Salesforce Dx extension for vscode</a></p><p>With vscode and salesforce dx extension you can:</p><ul><li>Connect your codes into a version control system like git</li><li>Easily manage your custom objects, apex classes. View the actual codes in a code editor which is more natural to developers rather than a web portal.</li><li>Code completion!</li><li>Deploy directly to the connected organisation. (It is highly recommended though NOT to deploy directly to prod, Use development environments)<br>Salesforce environment</li></ul><p>To learn how to properly set up your machine with vscode check the step by step guide <a href="https://github.com/forcedotcom/salesforcedx-vscode/wiki/Getting-Started-with-Visual-Studio-Code-for-Salesforce-Developers">here</a>. If you want to see it in action, here are some of the videos I recommend you watch.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2FhXST9yOyQLk%3Ffeature%3Doembed&amp;url=http%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DhXST9yOyQLk&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2FhXST9yOyQLk%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="854" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/22ad07f1f0fa98ded3de904f610aff4e/href">https://medium.com/media/22ad07f1f0fa98ded3de904f610aff4e/href</a></iframe><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2Fhw9LBvjo4PQ%3Ffeature%3Doembed&amp;url=http%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3Dhw9LBvjo4PQ&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2Fhw9LBvjo4PQ%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="854" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/1c2c1c9394ddc4927d8c3c681980bc2c/href">https://medium.com/media/1c2c1c9394ddc4927d8c3c681980bc2c/href</a></iframe><h3>Apex Triggers</h3><p>One nice development approach or paradigm that is available at your disposable in SF are Apex Triggers. They are like hooks or events that you can subscribe with whenever there are changes to your Salesforce object.</p><p>You can implement a simple logic after your object has been inserted or updated through triggers. In the project that I am working on after the record has been inserted or updated a trigger will fire that will dispatch an API callout (more on callouts later).</p><p>Here is a sample trigger that is executed after an `insert`</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/58b952d91f6e484caaf481c8ecb112f5/href">https://medium.com/media/58b952d91f6e484caaf481c8ecb112f5/href</a></iframe><p>Learn more about triggers <a href="https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_triggers.htm">here</a>.</p><h3>SOQL</h3><p>Salesforce Object Query Language (SOQL) — Is basically, SQL for Salesforce objects. If you’re familiar with SQL statements like SELECT, then you’re good to go. Just understand that instead of tables you will be querying salesforce objects.</p><p>A sample SOQL:</p><pre>SELECT one or more fields <br>FROM an object <br>WHERE filter statements and, optionally, results are ordered</pre><h3>Callouts</h3><p>Nowadays, API to API communication is a pretty common scenario. SF supports these use cases through callouts. SOAP and REST integrations are supported out of the box.</p><p>I used callout in the project that I am working in together with <strong>Apex Triggers</strong>, <strong>SOQL</strong> and the dispatch a <strong>Callout</strong>.</p><p>In summary, the sequence flow is:</p><ol><li>I insert or update a salesforce object through the UI</li><li>An Apex Trigger will then be executed and I will pass the Id of the object to a custom apex class</li><li>Using the Id I will then query the object that was inserted or updated and I will select all the relevant fields</li><li>After I will run some validations and use case specific logic</li><li>Then dispatch a Callout to an external API service endpoint that SF is integrating with.</li></ol><p>To learn more about Callouts click this <a href="https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_callouts.htm">link</a>.</p><h3>Deployment</h3><p>To deploy your changes to the production organisation, make sure your sandbox environment is connected to the organisation you want to deploy in.<br>Also, make sure that the production org is configured to accept inbound changesets.</p><p>Please check this <a href="https://help.salesforce.com/articleView?id=000171224&amp;language=en_US&amp;type=1">article</a> for step by step guide make sure your environment is ready for deployment.</p><h3>Conclusion</h3><p>Hopefully, this article has given you an insight into the massive world of Salesforce development and has pointed you in the right direction.</p><p><strong>Other tools that might be useful</strong><br><a href="https://workbench.developerforce.com/login.php">Workbenc</a>h — A free tool to access your Salesforce environment, invoke SOAP and Restful endpoints to test integration points.</p><p><a href="https://www.getpostman.com/">Postman</a> — You can also use postman to invoke Salesforce endpoints</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=cd0636894373" width="1" height="1" alt=""><hr><p><a href="https://medium.com/stationfive/how-to-integrate-your-application-with-salesforce-cd0636894373">How to Integrate Your Application With Salesforce</a> was originally published in <a href="https://medium.com/stationfive">Station Five</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to migrate from Recompose to React Hooks]]></title>
            <link>https://medium.com/stationfive/how-to-migrate-from-recompose-to-react-hooks-89b2981c03d?source=rss----93af788b21cc---4</link>
            <guid isPermaLink="false">https://medium.com/p/89b2981c03d</guid>
            <category><![CDATA[react-hook]]></category>
            <category><![CDATA[recompose]]></category>
            <category><![CDATA[functional-programming]]></category>
            <category><![CDATA[react]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Terence Jeong]]></dc:creator>
            <pubDate>Tue, 08 Jan 2019 23:09:34 GMT</pubDate>
            <atom:updated>2019-05-25T11:47:07.179Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1000/1*MeXlXdFrLW9asVPu8GnQ9A.jpeg" /></figure><p><strong>I</strong>n late 2018, the React team recently released the RFC for the new fancy React Hooks API. This new API was met with excitement, eagerness and anticipation by the React Community and me. That being said, it was also met with some trepidation. Was this the end of the Higher Order Components(HOC) and Render Props era? Does this mean I have to re-architect my projects?</p><p>I personally believe in the functional programming paradigm and as a result I try to stay away from class components within my React projects. I find that class components have the following downsides.</p><ul><li>Confusing (for both humans and computers, especially binding and ‘this’ keyword.)</li><li>Component is very rarely reusable.</li><li>Logic spread over lifecycle methods.</li><li>Hard to test compared to functional components.</li><li>Compiled code size and compile time.</li></ul><p>Instead, I use the Higher Order Component Pattern with the Recompose library to build reusable component logic in a composable way. By using pure functions and functional programming concepts I ensure the following advantages.</p><ul><li>The code is highly reusable.</li><li>Functions can be composed together.</li><li>The code is easily tested.</li><li>There aren’t any observable side effects.</li><li>The data is immutable.</li></ul><p>However, Andrew ‘acdlite’ Clark, a core team member of the React team and author of the hugely popular Recompose library announced that</p><blockquote>“Hooks solves all the problems I attempted to address with Recompose three years ago, and more on top of that. I will be discontinuing active maintenance of this package (excluding perhaps bug fixes or patches for compatibility with future React releases), and recommending that people use Hooks instead.”</blockquote><p>Given this announcement, I thought it was important to look into using Hooks and how it is different to my current use of HOC’s with the recompose library.</p><p>The purpose of this article is to show you,</p><p>1. How I use recompose and the HOC pattern to compose functions to give functional components ‘state’.</p><p>2. How easy it is to move from recompose to the new React Hooks API and reduce the use of HOC or Render Props.</p><p><strong><em>** Spoiler Alert ** </em><br></strong>It is very similar, and I am extremely excited to see how and which direction Hooks will go!</p><h3>Class Components</h3><p>Let’s take a look at the code below. It is a simple form that allows the user to submit their first name, last name and age.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/417d13a60fe21d4499c2a5868eddb299/href">https://medium.com/media/417d13a60fe21d4499c2a5868eddb299/href</a></iframe><p><strong>This form is your standard class component and to me it has the following problems:</strong></p><ol><li>Unable to reuse stateful logic between components</li><li>Each input needs it’s own onChange handler and for a large form it could get quite tricky to organise these handlers.</li><li>Remembering to bind this or when to use arrow functions gets confusing.</li><li>The file does not allow separation of concerns. This class component is managing state, as well as rendering a view.</li></ol><h3>Higher Order Component Pattern</h3><p><strong>Let’s see how we can take a functional approach using the HOC pattern and recompose so we can:</strong></p><ol><li>Allow the separation of concerns.</li><li>Reuse stateful logic between components.</li></ol><p>Firstly, if you are coding along assuming you have done create-react-app, please note that you will need to install the recompose library. This can be done by running yarn add recompose.</p><p>So our component is going to be a functional component that is only concerned with displaying the props that it is given. It has no idea about state or anything else.</p><p>Looking at the code below we can see that it is very similar to the form from the class component above. Except this time, it is just a function and unlike the class component, it doesn’t give a hoot about where the props are from, or even what they are. The sole job of this component is to render the props it has been given.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ecbb289e2ac587ebfb7f0d0d3ae74731/href">https://medium.com/media/ecbb289e2ac587ebfb7f0d0d3ae74731/href</a></iframe><p><strong>But how did it get the props? From the Higher Order Components of course!</strong></p><p>Looking at the code below, we can really see the power of the recompose library. We have created a function that takes a Base Component, and ‘enriches’ it with props and returns a new component.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c300882690d3265df47bc38ef67dafcd/href">https://medium.com/media/c300882690d3265df47bc38ef67dafcd/href</a></iframe><p>withState is a HOC that provides the following</p><ol><li>The first argument declares the state variable, in this case firstName, lastName, age</li><li>The second argument is the function to update the state variable, in this case setFirstName, setLastName, setAge</li><li>The third argument is the initial value for the state variable, in this case Jon, Travis, 22</li></ol><p>If this is confusing, basically what this enhancer is doing is replicating the state object we have in our class component. These values are passed to the base component as props instead of state. We also do not need to bind any handlers since they are declared as the 2nd argument in the withState HOC.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/223/1*7xetiWQTWmRcu4Ne_S-hQA.png" /><figcaption>State from the Class Component.</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/235/1*SfstyVUyAwqhIIxK-U_JOA.png" /><figcaption>Props from the Functional Component.</figcaption></figure><p>withHandlers is a HOC that accepts a set of props and returns a function handler. This function handler is passed to the base component as props.</p><p>compose is a handy utility function from recompose that is used to compose multiple higher order components into a single, nested higher-order component. In this case, it allows use to compose our multiple withState and withHandlers together.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/79d65fb18cdc2c17bd1208a17fb600b6/href">https://medium.com/media/79d65fb18cdc2c17bd1208a17fb600b6/href</a></iframe><p>Putting it all together we can now use this functional component anywhere in our app.</p><p><strong>So what are the advantages of this pattern?</strong></p><ol><li>You can see that the code is a lot more organised. If I need to add or remove logic from the component, I can simply do so in the enhancer file.</li><li>It is a lot more reusable. I can share the stateful logic between components via the enhancer.</li><li>I can compose many HOC’s</li><li>Easily testable, since it is just a pure function.</li></ol><h3>React Hooks</h3><p>Ok so now are are the really good stuff, converting the form using the new React Hooks API.</p><p>Firstly, what are React Hooks? From the React Docs.</p><blockquote>Hooks are functions that let you “hook into” React state and lifecycle features from function components.</blockquote><p><strong>In super simple terms, React Hooks allow our functional components to have local state and lifecycle features! WHAT?!</strong></p><p>The whole point of me using the HOC pattern was to reuse stateful logic between components. But as you see this meant that I had to restructure my components and there is a bit of boilerplate. If you look at the image below you can see ‘wrapper hell’ and this is generally pretty common when using the HOC pattern.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/707/1*yJyHd9CKkqgToX7DtI6fcg.png" /><figcaption>Wrapper Hell</figcaption></figure><p>As you can see, it can get quite messy up in here.</p><p><strong>Hooks will allow you to use stateful logic without changing the component hierarchy and clean up the mess we see before us.</strong></p><h3><strong>useState Hook</strong></h3><p>To make it as simple as possible, let’s look at the <strong>useState </strong>hook.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1eb57cf8a03c41e0fe8096c7f6202480/href">https://medium.com/media/1eb57cf8a03c41e0fe8096c7f6202480/href</a></iframe><p>If you look at the code above, there are some things that stand out. The useState hook is used to manage local state in function components. Much like recompose withState the <strong>useState </strong>hook gives the functional component local state.</p><ol><li>The first item in the array, is our state variable. In this case it is firstName, lastName and setAge</li><li>The second item in the array, is what we use to update the state variable. In this case it is, setFirstName, setLastName, setAge</li><li>The value in the useState function is the initial value to the state variable. In this case it is Jon, Travis 23</li></ol><p>Does this look familiar? It should! It follows a very similar pattern to withState HOC from recompose. Pulling it all together, let’s see what the form looks like just using hooks.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7dfbe86a1c6c6ba114bb7366f5e66672/href">https://medium.com/media/7dfbe86a1c6c6ba114bb7366f5e66672/href</a></iframe><p>Through the <strong>useState </strong>hook we were able to completely remove the need for HOC’s and if we have a look at our component tree.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/479/1*ZHrBvdGPMHpobt_GTgPg8A.png" /><figcaption>What a clean component tree!</figcaption></figure><p>Isn’t that absolutely beautiful! It is so tidy and will grow into a beautiful easily trackable tree. Just to highlight the difference here are both of them together.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/570/1*WbwuUUzoprkwhInjXuKHtw.png" /></figure><p>And if we compare what our form looked like as a Class component compared to the functional component powered by hooks have a look at the image below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/498/1*liFCW_JrfKE9o7hWqZyFDA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/645/1*hUZkASuqxCoE1YeR62gogg.png" /></figure><h3>useEffect Hook</h3><p>As mentioned before, hooks allow functional components the ability to manage state and lifecycle methods (aka side-effects). We saw how to manage state with the <strong>useState </strong>hook. The <strong>useEffect</strong> hook comes into play for life cycle methods / side-effects which are usually used for interactions with the Browser/DOM API.</p><p>Let’s take a look at the componentDidMount lifecycle. In your classic class component we have the following. We can see that when the component is mounted it will set the state of initial value to ‘Down with classes!’</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/94bf9e1bf96b27f1a1831d5f54f3f2a2/href">https://medium.com/media/94bf9e1bf96b27f1a1831d5f54f3f2a2/href</a></iframe><p>Now how would we replicate this with the <strong>useEffect</strong> hook? Well firstly, let’s look at the <strong>useEffect </strong>hook in a bit more depth.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/cddd2bd19d153f930e2ac240bd0b3f31/href">https://medium.com/media/cddd2bd19d153f930e2ac240bd0b3f31/href</a></iframe><p>When you use the <strong>useEffect</strong> hook you are telling React that your component needs to do something <strong>after render</strong>. React will remember the function you passed and will call it <strong>after the DOM updates.</strong></p><p>So in this case after the render, the <strong>useEffect </strong>hook will run and the initial value will be set using the setInitialValue function to ‘Functional Components FTW’.</p><p>So, 2 paragraphs ago I mentioned that the <strong>useEffect </strong>hook will run after the DOM updates. So doesn’t that mean in the code above we will be in an endless loop?</p><p>From the React Docs</p><blockquote>If you want to run an effect and clean it up only once (on mount and unmount), you can pass an empty array ([]) as a second argument. This tells React that your effect doesn’t depend on <em>any</em> values from props or state, so it never needs to re-run.</blockquote><p>So as you can see we passed an empty array meaning that it will only run once.</p><h4><strong>So why do we have to place it inside the functional component?</strong></h4><p>By doing so, you can access the state variable right from the effect. How you ask? Well since we have the <strong>useEffect</strong> hook in the functional component, it is within the functional components scope.</p><p>That means through closure, we can access the state variable. There is no special API required, it is just using normal Javascript closures! If you want a good description of closures, please check out <a href="https://github.com/getify/You-Dont-Know-JS/blob/master/scope%20%26%20closures/ch5.md">Kyle Simpson’s You Don’t Know JS.</a></p><h4><strong>A more detailed useEffect example</strong></h4><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ec8c76e97cce6fe56c31953224e15811/href">https://medium.com/media/ec8c76e97cce6fe56c31953224e15811/href</a></iframe><p>So the custom hook above is responsible for determining if the screen size is mobile or not. Let’s just look at the <strong>useEffect</strong> section of the code.</p><p>So the key things to look at here is that <strong>after </strong>the component <strong>renders</strong> it will run the effect which will add the event listener to get the width of the screen.</p><h4><strong>But why is it returning a function?</strong></h4><p>Well, for <strong>useEffect</strong> if your effect returns a function, React will run that function when it is time to clean up. So in this case, we are removing the event listener. Sound familiar? It is like your <strong>componentWillUnmount.</strong></p><p>However, I think it is a bit cleaner as it keeps the logic for adding and removing subscriptions close to each other since they are part of the same effect.</p><h4>But why is there a value in the array now?</h4><p>That’s a really good question. You know how I’ve mentioned many times now that <strong>useEffect</strong> will run after the component renders and the DOM updates. Well, by adding the width value in the array, you can tell React to <strong>skip</strong> applying an effect if the value hasn’t changed between re-renders.</p><p>So if the component re-renders, React will compare the values of the previous render and the next render. If they are the same, React will skip the effect, if they are different React will run the effect. Sound familiar? It is like your <strong>componentDidUpdate.</strong></p><h3>Custom Hooks</h3><p>So we now know about <strong>useState and useEffect! </strong>Now the last thing I want to mention is custom hooks. WHAT?</p><p>That’s right, since hooks are just Javascript functions, you can write your own hooks that can be reused in the application and other applications. That literally means you can reuse stateful logic between components without HOC’s or Render props.</p><p>The hook above <strong>useIsMobile</strong> is a custom hook that can be used in any part of the application!</p><p>This is the most exciting part about hooks, the reusability. There is so much potential to grow an ecosystem of hooks to use in all your applications.</p><p>Now, I want to stress that there are a whole bunch more of React Hooks such as <strong>useEffect, useContext, useCallback, useMemo, useRef </strong>and many more. You can read all about them <a href="https://reactjs.org/docs/hooks-intro.html">here</a>, and I may even write some more about them down the track.</p><h3><strong>Conclusion</strong></h3><p>I hope this article has helped you understand how seamless it is to go from using the recompose library to hooks. The HOC’s approach is a purely functional way of coding in React and with solid coding conventions it results in robust code. There is absolutely nothing wrong with using this pattern, I still enjoy it and feel it makes me a better developer.</p><p>However, the potential to grow your own library of hooks, the ease of reusing stateful logic between components has me thinking that this will be the norm for React moving forward. I am extremely excited in this development and can’t wait to see the way hooks evolves!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=89b2981c03d" width="1" height="1" alt=""><hr><p><a href="https://medium.com/stationfive/how-to-migrate-from-recompose-to-react-hooks-89b2981c03d">How to migrate from Recompose to React Hooks</a> was originally published in <a href="https://medium.com/stationfive">Station Five</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>