<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Jose Javier Señaris</title>
    <description>The latest articles on DEV Community by Jose Javier Señaris (@pepesenaris).</description>
    <link>https://dev.to/pepesenaris</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F56332%2F3c12088e-bb8e-4f4c-821a-01ebf81b2b07.jpg</url>
      <title>DEV Community: Jose Javier Señaris</title>
      <link>https://dev.to/pepesenaris</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/pepesenaris"/>
    <language>en</language>
    <item>
      <title>Sending emails from a Create-React-App</title>
      <dc:creator>Jose Javier Señaris</dc:creator>
      <pubDate>Thu, 19 Jul 2018 20:38:33 +0000</pubDate>
      <link>https://dev.to/pepesenaris/sending-emails-from-a-create-react-app-2gm6</link>
      <guid>https://dev.to/pepesenaris/sending-emails-from-a-create-react-app-2gm6</guid>
      <description>&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F1024%2F1%2AIiQuFZskyWATRJBMXGVXkQ.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F1024%2F1%2AIiQuFZskyWATRJBMXGVXkQ.jpeg"&gt;&lt;/a&gt;Castillo de los Tres Reyes Magos del Morro, Havana, Cuba&lt;/p&gt;

&lt;p&gt;A few days ago we added a new &lt;strong&gt;Invite people by email&lt;/strong&gt; feature, to an app we’re developing internally at &lt;a href="http://ingenious.agency" rel="noopener noreferrer"&gt;Ingenious&lt;/a&gt;. The app consists of a board with a collaborative text editor and some cards that complement the behavioral insight approach used in product design &lt;a href="http://ingsw.com/agency/" rel="noopener noreferrer"&gt;consulting&lt;/a&gt;. Tech-wise the app is built with React, bootstrapped with &lt;a href="https://github.com/facebook/create-react-app" rel="noopener noreferrer"&gt;create-react-app&lt;/a&gt;, Firebase as the backend and hosted on Heroku.&lt;/p&gt;

&lt;p&gt;We needed to invite people as Editors of the board by sending an email with the board URL. To keep things simple, we first tried &lt;a href="http://www.emailjs.com/" rel="noopener noreferrer"&gt;EmailJS&lt;/a&gt;, a service that allows sending emails without focusing on any server code. EmailJS would have been an excellent choice but we needed a bit more of control on how the email was generated, for example, it is impossible to add custom fonts to the HTML version of the email.&lt;/p&gt;

&lt;p&gt;We choose, as the path of least disruption, to set up a small Express (Node) backend to serve our React app and publish an endpoint that takes care of sending the email. I’ve found several tutorials on the topic, but most of them were outdated or incomplete, so wanted to share a walk through of our solution in case it benefits you as well.&lt;/p&gt;

&lt;p&gt;If you like to deep dive into the code, check the demo link in the end.&lt;/p&gt;

&lt;p&gt;The first task is to transform our client only React project into a full express app with the following steps:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Remove create-react-app-buildpack from the app and setup the default &lt;a href="https://devcenter.heroku.com/articles/nodejs-support#activation" rel="noopener noreferrer"&gt;heroku/nodejs buildpack&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;Move the root React app files into a subdirectory, e.g. &lt;em&gt;react-ui&lt;/em&gt;.&lt;/li&gt;
&lt;li&gt;Copy &lt;em&gt;server/index.js&lt;/em&gt;, &lt;em&gt;package.json&lt;/em&gt;, and_ .gitignore_ files in the root of the project from the buildpack &lt;a href="https://github.com/mars/heroku-cra-node#switching-from-create-react-app-buildpack" rel="noopener noreferrer"&gt;example repo&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;Commit and push to the Heroku origin.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;To keep the React SPA up and working we need to serve all its assets using the static express middleware. Afterward, we can process any request to perform custom actions, such as sending an email, using the common &lt;strong&gt;get(…)&lt;/strong&gt;, &lt;strong&gt;post(…)&lt;/strong&gt; express handlers. Finally, we can re-route any non API request to the SPA entry point, that way the client side routing continues to work as expected. Check the code below:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;The key here is to run npm run buildin the React project which copies all the assets: JS, CSS, images, etc. to the &lt;em&gt;build&lt;/em&gt; folder. The command also generates an &lt;em&gt;index.html&lt;/em&gt; file which can be returned by our web server. This process can be triggered by adding a post-build script in the express &lt;em&gt;package.json&lt;/em&gt; file:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;To generate the email we can rely on the email-templates package. It allows us to obtain . &lt;strong&gt;html&lt;/strong&gt; and . &lt;strong&gt;text&lt;/strong&gt; versions of the content and later send the email with a 3rd party provider. We’ll show how to work with the &lt;a href="https://www.mailjet.com" rel="noopener noreferrer"&gt;Mailjet&lt;/a&gt; API but, any other vendor will do.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Finally, we modify the express route handler to obtain the email address, alongside the user-provided content used in the &lt;em&gt;.&lt;/em&gt; &lt;strong&gt;ejs&lt;/strong&gt; template and send the email.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Creating HTML emails that work well in many different email clients is a daunting task. If you ever need to build a custom email take a look into &lt;a href="https://mjml.io/" rel="noopener noreferrer"&gt;MJML&lt;/a&gt;, a component-based framework that generates responsive emails templates. It ships with some excellent templates and an easy to grasp documentation that gets the task done.&lt;/p&gt;

&lt;p&gt;You can check a full demo repository &lt;a href="https://github.com/pepesenaris/emails-from-create-react-app-example" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/p&gt;




</description>
      <category>react</category>
      <category>express</category>
      <category>email</category>
      <category>mjml</category>
    </item>
    <item>
      <title>A tale of Higher-Order Components &amp; Render Props</title>
      <dc:creator>Jose Javier Señaris</dc:creator>
      <pubDate>Sat, 03 Feb 2018 14:57:16 +0000</pubDate>
      <link>https://dev.to/pepesenaris/a-tale-of-higher-order-components--render-props-3229</link>
      <guid>https://dev.to/pepesenaris/a-tale-of-higher-order-components--render-props-3229</guid>
      <description>&lt;p&gt;In the past few months there has been an endless debate in the react community on &lt;a href="https://reactjs.org/docs/higher-order-components.html" rel="noopener noreferrer"&gt;Higher-Order Components&lt;/a&gt; vs. &lt;a href="https://reactjs.org/docs/render-props.html" rel="noopener noreferrer"&gt;Render Props&lt;/a&gt; as techniques for sharing concerns and reusing common logic among components. There are tons of tutorials and courses explaining their benefits and drawbacks. Just in case you haven’t seen any of those resources, head over to the React docs which have a short and focused introduction to both terms.&lt;/p&gt;

&lt;p&gt;Here at &lt;a href="http://ingenious.agency/" rel="noopener noreferrer"&gt;Ingenious&lt;/a&gt; we have our fair share of React Projects, of all sizes, among various teams. So far we’ve been using Higher-Order Components (HOC for short) mainly through libraries like &lt;a href="https://github.com/acdlite/recompose" rel="noopener noreferrer"&gt;recompose&lt;/a&gt; for implementing our own “Reuse this logic” component or as direct clients of popular libraries like &lt;a href="https://github.com/reactjs/react-redux" rel="noopener noreferrer"&gt;react-redux&lt;/a&gt; whose main features are exposed via HOC. Just a few days ago we had the opportunity of “joining” the debate and choose one of the two approaches to implementing a new feature. The story goes like this.&lt;/p&gt;

&lt;p&gt;We built a product where users need to leave recommendations to other users, and each recommendation can have a list of comments. Think of “Judges” that provide advice to “Participants” of a contest where anyone, be it a Judge or Participant, can give feedback multiple times on a given Recommendation.&lt;/p&gt;

&lt;p&gt;We went all in and proposed a beautiful UI that looks similar to &lt;a href="https://github.com/pepesenaris/hoc-and-render-props-example/tree/create-only" rel="noopener noreferrer"&gt;this&lt;/a&gt;:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F484%2F1%2AbgQPgyboVVnvqh1VVBKWvA.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F484%2F1%2AbgQPgyboVVnvqh1VVBKWvA.png"&gt;&lt;/a&gt;Recommendations with nested comments&lt;/p&gt;

&lt;p&gt;Everything went smoothly, client was happy and end of blog post.&lt;/p&gt;




&lt;p&gt;Actually, at some point, the client requested that both Recommendations and their Comments should be editable within the first 10 minutes of being created. The final intended set of actions a user can perform with Recommendations and Comments were quite different, but the Edit workflow was pretty much the same for both entities. We wanted to make the edition similar to what users already know, which meant reusing as much as possible the UI we already had. The goal now was to add a way of selecting a recommendation or comment, fill the same input used to create it, and save the modification.&lt;/p&gt;

&lt;p&gt;We started with Comments and built a HOC to allow editing them. Fast forwarding in time, after refactoring the common bits, we ended up with a component that allowed us to use the same logic to create/edit both Recommendations and Comments. Something like &lt;a href="https://github.com/pepesenaris/hoc-and-render-props-example/tree/edit-hoc" rel="noopener noreferrer"&gt;this&lt;/a&gt;:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F1024%2F1%2AAg10snUKWqjglH3N_lPBaQ.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F1024%2F1%2AAg10snUKWqjglH3N_lPBaQ.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;By wrapping the Recommendations and Comments listings with editRecentEntity, we only need to toggle the edition mode in both entities and problem solved :-)&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F958%2F1%2A1W4pUkKMoqAbWscCRP-sPw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F958%2F1%2A1W4pUkKMoqAbWscCRP-sPw.png"&gt;&lt;/a&gt;The Edit/Create links toggle the Edition mode for either Recommendations or Comments&lt;/p&gt;




&lt;p&gt;A few days later another client request arrived. Now we needed to show &lt;em&gt;“archived”&lt;/em&gt; Recommendations, which are read-only entries with more than three months old, and present a &lt;em&gt;Loading&lt;/em&gt; indicator instead of the input form while the more recent &lt;em&gt;“active”&lt;/em&gt; Recommendations are being retrieved from the server.&lt;/p&gt;

&lt;p&gt;Up to this point we simply rendered a list of Recommendations followed by a Form component, all wrapped up with the editRecentEntity HOC. The Comments listing used the same pattern as well.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F1024%2F1%2AvOjJwgzlVqmJp8QtzfSZKw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F1024%2F1%2AvOjJwgzlVqmJp8QtzfSZKw.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Hiding the forms for the archived entries has a straightforward solution, the problem then was that adding more code to show a Loading Spinner, instead of the Form, seems a bit clumsy given that it was only necessary for one type of Recommendations.&lt;/p&gt;

&lt;p&gt;A clean solution here, was to move the responsibility of when and how to show the form, to the parent component by using the special React children prop. This idea combined with the Render Props pattern allowed to pass the necessary props to the form, so we can continue to support the creation and edition of Recommendations.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F1024%2F1%2A9TpV39GzU2jc0OWqT-P3HA.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F1024%2F1%2A9TpV39GzU2jc0OWqT-P3HA.png"&gt;&lt;/a&gt;Render Props to the rescue…&lt;/p&gt;

&lt;p&gt;Here the Render Props pattern provides an explicit API for communication between Parent and Children components. All three properties, passed to the form, depends on the logic already implemented in editRecentEntity and at the same time the &lt;em&gt;“marvelous”&lt;/em&gt; Loading component can be used only where and how it’s needed. Go and play with the final &lt;a href="https://github.com/pepesenaris/hoc-and-render-props-example" rel="noopener noreferrer"&gt;code&lt;/a&gt; if that’s your thing.&lt;/p&gt;

&lt;p&gt;The main lesson here, at least the one we learned building this feature, is that what matters most is solving the problem at hand without taking sides on hype-oriented battles of which pattern is better than the other. If the client would have stopped with the Create/Edit functionality, our code will be as worthy as the final version. Be it Higher-Order Components or Render Props, always try to choose whatever pattern, tool, library helps you solve the problem as clear as possible.&lt;/p&gt;

&lt;p&gt;That’s all folks… Happy Coding.&lt;/p&gt;




&lt;p&gt;&lt;em&gt;PS: If you liked, or even better, disliked what you read here please&lt;/em&gt; &lt;a href="http://ingenious.agency/contact/" rel="noopener noreferrer"&gt;&lt;em&gt;drops us a line&lt;/em&gt;&lt;/a&gt;&lt;em&gt;. We love to talk about technology and are always looking for awesome people that enjoy learning with and teaching us how to solve people problems.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>react</category>
      <category>javascript</category>
      <category>renderprops</category>
      <category>hoc</category>
    </item>
  </channel>
</rss>
