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.
Now that we can easily link to our /settings route, we should add the ability to create a new organization from within that component.
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.
import React from "react"import cssModules from "react-css-modules"import { connect } from "react-redux"import { Link } from "react-router"import style from "./style.css"import Actions from "../../redux/actions"import Button from "../Button"export class Settings extends React.Component { constructor(props) { super(props) this.state = { website: "", } this.submit = this.submit.bind(this) } submit() { const organization = { website: this.state.website, owner_id: this.props.user.id, } this.props.dispatch(Actions.organizationNew(organization)) } handleChange(input, e) { this.setState({ [input]: e.target.value }) } renderOrganization() { if (!this.props.user.public_key) { return ( <div className={style.inputGroup}> <input onChange={e => { this.handleChange("website", e) }} placeholder="Website (e.g. https://phoenixchat.io)" type="text" className={style.input} /> <Button onClick={this.submit} style={{ marginTop: "15px" }} type="primary"> Create Organization </Button> </div> ) } return <div className={style.apiKey}>Your API key: {this.props.user.public_key}</div> } render() { return ( <div> <div className={style.nav}> <Link className={style.logo} to="/"> <img alt="learnphoenix logo" className={style.image} src="/learnphoenix-static-assets/favicons/favicon-96x96.png" /> <div className={style.title}>PhoenixChat.io</div> </Link> <div /> </div> <div className={style.settings}>{this.renderOrganization()}</div> </div> ) }}Settings.propTypes = { user: React.PropTypes.object, organization: React.PropTypes.object, dispatch: React.PropTypes.func,}const mapStateToProps = state => ({ user: state.user,})export default connect(mapStateToProps)(cssModules(Settings, style))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.
$ touch app/components/Settings/{style.css,spec.js}.settings { display: flex; flex-flow: column nowrap; align-items: center; height: calc(100vh - 60px); margin-top: 50px; position: relative; z-index: 100;}.nav { height: 60px; padding: 0 30px; display: flex; flex-flow: row nowrap; justify-content: space-between; align-items: center;}.logo { display: flex; flex-flow: row nowrap; align-items: center;}.title { font-size: 1.5em; cursor: pointer; color: #4c4c4c;}.image { height: 50px; padding-right: 5px;}.input { padding: 1rem 1rem; border-radius: 3px; border: 1px solid #ccc; font-size: 1.1em; outline: none; width: 400px;}.inputGroup { display: flex; flex-flow: column nowrap;}.apiKey { font-size: 1.5em;}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.
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).
import React from "react"import expect from "expect"import { shallow } from "enzyme"import { Settings } from "./"const props = { user: { email: "foo", id: 1234, public_key: "asdf", },}describe("<Settings />", () => { it("should render", () => { const renderedComponent = shallow(<Settings {...props} />) expect(renderedComponent.is("div")).toEqual(true) }) it("should have a submit function", () => { const component = new Settings() expect(component.submit).toExist() })})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.
Previous: Create and Join an Organization on SignupNext: Pass API Key from Frontend