{"componentChunkName":"component---src-templates-lesson-js","path":"/phoenix-chat/login-and-signup-forms","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\": \"Login and Signup Forms\",\n  \"order\": 15,\n  \"free\": false,\n  \"active\": true,\n  \"tech\": [\"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, \"Login and Signup Forms\"), mdx(\"ul\", null, mdx(\"li\", {\n    \"parentName\": \"ul\"\n  }, \"Form components\"), mdx(\"li\", {\n    \"parentName\": \"ul\"\n  }, \"Login and signup views\")), mdx(\"p\", null, \"It is notoriously difficult to create reusable \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"Form\"), \" components. That's because there is so much variation in the inputs and what you end up doing with them. Since we will only have a few forms, we are going to make each one separately. If we end up creating lots of forms with identical characteristics, we can refactor them out later.\"), mdx(\"p\", null, \"Go ahead and create \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"Signup\"), \" and \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"Login\"), \" directories and populate them with the standard files.\"), mdx(\"pre\", null, mdx(\"code\", {\n    \"className\": \"language-bash\",\n    \"parentName\": \"pre\"\n  }, \"$ mkdir app/components/{Signup,Login}\\n$ touch app/components/Signup/{README.md,index.js,style.css,spec.js} \\\\\\n  app/components/Login/{README.md,index.js,style.css,spec.js}\\n\")), mdx(\"p\", null, \"Then we should create the \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"Signup\"), \" component in our \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"index.js\"), \" file.\"), mdx(\"pre\", null, mdx(\"code\", {\n    \"className\": \"language-js\",\n    \"parentName\": \"pre\"\n  }, \"import React from \\\"react\\\"\\nimport cssModules from \\\"react-css-modules\\\"\\nimport style from \\\"./style.css\\\"\\n\\nimport { default as Button } from \\\"../Button\\\"\\n\\nexport class Signup extends React.Component {\\n  render() {\\n    return (\\n      <div className={style.wrapper}>\\n        <div className={style.form}>\\n          <div className={style.inputGroup}>\\n            <input\\n              placeholder=\\\"Username\\\"\\n              className={style.input}\\n              type=\\\"text\\\"\\n              id=\\\"signup-username\\\"\\n            />\\n          </div>\\n          <div className={style.inputGroup}>\\n            <input placeholder=\\\"Email\\\" className={style.input} type=\\\"text\\\" id=\\\"signup-email\\\" />\\n          </div>\\n          <div className={style.inputGroup}>\\n            <input\\n              placeholder=\\\"Password\\\"\\n              className={style.input}\\n              type=\\\"password\\\"\\n              id=\\\"signup-password\\\"\\n            />\\n          </div>\\n          <div className={style.inputGroup}>\\n            <input\\n              placeholder=\\\"Verify Password\\\"\\n              className={style.input}\\n              type=\\\"password\\\"\\n              id=\\\"signup-verify-password\\\"\\n            />\\n          </div>\\n          <Button style={{ width: \\\"100%\\\" }} type=\\\"primary\\\">\\n            Submit\\n          </Button>\\n        </div>\\n      </div>\\n    )\\n  }\\n}\\n\\nexport default cssModules(Signup, style)\\n\")), mdx(\"p\", null, \"There are many ways to style inputs. If you have a preference for another \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"input\"), \" style, by all means, use that. For now, we're just going to implement some basic styling to make our form more usable. Within \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"style.css\"), \", add the following:\"), mdx(\"pre\", null, mdx(\"code\", {\n    \"className\": \"language-css\",\n    \"parentName\": \"pre\"\n  }, \".wrapper {\\n  display: flex;\\n  flex-flow: column nowrap;\\n  justify-content: center;\\n  padding-bottom: 2rem;\\n  width: 400px;\\n}\\n\\n.form {\\n  display: flex;\\n  flex-direction: column;\\n  justify-content: center;\\n  padding: 2rem;\\n  width: 400px;\\n}\\n\\n.input {\\n  padding: 1rem 1rem;\\n  border-radius: 3px;\\n  border: 1px solid #ccc;\\n  font-size: 1.1em;\\n  outline: none;\\n}\\n\\n.inputGroup {\\n  display: flex;\\n  flex-flow: column nowrap;\\n  padding: 10px 0;\\n}\\n\")), mdx(\"p\", null, \"To get this to render, let's quickly change our \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"Home\"), \" component to the code below.\"), mdx(\"pre\", null, mdx(\"code\", {\n    \"className\": \"language-js\",\n    \"parentName\": \"pre\"\n  }, \"import React from \\\"react\\\"\\nimport cssModules from \\\"react-css-modules\\\"\\nimport style from \\\"./style.css\\\"\\n\\nimport { default as Signup } from \\\"../Signup\\\"\\n\\nexport class Home extends React.Component {\\n  render() {\\n    return (\\n      <div>\\n        <Signup />\\n      </div>\\n    )\\n  }\\n}\\n\\nexport default cssModules(Home, style)\\n\")), mdx(\"p\", null, \"We should also create the \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"Login\"), \", which will be almost identical but with fewer fields.\"), mdx(\"pre\", null, mdx(\"code\", {\n    \"className\": \"language-js\",\n    \"parentName\": \"pre\"\n  }, \"import React from \\\"react\\\"\\nimport cssModules from \\\"react-css-modules\\\"\\nimport style from \\\"./style.css\\\"\\n\\nimport { default as Button } from \\\"../Button\\\"\\n\\nexport class Login extends React.Component {\\n  render() {\\n    return (\\n      <div className={style.wrapper}>\\n        <div className={style.form}>\\n          <div className={style.inputGroup}>\\n            <input placeholder=\\\"Email\\\" className={style.input} type=\\\"text\\\" id=\\\"signup-email\\\" />\\n          </div>\\n          <div className={style.inputGroup}>\\n            <input\\n              placeholder=\\\"Password\\\"\\n              className={style.input}\\n              type=\\\"password\\\"\\n              id=\\\"signup-password\\\"\\n            />\\n          </div>\\n          <Button style={{ width: \\\"100%\\\" }} type=\\\"primary\\\">\\n            Submit\\n          </Button>\\n        </div>\\n      </div>\\n    )\\n  }\\n}\\n\\nexport default cssModules(Login, style)\\n\")), mdx(\"p\", null, \"Also add the same styles to the \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"style.css\"), \" file. At some point in the future, we can write code that is more \", mdx(\"a\", {\n    \"href\": \"https://en.wikipedia.org/wiki/Don%27t_repeat_yourself\",\n    \"parentName\": \"p\"\n  }, \"DRY\"), \" with shared CSS, but for now, let's do it the easy way and just copy-paste.\"), mdx(\"p\", null, \"We will eventually want to submit our form, so let's create a \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"submit\"), \" function that we will attach to our button.\"), mdx(\"p\", null, \"In each of our forms, add a function and update our submit button to handle a click event.\"), mdx(\"pre\", null, mdx(\"code\", {\n    \"className\": \"language-js\",\n    \"parentName\": \"pre\"\n  }, \"...\\nexport class Signup extends React.Component {\\n  constructor(props) {\\n    super(props)\\n    this.submit = this.submit.bind(this)\\n  }\\n\\n  submit(e) {\\n    console.log(\\\"Submit button clicked\\\")\\n  }\\n\\n  render() {\\n    return (\\n      <div className={style.wrapper}>\\n\\n        ...\\n\\n        <Button\\n          onClick={this.submit}\\n          style={{ width: \\\"100%\\\" }}\\n          type=\\\"primary\\\">\\n          Submit\\n        </Button>\\n      </div>\\n    )\\n  }\\n}\\n...\\n\")), mdx(\"p\", null, \"Add the same \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"submit\"), \" function and \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"onClick\"), \" event to our \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"Login\"), \" component. Be sure to bind the \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"submit\"), \" function in the constructor as well.\"), mdx(\"p\", null, mdx(\"em\", {\n    \"parentName\": \"p\"\n  }, \"Note: Whenever you write a function that needs access to the \", mdx(\"inlineCode\", {\n    \"parentName\": \"em\"\n  }, \"this\"), \" context, you need to bind it--usually in the constructor.\"), \" For example, a component that simply returns some jsx does not need to be bound, while a function that takes in a value from an event triggered by another element does need to be bound.\"), mdx(\"p\", null, \"Now when click on the button, we see a log in our console that says \\\"Submit button clicked\\\". We will eventually use this button to submit the form, but for now, we are just going to use it to write our tests.\"), mdx(\"h2\", null, \"Unit tests\"), mdx(\"p\", null, \"There's not a lot that we need to worry about as far as unit tests go at this point. We need to make sure that 1. The form renders, 2. The form has a \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"submit\"), \" function, and 3. The submit button calls the submit function.\"), mdx(\"p\", null, \"Remember, since our components are just classes, we can instantiate them by simply calling \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"new <Component>\"), \" and immediately have access to all their functions to test.\"), mdx(\"pre\", null, mdx(\"code\", {\n    \"className\": \"language-js\",\n    \"parentName\": \"pre\"\n  }, \"import React from \\\"react\\\"\\nimport expect from \\\"expect\\\"\\nimport { shallow } from \\\"enzyme\\\"\\n\\nimport { Signup } from \\\"./\\\"\\n\\ndescribe(\\\"<Signup />\\\", () => {\\n  it(\\\"should render\\\", () => {\\n    const renderedComponent = shallow(<Signup />)\\n    expect(renderedComponent.is(\\\"div\\\")).toEqual(true)\\n  })\\n  it(\\\"should have a submit function\\\", () => {\\n    const component = new Signup()\\n    expect(component.submit).toExist()\\n  })\\n  // TODO need update\\n  // it('should call submit function when button is clicked', () => {\\n  //   const renderedComponent = shallow(\\n  //     <Signup />\\n  //   )\\n  //   const spy = expect.spyOn(renderedComponent.instance(), 'submit')\\n  //   renderedComponent.find('button').simulate('click')\\n  //   expect(spy).toHaveBeenCalled()\\n  // })\\n})\\n\")), mdx(\"p\", null, \"The first new test is really simple. Since our components are just JavaScript classes, we can don't actually have to render them to test certain parts. We're checking to make sure there is a \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"submit\"), \" function, and all we need to do to run this test is instantiate the class using \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"new\"), \", then checking that the \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"submit\"), \" function exists on that \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"class\"), \". This is just plain-old JavaScript.\"), mdx(\"p\", null, \"You probably noticed the use of a \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"spy\"), \" in the last test. It would behoove you to learn more about spies for the purpose of writing better tests. A good place to start is the \", mdx(\"a\", {\n    \"href\": \"https://github.com/mjackson/expect#spies\",\n    \"parentName\": \"p\"\n  }, \"expect documentation\"), \" to see a few use cases.\"), mdx(\"p\", null, \"Now that we have our form in place, we need to submit it. We will cover that in the next lesson.\"), mdx(\"h2\", null, \"Styling the homepage\"), mdx(\"p\", null, \"The last thing we should do is add some basic styling to our \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"Home\"), \" as well as our \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"Login\"), \" component in the event the user already has an account. We're also going to add an image that we can absolutely position behind our input.\"), mdx(\"pre\", null, mdx(\"code\", {\n    \"className\": \"language-js\",\n    \"parentName\": \"pre\"\n  }, \"...\\n\\nimport { default as Signup } from \\\"../Signup\\\"\\nimport { default as Login } from \\\"../Login\\\"\\n\\nexport class Home extends React.Component {\\n  constructor(props) {\\n    super(props)\\n    this.state = {\\n      formState: \\\"signup\\\"\\n    }\\n  }\\n\\n  render() {\\n    return (\\n      <div className={style.leader}>\\n        <h1 className={style.title}>Phoenix Chat</h1>\\n        {this.state.formState === \\\"signup\\\" ? <Signup /> : null}\\n        {this.state.formState === \\\"login\\\" ? <Login /> : null}\\n        <img\\n          role=\\\"presentation\\\"\\n          className={style.circles}\\n          src=\\\"/learnphoenix-static-assets/images/circles-full.png\\\" />\\n      </div>\\n    )\\n  }\\n}\\n\\nexport default cssModules(Home, style)\\n\")), mdx(\"p\", null, \"You may have also noticed the \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"role=\\\"presentation\\\"\"), \", which is a replacement for the \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"alt\"), \" text for images where it does not make sense to add \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"alt\"), \" text. In this case, since this image is just going to be a background image, we don't need \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"alt\"), \" text.\"), mdx(\"p\", null, \"And we should center the content to make it look nicer:\"), mdx(\"pre\", null, mdx(\"code\", {\n    \"className\": \"language-css\",\n    \"parentName\": \"pre\"\n  }, \".leader {\\n  height: 100vh;\\n  display: flex;\\n  position: relative;\\n  align-items: center;\\n  justify-content: center;\\n  flex-flow: column nowrap;\\n  overflow: hidden;\\n  z-index: 100;\\n  background: white;\\n}\\n\\n.title {\\n  font-size: 1.8em;\\n  color: rgb(239, 95, 78);\\n}\\n\\n.changeLink {\\n  cursor: pointer;\\n  color: #42a5f5;\\n}\\n\\n.circles {\\n  position: absolute;\\n  right: 0;\\n  left: 0;\\n  top: 0;\\n  margin: auto;\\n  height: 650px;\\n  opacity: 0.05;\\n  z-index: -1;\\n  transform: scale(3);\\n}\\n\")), mdx(\"p\", null, \"The only tricky piece here is how we're handling the image. We're reducing the opacity and scaling it up by an order of 3. Then we're positioning it in the center and putting the \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"z-index\"), \" at -1 so it stays behind our form.\"), mdx(\"p\", null, \"It's not good practice to have random \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"z-index\"), \" numbers all over the place, but in this instance, it doesn't matter. A common way to do this is to have something like 10 built-in \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"z-index\"), \" values at 100, 200, ..., 1000 and use whichever makes the most sense for the current element.\"), mdx(\"p\", null, \"For now, if you want to access the \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"Login\"), \" component, you can change the hard-coded \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"formState\"), \" to \\\"login\\\". In the future, this will be handled programmatically.\"));\n}\n;\nMDXContent.isMDXComponent = true;","excerpt":"Login and Signup Forms Form components Login and signup views It is notoriously difficult to create reusable  Form  components. That's…","frontmatter":{"title":"Login and Signup Forms","order":15,"active":true,"free":false,"tech":["react"]}}},"pageContext":{"slug":"login-and-signup-forms","prevNode":{"frontmatter":{"title":"Create a Reusable Button Component","active":true,"free":false,"order":14,"tech":["react"]},"fields":{"slug":"create-a-reusable-button-component"}},"nextNode":{"frontmatter":{"title":"Connect the API","active":true,"free":false,"order":16,"tech":["react"]},"fields":{"slug":"connect-the-api"}}}},"staticQueryHashes":["1723017916"]}