{"componentChunkName":"component---src-templates-lesson-js","path":"/phoenix-chat/create-organization-from-settings-route","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\": \"Create Organization from Settings Route\",\n  \"order\": 46,\n  \"free\": false,\n  \"active\": true,\n  \"tech\": [\"react\", \"redux\"]\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, \"Create Organization from Settings Route\"), mdx(\"ul\", null, mdx(\"li\", {\n    \"parentName\": \"ul\"\n  }, \"Update Settings component\")), mdx(\"p\", null, \"So at this point, we can create an organization on signup, but we cannot create an organization after we've signed up. What we need is a settings page that will allow unaffiliated administrators to create a new organization and will allow affiliated administrators to see their API key.\"), mdx(\"h2\", null, \"Update Settings component\"), mdx(\"p\", null, \"Now that we can easily link to our \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"/settings\"), \" route, we should add the ability to create a new organization from within that component.\"), mdx(\"p\", null, \"This is a lot of code, but none of it is new. We're adding a form just like we did in all our other forms and we're selectively rendering either a form or an API key depending on whether or not the current user has an associated organization.\"), mdx(\"pre\", null, mdx(\"code\", {\n    \"className\": \"language-js\",\n    \"parentName\": \"pre\"\n  }, \"import React from \\\"react\\\"\\nimport cssModules from \\\"react-css-modules\\\"\\nimport { connect } from \\\"react-redux\\\"\\nimport { Link } from \\\"react-router\\\"\\nimport style from \\\"./style.css\\\"\\nimport Actions from \\\"../../redux/actions\\\"\\n\\nimport Button from \\\"../Button\\\"\\n\\nexport class Settings extends React.Component {\\n  constructor(props) {\\n    super(props)\\n    this.state = {\\n      website: \\\"\\\",\\n    }\\n    this.submit = this.submit.bind(this)\\n  }\\n\\n  submit() {\\n    const organization = {\\n      website: this.state.website,\\n      owner_id: this.props.user.id,\\n    }\\n    this.props.dispatch(Actions.organizationNew(organization))\\n  }\\n\\n  handleChange(input, e) {\\n    this.setState({ [input]: e.target.value })\\n  }\\n\\n  renderOrganization() {\\n    if (!this.props.user.public_key) {\\n      return (\\n        <div className={style.inputGroup}>\\n          <input\\n            onChange={e => {\\n              this.handleChange(\\\"website\\\", e)\\n            }}\\n            placeholder=\\\"Website (e.g. https://phoenixchat.io)\\\"\\n            type=\\\"text\\\"\\n            className={style.input}\\n          />\\n          <Button onClick={this.submit} style={{ marginTop: \\\"15px\\\" }} type=\\\"primary\\\">\\n            Create Organization\\n          </Button>\\n        </div>\\n      )\\n    }\\n    return <div className={style.apiKey}>Your API key: {this.props.user.public_key}</div>\\n  }\\n\\n  render() {\\n    return (\\n      <div>\\n        <div className={style.nav}>\\n          <Link className={style.logo} to=\\\"/\\\">\\n            <img\\n              alt=\\\"learnphoenix logo\\\"\\n              className={style.image}\\n              src=\\\"/learnphoenix-static-assets/favicons/favicon-96x96.png\\\"\\n            />\\n            <div className={style.title}>PhoenixChat.io</div>\\n          </Link>\\n          <div />\\n        </div>\\n        <div className={style.settings}>{this.renderOrganization()}</div>\\n      </div>\\n    )\\n  }\\n}\\n\\nSettings.propTypes = {\\n  user: React.PropTypes.object,\\n  organization: React.PropTypes.object,\\n  dispatch: React.PropTypes.func,\\n}\\n\\nconst mapStateToProps = state => ({\\n  user: state.user,\\n})\\n\\nexport default connect(mapStateToProps)(cssModules(Settings, style))\\n\")), mdx(\"p\", null, \"That's a lot of code, but it's nothing you haven't seen before. And now to make this page usable, we need to add some styling.\"), mdx(\"pre\", null, mdx(\"code\", {\n    \"className\": \"language-bash\",\n    \"parentName\": \"pre\"\n  }, \"$ touch app/components/Settings/{style.css,spec.js}\\n\")), mdx(\"pre\", null, mdx(\"code\", {\n    \"className\": \"language-css\",\n    \"parentName\": \"pre\"\n  }, \".settings {\\n  display: flex;\\n  flex-flow: column nowrap;\\n  align-items: center;\\n  height: calc(100vh - 60px);\\n  margin-top: 50px;\\n  position: relative;\\n  z-index: 100;\\n}\\n\\n.nav {\\n  height: 60px;\\n  padding: 0 30px;\\n  display: flex;\\n  flex-flow: row nowrap;\\n  justify-content: space-between;\\n  align-items: center;\\n}\\n\\n.logo {\\n  display: flex;\\n  flex-flow: row nowrap;\\n  align-items: center;\\n}\\n\\n.title {\\n  font-size: 1.5em;\\n  cursor: pointer;\\n  color: #4c4c4c;\\n}\\n\\n.image {\\n  height: 50px;\\n  padding-right: 5px;\\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  width: 400px;\\n}\\n\\n.inputGroup {\\n  display: flex;\\n  flex-flow: column nowrap;\\n}\\n\\n.apiKey {\\n  font-size: 1.5em;\\n}\\n\")), mdx(\"p\", null, \"Now, if you're signed in and not affiliated with an organization, you can create one. Otherwise, it shows your API key. Our settings page is a little bit sparse, but to be fair, we don't have a lot of settings at the moment.\"), mdx(\"h2\", null, \"Settings tests\"), mdx(\"p\", null, \"We should also write a few tests. This component really doesn't do all that much, so all we should really test is that it renders, it has a submit function, and that the proper organization option is rendered (form with button or the key).\"), 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 { Settings } from \\\"./\\\"\\n\\nconst props = {\\n  user: {\\n    email: \\\"foo\\\",\\n    id: 1234,\\n    public_key: \\\"asdf\\\",\\n  },\\n}\\n\\ndescribe(\\\"<Settings />\\\", () => {\\n  it(\\\"should render\\\", () => {\\n    const renderedComponent = shallow(<Settings {...props} />)\\n    expect(renderedComponent.is(\\\"div\\\")).toEqual(true)\\n  })\\n  it(\\\"should have a submit function\\\", () => {\\n    const component = new Settings()\\n    expect(component.submit).toExist()\\n  })\\n})\\n\")), mdx(\"p\", null, \"Now that we have access to our public key, let's add that to our npm component, which will pass it along to our API in order to properly route messages.\"));\n}\n;\nMDXContent.isMDXComponent = true;","excerpt":"Create Organization from Settings Route Update Settings component So at this point, we can create an organization on signup, but we cannot…","frontmatter":{"title":"Create Organization from Settings Route","order":46,"active":true,"free":false,"tech":["react","redux"]}}},"pageContext":{"slug":"create-organization-from-settings-route","prevNode":{"frontmatter":{"title":"Create and Join an Organization on Signup","active":true,"free":false,"order":45,"tech":["react","redux"]},"fields":{"slug":"create-and-join-an-organization-on-signup"}},"nextNode":{"frontmatter":{"title":"Pass API Key from Frontend","active":true,"free":false,"order":47,"tech":["react"]},"fields":{"slug":"pass-api-key-from-frontend"}}}},"staticQueryHashes":["1723017916"]}