add UI components for adding data and managing local state
This commit is contained in:
parent
8364b3423a
commit
74e6f99e63
9 changed files with 236 additions and 31 deletions
|
@ -54,7 +54,7 @@ export const registerUser = (username, password, onSuccess, onError) => dispatch
|
||||||
service.register(username, password, scb, ecb);
|
service.register(username, password, scb, ecb);
|
||||||
};
|
};
|
||||||
|
|
||||||
export const getIdentity = () => dispatch => {
|
export const getIdentity = (onSuccess, onError) => dispatch => {
|
||||||
const service = new ApiService();
|
const service = new ApiService();
|
||||||
const scb = (data) => {
|
const scb = (data) => {
|
||||||
if (data.id) {
|
if (data.id) {
|
||||||
|
@ -63,8 +63,11 @@ export const getIdentity = () => dispatch => {
|
||||||
data: data
|
data: data
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
if (onSuccess) onSuccess();
|
||||||
};
|
};
|
||||||
const ecb = (error) => { };
|
const ecb = (error) => {
|
||||||
|
if (onError) onError(error);
|
||||||
|
};
|
||||||
|
|
||||||
service.identity(scb, ecb);
|
service.identity(scb, ecb);
|
||||||
};
|
};
|
||||||
|
|
19
client/src/actions/gameActions.js
Normal file
19
client/src/actions/gameActions.js
Normal file
|
@ -0,0 +1,19 @@
|
||||||
|
import ApiGameService from '../services/ApiGameService';
|
||||||
|
import {
|
||||||
|
PUBLIC_STATE_CREATED
|
||||||
|
} from './types';
|
||||||
|
|
||||||
|
export const createPublicState = (requestData, onSuccess, onError) => dispatch => {
|
||||||
|
const service = new ApiGameService();
|
||||||
|
const scb = (data) => {
|
||||||
|
dispatch({
|
||||||
|
type: PUBLIC_STATE_CREATED,
|
||||||
|
data: data
|
||||||
|
});
|
||||||
|
if (onSuccess) onSuccess(data);
|
||||||
|
};
|
||||||
|
const ecb = (error) => {
|
||||||
|
if (onError) onError(error);
|
||||||
|
};
|
||||||
|
service.createPublicState(requestData, scb, ecb);
|
||||||
|
};
|
|
@ -3,3 +3,5 @@ export const USER_LOGGED_OUT = 'USER_LOGGED_OUT';
|
||||||
export const USER_REGISTERED = 'USER_REGISTERED';
|
export const USER_REGISTERED = 'USER_REGISTERED';
|
||||||
export const QUESTIONS_LOADED = 'QUESTIONS_LOADED';
|
export const QUESTIONS_LOADED = 'QUESTIONS_LOADED';
|
||||||
export const QUESTIONS_LOAD_ERROR = 'QUESTIONS_LOADED';
|
export const QUESTIONS_LOAD_ERROR = 'QUESTIONS_LOADED';
|
||||||
|
|
||||||
|
export const PUBLIC_STATE_CREATED = 'PUBLIC_STATE_CREATED';
|
|
@ -0,0 +1,108 @@
|
||||||
|
import React, { Component } from 'react';
|
||||||
|
import { Form, Input, Button, Typography } from 'antd';
|
||||||
|
|
||||||
|
const layout = {
|
||||||
|
labelCol: {
|
||||||
|
span: 8,
|
||||||
|
},
|
||||||
|
wrapperCol: {
|
||||||
|
span: 16,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
const tailLayout = {
|
||||||
|
wrapperCol: {
|
||||||
|
offset: 8,
|
||||||
|
span: 16,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
class CreatePublicState extends Component {
|
||||||
|
|
||||||
|
constructor(props) {
|
||||||
|
super(props);
|
||||||
|
this.state = {
|
||||||
|
createErrorMessage: undefined
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
onFinish = values => {
|
||||||
|
// this.props.registerUser(values.username, values.password, (user) => {
|
||||||
|
// this.setState({
|
||||||
|
// registerErrorMessage: undefined
|
||||||
|
// });
|
||||||
|
// }, (error) => {
|
||||||
|
// this.setState({
|
||||||
|
// registerErrorMessage: error.message
|
||||||
|
// });
|
||||||
|
// });
|
||||||
|
};
|
||||||
|
|
||||||
|
onFinishFailed = errorInfo => { };
|
||||||
|
|
||||||
|
render() {
|
||||||
|
|
||||||
|
let errorMessageStyle = { display: 'none' };
|
||||||
|
if (this.state.createErrorMessage) {
|
||||||
|
errorMessageStyle = {};
|
||||||
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div>
|
||||||
|
<h2 style={{ textAlign: 'center' }}>Create Public State</h2>
|
||||||
|
<Form
|
||||||
|
{...layout}
|
||||||
|
name="basic"
|
||||||
|
initialValues={{
|
||||||
|
remember: true,
|
||||||
|
}}
|
||||||
|
onFinish={this.onFinish}
|
||||||
|
onFinishFailed={this.onFinishFailed}
|
||||||
|
>
|
||||||
|
<Form.Item
|
||||||
|
label="Name"
|
||||||
|
name="name"
|
||||||
|
rules={[
|
||||||
|
{
|
||||||
|
required: true,
|
||||||
|
message: 'Please input the name of the Public State!',
|
||||||
|
},
|
||||||
|
]}
|
||||||
|
>
|
||||||
|
<Input />
|
||||||
|
</Form.Item>
|
||||||
|
|
||||||
|
<Form.Item
|
||||||
|
label="Title"
|
||||||
|
name="title"
|
||||||
|
rules={[
|
||||||
|
{
|
||||||
|
required: false,
|
||||||
|
message: 'Please input the title of the public state',
|
||||||
|
},
|
||||||
|
]}
|
||||||
|
>
|
||||||
|
<Input />
|
||||||
|
</Form.Item>
|
||||||
|
|
||||||
|
<Form.Item
|
||||||
|
style={errorMessageStyle}
|
||||||
|
label=" "
|
||||||
|
colon={false}
|
||||||
|
name="error message">
|
||||||
|
<Typography.Text className="ant-form-text" type="danger">
|
||||||
|
{this.state.createErrorMessage}
|
||||||
|
</Typography.Text>
|
||||||
|
</Form.Item>
|
||||||
|
|
||||||
|
<Form.Item {...tailLayout}>
|
||||||
|
<Button type="primary" htmlType="submit">
|
||||||
|
Submit
|
||||||
|
</Button>
|
||||||
|
</Form.Item>
|
||||||
|
</Form>
|
||||||
|
</div >
|
||||||
|
)
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
export default CreatePublicState;
|
5
client/src/components/state/index.js
Normal file
5
client/src/components/state/index.js
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
import CreatePublicState from './CreatePublicState/CreatePublicState';
|
||||||
|
|
||||||
|
export {
|
||||||
|
CreatePublicState
|
||||||
|
};
|
|
@ -1,11 +1,15 @@
|
||||||
import React, { Component } from 'react';
|
import React, { Component } from 'react';
|
||||||
|
|
||||||
|
import { CreatePublicState } from '../../components/state';
|
||||||
|
|
||||||
class DashboardPage extends Component {
|
class DashboardPage extends Component {
|
||||||
|
|
||||||
render() {
|
render() {
|
||||||
return (
|
return (
|
||||||
<div>
|
<div>
|
||||||
<h1>Dashboard</h1>
|
<h1>Dashboard</h1>
|
||||||
|
<hr />
|
||||||
|
<CreatePublicState />
|
||||||
</div>
|
</div>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
22
client/src/reducers/stateData.js
Normal file
22
client/src/reducers/stateData.js
Normal file
|
@ -0,0 +1,22 @@
|
||||||
|
import {
|
||||||
|
PUBLIC_STATE_CREATED
|
||||||
|
} from '../actions/types';
|
||||||
|
|
||||||
|
const initialState = {
|
||||||
|
publicState: undefined,
|
||||||
|
privateStates: []
|
||||||
|
};
|
||||||
|
|
||||||
|
export default function (state = initialState, action) {
|
||||||
|
|
||||||
|
switch (action.type) {
|
||||||
|
case PUBLIC_STATE_CREATED: {
|
||||||
|
return {
|
||||||
|
...state,
|
||||||
|
data: action.data
|
||||||
|
};
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
return state;
|
||||||
|
}
|
||||||
|
};
|
|
@ -25,7 +25,15 @@ const { Content } = Layout;
|
||||||
// screen if you're not yet authenticated.
|
// screen if you're not yet authenticated.
|
||||||
export const PrivateRoute = ({ children, ...rest }) => {
|
export const PrivateRoute = ({ children, ...rest }) => {
|
||||||
|
|
||||||
|
console.log('children: ' + children);
|
||||||
|
const keys = Object.keys(children);
|
||||||
|
console.log('children.keys: ' + keys);
|
||||||
|
console.log('children.props: ' + keys.props);
|
||||||
|
const childrenPropKeys = Object.keys(children.props);
|
||||||
|
console.log('children.props.keys: ' + childrenPropKeys);
|
||||||
console.log('rest: ' + rest);
|
console.log('rest: ' + rest);
|
||||||
|
const restKeys = Object.keys(rest);
|
||||||
|
console.log('rest.keys: ' + restKeys);
|
||||||
console.log('rest.exact: ' + rest.exact);
|
console.log('rest.exact: ' + rest.exact);
|
||||||
console.log('rest.user: ' + rest.user);
|
console.log('rest.user: ' + rest.user);
|
||||||
|
|
||||||
|
@ -51,39 +59,60 @@ export const PrivateRoute = ({ children, ...rest }) => {
|
||||||
|
|
||||||
class AppRouter extends Component {
|
class AppRouter extends Component {
|
||||||
|
|
||||||
|
constructor(props) {
|
||||||
|
super(props);
|
||||||
|
this.state = {
|
||||||
|
identityChecked: false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
componentDidMount = () => {
|
componentDidMount = () => {
|
||||||
this.props.getIdentity();
|
const scb = () => {
|
||||||
|
this.setState({
|
||||||
|
identityChecked: true
|
||||||
|
})
|
||||||
|
};
|
||||||
|
this.props.getIdentity(scb);
|
||||||
};
|
};
|
||||||
|
|
||||||
render() {
|
render() {
|
||||||
return (
|
|
||||||
<Router>
|
if (this.state.identityChecked) {
|
||||||
<Layout style={{ height: "100%" }}>
|
console.log('user: ' + this.props.user);
|
||||||
<AppHeader />
|
return (
|
||||||
<Content style={{ padding: '0 50px', marginTop: 64 }}>
|
<Router>
|
||||||
<div style={{ background: '#fff', padding: 24 }}>
|
<Layout style={{ height: "100%" }}>
|
||||||
<Switch>
|
<AppHeader />
|
||||||
<Route exact path='/'>
|
<Content style={{ padding: '0 50px', marginTop: 64 }}>
|
||||||
<WelcomePage />
|
|
||||||
</Route>
|
<div style={{ background: '#fff', padding: 24 }}>
|
||||||
<PrivateRoute exact path='/start' user={this.props.user}>
|
<Switch>
|
||||||
<DashboardPage />
|
<Route exact path='/'>
|
||||||
</PrivateRoute>
|
<WelcomePage />
|
||||||
<Route exact path='/foo'>
|
</Route>
|
||||||
<SplashScreenPage />
|
<PrivateRoute exact path='/start' user={this.props.user}>
|
||||||
</Route>
|
<DashboardPage />
|
||||||
<Route exact path='/login'>
|
</PrivateRoute>
|
||||||
<LoginPage />
|
<Route exact path='/foo'>
|
||||||
</Route>
|
<SplashScreenPage />
|
||||||
<Route exact path='/register'>
|
</Route>
|
||||||
<RegisterPage />
|
<Route exact path='/login'>
|
||||||
</Route>
|
<LoginPage />
|
||||||
</Switch>
|
</Route>
|
||||||
</div>
|
<Route exact path='/register'>
|
||||||
</Content>
|
<RegisterPage />
|
||||||
</Layout>
|
</Route>
|
||||||
</Router>
|
</Switch>
|
||||||
)
|
</div>
|
||||||
|
</Content>
|
||||||
|
</Layout>
|
||||||
|
</Router>
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
return (
|
||||||
|
<h2 style={{textAlign: 'center'}}>loading application...</h2>
|
||||||
|
)
|
||||||
|
}
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
13
client/src/services/ApiGameService.js
Normal file
13
client/src/services/ApiGameService.js
Normal file
|
@ -0,0 +1,13 @@
|
||||||
|
import ApiService from './ApiService';
|
||||||
|
|
||||||
|
class ApiGameService {
|
||||||
|
constructor() {
|
||||||
|
this.service = new ApiService();
|
||||||
|
};
|
||||||
|
|
||||||
|
createPublicState = (data, onSuccess, onError) => {
|
||||||
|
this.service._post('/api/state', data, onSuccess, onError);
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
export default ApiGameService;
|
Loading…
Reference in a new issue