Semi-working version of create new team form

This commit is contained in:
Luka Murn 2017-09-18 09:13:37 +02:00
parent df44976fc6
commit 80c05e50ec
5 changed files with 103 additions and 48 deletions

View file

@ -8,31 +8,42 @@ module ClientApi
teams: current_user.teams_data)
end
def new; end
def create
teams_service = ClientApi::TeamsService.new(current_user: current_user,
params: team_params)
teams_service.create_team!
success_response('/client_api/teams/index', teams_service.teams_data)
rescue ClientApi::CustomTeamError => error
error_response(error.to_s)
end
def details
team_service = ClientApi::TeamsService.new(team_id: params[:team_id],
teams_service = ClientApi::TeamsService.new(team_id: params[:team_id],
current_user: current_user)
success_response('/client_api/teams/details',
team_service.team_page_details_data)
teams_service.team_page_details_data)
rescue ClientApi::CustomTeamError
error_response
end
def change_team
team_service = ClientApi::TeamsService.new(team_id: params[:team_id],
teams_service = ClientApi::TeamsService.new(team_id: params[:team_id],
current_user: current_user)
team_service.change_current_team!
success_response('/client_api/teams/index', team_service.teams_data)
teams_service.change_current_team!
success_response('/client_api/teams/index', teams_service.teams_data)
rescue ClientApi::CustomTeamError
error_response
end
def update
team_service = ClientApi::TeamsService.new(team_id: params[:team_id],
teams_service = ClientApi::TeamsService.new(team_id: params[:team_id],
current_user: current_user,
params: team_params)
team_service.update_team!
teams_service.update_team!
success_response('/client_api/teams/update_details',
team_service.single_team_details_data)
teams_service.single_team_details_data)
rescue ClientApi::CustomTeamError => error
error_response(error.to_s)
end
@ -40,7 +51,7 @@ module ClientApi
private
def team_params
params.require(:team).permit(:description, :name)
params.require(:team).permit(:name, :description)
end
def success_response(template, locals)

View file

@ -11,7 +11,7 @@ export const SETTINGS_ACCOUNT_PREFERENCES_PATH =
// teams
export const TEAMS_PATH = "/client_api/teams";
export const TEAMS_NEW_PATH = "/client_api/teams/new";
export const TEAMS_NEW_PATH = "/client_api/teams";
export const CHANGE_TEAM_PATH = "/client_api/teams/change_team";
export const TEAM_DETAILS_PATH = "/client_api/teams/:team_id/details";
export const TEAM_UPDATE_PATH = "/client_api/teams/update";

View file

@ -1,10 +1,11 @@
import React, { Component } from "react";
import update from "immutability-helper";
import styled from "styled-components";
import axios from "../../../../../app/axios";
import { Breadcrumb, FormGroup, FormControl, ControlLabel, HelpBlock, Button } from "react-bootstrap";
import { Redirect } from "react-router";
import { LinkContainer } from "react-router-bootstrap";
import { FormattedMessage } from "react-intl";
import update from "immutability-helper";
import styled from "styled-components";
import axios from "../../../../../config/axios";
import { SETTINGS_TEAMS_ROUTE } from "../../../../../config/routes";
import { TEAMS_NEW_PATH } from "../../../../../config/api_endpoints";
import {
@ -31,15 +32,14 @@ class SettingsNewTeam extends Component {
super(props);
this.state = {
team: {
name: {
value: "",
errorMessage: "",
},
description: {
value: "",
errorMessage: "",
}
}
name: "",
description: "",
},
errorMessages: {
name: "",
description: ""
},
formSuccess: false
};
this.getValidationState = this.getValidationState.bind(this);
@ -48,8 +48,27 @@ class SettingsNewTeam extends Component {
this.onSubmit = this.onSubmit.bind(this);
}
onSubmit() {
axios({
method: "post",
url: TEAMS_NEW_PATH,
withCredentials: true,
data: { team: this.state.team }
})
.then(response => {
// Redirect back to teams page
this.newState = { ...this.state };
this.newState = update(
this.newState,
{ formSuccess: { $set: true } }
);
this.setState(this.newState);
})
.catch(error => this.setState({ errorMessage: error.message })); // TODO: Generic error message
}
getValidationState(attr) {
if (this.state.team[attr].errorMessage.length > 0) {
if (this.state.errorMessages[attr].length > 0) {
return "error";
}
return null;
@ -68,7 +87,7 @@ class SettingsNewTeam extends Component {
this.newState = update(
this.newState,
{ team: { name: { errorMessage: { $set: errorMessage } } } }
{ errorMessages: { name: { $set: errorMessage } } }
);
} else if (key === "description") {
errorMessage = "";
@ -79,7 +98,7 @@ class SettingsNewTeam extends Component {
this.newState = update(
this.newState,
{ team: { description: { errorMessage: { $set: errorMessage } } } }
{ errorMessages: { description: { $set: errorMessage } } }
);
}
}
@ -93,7 +112,7 @@ class SettingsNewTeam extends Component {
// Update value in the state
this.newState = update(
this.newState,
{ team: { [key]: { value: { $set: value } } } }
{ team: { [key]: { $set: value } } }
);
// Validate the input
@ -103,20 +122,11 @@ class SettingsNewTeam extends Component {
this.setState(this.newState);
}
onSubmit() {
axios({
method: "post",
url: TEAMS_NEW_PATH,
withCredentials: true,
data: { team: this.state.team }
})
.then(response => {
// TODO: Redirect to team page
})
.catch(error => this.setState({ errorMessage: error.message }));
}
render() {
if (this.state.formSuccess) {
return <Redirect to={SETTINGS_TEAMS_ROUTE} />;
}
return (
<Wrapper>
<Breadcrumb>
@ -140,12 +150,12 @@ class SettingsNewTeam extends Component {
<FormattedMessage id="settings_page.new_team.name_label" />
</ControlLabel>
<NameFormControl
value={this.state.team.name.value}
value={this.state.team.name}
onChange={this.handleChange}
name="name"
/>
<FormControl.Feedback />
<HelpBlock>{this.state.team.name.errorMessage}</HelpBlock>
<HelpBlock>{this.state.errorMessages.name}</HelpBlock>
</FormGroup>
<small>
<FormattedMessage id="settings_page.new_team.name_sublabel" />
@ -162,12 +172,12 @@ class SettingsNewTeam extends Component {
</ControlLabel>
<FormControl
componentClass="textarea"
value={this.state.team.description.value}
value={this.state.team.description}
onChange={this.handleChange}
name="description"
/>
<FormControl.Feedback />
<HelpBlock>{this.state.team.description.errorMessage}</HelpBlock>
<HelpBlock>{this.state.errorMessages.description}</HelpBlock>
</FormGroup>
<small>
<FormattedMessage id="settings_page.new_team.description_sublabel" />

View file

@ -1,11 +1,42 @@
require 'aspector'
module ClientApi
class TeamsService
def initialize(arg)
team_id = arg.fetch(:team_id) { raise ClientApi::CustomTeamError }
@params = arg.fetch(:params) { false }
@team = Team.find_by_id(team_id)
@user = arg.fetch(:current_user) { raise ClientApi::CustomTeamError }
raise ClientApi::CustomTeamError unless @user.teams.include? @team
@arg = arg || {}
@params = @arg.fetch(:params) { false }
@user = @arg.fetch(:current_user) { raise ClientApi::CustomTeamError }
end
aspector do
before %w(
change_current_team!
team_page_details_data
single_team_details_data
update_team!
teams_data
) do
team_id = @arg.fetch(:team_id) { raise ClientApi::CustomTeamError }
@team = Team.find_by_id(team_id)
raise ClientApi::CustomTeamError unless @user.teams.include? @team
end
end
def create_team!
@team = Team.new(@params)
@team.created_by = @user
if @team.save
# Okay, team is created, now
# add the current user as admin
UserTeam.create(
user: @user,
team: @team,
role: 2
)
else
raise ClientApi::CustomTeamError, @team.errors.full_messages
end
end
def change_current_team!
@ -34,5 +65,6 @@ module ClientApi
{ teams: @user.teams_data }
end
end
CustomTeamError = Class.new(StandardError)
end

View file

@ -19,7 +19,9 @@ Rails.application.routes.draw do
# teams
get '/teams', to: 'teams/teams#index'
namespace :teams do
get '/new', to: 'teams#new'
get '/:team_id/details', to: 'teams#details'
post '/', to: 'teams#create'
post '/change_team', to: 'teams#change_team'
post '/update', to: 'teams#update'
end