{"componentChunkName":"component---src-templates-lesson-js","path":"/phoenix-chat/directory-structure","result":{"data":{"mdx":{"body":"function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }\n\nfunction _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }\n\nfunction _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }\n\n/* @jsx mdx */\nvar _frontmatter = {\n  \"title\": \"Directory Structure\",\n  \"order\": 2,\n  \"free\": true,\n  \"active\": true,\n  \"tech\": [\"phoenix\", \"react\"]\n};\n\nvar makeShortcode = function makeShortcode(name) {\n  return function MDXDefaultShortcode(props) {\n    console.warn(\"Component \" + name + \" was not imported, exported, or provided by MDXProvider as global scope\");\n    return mdx(\"div\", props);\n  };\n};\n\nvar layoutProps = {\n  _frontmatter: _frontmatter\n};\nvar MDXLayout = \"wrapper\";\nreturn function MDXContent(_ref) {\n  var components = _ref.components,\n      props = _objectWithoutProperties(_ref, [\"components\"]);\n\n  return mdx(MDXLayout, _extends({}, layoutProps, props, {\n    components: components,\n    mdxType: \"MDXLayout\"\n  }), mdx(\"h1\", null, \"Directory structure\"), mdx(\"ul\", null, mdx(\"li\", {\n    \"parentName\": \"ul\"\n  }, \"Create a new Phoenix project\"), mdx(\"li\", {\n    \"parentName\": \"ul\"\n  }, \"Create a new React project\"), mdx(\"li\", {\n    \"parentName\": \"ul\"\n  }, \"Overview of directory structure\")), mdx(\"p\", null, \"This app is separated into two pieces: the \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"frontend\"), \" and the \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"backend\"), \". This separation should be familiar to you, as it is the general structure of just about every app. The frontend will be built with React, and the backend with Phoenix.\"), mdx(\"p\", null, \"The first thing we're going to do is create a directory (also known as a \\\"folder\\\") with the name of our app. The app we are building is a clone of the \", mdx(\"a\", {\n    \"href\": \"https://www.intercom.io/live-chat\",\n    \"parentName\": \"p\"\n  }, \"Acquire\"), \" functionality of \", mdx(\"a\", {\n    \"href\": \"intercom.io\",\n    \"parentName\": \"p\"\n  }, \"Intercom.io\"), \". You've probably seen it before--it's the little chat bubble on the bottom right of many websites that allows you to chat with a representative of the company:\"), mdx(\"p\", null, mdx(\"img\", {\n    \"src\": \"https://raw.githubusercontent.com/LearnPhoenix/graphics/master/intercom-chat-bubble.png\",\n    \"alt\": \"intercom chat bubble\",\n    \"parentName\": \"p\"\n  })), mdx(\"p\", null, \"When the chat bubble is clicked, a chat window is overlayed on the page:\"), mdx(\"p\", null, mdx(\"img\", {\n    \"src\": \"https://raw.githubusercontent.com/LearnPhoenix/graphics/master/intercom-side-panel.png\",\n    \"alt\": \"intercom side panel\",\n    \"parentName\": \"p\"\n  })), mdx(\"p\", null, \"We will also want to build a web and mobile app that allows the company representative to easily manage several chats at once. We will model this chat feature on Slack and WhatsApp's web app.\"), mdx(\"p\", null, \"Since we are not especially creative when it comes to naming our projects, we'll call this app \", mdx(\"a\", {\n    \"href\": \"phoenixchat.io\",\n    \"parentName\": \"p\"\n  }, \"PhoenixChat\"), \" and we will keep a live, production-ready version of the completed app at \", mdx(\"a\", {\n    \"href\": \"phoenixchat.io\",\n    \"parentName\": \"p\"\n  }, \"PhoenixChat.io\"), \".\"), mdx(\"pre\", null, mdx(\"code\", {\n    \"className\": \"language-bash\",\n    \"parentName\": \"pre\"\n  }, \"$ mkdir phoenix-chat-api phoenix-chat-frontend\\n\")), mdx(\"p\", null, \"Now that we have directories to contain our project, we need to generate the code for our React frontend and Phoenix backend.\"), mdx(\"h2\", null, \"Generating the frontend\"), mdx(\"p\", null, \"Now we need to create our React app. Let's create the folder \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"frontend\"), \", then enter it and create a Node app with \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"npm init\"), \", filling out the command line prompts as necessary (you can also use the \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"-y\"), \" flag to accept all defaults if you're feeling lazy):\"), mdx(\"pre\", null, mdx(\"code\", {\n    \"className\": \"language-bash\",\n    \"parentName\": \"pre\"\n  }, \"$ cd phoenix-chat-frontend && npm init\\n\")), mdx(\"p\", null, \"That's all you need to do for React at the moment. We will go over React in more detail in the next section.\"), mdx(\"h2\", null, \"Frontend directory structure\"), mdx(\"p\", null, \"We are not going to set up the directory structure in advance of the app, but the general structure will be (and for those of you who want to know how the tree below was generated, check out \", mdx(\"a\", {\n    \"href\": \"http://brewformulas.org/tree\",\n    \"parentName\": \"p\"\n  }, \"tree\"), \"):\"), mdx(\"pre\", null, mdx(\"code\", {\n    \"className\": \"language-bash\",\n    \"parentName\": \"pre\"\n  }, \"phoenix-chat-frontend\\n  |-- node_modules\\n  |-- app\\n      |-- components\\n      |-- images\\n      |-- redux\\n      |-- styles\\n\")), mdx(\"p\", null, mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"frontend\"), \" will contain everything for the frontend of our application. This includes code for our development environment, as well as our production code. This includes our webpack configuration, our \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"package.json\"), \" file, and everything else we'll need to compile our app into something we can run in production.\"), mdx(\"p\", null, mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"app\"), \" contains just the code that will become our app. This includes our components, our styles, and how all those components fit together.\"), mdx(\"p\", null, mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"components\"), \" will contain all of our React components. This is what renders on the screen and displays our data.\"), mdx(\"p\", null, mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"images\"), \" will contain our static images.\"), mdx(\"p\", null, mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"redux\"), \" will contain our \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"actions\"), \", our \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"reducers\"), \", and our \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"store\"), \". These are the core components of Redux and will be covered in a later chapter.\"), mdx(\"p\", null, mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"styles\"), \" will contain default styling, such as a css reset or other css libraries we might need import.\"), mdx(\"p\", null, \"Overall, the React structure is pretty straight-forward.\"), mdx(\"h2\", null, \"Generating the backend\"), mdx(\"p\", null, \"Fortunately, Phoenix makes it easy to generate boilerplate code. We can use \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"mix phoenix.new\"), \" to create a simple app. We are creating our Phoenix app without \", mdx(\"a\", {\n    \"href\": \"http://brunch.io/\",\n    \"parentName\": \"p\"\n  }, \"Brunch\"), \" because we are not using Phoenix for our frontend. When you run \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"mix phoenix.new\"), \", you will be prompted to fetch and install dependencies. You want to enter \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"y\"), \" to install your dependencies.\"), mdx(\"p\", null, \"Run the following commands to go back a directory, and create your new app.\"), mdx(\"pre\", null, mdx(\"code\", {\n    \"className\": \"language-bash\",\n    \"parentName\": \"pre\"\n  }, \"$ cd ..\\n$ mix phoenix.new phoenix-chat-api --app phoenix_chat \\\\\\n  --module PhoenixChat --no-brunch\\n\")), mdx(\"p\", null, \"The first command sends you back a directory (\", mdx(\"em\", {\n    \"parentName\": \"p\"\n  }, \"we may not include directory changes in the future\"), \"), then the second command creates your new project within the \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"phoenix-chat-api\"), \" directory we made earlier.\"), mdx(\"p\", null, \"You probably noticed \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"mix\"), \", which is a command line tool that we will use extensively with Phoenix.\"), mdx(\"p\", null, \"We then choose the \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"phoenix-chat-api\"), \" directory and create a new application called \\\"PhoenixChat\\\". We are passing in the \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"--no-brunch\"), \" flag because we do not need to render HTML or generate all the boilerplate for a frontend since we are building our frontend with React.\"), mdx(\"h2\", null, \"Backend directory structure\"), mdx(\"p\", null, \"The Phoenix generator has taken care of most of our boilerplate for us. The general structure is as follows (with some directories omitted for the sake of brevity):\"), mdx(\"pre\", null, mdx(\"code\", {\n    \"className\": \"language-bash\",\n    \"parentName\": \"pre\"\n  }, \"phoenix-chat-api\\n  |-- config\\n  |-- deps\\n  |-- lib\\n  |   |-- phoenix_chat\\n  |-- priv\\n  |   |-- repo\\n  |   |-- static\\n  |-- test\\n  |-- web\\n      |-- channels\\n      |-- controllers\\n      |-- models\\n      |-- static\\n      |-- templates\\n      |-- views\\n\")), mdx(\"p\", null, mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"config\"), \" contains several files related to configuration. Among the most useful are \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"prod.secret.exs\"), \", which can hold all your secret production keys with less risk.\"), mdx(\"p\", null, mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"deps\"), \" contains all the dependencies you installed.\"), mdx(\"p\", null, mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"lib\"), \" contains configuration for your endpoints and your Ecto repositories (don't worry about what that means, we will cover it later).\"), mdx(\"p\", null, mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"priv\"), \" contains all of your data migrations (i.e. a record of all your database changes), and your static files.\"), mdx(\"p\", null, mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"test\"), \" contains all your Elixir tests.\"), mdx(\"p\", null, mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"web\"), \" contains everything related to the web API, including \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"channels\"), \", \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"controllers\"), \", \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"models\"), \", and other useful goodies.\"), mdx(\"p\", null, mdx(\"strong\", {\n    \"parentName\": \"p\"\n  }, \"If you do not know what all of this means, do not be concerned\"), \". We will go into each of these in greater detail in later chapters as they become relevant. For now, just know that Phoenix has generated most of what we need to get started. Since we are creating a web API, most of what we are interested in lives in the \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"web\"), \" directory.\"));\n}\n;\nMDXContent.isMDXComponent = true;","excerpt":"Directory structure Create a new Phoenix project Create a new React project Overview of directory structure This app is separated into two…","frontmatter":{"title":"Directory Structure","order":2,"active":true,"free":true,"tech":["phoenix","react"]}}},"pageContext":{"slug":"directory-structure","prevNode":{"frontmatter":{"title":"Installing Phoenix and React","active":true,"free":true,"order":1,"tech":["phoenix","react"]},"fields":{"slug":"installing-phoenix-and-react"}},"nextNode":{"frontmatter":{"title":"Basics of React","active":true,"free":true,"order":3,"tech":["react","webpack","babel"]},"fields":{"slug":"basics-of-react"}}}},"staticQueryHashes":["1723017916"]}