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

62 lines
1.5 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
} from '../actions/gab_trends'
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,
}),
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)
2020-04-04 00:18:26 +01:00
default:
return state
}
}