From 4639eade8d6289b0bcef22b82bcff24921eaeeef Mon Sep 17 00:00:00 2001 From: Markus Schubert Date: Sun, 29 Mar 2020 18:45:51 +0200 Subject: [PATCH] show list of public states in frontend --- app/controllers/AdminController.js | 9 +++--- client/src/actions/gameActions.js | 30 ++++++++++++++----- client/src/actions/types.js | 6 ++-- .../PublicStateList/PublicStateList.js | 29 ++++++++++++++++-- client/src/reducers/adminData.js | 21 +++++++++++++ client/src/reducers/gameData.js | 4 +-- client/src/reducers/index.js | 2 ++ client/src/services/ApiGameService.js | 7 ++++- 8 files changed, 89 insertions(+), 19 deletions(-) create mode 100644 client/src/reducers/adminData.js diff --git a/app/controllers/AdminController.js b/app/controllers/AdminController.js index e87bac9..75348c3 100644 --- a/app/controllers/AdminController.js +++ b/app/controllers/AdminController.js @@ -7,13 +7,12 @@ class AdminController { getAll(onSuccess, onError) { let collection = this.database.collection('publicStates'); - let cursor = collection.find({}, {}); - cursor.toArray((item) => { - console.log('item: ' + item); + let cursor = collection.find({}, {}).toArray(); + cursor.then((items) => { + onSuccess(items); }); - onSuccess({}); }; }; -module.exports = AdminController; \ No newline at end of file +module.exports = AdminController; diff --git a/client/src/actions/gameActions.js b/client/src/actions/gameActions.js index 29d08a5..c0c5722 100644 --- a/client/src/actions/gameActions.js +++ b/client/src/actions/gameActions.js @@ -1,19 +1,35 @@ import ApiGameService from '../services/ApiGameService'; import { - PUBLIC_STATE_CREATED + PUBLIC_STATE_CREATED, + PUBLIC_STATES_LOADED } 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); + dispatch({ + type: PUBLIC_STATE_CREATED, + data: data + }); + if (onSuccess) onSuccess(data); }; const ecb = (error) => { - if (onError) onError(error); + if (onError) onError(error); }; 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); +}; diff --git a/client/src/actions/types.js b/client/src/actions/types.js index d85d8ea..b46420d 100644 --- a/client/src/actions/types.js +++ b/client/src/actions/types.js @@ -1,7 +1,9 @@ export const USER_AUTHENTICATED = 'USER_AUTHENTICATED'; export const USER_LOGGED_OUT = 'USER_LOGGED_OUT'; 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_LOAD_ERROR = 'QUESTIONS_LOADED'; - -export const PUBLIC_STATE_CREATED = 'PUBLIC_STATE_CREATED'; \ No newline at end of file diff --git a/client/src/components/PublicStateList/PublicStateList.js b/client/src/components/PublicStateList/PublicStateList.js index 2041cf9..aeb5515 100644 --- a/client/src/components/PublicStateList/PublicStateList.js +++ b/client/src/components/PublicStateList/PublicStateList.js @@ -1,15 +1,40 @@ import React, { Component } from 'react'; +import PropTypes from 'prop-types'; +import { loadPublicStates } from '../../actions/gameActions'; +import { connect } from 'react-redux'; class PublicStateList extends Component { + componentDidMount() { + this.props.loadPublicStates(); + }; + render() { + + console.log('public states: ' + this.props.publicStates); + + const pubStateList = this.props.publicStates.map((ps) => { + return ( +
{ps.name}
+ ) + }); + return (

PublicStateList

+ {pubStateList}
) } - }; -export default PublicStateList; \ No newline at end of file +PublicStateList.propTypes = { + loadPublicStates: PropTypes.func.isRequired, + publicStates: PropTypes.array, +}; + +const mapStateToProps = state => ({ + publicStates: state.adminData.publicStates +}); + +export default connect(mapStateToProps, { loadPublicStates })(PublicStateList); diff --git a/client/src/reducers/adminData.js b/client/src/reducers/adminData.js new file mode 100644 index 0000000..7b2a69f --- /dev/null +++ b/client/src/reducers/adminData.js @@ -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; + } +}; diff --git a/client/src/reducers/gameData.js b/client/src/reducers/gameData.js index a6298dc..8877abf 100644 --- a/client/src/reducers/gameData.js +++ b/client/src/reducers/gameData.js @@ -1,5 +1,5 @@ import { - PUBLIC_STATE_CREATED + PUBLIC_STATE_CREATED, } from '../actions/types'; const initialState = { @@ -13,7 +13,7 @@ export default function (state = initialState, action) { case PUBLIC_STATE_CREATED: { return { ...state, - data: action.data + publicState: action.data }; } default: diff --git a/client/src/reducers/index.js b/client/src/reducers/index.js index 068f0dd..b06f074 100644 --- a/client/src/reducers/index.js +++ b/client/src/reducers/index.js @@ -1,9 +1,11 @@ import { combineReducers } from 'redux'; import appData from './appData'; +import adminData from './adminData'; import gameData from './gameData' export default combineReducers({ appData, + adminData, gameData }); diff --git a/client/src/services/ApiGameService.js b/client/src/services/ApiGameService.js index d3358fa..3c1b63a 100644 --- a/client/src/services/ApiGameService.js +++ b/client/src/services/ApiGameService.js @@ -8,6 +8,11 @@ class ApiGameService { createPublicState = (data, onSuccess, onError) => { this.service._post('/api/state', data, onSuccess, onError); }; + + getPublicStates = (onSuccess, onError) => { + this.service._get('/api/state', onSuccess, onError); + }; + }; -export default ApiGameService; \ No newline at end of file +export default ApiGameService;