2020-05-04 19:44:37 +01:00
|
|
|
import { Map as ImmutableMap, List as ImmutableList } from 'immutable'
|
2020-06-04 23:48:24 +01:00
|
|
|
import {
|
|
|
|
GROUPS_FETCH_REQUEST,
|
|
|
|
GROUPS_FETCH_SUCCESS,
|
|
|
|
GROUPS_FETCH_FAIL,
|
|
|
|
} from '../actions/groups'
|
|
|
|
|
|
|
|
const tabs = ['new', 'featured', 'member', 'admin']
|
2019-07-15 14:47:05 +01:00
|
|
|
|
|
|
|
const initialState = ImmutableMap({
|
2020-06-04 23:48:24 +01:00
|
|
|
new: ImmutableMap({
|
2020-06-04 23:52:26 +01:00
|
|
|
isFetched: false,
|
|
|
|
isLoading: false,
|
2020-06-04 23:48:24 +01:00
|
|
|
items: ImmutableList(),
|
|
|
|
}),
|
|
|
|
featured: ImmutableMap({
|
2020-06-04 23:52:26 +01:00
|
|
|
isFetched: false,
|
|
|
|
isLoading: false,
|
2020-06-04 23:48:24 +01:00
|
|
|
items: ImmutableList(),
|
|
|
|
}),
|
|
|
|
member: ImmutableMap({
|
2020-06-04 23:52:26 +01:00
|
|
|
isFetched: false,
|
|
|
|
isLoading: false,
|
2020-06-04 23:48:24 +01:00
|
|
|
items: ImmutableList(),
|
|
|
|
}),
|
|
|
|
admin: ImmutableMap({
|
2020-06-04 23:52:26 +01:00
|
|
|
isFetched: false,
|
|
|
|
isLoading: false,
|
2020-06-04 23:48:24 +01:00
|
|
|
items: ImmutableList(),
|
|
|
|
}),
|
2020-05-04 19:44:37 +01:00
|
|
|
})
|
2019-07-15 14:47:05 +01:00
|
|
|
|
|
|
|
export default function groupLists(state = initialState, action) {
|
2020-06-04 23:48:24 +01:00
|
|
|
if (tabs.indexOf(action.tab) === -1) return state
|
|
|
|
|
2019-07-15 14:47:05 +01:00
|
|
|
switch(action.type) {
|
2020-06-04 23:48:24 +01:00
|
|
|
case GROUPS_FETCH_REQUEST:
|
|
|
|
return state.withMutations((mutable) => {
|
2020-06-04 23:52:26 +01:00
|
|
|
mutable.setIn([action.tab, 'isLoading'], true)
|
2020-06-04 23:48:24 +01:00
|
|
|
});
|
2019-07-15 14:47:05 +01:00
|
|
|
case GROUPS_FETCH_SUCCESS:
|
2020-06-04 23:48:24 +01:00
|
|
|
return state.withMutations((mutable) => {
|
2020-07-14 06:22:15 +01:00
|
|
|
let list = ImmutableList(action.groups.map(item => item.id))
|
|
|
|
if (action.tab === 'featured') list = list.sortBy(Math.random)
|
|
|
|
|
|
|
|
mutable.setIn([action.tab, 'items'], list)
|
2020-06-04 23:52:26 +01:00
|
|
|
mutable.setIn([action.tab, 'isLoading'], false)
|
|
|
|
mutable.setIn([action.tab, 'isFetched'], true)
|
2020-06-04 23:48:24 +01:00
|
|
|
})
|
|
|
|
case GROUPS_FETCH_FAIL:
|
|
|
|
return state.withMutations((mutable) => {
|
|
|
|
mutable.setIn([action.tab, 'items'], ImmutableList())
|
2020-06-04 23:52:26 +01:00
|
|
|
mutable.setIn([action.tab, 'isLoading'], false)
|
|
|
|
mutable.setIn([action.tab, 'isFetched'], true)
|
2020-06-04 23:48:24 +01:00
|
|
|
})
|
2019-07-15 14:47:05 +01:00
|
|
|
default:
|
2020-05-04 19:44:37 +01:00
|
|
|
return state
|
2019-07-15 14:47:05 +01:00
|
|
|
}
|
2020-05-04 19:44:37 +01:00
|
|
|
}
|