show list of public states in frontend

This commit is contained in:
Markus Schubert 2020-03-29 18:45:51 +02:00
parent f399b14ffa
commit 4639eade8d
8 changed files with 89 additions and 19 deletions

View file

@ -7,13 +7,12 @@ class AdminController {
getAll(onSuccess, onError) { getAll(onSuccess, onError) {
let collection = this.database.collection('publicStates'); let collection = this.database.collection('publicStates');
let cursor = collection.find({}, {}); let cursor = collection.find({}, {}).toArray();
cursor.toArray((item) => { cursor.then((items) => {
console.log('item: ' + item); onSuccess(items);
}); });
onSuccess({});
}; };
}; };
module.exports = AdminController; module.exports = AdminController;

View file

@ -1,19 +1,35 @@
import ApiGameService from '../services/ApiGameService'; import ApiGameService from '../services/ApiGameService';
import { import {
PUBLIC_STATE_CREATED PUBLIC_STATE_CREATED,
PUBLIC_STATES_LOADED
} from './types'; } from './types';
export const createPublicState = (requestData, onSuccess, onError) => dispatch => { export const createPublicState = (requestData, onSuccess, onError) => dispatch => {
const service = new ApiGameService(); const service = new ApiGameService();
const scb = (data) => { const scb = (data) => {
dispatch({ dispatch({
type: PUBLIC_STATE_CREATED, type: PUBLIC_STATE_CREATED,
data: data data: data
}); });
if (onSuccess) onSuccess(data); if (onSuccess) onSuccess(data);
}; };
const ecb = (error) => { const ecb = (error) => {
if (onError) onError(error); if (onError) onError(error);
}; };
service.createPublicState(requestData, scb, ecb); service.createPublicState(requestData, scb, ecb);
}; };
export const loadPublicStates = (onSuccess, onError) => dispatch => {
const service = new ApiGameService();
const scb = (data) => {
dispatch({
type: PUBLIC_STATES_LOADED,
data: data
});
if (onSuccess) onSuccess(data);
};
const ecb = (error) => {
if (onError) onError(error);
};
service.getPublicStates(scb, ecb);
};

View file

@ -1,7 +1,9 @@
export const USER_AUTHENTICATED = 'USER_AUTHENTICATED'; export const USER_AUTHENTICATED = 'USER_AUTHENTICATED';
export const USER_LOGGED_OUT = 'USER_LOGGED_OUT'; export const USER_LOGGED_OUT = 'USER_LOGGED_OUT';
export const USER_REGISTERED = 'USER_REGISTERED'; export const USER_REGISTERED = 'USER_REGISTERED';
export const PUBLIC_STATE_CREATED = 'PUBLIC_STATE_CREATED';
export const PUBLIC_STATES_LOADED = 'PUBLIC_STATES_LOADED';
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

@ -1,15 +1,40 @@
import React, { Component } from 'react'; import React, { Component } from 'react';
import PropTypes from 'prop-types';
import { loadPublicStates } from '../../actions/gameActions';
import { connect } from 'react-redux';
class PublicStateList extends Component { class PublicStateList extends Component {
componentDidMount() {
this.props.loadPublicStates();
};
render() { render() {
console.log('public states: ' + this.props.publicStates);
const pubStateList = this.props.publicStates.map((ps) => {
return (
<div key={ps.id}>{ps.name}</div>
)
});
return ( return (
<div> <div>
<h1>PublicStateList</h1> <h1>PublicStateList</h1>
{pubStateList}
</div> </div>
) )
} }
}; };
export default PublicStateList; PublicStateList.propTypes = {
loadPublicStates: PropTypes.func.isRequired,
publicStates: PropTypes.array,
};
const mapStateToProps = state => ({
publicStates: state.adminData.publicStates
});
export default connect(mapStateToProps, { loadPublicStates })(PublicStateList);

View file

@ -0,0 +1,21 @@
import {
PUBLIC_STATES_LOADED
} from '../actions/types';
const initialState = {
publicStates: []
};
export default function (state = initialState, action) {
switch (action.type) {
case PUBLIC_STATES_LOADED: {
return {
...state,
publicStates: action.data
}
}
default:
return state;
}
};

View file

@ -1,5 +1,5 @@
import { import {
PUBLIC_STATE_CREATED PUBLIC_STATE_CREATED,
} from '../actions/types'; } from '../actions/types';
const initialState = { const initialState = {
@ -13,7 +13,7 @@ export default function (state = initialState, action) {
case PUBLIC_STATE_CREATED: { case PUBLIC_STATE_CREATED: {
return { return {
...state, ...state,
data: action.data publicState: action.data
}; };
} }
default: default:

View file

@ -1,9 +1,11 @@
import { combineReducers } from 'redux'; import { combineReducers } from 'redux';
import appData from './appData'; import appData from './appData';
import adminData from './adminData';
import gameData from './gameData' import gameData from './gameData'
export default combineReducers({ export default combineReducers({
appData, appData,
adminData,
gameData gameData
}); });

View file

@ -8,6 +8,11 @@ class ApiGameService {
createPublicState = (data, onSuccess, onError) => { createPublicState = (data, onSuccess, onError) => {
this.service._post('/api/state', data, onSuccess, onError); this.service._post('/api/state', data, onSuccess, onError);
}; };
getPublicStates = (onSuccess, onError) => {
this.service._get('/api/state', onSuccess, onError);
};
}; };
export default ApiGameService; export default ApiGameService;