{"componentChunkName":"component---src-templates-lesson-js","path":"/phoenix-chat/create-and-join-an-organization-on-signup","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 and Join an Organization on Signup\",\n  \"order\": 45,\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 and Join an Organization on Signup\"), mdx(\"ul\", null, mdx(\"li\", {\n    \"parentName\": \"ul\"\n  }, \"Update Signup component\"), mdx(\"li\", {\n    \"parentName\": \"ul\"\n  }, \"Connect to endpoint\"), mdx(\"li\", {\n    \"parentName\": \"ul\"\n  }, \"Create new action\"), mdx(\"li\", {\n    \"parentName\": \"ul\"\n  }, \"Add header to Chat component\")), mdx(\"p\", null, \"Now that we can create an organization on our backend, let's add a new view on our frontend that will access our new endpoints. We already have a \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"/settings\"), \" route, so let's use that for creating an organization from an existing account and let's update our \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"Signup\"), \" component to handle an optional organization input.\"), mdx(\"p\", null, \"Since creating an organization from initial signup is easier, we will start with that the move on to creating a user from our \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"/settings\"), \" route.\"), mdx(\"h2\", null, \"Update Signup component\"), mdx(\"p\", null, \"As it turns out, this requires very little effort. All we have to do is add a \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"website\"), \" field to our form and send it to our backend in the format that it expects.\"), mdx(\"pre\", null, mdx(\"code\", {\n    \"className\": \"language-js\",\n    \"parentName\": \"pre\"\n  }, \"...\\n\\nexport class Signup extends React.Component {\\n  constructor(props) {\\n    super(props)\\n    this.state = {\\n      username: \\\"\\\",\\n      email: \\\"\\\",\\n      password: \\\"\\\",\\n      passwordVerify: \\\"\\\",\\n      website: \\\"\\\"\\n    }\\n    this.submit = this.submit.bind(this)\\n  }\\n\\n  submit() {\\n    if (this.state.website) {\\n      const userWithOrg = {\\n        username: this.state.username,\\n        email: this.state.email,\\n        password: this.state.password,\\n        owned_organization: {\\n          website: this.state.website\\n        }\\n      }\\n      this.props.dispatch(Actions.userNew(userWithOrg))\\n    } else {\\n      const user = {\\n        username: this.state.username,\\n        email: this.state.email,\\n        password: this.state.password,\\n      }\\n      this.props.dispatch(Actions.userNew(user))\\n    }\\n  }\\n\\n  render() {\\n    return (\\n      <div className={style.wrapper}>\\n        <div className={style.form}>\\n          ...\\n          <div className={style.inputGroup}>\\n            <input\\n              onChange={e => { this.handleChange(\\\"website\\\", e) }}\\n              value={this.state.website}\\n              placeholder=\\\"Website (optional)\\\"\\n              className={style.input}\\n              type=\\\"text\\\" />\\n          </div>\\n          ...\\n        </div>\\n      </div>\\n    )\\n  }\\n}\\n...\\n\")), mdx(\"p\", null, \"Now go ahead and logout by deleting your token from localStorage (\", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"delete localStorage.token\"), \"), then refresh the page. You are now logged out and unauthenticated. Now, when you create a valid account with a valid website, you'll create both a user and an organization at the same time.\"), mdx(\"p\", null, \"To check to make sure this is working, try putting a \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"console.log\"), \" in your \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"userAuth\"), \" action. If you do, you should now see \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"public_key\"), \" and \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"website\"), \" along with the data you previously received from the server.\"), mdx(\"p\", null, \"The next step is to allow a user who already has an account but no organization create an organization from the \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"/settings\"), \" route.\"), mdx(\"h2\", null, \"Create an action\"), mdx(\"p\", null, \"Go ahead and sign out again by deleting your token and refreshing, then create a new user \", mdx(\"em\", {\n    \"parentName\": \"p\"\n  }, \"without\"), \" including a website. From here, we'll create a new Redux action that connects to our endpoint so that the existing user can create a new organization.\"), mdx(\"p\", null, \"Everything in this action should look familiar. In the event of an error, we're passing our errors to our reducers but we aren't going to bother with proper error handling at the moment.\"), mdx(\"p\", null, \"The \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"organization\"), \" parameter we're sending to our server expects a \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"website\"), \" and an \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"owner_id\"), \", which is the ID of the currently logged-in user. At some point in the future, we will refactor this to handle authentication on the backend using \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"Guardian\"), \", but for now, let's just pass the user's ID we're storing in \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"props\"), \" as the \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"owner_id\"), \".\"), mdx(\"p\", null, \"If the organization is created successfully, we will re-authorize the user so we can get the credentials for this organization, which come from our recently-changed \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"auth/me\"), \" route.\"), mdx(\"pre\", null, mdx(\"code\", {\n    \"className\": \"language-js\",\n    \"parentName\": \"pre\"\n  }, \"Actions.organizationNew = function organizationNew(organization) {\\n  return dispatch =>\\n    fetch(`${API_HOST}/api/organizations`, {\\n      method: \\\"POST\\\",\\n      headers: {\\n        Accept: \\\"application/json\\\",\\n        \\\"Content-Type\\\": \\\"application/json\\\",\\n      },\\n      body: JSON.stringify({ organization }),\\n    })\\n      .then(res => {\\n        return res.json()\\n      })\\n      .then(res => {\\n        if (res.errors) {\\n          return dispatch({\\n            type: \\\"ORGANIZATION_NEW_ERROR\\\",\\n            payload: {\\n              errors: res.errors,\\n            },\\n          })\\n        }\\n        dispatch({\\n          type: \\\"ORGANIZATION_NEW\\\",\\n        })\\n        return dispatch(Actions.userAuth())\\n      })\\n      .catch(err => {\\n        console.warn(err)\\n      })\\n}\\n\")), mdx(\"p\", null, \"We should also update our \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"user\"), \" reducer to take in the new information that we get from the \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"auth/me\"), \" endpoint. Recall that we now receive \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"public_key\"), \" and \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"website\"), \" from that endpoint.\"), mdx(\"pre\", null, mdx(\"code\", {\n    \"className\": \"language-js\",\n    \"parentName\": \"pre\"\n  }, \"function user(state = {\\n  email: \\\"\\\",\\n  username: \\\"\\\",\\n  id: \\\"\\\",\\n  public_key: \\\"\\\",\\n  website: \\\"\\\"\\n}, action) {\\n  switch (action.type) {\\n\\n    ...\\n\\n    case \\\"USER_AUTH\\\":\\n      return Object.assign({}, state, {\\n        email: action.payload.user.email,\\n        username: action.payload.user.username,\\n        id: action.payload.user.id,\\n        public_key: action.payload.user.public_key,\\n        website: action.payload.user.website\\n      })\\n    default: return state\\n  }\\n})\\n\")), mdx(\"p\", null, \"The next step is to update our \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"Settings\"), \" component and connect it to this action. But before we do that, let's update our \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"Chat\"), \" component with a header that can easily link us to the \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"/settings\"), \" route.\"), mdx(\"h2\", null, \"Add header to Chat component\"), mdx(\"p\", null, \"Now that we have an endpoint, we should add a header to the top of our app that contains a link to our \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"/settings\"), \" route. From there, can add the functionality to create a new organization.\"), mdx(\"p\", null, \"We're going to make a fairly simple header that shows the name of the currently-selected anonymous user (i.e. room), displays a (currently placeholder) avatar, and shows when they were last active (currently a placeholder as well).\"), mdx(\"p\", null, \"If there is no current room selected (\", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"!this.state.currentRoom\"), \"), then we render an empty header with just the settings cog.\"), mdx(\"pre\", null, mdx(\"code\", {\n    \"className\": \"language-js\",\n    \"parentName\": \"pre\"\n  }, \"...\\nimport { Link } from \\\"react-router\\\"\\n...\\n\\nexport class Chat extends React.Component {\\n  ...\\n\\n  renderHeader() {\\n    if (!this.state.currentRoom) {\\n      return (\\n        <div className={style.header}>\\n          <div />\\n          <Link to=\\\"settings\\\" className={style.settings}>\\n            <img\\n              alt=\\\"link to settings\\\"\\n              className={style.cog}\\n              src=\\\"/learnphoenix-static-assets/icons/cog.png\\\" />\\n          </Link>\\n        </div>\\n      )\\n    }\\n\\n    const avatar = {\\n      height: \\\"40px\\\",\\n      width: \\\"40px\\\",\\n      background: \\\"#ccc\\\",\\n      border: \\\"1px solid #888\\\",\\n      borderRadius: \\\"50%\\\"\\n    }\\n\\n    return (\\n      <div className={style.header}>\\n        <div className={style.identity}>\\n          <div style={avatar} />\\n          <div className={style.titleGroup}>\\n            <h3 className={style.title}>\\n              { this.state.currentRoom }\\n            </h3>\\n            <div className={style.lastActive}>\\n              Last active: __ minutes ago\\n            </div>\\n          </div>\\n        </div>\\n        <Link to=\\\"settings\\\" className={style.settings}>\\n          <img\\n            alt=\\\"link to settings\\\"\\n            className={style.cog}\\n            src=\\\"/learnphoenix-static-assets/icons/cog.png\\\" />\\n        </Link>\\n      </div>\\n    )\\n  }\\n\\n  render() {\\n    return (\\n      <div>\\n        <Sidebar\\n          presences={this.state.presences}\\n          onRoomClick={this.changeChatroom}\\n          lobbyList={this.state.lobbyList} />\\n        <div className={style.chatWrapper}>\\n          { this.renderHeader() }\\n          <div\\n            className={style.chatContainer}\\n            ref={ref => { this.chatContainer = ref }}>\\n            { this.renderEmpty() }\\n            { this.renderMessages() }\\n          </div>\\n          { this.renderInput() }\\n        </div>\\n        { this.props.children }\\n      </div>\\n    )\\n  }\\n}\\n...\\n\")), mdx(\"p\", null, \"From here we can add some styling to make our header look more like we would expect.\"), mdx(\"pre\", null, mdx(\"code\", {\n    \"className\": \"language-css\",\n    \"parentName\": \"pre\"\n  }, \"... .header {\\n  position: absolute;\\n  top: 0;\\n  left: 0;\\n  width: 100%;\\n  background: rgb(238, 238, 239);\\n  border: 1px solid rgb(213, 213, 213);\\n  height: 60px;\\n  display: flex;\\n  flex-flow: row nowrap;\\n  justify-content: space-between;\\n  align-items: center;\\n  z-index: 100;\\n}\\n\\n.identity {\\n  display: flex;\\n  flex-flow: row nowrap;\\n  align-items: center;\\n  padding-left: 30px;\\n}\\n\\n.titleGroup {\\n  padding-left: 10px;\\n}\\n\\n.title {\\n  font-size: 1em;\\n  font-weight: normal;\\n  color: #333;\\n}\\n\\n.lastActive {\\n  padding-top: 6px;\\n  color: #727272;\\n  font-size: 0.8em;\\n}\\n\\n.settings {\\n  padding-right: 30px;\\n}\\n\\n.cog {\\n  filter: invert(40%);\\n  height: 35px;\\n}\\n\")), mdx(\"p\", null, \"Now we have an easy way to navigate to our \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"/settings\"), \" route by clicking on the cog in the header. From here, we need to add the ability to create an organization from the settings page or display the affiliated organization's API key.\"));\n}\n;\nMDXContent.isMDXComponent = true;","excerpt":"Create and Join an Organization on Signup Update Signup component Connect to endpoint Create new action Add header to Chat component Now…","frontmatter":{"title":"Create and Join an Organization on Signup","order":45,"active":true,"free":false,"tech":["react","redux"]}}},"pageContext":{"slug":"create-and-join-an-organization-on-signup","prevNode":{"frontmatter":{"title":"CRUD Endpoints for Organization: Part 2","active":true,"free":false,"order":44,"tech":["phoenix"]},"fields":{"slug":"crud-endpoints-for-organization-part-2"}},"nextNode":{"frontmatter":{"title":"Create Organization from Settings Route","active":true,"free":false,"order":46,"tech":["react","redux"]},"fields":{"slug":"create-organization-from-settings-route"}}}},"staticQueryHashes":["1723017916"]}