add UI components for adding data and managing local state

This commit is contained in:
Markus Schubert 2020-03-25 19:28:35 +01:00
parent 8364b3423a
commit 74e6f99e63
9 changed files with 236 additions and 31 deletions

View file

@ -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) => {
if (onError) onError(error);
}; };
const ecb = (error) => { };
service.identity(scb, ecb); service.identity(scb, ecb);
}; };

View 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);
};

View file

@ -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';

View file

@ -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;

View file

@ -0,0 +1,5 @@
import CreatePublicState from './CreatePublicState/CreatePublicState';
export {
CreatePublicState
};

View file

@ -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>
) )
} }

View 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;
}
};

View file

@ -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,16 +59,32 @@ 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() {
if (this.state.identityChecked) {
console.log('user: ' + this.props.user);
return ( return (
<Router> <Router>
<Layout style={{ height: "100%" }}> <Layout style={{ height: "100%" }}>
<AppHeader /> <AppHeader />
<Content style={{ padding: '0 50px', marginTop: 64 }}> <Content style={{ padding: '0 50px', marginTop: 64 }}>
<div style={{ background: '#fff', padding: 24 }}> <div style={{ background: '#fff', padding: 24 }}>
<Switch> <Switch>
<Route exact path='/'> <Route exact path='/'>
@ -84,6 +108,11 @@ class AppRouter extends Component {
</Layout> </Layout>
</Router> </Router>
) )
} else {
return (
<h2 style={{textAlign: 'center'}}>loading application...</h2>
)
}
}; };
}; };

View 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;