<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Paul Hayes on Medium]]></title>
        <description><![CDATA[Stories by Paul Hayes on Medium]]></description>
        <link>https://medium.com/@paulhayes_74666?source=rss-b2424b63c0ab------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*BKkbunsVaAFVlL8A.</url>
            <title>Stories by Paul Hayes on Medium</title>
            <link>https://medium.com/@paulhayes_74666?source=rss-b2424b63c0ab------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 20 Nov 2026 15:04:49 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@paulhayes_74666/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Why presentational/container components pattern is still important in 2021]]></title>
            <link>https://medium.com/silex-technologies/why-presentational-container-components-pattern-is-still-important-in-2021-44b4f54d6493?source=rss-b2424b63c0ab------2</link>
            <guid isPermaLink="false">https://medium.com/p/44b4f54d6493</guid>
            <category><![CDATA[clean-code]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[react]]></category>
            <category><![CDATA[nextjs]]></category>
            <category><![CDATA[reactjs]]></category>
            <dc:creator><![CDATA[Paul Hayes]]></dc:creator>
            <pubDate>Fri, 23 Apr 2021 15:42:50 GMT</pubDate>
            <atom:updated>2021-04-23T15:42:50.480Z</atom:updated>
            <content:encoded><![CDATA[<p>Below is a typical component you will find in a repository.</p><pre>import React from &#39;react&#39;<br>import clsx from &#39;clsx&#39;<br>import { useFormat } from &#39;hooks/useFormat&#39;<br>import { useRouter } from &#39;next/router&#39;<br>import { Button, ButtonTypes } from &#39;presentational/inputs/button/Button&#39;<br>import { useCustomErrorStyles } from &#39;./CustomError.style&#39;</pre><pre>export interface ICustomErrorProps {<br>  statusCode: number<br>  title: string<br>  message: string<br>  componentId: string<br>  coloredBackground?: boolean<br>}</pre><pre>export const CustomError: React.FunctionComponent&lt;ICustomErrorProps&gt; = ({<br>  statusCode,<br>  title,<br>  message,<br>  componentId,<br>  coloredBackground = false,<br>}) =&gt; {</pre><pre>  const router = useRouter()<br>  const { translate } = useFormat()<br>  const classes = useCustomErrorStyles()</pre><pre>return (<br>  &lt;&gt;<br>    &lt;div className={clsx(classes.root, {     [classes.coloredBackground]: coloredBackground })}&gt;<br>      &lt;div className={classes.titleWrapper}&gt;<br>        &lt;div className={classes.statusCode}&gt;{statusCode}&lt;/div&gt;<br>        &lt;div className={classes.title}&gt;{title}&lt;/div&gt;<br>      &lt;/div&gt;<br>      &lt;div className={classes.message}&gt;<br>        {componentId}: {message}<br>      &lt;/div&gt;<br>      {statusCode === 403 &amp;&amp; (<br>      &lt;div className={classes.redirect}&gt;<br>        &lt;Button<br>          type={ButtonTypes.secondary}<br>          label={translate(&#39;back_to_login&#39;)}<br>          handleClick={() =&gt; router.push(&#39;/api/auth/logout&#39;)}<br>        /&gt;<br>      &lt;/div&gt;<br>    )}<br>    &lt;/div&gt;<br>  &lt;/&gt;<br>  )<br>}</pre><p>What is wrong with the above code?</p><p>This component is coupled to the architecture of the application.</p><p>By importing the Next.js router to handle the click inside the presentation layer, this component has coupled itself to the architecture of the application. It can’t be used in any other application that isn’t using Next.js.</p><p>Also, it is my opinion that by translating the button label in the presentation components reduces the re-usability of the components and ties the component to specific application flows, rather than the component being agnostic and unaware of the application.</p><p>Also, if you’re using something like Storybook, it would have to be configured to be aware of the exact translation library your using, otherwise the component might work perfectly fine in your application but in Storybook it will error out like below:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*lFclV2zbpi5yLopSSDQtmA.png" /></figure><p>And sure you could configure Storybook to use the same translation library as your application, but if you ever want to change from react-intl to react-i18n for example, it’s going to be far more difficult. You now have to re-configure storybook as well as your application. And what if you want to use that component for a different application, does that have to have to now have it’s own useFormat function? Maybe it’s a legacy application with it’s own translation function…???</p><p>And this is just one component, imagine if all of your components are tied to the architecture of your application and all the libraries its using — redux connect for example — it makes it harder and harder to reason about your code, to fix bugs and it makes it extremely difficult and time consuming to change your applications architecture at some point.</p><p>What it should be:</p><pre>import React from &#39;react&#39;<br>import clsx from &#39;clsx&#39;<br>import { Button, ButtonTypes } from &#39;presentational/inputs/button/Button&#39;<br>import { useCustomErrorStyles } from &#39;./CustomError.style&#39;</pre><pre>export interface ICustomErrorProps {<br>  statusCode: number<br>  title: string<br>  message: string<br>  componentId: string<br>  buttonLabel: string<br>  handleClick: () =&gt; void<br>  coloredBackground?: boolean<br>}</pre><pre>export const CustomError: React.FunctionComponent&lt;ICustomErrorProps&gt; = ({<br>  statusCode,<br>  title,<br>  message,<br>  componentId,<br>  buttonLabel,<br>  handleClick,<br>  coloredBackground = false,<br>}) =&gt; {<br>  const classes = useCustomErrorStyles()</pre><pre>return (<br>  &lt;&gt;<br>    &lt;div className={clsx(classes.root, {     [classes.coloredBackground]: coloredBackground })}&gt;<br>      &lt;div className={classes.titleWrapper}&gt;<br>        &lt;div className={classes.statusCode}&gt;{statusCode}&lt;/div&gt;<br>        &lt;div className={classes.title}&gt;{title}&lt;/div&gt;<br>      &lt;/div&gt;<br>      &lt;div className={classes.message}&gt;<br>        {componentId}: {message}<br>      &lt;/div&gt;<br>      {statusCode === 403 &amp;&amp; (<br>      &lt;div className={classes.redirect}&gt;<br>        &lt;Button<br>          type={ButtonTypes.secondary}<br>          label={buttonLabel}<br>          handleClick={handleClick}<br>        /&gt;<br>      &lt;/div&gt;<br>    )}<br>    &lt;/div&gt;<br>  &lt;/&gt;<br>  )<br>}</pre><p>The only coupling here this component has to the way it’s styled. And since it’s a presentational component, I think this is ok to couple presentational components to a library like JSS or Bootstrap or Material UI.</p><p>So the following are the rules and the mentality we follow that drive our presentational/container pattern:</p><p>Presentational components rules/mindset:</p><ol><li>Can use only html, css and other presentational components. There is never a single exception where a presentational component imports a container component.</li><li>Is completely unaware of the application, in fact we always ask ourselves, can this be used by another application if the need arises?</li><li>Only cares about its own state, and this should be simple.</li><li>Other than styling related hooks, they never contain hooks.</li><li>They are never wrapped in higher order components — if using redux for example, the connect higher order component would be added to the container component.</li><li>Are as small as possible, akin to many small lego blocks.</li><li>Often accept props that change the visual nature of the component — like isDisabled or isColoredBackground</li><li>Often accept props that load a completely different stylesheet, for example…</li></ol><pre>import React from &#39;react&#39;<br>import { Button as MuiButton } from &#39;@material-ui/core&#39;<br>import { useDefaultButtonStyles } from &#39;./DefaultButton.styles&#39;<br>import { usePrimaryButtonStyles } from &#39;./PrimaryButton.styles&#39;<br>import { useSecondaryButtonStyles } from &#39;./SecondaryButton.styles&#39;</pre><pre>export enum ButtonTypes {<br>  default = &#39;default&#39;,<br>  primary = &#39;primary&#39;,<br>  secondary = &#39;secondary&#39;,<br>}</pre><pre>enum ButtonVariants {<br>  text = &#39;text&#39;,<br>  contained = &#39;contained&#39;,<br>  outlined = &#39;outlined&#39;,<br>}</pre><pre>export interface IButtonProps {<br>  type: ButtonTypes<br>  label: string<br>  handleClick: () =&gt; void<br>  disabled?: boolean<br>}</pre><pre>export const Button: React.FunctionComponent&lt;IButtonProps&gt; = ({<br>  type = ButtonTypes.default,<br>  label,<br>  handleClick,<br>  disabled,<br>}) =&gt; {</pre><pre>  let classes = useDefaultButtonStyles()<br>  let variant: keyof typeof ButtonVariants | undefined<br>  variant = ButtonVariants.outlined</pre><pre>  if (type === ButtonTypes.primary) {<br>    classes = usePrimaryButtonStyles()<br>    variant = ButtonVariants.outlined<br>  }</pre><pre>  if (type === ButtonTypes.secondary) {<br>    classes = useSecondaryButtonStyles()<br>    variant = ButtonVariants.outlined<br>  }</pre><pre>  return (<br>    &lt;MuiButton variant={variant} classes={classes} onClick=. {handleClick} disabled={disabled}&gt;<br>      {label}<br>    &lt;/MuiButton&gt;<br>  )<br>}</pre><p>Container components rules/mindset:</p><ol><li>Never include html or styling — the only exception we have is when charting or grid libraries require a very specific way of styling.</li><li>Can import and use presentation components and other container components.</li><li>All the logic goes into a hook file, because we aim to test only the hook using react-hooks-testing-library, rather than testing the component with react-testing-library.</li><li>Can accept props</li><li>Can use context, create side effects, request CRUD operations to the db</li><li>All CRUD operations are optimistic, we expect the CRUD operations to succeed, update the UI immediately, and only rollback the UI changes if the server response indicates an error occured.</li><li>Always declare the test ids in the container components and pass the test id’s to the components</li></ol><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=44b4f54d6493" width="1" height="1" alt=""><hr><p><a href="https://medium.com/silex-technologies/why-presentational-container-components-pattern-is-still-important-in-2021-44b4f54d6493">Why presentational/container components pattern is still important in 2021</a> was originally published in <a href="https://medium.com/silex-technologies">Silex Technologies</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Fluid design, JSS & Consistency]]></title>
            <link>https://medium.com/silex-technologies/fluid-design-jss-consistency-ed43a6ba024d?source=rss-b2424b63c0ab------2</link>
            <guid isPermaLink="false">https://medium.com/p/ed43a6ba024d</guid>
            <category><![CDATA[reactjs]]></category>
            <category><![CDATA[react]]></category>
            <category><![CDATA[css]]></category>
            <category><![CDATA[nextjs]]></category>
            <dc:creator><![CDATA[Paul Hayes]]></dc:creator>
            <pubDate>Fri, 16 Apr 2021 17:27:17 GMT</pubDate>
            <atom:updated>2021-04-16T17:27:17.029Z</atom:updated>
            <content:encoded><![CDATA[<p>Silex’s flagship application SparkWeb has some very interesting design challenges. It should not only be responsive but also fluid — and it need to be consistent.</p><p>Let’s take a look at how we achieve consistent spacing.</p><p>First, we start with defining our breakpoints and our base:</p><p>src/presentational/customization/spacings.ts</p><pre>const baseUnit = 8<br>export const base = (number: number) =&gt; number * baseUnit</pre><pre>interface ISpacings {<br>  lg: {<br>    min: number<br>    max: number<br>  }<br>  xl: {<br>    min: number<br>    max: number<br>  }<br>}</pre><pre>export const spacings: ISpacings = {<br>  lg: {<br>    min: base(3),<br>    max: base(4),<br>  },<br>  xl: {<br>    min: base(4),<br>    max: base(4),<br>  },<br>}</pre><p>What we’re doing here is ensuring that a) We use a base for all of design, and that any spacings used is consistent.</p><p>src/presentational/customization/breakpoints.ts</p><pre>export const breakpoints = {<br>  values: {<br>    xs: 0,<br>    sm: 600,<br>    md: 960,<br>    lg: 1280,<br>    xl: 1920,<br> },<br>}</pre><pre>export type TColumnSize = 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 |<br>11 | 12</pre><pre>export type TBreakPoints = &#39;lg&#39; | &#39;xs&#39; | &#39;sm&#39; | &#39;md&#39; | &#39;xl&#39;</pre><p>Secondly, we use the following helper functions:</p><p>src/utils/clamps.ts</p><pre>import { breakpoints, TBreakPoints } from &#39;../presentational/customization/breakpoints&#39;</pre><pre>export const clamp = (<br>firstBreakpoint: TBreakPoints,<br>firstSize: number,<br>secondBreakpoint: TBreakPoints,<br>secondSize: number<br>): string =&gt; `calc(${firstSize}px + (${secondSize} - ${firstSize}) *      (100vw - ${breakpoints.values[firstBreakpoint]}px) /  (${breakpoints.values[secondBreakpoint]} -  ${breakpoints.values[firstBreakpoint]}))`</pre><p>This clamp function does all the heavy lifting. Pass any two breakpoints and the number of pixels it should be at that breakpoint, and it will automagically scale the size between the two.</p><p>Then, we can use this function to create some very useful utility functions…</p><p>src/utils/fluidSize.ts</p><pre>import { breakpoints } from &#39;../presentational/customization/breakpoints&#39;<br>import { clamp } from &#39;./clamp&#39;</pre><pre>export const fluidSize = (property: string, xlValue: number, lgValue: number) =&gt; {<br>  return {<br>    [`@media (min-width: ${breakpoints.values.lg}px)`]: {<br>      [property]: `${clamp(&#39;xl&#39;, xlValue, &#39;lg&#39;, lgValue)}`,<br>    },<br>    [`@media (min-width: ${breakpoints.values.xl}px)`]: {<br>      [property]: `${xlValue}px`,<br>    },<br>  }<br>}</pre><p>You can use this function when you want one property to scale between any two breakpoints like so:</p><pre>someElement: {</pre><pre>  ...fluidSize(&#39;width&#39;, 240, 170),</pre><pre>},</pre><p>src/utils/fluidSizes.ts</p><pre>import { breakpoints } from &#39;../presentational/customization/breakpoints&#39;<br>import { clamp } from &#39;./clamp&#39;</pre><pre>export const fluidSizes = (<br>  values: {<br>    property: string<br>    xl: number<br>    lg: number<br>  }[]<br>) =&gt; {<br>const lgValues = values.reduce((accumalator, current) =&gt; ({<br>  ...accumalator,<br>  [current.property]: `${clamp(&#39;xl&#39;, current.xl, &#39;lg&#39;,   current.lg)}`,<br>  }), {})</pre><pre>  const xlValues = values.reduce((accumalator, current) =&gt; ({     ...accumalator, [current.property]: `${current.xl}px` }),<br>  {})</pre><pre>  return {<br>    [`@media (min-width: ${breakpoints.values.lg}px)`]: {<br>      ...lgValues,<br>    },<br>    [`@media (min-width: ${breakpoints.values.xl}px)`]: {<br>      ...xlValues,<br>    },<br>  }<br>}</pre><p>You can use this function when you want multiple properties to scale between any two breakpoints like so:</p><pre>import { base } from &#39;../presentational/customization/spacings&#39;</pre><pre>someElement: {<br>  ...fluidSizes([<br>    {<br>      property: &#39;width&#39;,<br>      xl: base(1),<br>      lg: base(2),<br>    },<br>    {<br>      property: &#39;height&#39;, <br>      xl: base(1),<br>      lg: base(2),<br>    },<br>  ]),<br>},</pre><p>What we’re aiming to achieve here is the ability to have consistent paddings at every possible resolution.</p><p>We also have utility functions just for the different types of spacings, to ensure all the spacings are consistent.</p><p>src/utils/fluidSpace.ts</p><pre>import { breakpoints } from &#39;../presentational/customization/breakpoints&#39;<br>import { spacings } from &#39;../presentational/customization/spacings&#39;<br>import { clampSpace } from &#39;./clampSpace&#39;</pre><pre>export type TSpaceType =<br>| &#39;margin&#39;<br>| &#39;marginLeft&#39;<br>| &#39;marginRight&#39;<br>| &#39;marginTop&#39;<br>| &#39;marginBottom&#39;<br>| &#39;padding&#39;<br>| &#39;paddingLeft&#39;<br>| &#39;paddingRight&#39;<br>| &#39;paddingTop&#39;<br>| &#39;paddingBottom&#39;<br>| &#39;gridGap&#39;</pre><pre>export const fluidSpace = (property: TSpaceType) =&gt; ({<br>  [`@media (min-width: ${breakpoints.values.lg}px)`]: {<br>    [property]: `${clampSpace(&#39;xl&#39;, &#39;lg&#39;)}`,<br>  },</pre><pre>  [`@media (min-width: ${breakpoints.values.xl}px)`]: {<br>    [property]: `${spacings.xl.min}px`,<br>  },<br>})</pre><pre>export const fluidHorizontalSpace = (property: TSpaceType) =&gt; ({<br>  [`@media (min-width: ${breakpoints.values.lg}px)`]: {<br>    [property]: `0 ${clampSpace(&#39;xl&#39;, &#39;lg&#39;)}`,<br>  },<br>  [`@media (min-width: ${breakpoints.values.xl}px)`]: {<br>    [property]: `0 ${spacings.xl.min}px`,<br>  },<br>})</pre><pre>export const fluidVerticalSpace = (property: TSpaceType) =&gt; ({<br>  [`@media (min-width: ${breakpoints.values.lg}px)`]: {<br>    [property]: `${clampSpace(&#39;xl&#39;, &#39;lg&#39;)} 0`,<br>  },<br>  [`@media (min-width: ${breakpoints.values.xl}px)`]: {<br>     [property]: `${spacings.xl.min}px 0`,<br>  },<br>})</pre><p>This will give consistent fluid paddings, grid gaps and margins.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/876/1*qnsaMOP1rTLoJ_YSYt_jMg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/772/1*G-VOBOjEJHPJprsNyXO-eg.png" /></figure><p>You can see the padding increase at the different resolution, point of a pixel at a time. We can use these flexible spacings anywhere, it is consistent and fluid.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ed43a6ba024d" width="1" height="1" alt=""><hr><p><a href="https://medium.com/silex-technologies/fluid-design-jss-consistency-ed43a6ba024d">Fluid design, JSS &amp; Consistency</a> was originally published in <a href="https://medium.com/silex-technologies">Silex Technologies</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Scalable file/folder structure and tooling for a Next.JS project — Part 3]]></title>
            <link>https://medium.com/silex-technologies/scalable-file-folder-structure-and-tooling-for-a-next-js-project-part-3-5725c7c18d76?source=rss-b2424b63c0ab------2</link>
            <guid isPermaLink="false">https://medium.com/p/5725c7c18d76</guid>
            <category><![CDATA[reactjs]]></category>
            <category><![CDATA[react]]></category>
            <category><![CDATA[nextjs]]></category>
            <category><![CDATA[clean-code]]></category>
            <dc:creator><![CDATA[Paul Hayes]]></dc:creator>
            <pubDate>Thu, 15 Apr 2021 14:11:29 GMT</pubDate>
            <atom:updated>2021-04-15T14:11:29.158Z</atom:updated>
            <content:encoded><![CDATA[<h3>Scalable file/folder structure and tooling for a Next.JS project — Part 3</h3><p>Let’s take a closer look at how we perform write operations, which live in the mutations folder.</p><p>Since we are using SWR — we need to first mutate the SWR cache, the following is an example.</p><pre>import { mutate } from &#39;swr&#39;<br>import { swrKeys } from &#39;../constants/swrKeys&#39;</pre><pre>export const handleLogout = async () =&gt; {<br>// Optimistic UI update<br>mutate(swrKeys.user,<br>  { <br>    user: { isLoggedOut: true } <br>  }, false)<br>}</pre><p>In the above example, there is no need to update the database. As we are simply logging out.</p><p>But if we had to update the database, we would do so optimistically, we would expect the database write operation to succeed, and only roll back the client side changes if it failed for some reason.</p><p>We have only global context, that we use when ever we want to share state with multiple components, which actually with our architecture isn’t very often.</p><pre>import { useRouter } from &#39;next/router&#39;<br>import React, { useRef, useState } from &#39;react&#39;<br>import { useEffect } from &#39;react&#39;<br>import { useFormat } from &#39;../hooks/useFormat&#39;<br>import { ISelectOption } from &#39;../presentational/inputs/select/Select&#39;<br>import { AppContext } from &#39;./AppContext&#39;</pre><pre>export const AppContextProvider: React.FunctionComponent = ({ children }) =&gt; {</pre><pre>  const { translate } = useFormat()<br>  const router = useRouter()<br>  const lastVisitedLocationRef = useRef&lt;string[]&gt;([])<br>  const [language, setLanguage] = useState&lt;ISelectOption&gt;({<br>     value: &#39;en-GB&#39;,<br>     label: translate(&#39;en-GB&#39;)<br>  }) </pre><pre>  useEffect(() =&gt; {<br>    if (lastVisitedLocationRef.current.length &gt; 1) {<br>      lastVisitedLocationRef.current.shift()<br>    }</pre><pre>    lastVisitedLocationRef.current.unshift(router.pathname)<br>  }, [router.pathname])</pre><pre>  const context = {<br>    lastVisitedLocationRef,<br>    language,<br>    setLanguage<br>  }</pre><pre>  return &lt;AppContext.Provider value={context}&gt;{children}.   &lt;/AppContext.Provider&gt;</pre><pre>}</pre><p>In the above example, we can access and set the selected language and reference the users last visited location anywhere in the application without any spaghetti like prop drilling. Most state doesn’t need redux like global state because actually in redux most state is being used simply as a cache of the database. In this way, SWR by vercel is far far superior and allows us to have an incredibly small global app context file.</p><p>All of our translations are stored in…</p><p>/src/content/locale/</p><pre>en.ts<br>fr.ts<br>de.ts<br>es.ts<br>pt.ts<br>... etc</pre><p>Though in the future we will probably moved to a hosted solutions for translations because managing translations in a git repository is not an ideal workflow for multiple reasons. It puts the burden of translating strings into developers, when it should really be managed by a dedicated content person or team.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=5725c7c18d76" width="1" height="1" alt=""><hr><p><a href="https://medium.com/silex-technologies/scalable-file-folder-structure-and-tooling-for-a-next-js-project-part-3-5725c7c18d76">Scalable file/folder structure and tooling for a Next.JS project — Part 3</a> was originally published in <a href="https://medium.com/silex-technologies">Silex Technologies</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Scalable file/folder structure and tooling for a Next.JS project — Part 2]]></title>
            <link>https://medium.com/silex-technologies/scalable-file-folder-structure-and-tooling-for-a-next-js-project-part-2-be6c042b6b8?source=rss-b2424b63c0ab------2</link>
            <guid isPermaLink="false">https://medium.com/p/be6c042b6b8</guid>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[clean-code]]></category>
            <category><![CDATA[nextjs]]></category>
            <category><![CDATA[react]]></category>
            <dc:creator><![CDATA[Paul Hayes]]></dc:creator>
            <pubDate>Wed, 14 Apr 2021 12:47:26 GMT</pubDate>
            <atom:updated>2021-04-14T12:47:26.794Z</atom:updated>
            <content:encoded><![CDATA[<h3>Scalable file/folder structure and tooling for a Next.JS project — Part 2</h3><p>Let’s take a closer look at how we seperate presentation from logic..</p><p>Our presentational components is a component library that is developed in storybook and is completely unaware of the application logic.</p><p>Each presentation component is made of three files. The component file, the storybook file and the styles file. The presentation components are the only components that are allowed html and css. They can contain basic local state but will never perform any CRUD operations to any api.</p><p>The following is an example of an Avatar component</p><p>src/presentational/navigation/Avatar/Avatar.tsx</p><pre>import React from &#39;react&#39;<br>import { useAvatarStyles } from &#39;./Avatar.styles&#39;<br>import { Avatar as MuiAvatar } from &#39;@material-ui/core&#39;</pre><pre>export interface IAvatarProps {<br>   title: string<br>   avatar: string<br>   testid?: string<br>}</pre><pre>export const Avatar: React.FunctionComponent&lt;IAvatarProps&gt; = ({ title, testid, avatar }) =&gt; {</pre><pre>  const classes = useAvatarStyles()</pre><pre>  return (<br>    &lt;div data-testid={testid} className={classes.root}&gt;<br>      &lt;MuiAvatar avatar={avatar} className={classes.avatar}&gt;{title}&lt;/MuiAvatar&gt;<br>    &lt;/div&gt;<br>  )<br>}</pre><p>src/presentational/navigation/Avatar/Avatar.styles.tsx</p><pre>import { makeStyles } from &#39;@material-ui/core/styles&#39;<br>import { fluidHeight } from &#39;../../../utils/fluidHeight&#39;</pre><pre>export const useAvatarStyles = makeStyles(() =&gt; ({<br>  root: {<br>    ...fluidHeight(165, 2),<br>    paddingTop: 5,<br>    display: &#39;flex&#39;,<br>    justifyContent: &#39;center&#39;,<br>  },<br>    avatar: {<br>      width: &#39;122px&#39;,<br>      height: &#39;122px&#39;,<br>    },<br>  }),<br>  { name: &#39;avatar&#39; }<br>)</pre><p>src/presentational/navigation/Avatar/Avatar.stories.tsx</p><pre>import React from &#39;react&#39;<br>import { Story, Meta } from &#39;@storybook/react/types-6-0&#39;<br>import { Avatar, IAvatarProps } from &#39;./Avatar&#39;</pre><pre>export default {<br>  title: &#39;navigation/Avatar&#39;,<br>  component: Avatar,<br>} as Meta</pre><pre>const Template: Story&lt;IAvatarProps&gt; = (args) =&gt; &lt;Avatar {...args} /&gt;</pre><pre>export const Default = Template.bind({})</pre><pre>Default.args = {  <br>  title: &#39;Some Title&#39;,<br>}</pre><p>Our avatar image is coming from a CMS , and because we want our presentational components to be pure, we fetch the data in their container components which we call ‘View’ components.</p><p>View components never have html or css, and all the logic goes in a hook file in order to make testing easy. Because then we can test the hook with react-hooks-testing-library, rather than testing the entire component.</p><p>src/views/navigation/AvatarView/AvatarView.tsx</p><pre>import React from &#39;react&#39;<br>import { Avatar } from &#39;../../../presentational/navigation/Avatar/Avatar&#39;<br>import { useAvatar } from &#39;./useAvatarView&#39;</pre><pre>export const AvatarView: React.FunctionComponent = () =&gt; {<br>  const { ...props } = useAvatar()<br>  <br>  return &lt;Avatar testid={&#39;user-avatar&#39;} {...props} /&gt;<br>}</pre><p>src/views/navigation/AvatarView/useAvatarView.tsx</p><pre>import { useUser } from &#39;../../../context/UserContextProvider&#39;</pre><pre>export const useAvatar = () =&gt; {<br>  const { avatar, isLoading, error } = getAvatar()<br>  const { user } = useUser()</pre><pre>return {<br>    isLoading,<br>    error,<br>    avatar,<br>    title: user.name<br>  }<br>}</pre><p>All read operations are done in files with the prefix ‘get’ and live in the queries folder.</p><p>src/queries/getAvatar.ts</p><pre>import useSWR from &#39;swr&#39;<br>import { swrKeys } from &#39;../constants/swrKeys&#39;<br>import { IAvatarDTO } from &#39;../pages/api/avatar&#39;</pre><pre>export function getAvatar() {<br>  const swr = useSWR&lt;IAlertsDTO&gt;(swrKeys.avatar)</pre><pre>  return {<br>    ...swr,<br>    avatar: swr.data?.avatar,<br>    isLoading: !swr.data &amp;&amp; !swr.error,<br>  }<br>}</pre><p>In web development, we typically read far more than we perform write operations, so this is why we seperate our queries into it’s own folder.</p><p>Each read gets it’s own file, it never becomes cluttered.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=be6c042b6b8" width="1" height="1" alt=""><hr><p><a href="https://medium.com/silex-technologies/scalable-file-folder-structure-and-tooling-for-a-next-js-project-part-2-be6c042b6b8">Scalable file/folder structure and tooling for a Next.JS project — Part 2</a> was originally published in <a href="https://medium.com/silex-technologies">Silex Technologies</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Scalable file/folder structure and tooling for a Next.JS project — Part 1]]></title>
            <link>https://medium.com/silex-technologies/scalable-file-folder-structure-and-tooling-for-a-next-js-project-part-1-da738d2302f3?source=rss-b2424b63c0ab------2</link>
            <guid isPermaLink="false">https://medium.com/p/da738d2302f3</guid>
            <category><![CDATA[nextjs]]></category>
            <category><![CDATA[clean-code]]></category>
            <category><![CDATA[react]]></category>
            <category><![CDATA[front-end-development]]></category>
            <dc:creator><![CDATA[Paul Hayes]]></dc:creator>
            <pubDate>Tue, 29 Dec 2020 11:07:03 GMT</pubDate>
            <atom:updated>2021-04-15T09:15:33.231Z</atom:updated>
            <content:encoded><![CDATA[<h3>Scalable file/folder structure and tooling for a Next.JS project — Part 1</h3><p>The following is a detailed description of the tools and file/folder structure used by Spark’s Frontend repository.</p><p>Spark uses:</p><ul><li><a href="https://www.cypress.io/">Cypress</a> for end to end testing</li><li><a href="https://jestjs.io/">Jest</a> with <a href="https://testing-library.com/docs/react-testing-library/intro/">React Testing Library</a> for unit testing</li><li><a href="https://storybook.js.org/">Storybook</a> to develop presentational components in an isolated environment</li><li><a href="https://material-ui.com/">Material-UI</a> to massively decrease the time it takes to develop components</li><li><a href="https://miragejs.com/">Mirage</a> so our frontend development work is uncoupled from the backend — we can even work without internet.</li><li><a href="https://github.com/typicode/husky#readme">Husky</a> to prevent pushing bad commits</li><li><a href="https://prettier.io/">Prettier</a> to prevent wasting time discussing code formatting</li><li><a href="https://www.docker.com/">Docker</a> to prevent nasty bugs caused by differences in environments</li><li><a href="https://www.typescriptlang.org/">Typescript</a> for an enhanced development experience</li><li><a href="https://vercel.com/">Vercel</a> so we only need to define our local environment variables in our repository</li><li><a href="https://swr.vercel.app/">SWR</a> for data fetching</li><li><a href="https://formatjs.io/">Formatjs</a> for localisation</li></ul><p>Folder structure at the root level:</p><pre>- .next<br>- .storybook<br>- .vscode<br>- cypress<br>- material-ui<br>- mirage<br>- public<br>- sandbox<br>- src<br>- test</pre><ul><li>The .vscode settings folder is there so we can share our core settings easily</li><li>The sandbox folder is there so anyone can commit and share ideas, prototypes and try out new libraries in an isolated folder.</li><li>The .next, public and src folders belong to Next.Js</li></ul><p>Files at the root level:</p><pre>.babelrc<br>.dockerignore<br>.env<br>.env.local<br>.huskyrc<br>.prettierrc.js<br>cypress.json<br>Dockerfile<br>jest.config.js<br>jest.setup.js<br>next-env.d.ts<br>next.config.js<br>package-lock.json<br>package.json<br>README.md<br>tsconfig.json</pre><p>Inside the /src folder:</p><pre>- constants<br>- content<br>- hooks<br>- layout<br>- licenses<br>- mutations<br>- pages<br>- pages/api<br>- presentational<br>- queries<br>- utils<br>- views</pre><ul><li>constants: global constants and enums live here</li><li>hooks: global utility custom React hooks live here</li><li>mutations: <a href="https://swr.vercel.app/">SWR</a> create, update and delete operations live here</li><li>pages: <a href="https://nextjs.org/docs/basic-features/pages">Next.js pages</a> live here</li><li>pages/api: <a href="https://nextjs.org/docs/api-routes/introduction">Next.js api routes</a> live here</li><li>presentational: presentational components live here and are developed solely using <a href="https://storybook.js.org/">Storybook</a> to ensure they are isolated from any application logic and to improve re-usability. Our folder structure within follows as closely as possible the taxonomy of <a href="https://material-ui.com/">Material-UI</a>.</li><li>queries: <a href="https://swr.vercel.app/">SWR</a> read operations live here</li><li>utils: global utility functions live here</li><li>views: container components live here, these components never use html or styles and contain the application logic. Their primary job is to handle the data fetching, transform the data into its correct localised format, and pass it on to the presentational components for rendering.</li></ul><p>In 2021 and beyond there is arguably still a huge advantage of isolating logic from HTML &amp; CSS and in the Spark Frontend repository we do that with dividing our components into two separate types — Presentational (dumb) and View (smart) components.</p><p><a href="https://medium.com/silex-technologies/scalable-file-folder-structure-and-tooling-for-a-next-js-project-part-2-be6c042b6b8">In part 2</a> we will take a closer look at the presentational and view folders and see how we differentiate between our presentational components and view components.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=da738d2302f3" width="1" height="1" alt=""><hr><p><a href="https://medium.com/silex-technologies/scalable-file-folder-structure-and-tooling-for-a-next-js-project-part-1-da738d2302f3">Scalable file/folder structure and tooling for a Next.JS project — Part 1</a> was originally published in <a href="https://medium.com/silex-technologies">Silex Technologies</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>