gab-social/app/javascript/gabsocial/reducers/gab.js

76 lines
1.9 KiB
JavaScript
Raw Normal View History

2020-04-04 00:18:26 +01:00
import {
GAB_TRENDS_RESULTS_FETCH_REQUEST,
GAB_TRENDS_RESULTS_FETCH_SUCCESS,
GAB_TRENDS_RESULTS_FETCH_FAIL,
GAB_NEWS_RESULTS_FETCH_REQUEST,
GAB_NEWS_RESULTS_FETCH_SUCCESS,
GAB_NEWS_RESULTS_FETCH_FAIL,
} from '../actions/gab'
2020-04-04 00:18:26 +01:00
import {
Map as ImmutableMap,
List as ImmutableList,
fromJS,
2020-04-04 00:18:26 +01:00
} from 'immutable'
const initialState = ImmutableMap({
feed: ImmutableMap({
items: ImmutableList(),
isLoading: false,
isError: false,
}),
partner: ImmutableMap({
items: {},
isLoading: false,
isError: false,
}),
news: ImmutableMap({
items: {},
isLoading: false,
isError: false,
}),
2020-04-04 00:18:26 +01:00
})
const normalizeList = (state, type, items) => {
return state.set(type, ImmutableMap({
items: fromJS(items),
isLoading: false,
isError: false,
}))
}
const setListFailed = (state, type) => {
return state.set(type, ImmutableMap({
items: ImmutableList(),
isLoading: false,
isError: true,
}))
}
2020-04-04 00:18:26 +01:00
export default function (state = initialState, action) {
switch (action.type) {
case GAB_TRENDS_RESULTS_FETCH_REQUEST:
return state.setIn([action.feedType, 'isLoading'], true);
2020-04-04 00:18:26 +01:00
case GAB_TRENDS_RESULTS_FETCH_SUCCESS:
if (action.feedType === 'feed') {
return normalizeList(state, action.feedType, action.items.items)
} else if (action.feedType === 'partner') {
return state.set('partner', ImmutableMap({
items: action.items['trends'] || {},
isLoading: false,
isError: false,
}))
}
return state
2020-04-04 00:18:26 +01:00
case GAB_TRENDS_RESULTS_FETCH_FAIL:
return setListFailed(state, action.feedType)
case GAB_NEWS_RESULTS_FETCH_REQUEST:
return state.setIn(['news', 'isLoading'], true);
case GAB_NEWS_RESULTS_FETCH_SUCCESS:
return normalizeList(state, 'news', action.items)
case GAB_NEWS_RESULTS_FETCH_FAIL:
return setListFailed(state, 'news')
2020-04-04 00:18:26 +01:00
default:
return state
}
}