Create and Join an Organization on Signup

  • Update Signup component
  • Connect to endpoint
  • Create new action
  • Add header to Chat component

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 /settings route, so let's use that for creating an organization from an existing account and let's update our Signup component to handle an optional organization input.

Since creating an organization from initial signup is easier, we will start with that the move on to creating a user from our /settings route.

Update Signup component

As it turns out, this requires very little effort. All we have to do is add a website field to our form and send it to our backend in the format that it expects.

...
export class Signup extends React.Component {
constructor(props) {
super(props)
this.state = {
username: "",
email: "",
password: "",
passwordVerify: "",
website: ""
}
this.submit = this.submit.bind(this)
}
submit() {
if (this.state.website) {
const userWithOrg = {
username: this.state.username,
email: this.state.email,
password: this.state.password,
owned_organization: {
website: this.state.website
}
}
this.props.dispatch(Actions.userNew(userWithOrg))
} else {
const user = {
username: this.state.username,
email: this.state.email,
password: this.state.password,
}
this.props.dispatch(Actions.userNew(user))
}
}
render() {
return (
<div className={style.wrapper}>
<div className={style.form}>
...
<div className={style.inputGroup}>
<input
onChange={e => { this.handleChange("website", e) }}
value={this.state.website}
placeholder="Website (optional)"
className={style.input}
type="text" />
</div>
...
</div>
</div>
)
}
}
...

Now go ahead and logout by deleting your token from localStorage (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.

To check to make sure this is working, try putting a console.log in your userAuth action. If you do, you should now see public_key and website along with the data you previously received from the server.

The next step is to allow a user who already has an account but no organization create an organization from the /settings route.

Create an action

Go ahead and sign out again by deleting your token and refreshing, then create a new user 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.

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.

The organization parameter we're sending to our server expects a website and an 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 Guardian, but for now, let's just pass the user's ID we're storing in props as the owner_id.

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 auth/me route.

Actions.organizationNew = function organizationNew(organization) {
return dispatch =>
fetch(`${API_HOST}/api/organizations`, {
method: "POST",
headers: {
Accept: "application/json",
"Content-Type": "application/json",
},
body: JSON.stringify({ organization }),
})
.then(res => {
return res.json()
})
.then(res => {
if (res.errors) {
return dispatch({
type: "ORGANIZATION_NEW_ERROR",
payload: {
errors: res.errors,
},
})
}
dispatch({
type: "ORGANIZATION_NEW",
})
return dispatch(Actions.userAuth())
})
.catch(err => {
console.warn(err)
})
}

We should also update our user reducer to take in the new information that we get from the auth/me endpoint. Recall that we now receive public_key and website from that endpoint.

function user(state = {
email: "",
username: "",
id: "",
public_key: "",
website: ""
}, action) {
switch (action.type) {
...
case "USER_AUTH":
return Object.assign({}, state, {
email: action.payload.user.email,
username: action.payload.user.username,
id: action.payload.user.id,
public_key: action.payload.user.public_key,
website: action.payload.user.website
})
default: return state
}
})

The next step is to update our Settings component and connect it to this action. But before we do that, let's update our Chat component with a header that can easily link us to the /settings route.

Add header to Chat component

Now that we have an endpoint, we should add a header to the top of our app that contains a link to our /settings route. From there, can add the functionality to create a new organization.

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).

If there is no current room selected (!this.state.currentRoom), then we render an empty header with just the settings cog.

...
import { Link } from "react-router"
...
export class Chat extends React.Component {
...
renderHeader() {
if (!this.state.currentRoom) {
return (
<div className={style.header}>
<div />
<Link to="settings" className={style.settings}>
<img
alt="link to settings"
className={style.cog}
src="/learnphoenix-static-assets/icons/cog.png" />
</Link>
</div>
)
}
const avatar = {
height: "40px",
width: "40px",
background: "#ccc",
border: "1px solid #888",
borderRadius: "50%"
}
return (
<div className={style.header}>
<div className={style.identity}>
<div style={avatar} />
<div className={style.titleGroup}>
<h3 className={style.title}>
{ this.state.currentRoom }
</h3>
<div className={style.lastActive}>
Last active: __ minutes ago
</div>
</div>
</div>
<Link to="settings" className={style.settings}>
<img
alt="link to settings"
className={style.cog}
src="/learnphoenix-static-assets/icons/cog.png" />
</Link>
</div>
)
}
render() {
return (
<div>
<Sidebar
presences={this.state.presences}
onRoomClick={this.changeChatroom}
lobbyList={this.state.lobbyList} />
<div className={style.chatWrapper}>
{ this.renderHeader() }
<div
className={style.chatContainer}
ref={ref => { this.chatContainer = ref }}>
{ this.renderEmpty() }
{ this.renderMessages() }
</div>
{ this.renderInput() }
</div>
{ this.props.children }
</div>
)
}
}
...

From here we can add some styling to make our header look more like we would expect.

... .header {
position: absolute;
top: 0;
left: 0;
width: 100%;
background: rgb(238, 238, 239);
border: 1px solid rgb(213, 213, 213);
height: 60px;
display: flex;
flex-flow: row nowrap;
justify-content: space-between;
align-items: center;
z-index: 100;
}
.identity {
display: flex;
flex-flow: row nowrap;
align-items: center;
padding-left: 30px;
}
.titleGroup {
padding-left: 10px;
}
.title {
font-size: 1em;
font-weight: normal;
color: #333;
}
.lastActive {
padding-top: 6px;
color: #727272;
font-size: 0.8em;
}
.settings {
padding-right: 30px;
}
.cog {
filter: invert(40%);
height: 35px;
}

Now we have an easy way to navigate to our /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.

Previous: CRUD Endpoints for Organization: Part 2Next: Create Organization from Settings Route