Part 1: How I built our condos's new web pages with Gatsby and Chakra UI
Tags: gatsby, react, webdev, auth0
Published: 2021-09-12 13:34:41

We have thousands of small and large condominium associations and housing cooperatives in Norway where I live, and common to them all is that they often need a place to publish information to their residents. But many small condos do not have large budgets to develop their own websites.

I'm a board member in a small condominium with 21 sections, and until recently we have managed with nothing more than email and a Facebook page to get information out to the residents. The plan was to continue like that - until a newly moved-in neighbor came over and asked why we did not have a website where he could find useful info. And why not? I just had to figure out how we could build it without spending too much money. 

The cheapest solution was of course to build everything myself.

I am a self-taught developer who have been coding in JavaScript and React for a few years, mostly on a hobby basis. Just before Christmas 2020 I came across a new component library called [Chakra UI](https://chakra-ui.com/) which promised making it easy to create great looking user interfaces without having to build everything from scratch.

After playing a bit with Chakra UI a few weekends and late nights, I decided to make something useful - instead of just another test website (or to-do app, God forbid). Maybe it was possible to create and operate a new website for the condo completely free of charge? I had to give it a try.

The result was a quite performant website that does not cost the condominium a penny to run - except for the 120 kroner (~12 euro) a year for the domain [https://gartnerihagen-askim.no](https://gartnerihagen-askim.no).

*This is how the front page of the finished web site looks like:*
![The front page of gartnerihagen-askim.no](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cz9n2p7qq0e9p3wu1xno.png)

## Separate area for logged-in users
The purpose of the web site was first and foremost to give the residents a place to find useful information for residents only. But the web site should also promote the condo and the neighbourhood for potential home buyers. 

I like to have a plan before I begin, so I started drawing a few sketches and wireframes with my favorite tool for these things: [Figma](https://figma.com/). 

The front page should be accessible to everyone, but it was also necessary to keep some content behind a login:

![Wireframe for the new web page](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/le5pgr0lks6krnr98ytc.png)

This is how the pages for logged in users look like:

![Screenshot of gated content on our new web page.](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1fkoxbjyqvg0i0aav62z.jpg)

## Technology choices

I wanted the new web site to be as user friendly as possible, while also being lightning fast and using best practices in modern web development. I wanted to use functional and reusable components in React.js with ["hooks"](https://reactjs.org/docs/hooks-intro.html) that lets me handle state easily.

I also decided to use TypeScript to reduce the risk of bugs when passing props down to variuous components - but also because using TypeScript gives me incredibly useful information in VS Code during coding:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p56t512kalaz464p71ac.png)

### Languages and frameworks - frontend
I decided quite early in the project to build the web site using the React framework [Gatsby](https://www.gatsbyjs.com/). I also considered using Next.js, but I had much more experience with Gatsby and then it also became the natural choice for me. 

One of the benefits of Gatsby is that you get websites that are extremely fast. Gatsby creates static HTML pages that are very well optimized, including best-in-class optimization of images (which has become even better with [Gatsby 3.0](https://www.gatsbyjs.com/blog/gatsby-v3)). It is also easy to create web pages with great SEO (search engine optimization).

To speed up the process of making a nice user interface, I chose [Chakra UI](https://chakra-ui.com/). With Chakra UI, you also get a number of bonus benefits, such as compliance with WAI-ARIA standards for accessibility, and that you can customize your own themes for your website. This means that you hardly need to write any CSS at all, but can define color palettes, fonts, distances and more by expanding the default theme (which already looks very nice).

Chakra UI also makes it easy to create responsive websites, in fact without having to write a single media query.

### Backend
When it came to where I should host the site, the choice stood between Gatsby's own [Gatsby Cloud](https://www.gatsbyjs.com/products/cloud/) , or [Netlify](https://www.netlify.com/). I decided on the latter, as little traffic and the modest needs of the condominium would make it possible to run the website on the free tier.

Getting a Gatsby website up and running on Netlify is as simple as pointing Netlify to the code repository in Git. As soon as I push new code to the project's main branch on Github, Netlify automatically runs a new build of the site.

For publishing content, I wanted a headless CMS solution. I considered both the Norwegian headless CMS solution [Sanity](https://sanity.io) and headless Wordpress, as I had experience with both. For example, my personal website [lekanger.no](https://www.lekanger.no) uses Sanity, while Wordpress has also come a long way as a "headless" CMS.

However, the choice fell on [Contentful](https://www.contentful.com/). The most important prerequisites were that the CMS I chose should be easy to use, and that it could deliver the content to Gatsby via GraphQL. Contentful has an official plugin for Gatsby that makes it easy to source the content into Gatsby. And although Contentful is relatively expensive, you should have quite a lot of content and traffic before you have to upgrade from the free tier.

### Authentication
Some areas on the condominium's website should be accessible to logged in users only. For that I used Gatsby's ["Client-only routes"](https://www.gatsbyjs.com/docs/how-to/routing/client-only-routes-and-user-authentication/). Unlike regular Gatsby pages, pages on client-only routes are not static, pre-generated pages. Instead they work more like a traditional React app running on the client. More about this in my next article, where I go through how the website are built.

In the first version of the website, I used [Netlify Identity](https://docs.netlify.com/visitor-access/identity/), which is Netlify's own authentication solution. With [Netlify Identity Widget](https://github.com/netlify/netlify-identity-widget) it is easy to add authentication, but I quickly discovered that I missed some more advanced functionality. I therefore switched to [Auth0](https://auth0.com/), which to a greater extent allowed me to tailor the login solution. Auth0 also had some functionality I needed to create a user admin dashboard, like role-based authentication. 

## Time to start coding!
In the next article, I will go into more details about how the website are built, how I built the pages that should require login, as well as some of the challenges I encountered along the way.

## Next step: [Part 2: A new web page for our condo. Setting up Gatsby and Contentful.](https://dev.to/klekanger/part-2-a-new-web-page-for-our-condo-setting-up-gatsby-and-contentful-2k0j)

Feel free to take a look at the finished website here: [https://gartnerihagen-askim.no](https://gartnerihagen-askim.no)

The project is open source, you can find the source code [at my Github](https://github.com/klekanger/gartnerihagen).

Here's a video showing the live site with the login protected pages and the user admin dashboard:

{% youtube XzkTRw5D5mg %}


*This is a translation, the original article in Norwegian is here: [Slik bygget jeg nye nettsider til sameiet med Gatsby og Chakra UI](https://www.lekanger.no/project/del-1-slik-bygget-jeg-nye-nettsider-til-sameiet-med-gatsby-og-chakra-ui)*