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,
|
2020-06-09 03:10:51 +01:00
|
|
|
fromJS,
|
2020-04-04 00:18:26 +01:00
|
|
|
} from 'immutable'
|
|
|
|
|
|
|
|
const initialState = ImmutableMap({
|
2020-06-09 03:10:51 +01:00
|
|
|
feed: ImmutableMap({
|
|
|
|
items: ImmutableList(),
|
|
|
|
isLoading: false,
|
|
|
|
isError: false,
|
|
|
|
}),
|
2020-07-02 03:39:43 +01:00
|
|
|
partner: ImmutableMap({
|
|
|
|
items: {},
|
|
|
|
isLoading: false,
|
|
|
|
isError: false,
|
|
|
|
}),
|
2020-04-04 00:18:26 +01:00
|
|
|
})
|
|
|
|
|
2020-06-09 03:10:51 +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:
|
2020-06-09 03:10:51 +01:00
|
|
|
return state.setIn([action.feedType, 'isLoading'], true);
|
2020-04-04 00:18:26 +01:00
|
|
|
case GAB_TRENDS_RESULTS_FETCH_SUCCESS:
|
2020-07-02 03:39:43 +01:00
|
|
|
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:
|
2020-06-09 03:10:51 +01:00
|
|
|
return setListFailed(state, action.feedType)
|
2020-04-04 00:18:26 +01:00
|
|
|
default:
|
|
|
|
return state
|
|
|
|
}
|
|
|
|
}
|