mirror of
https://github.com/wassname/talk.git
synced 2026-07-13 17:45:56 +08:00
Adding settings to redux store from stream.
This commit is contained in:
@@ -2,9 +2,7 @@ import React from 'react';
|
||||
import {render} from 'react-dom';
|
||||
import CommentStream from './CommentStream';
|
||||
import {Provider} from 'react-redux';
|
||||
import {fetchConfig, store} from '../../coral-framework';
|
||||
|
||||
store.dispatch(fetchConfig());
|
||||
import {store} from '../../coral-framework';
|
||||
|
||||
render(
|
||||
<Provider store={store}>
|
||||
|
||||
@@ -1,35 +0,0 @@
|
||||
import {fromJS} from 'immutable';
|
||||
|
||||
/**
|
||||
* Action name constants
|
||||
*/
|
||||
|
||||
export const FETCH_CONFIG_REQUEST = 'FETCH_CONFIG_REQUEST';
|
||||
export const FETCH_CONFIG_FAILED = 'FETCH_CONFIG_FAILED';
|
||||
export const FETCH_CONFIG_SUCCESS = 'FETCH_CONFIG_SUCCESS';
|
||||
|
||||
/**
|
||||
* Action creators
|
||||
*/
|
||||
|
||||
export function fetchConfig () {
|
||||
return (dispatch) => {
|
||||
|
||||
dispatch({type: FETCH_CONFIG_REQUEST});
|
||||
|
||||
return fetch('/api/v1/settings')
|
||||
.then(
|
||||
response => {
|
||||
return response.ok ? response.json()
|
||||
: Promise.reject(`${response.status} ${response.statusText}`);
|
||||
}
|
||||
)
|
||||
.then((json) => {
|
||||
return dispatch({type: FETCH_CONFIG_SUCCESS, config: fromJS(json)});
|
||||
})
|
||||
.catch((error) => {
|
||||
dispatch({type: FETCH_CONFIG_FAILED, error});
|
||||
});
|
||||
|
||||
};
|
||||
}
|
||||
@@ -1,3 +1,4 @@
|
||||
import {fromJS} from 'immutable';
|
||||
/* Item Actions */
|
||||
|
||||
/**
|
||||
@@ -6,6 +7,7 @@
|
||||
|
||||
export const ADD_ITEM = 'ADD_ITEM';
|
||||
export const UPDATE_ITEM = 'UPDATE_ITEM';
|
||||
export const UPDATE_SETTINGS = 'UPDATE_SETTINGS';
|
||||
export const APPEND_ITEM_ARRAY = 'APPEND_ITEM_ARRAY';
|
||||
|
||||
const getInit = (method, body) => {
|
||||
@@ -61,6 +63,7 @@ export const addItem = (item, item_type) => {
|
||||
* id - the id of the item to be posted
|
||||
* property - the property to be updated
|
||||
* value - the value that the property should be set to
|
||||
* item_type - the type of the item being updated (users, comments, etc)
|
||||
*
|
||||
*/
|
||||
export const updateItem = (id, property, value, item_type) => {
|
||||
@@ -73,6 +76,18 @@ export const updateItem = (id, property, value, item_type) => {
|
||||
};
|
||||
};
|
||||
|
||||
/*
|
||||
* Appends data to an array in an item in the local store without posting it to the server
|
||||
* Useful for adding a recently posted reply to a comment, etc.
|
||||
*
|
||||
* @params
|
||||
* id - the id of the item to be posted
|
||||
* property - the property to be updated (should be an array)
|
||||
* value - the value that should be added to the array
|
||||
* add_to_front - boolean that defines whether value is added at the beginning (unshift) or end (push)
|
||||
* item_type - the type of the item being updated (users, comments, etc)
|
||||
*
|
||||
*/
|
||||
export const appendItemArray = (id, property, value, add_to_front, item_type) => {
|
||||
return {
|
||||
type: APPEND_ITEM_ARRAY,
|
||||
@@ -112,6 +127,8 @@ export function getStream (assetUrl) {
|
||||
action.id = `${action.action_type}_${action.item_id}`;
|
||||
dispatch(addItem(action, 'actions'));
|
||||
}
|
||||
} else if (itemTypes[i] === 'settings') {
|
||||
return dispatch({type: UPDATE_SETTINGS, config: fromJS(json[itemTypes[i]])});
|
||||
} else {
|
||||
for (let j = 0; j < json[itemTypes[i]].length; j++ ) {
|
||||
dispatch(addItem(json[itemTypes[i]][j], itemTypes[i]));
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
import Notification from './modules/notification/Notification';
|
||||
import store from './store';
|
||||
import {fetchConfig} from './actions/config';
|
||||
import * as itemActions from './actions/items';
|
||||
import I18n from './modules/i18n/i18n';
|
||||
import * as notificationActions from './actions/notification';
|
||||
@@ -9,7 +8,6 @@ import * as authActions from './actions/auth';
|
||||
export {
|
||||
Notification,
|
||||
store,
|
||||
fetchConfig,
|
||||
itemActions,
|
||||
I18n,
|
||||
notificationActions,
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
/* @flow */
|
||||
|
||||
import {Map} from 'immutable';
|
||||
import * as actions from '../actions/config';
|
||||
import * as actions from '../actions/items';
|
||||
|
||||
const initialState = Map({
|
||||
features: Map({})
|
||||
@@ -9,15 +9,10 @@ const initialState = Map({
|
||||
|
||||
export default (state = initialState, action) => {
|
||||
switch(action.type) {
|
||||
case actions.FETCH_CONFIG_REQUEST:
|
||||
return state.set('loading', true);
|
||||
|
||||
case actions.FETCH_CONFIG_FAILED:
|
||||
return state.set('loading', false);
|
||||
|
||||
// Override config if worked
|
||||
case actions.FETCH_CONFIG_SUCCESS:
|
||||
return action.config.set('loading', false);
|
||||
// Override config if worked
|
||||
case actions.UPDATE_SETTINGS:
|
||||
return action.config;
|
||||
|
||||
default:
|
||||
return state;
|
||||
|
||||
Reference in New Issue
Block a user