show list of public states in frontend
This commit is contained in:
parent
f399b14ffa
commit
4639eade8d
8 changed files with 89 additions and 19 deletions
|
@ -7,11 +7,10 @@ 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({});
|
||||
};
|
||||
|
||||
};
|
||||
|
|
|
@ -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);
|
||||
};
|
||||
|
|
|
@ -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 QUESTIONS_LOADED = 'QUESTIONS_LOADED';
|
||||
export const QUESTIONS_LOAD_ERROR = 'QUESTIONS_LOADED';
|
||||
|
||||
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';
|
||||
|
|
|
@ -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 (
|
||||
<div key={ps.id}>{ps.name}</div>
|
||||
)
|
||||
});
|
||||
|
||||
return (
|
||||
<div>
|
||||
<h1>PublicStateList</h1>
|
||||
{pubStateList}
|
||||
</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);
|
||||
|
|
21
client/src/reducers/adminData.js
Normal file
21
client/src/reducers/adminData.js
Normal 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;
|
||||
}
|
||||
};
|
|
@ -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:
|
||||
|
|
|
@ -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
|
||||
});
|
||||
|
|
|
@ -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;
|
Loading…
Reference in a new issue