{"id":2257,"date":"2022-01-25T18:15:57","date_gmt":"2022-01-25T18:15:57","guid":{"rendered":"https:\/\/lvboard.infostore.in.ua\/?p=2257"},"modified":"2022-01-25T18:15:57","modified_gmt":"2022-01-25T18:15:57","slug":"creating-dashboards-in-react-with-react-dashboard","status":"publish","type":"post","link":"https:\/\/lvboard.infostore.in.ua\/?p=2257","title":{"rendered":"Creating dashboards in React with React Dashboard"},"content":{"rendered":"\n<p><a href=\"https:\/\/flatlogic.com\/\" target=\"_blank\" rel=\"noreferrer noopener\">Flatologic<\/a>&nbsp;is a company that offers free and premium dashboard templates in various frameworks. Whether in Angular, Vue, or React, developers can find many user-friendly pre-built applications to suit their needs.<\/p>\n\n\n\n<!--more-->\n\n\n\n<p>Rather than starting a project from scratch, this drastically reduces the amount of coding involved and gets a project running quickly. In this tutorial, we will focus on their free template called&nbsp;<a href=\"https:\/\/github.com\/flatlogic\/react-dashboard\" target=\"_blank\" rel=\"noreferrer noopener\">React Dashboard<\/a>.<\/p>\n\n\n\n<p>This template is built with React, Bootstrap, React Router, Redux, and GraphQL. It comes with a React frontend and a Node.js backend. Authentication is included and the layout is responsive.<\/p>\n\n\n\n<p>This tutorial will cover how to create a dashboard in three easy steps. Then, we will connect it to an SQL database with MAMP so you have some local data.<\/p>\n\n\n\n<h2>Setting up the React Dashboard project<\/h2>\n\n\n\n<p>To start with, let\u2019s clone the React Dashboard repository and choose a name for our new project.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">git clone -o React Dashboard -b master --single-branch https:\/\/github.com\/flatlogic\/React Dashboard.git &lt;app-name&gt;<\/pre>\n\n\n\n<p>Once the project is cloned on our local machine, we can proceed with installing the required dependencies.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">cd &lt;app-name&gt;\nyarn install<\/pre>\n\n\n\n<p>At the time of writing, React Dashboard runs into an issue when you run&nbsp;<code>yarn install<\/code>&nbsp;and there is a&nbsp;<a href=\"https:\/\/github.com\/flatlogic\/react-dashboard\/issues\/15\" target=\"_blank\" rel=\"noreferrer noopener\">GitHub issue<\/a>&nbsp;logged. Here is a snippet of the error:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">    error \/Users\/....\/node_modules\/sqlite3: Command failed.\n    Exit code: 1\n    Command: node-pre-gyp install --fallback-to-build<\/pre>\n\n\n\n<p>If you run&nbsp;<code>yarn&nbsp;add&nbsp;sqlite3<\/code>&nbsp;separately and then re-run&nbsp;<code>yarn install<\/code>, things should work fine.<\/p>\n\n\n\n<p>Now, we can run&nbsp;<code>yarn dev<\/code>. Once done, this will launch the backend and frontend servers, which will be accessible on port&nbsp;<code>5000<\/code>&nbsp;and&nbsp;<code>3000<\/code>, respectively. If we navigate to&nbsp;<code><a href=\"http:\/\/localhost:3000\/\">http:\/\/localhost:3000\/<\/a><\/code>, we should see our dashboard:<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img src=\"https:\/\/blog.logrocket.com\/wp-content\/uploads\/2022\/01\/react-dashboard-interface.png\" alt=\"React Dashboard Interface\" class=\"wp-image-85667\"\/><\/figure><\/div>\n\n\n\n<p><a href=\"https:\/\/blog.logrocket.com\/creating-dashboards-react-react-dashboard\/\"><\/a><\/p>\n\n\n\n<p>This project also comes with GraphQL. Once the app is launched, the playground is accessible at&nbsp;<code><a href=\"http:\/\/localhost:5000\/graphql\">http:\/\/localhost:5000\/graphql<\/a><\/code>. If you are unfamiliar with this technology, do not hesitate to read the&nbsp;<a href=\"https:\/\/graphql.org\/\" target=\"_blank\" rel=\"noreferrer noopener\">GraphQL documentation<\/a>&nbsp;and learn how to create queries and mutations.<\/p>\n\n\n\n<h2>Creating the dashboard and table<\/h2>\n\n\n\n<p>We have a great dashboard, but at the moment, it is working with hard-coded values. To make it more valuable, let\u2019s connect our new application to a local database.<\/p>\n\n\n\n<p>You can choose any database, but for the sake of this tutorial, I will connect a MySQL database. To create one, I am using&nbsp;<a href=\"https:\/\/www.mamp.info\/en\/mamp\/mac\/\" target=\"_blank\" rel=\"noreferrer noopener\">MAMP<\/a>, which allows you to get Apache, Nginx, PHP, and MySQL out of the box. If you do not have MAMP, you can&nbsp;<a href=\"https:\/\/www.mamp.info\/en\/downloads\/\" target=\"_blank\" rel=\"noreferrer noopener\">download it here<\/a>.<\/p>\n\n\n\n<p>Once it is installed, start the application and click&nbsp;<strong>Webstart<\/strong>.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img src=\"https:\/\/blog.logrocket.com\/wp-content\/uploads\/2022\/01\/mamp-interface.png\" alt=\"MAMP Interface\" class=\"wp-image-85670\"\/><\/figure><\/div>\n\n\n\n<p>This opens the MAMP index page at&nbsp;<code><a href=\"http:\/\/localhost:8888\/MAMP\">http:\/\/localhost:8888\/MAMP<\/a><\/code>. On this page, you will see plenty of information, but what we care about is under&nbsp;<strong>MySQL<\/strong>. Once opened, click on&nbsp;<strong>phpMyAdmin<\/strong>.<\/p>\n\n\n\n<p>This will open another tab, this one to&nbsp;<code><a href=\"http:\/\/localhost:8888\/phpMyAdmin\/index.php\">http:\/\/localhost:8888\/phpMyAdmin\/index.php<\/a><\/code>, where you will see the interface of&nbsp;<code>phpMyAdmin<\/code>. Thanks to this, we will be able to see, create, and work with our future MySQL databases.<\/p>\n\n\n\n<p>We can create a database by clicking on&nbsp;<strong>Databases<\/strong>. Then, enter the name of our new database and click&nbsp;<strong>Create<\/strong>.<\/p>\n\n\n\n<p>We may then create our first table. For our tutorial, name it&nbsp;<code>posts<\/code>&nbsp;and select five columns, as we will have five fields. Once we click&nbsp;<strong>Go<\/strong>, we will be able to enter our new fields.<\/p>\n\n\n\n<p>Our fields should be as such:<\/p>\n\n\n\n<ul><li>ID of type&nbsp;<code>VARCHAR<\/code>l; length&nbsp;<code>255<\/code>&nbsp;is our primary key<\/li><li>title of type&nbsp;<code>VARCHAR<\/code>; length&nbsp;<code>255<\/code><\/li><li>content of type&nbsp;<code>TEXT<\/code><\/li><li>createdAt of type DATE<\/li><li>updatedAt of type DATE<\/li><\/ul>\n\n\n\n<p>Here is a screenshot of our table with our new fields:<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img src=\"https:\/\/blog.logrocket.com\/wp-content\/uploads\/2022\/01\/mysql-posts.png\" alt=\"MySQL Posts Table\" class=\"wp-image-85672\"\/><\/figure><\/div>\n\n\n\n<p>Once we have inputted all the required information, click&nbsp;<strong>Save<\/strong>.<\/p>\n\n\n\n<h2>Linking the local database to your application<\/h2>\n\n\n\n<p>This project uses&nbsp;<a href=\"https:\/\/sequelize.org\/\" target=\"_blank\" rel=\"noreferrer noopener\">Sequelize<\/a>, a Node.js ORM that allows you to connect to SQL databases. In our data folder inside&nbsp;<code>src<\/code>, we find a<code>&nbsp;sequelize.js<\/code>&nbsp;file where we can connect our database.<\/p>\n\n\n\n<p><code>import&nbsp;Sequelize&nbsp;from&nbsp;'sequelize';<\/code><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">    \/\/ Configure the connection to your local database with your DB name, username, password and the port on which your DB runs.\n    const sequelize = new Sequelize('test', 'root', 'root', {\n      host: 'localhost',\n      port: 8889,\n      dialect: 'mysql',\n      operatorsAliases: false,\n    });\n\n    export default sequelize;<\/pre>\n\n\n\n<p>Then, in the same folder, open the&nbsp;<code>schema.js<\/code>&nbsp;file and uncomment the mutation for post creation. This will allow us to create new posts in our database from our frontend.<\/p>\n\n\n\n<p>Note: Once again, if you are unfamiliar with the concept of mutation, check out GraphQL\u2019s documentation.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">    import {\n      GraphQLSchema as Schema,\n      GraphQLObjectType as ObjectType,\n    } from 'graphql';\n\n    import me from '.\/queries\/me';\n    import news from '.\/queries\/news';\n    import posts from '.\/queries\/posts';\n    import addPost from '.\/mutations\/posts';\n\n    const schemaConfig = {\n      query: new ObjectType({\n        name: 'Query',\n        fields: {\n          me,\n          news,\n          posts,\n        },\n      }),\n    };\n\n    \/\/ If you want to enable mutation, uncomment the code below\n    schemaConfig.mutation = new ObjectType({\n      name: 'Mutation',\n      fields: {\n        addPost,\n      },\n    });\n\n    const schema = new Schema(schemaConfig);\n\n    export default schema;<\/pre>\n\n\n\n<h2>Testing the application<\/h2>\n\n\n\n<p>Kill the server with&nbsp;<strong>Cmd+C<\/strong>&nbsp;and start it again. If everything worked, we should be able to create new posts. In our dashboard, we can click on&nbsp;<strong>View all Posts<\/strong>, then&nbsp;<strong>Create New<\/strong>. After inputting a title and content, we can click&nbsp;<strong>Save<\/strong>. When we go back to the dashboard page, our new post will appear in the table.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img src=\"https:\/\/blog.logrocket.com\/wp-content\/uploads\/2022\/01\/post-creation.gif\" alt=\"Post Creation\" class=\"wp-image-85674\"\/><\/figure><\/div>\n\n\n\n<h2>Conclusion<\/h2>\n\n\n\n<p>In this tutorial, we learned how to clone the React Dashboard template from Flatologic and launch it locally. This allowed us to have a user-friendly dashboard up and running quickly.<\/p>\n\n\n\n<p>Then, we created a MySQL database with a table and some fields and connected it to our new application.<\/p>\n\n\n\n<p>Thanks to this pre-built dashboard, developers around the world can quickly build a React application. Starting a project from scratch is always difficult, and these templates come with all the functionalities needed to get started.<\/p>\n\n\n\n<h2>Full visibility into production React apps<\/h2>\n\n\n\n<p>Debugging React applications can be difficult, especially when users experience issues that are hard to reproduce. If you\u2019re interested in monitoring and tracking Redux state, automatically surfacing JavaScript errors, and tracking slow network requests and component load time,&nbsp;<a href=\"https:\/\/www2.logrocket.com\/react-performance-monitoring\" target=\"_blank\" rel=\"noreferrer noopener\">try LogRocket<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Flatologic&nbsp;is a company that offers free and premium dashboard templates in various frameworks. Whether in Angular, Vue, or React, developers can find many user-friendly pre-built applications to suit their needs.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[30],"tags":[177,65],"_links":{"self":[{"href":"https:\/\/lvboard.infostore.in.ua\/index.php?rest_route=\/wp\/v2\/posts\/2257"}],"collection":[{"href":"https:\/\/lvboard.infostore.in.ua\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lvboard.infostore.in.ua\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lvboard.infostore.in.ua\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/lvboard.infostore.in.ua\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2257"}],"version-history":[{"count":1,"href":"https:\/\/lvboard.infostore.in.ua\/index.php?rest_route=\/wp\/v2\/posts\/2257\/revisions"}],"predecessor-version":[{"id":2258,"href":"https:\/\/lvboard.infostore.in.ua\/index.php?rest_route=\/wp\/v2\/posts\/2257\/revisions\/2258"}],"wp:attachment":[{"href":"https:\/\/lvboard.infostore.in.ua\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2257"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lvboard.infostore.in.ua\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2257"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lvboard.infostore.in.ua\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2257"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}