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

48 lines
1.5 KiB
JavaScript
Raw Normal View History

2020-12-16 00:31:30 +00:00
import {
BOOKMARK_COLLECTIONS_FETCH_REQUEST,
BOOKMARK_COLLECTIONS_FETCH_SUCCESS,
BOOKMARK_COLLECTIONS_FETCH_FAIL,
BOOKMARK_COLLECTIONS_CREATE_SUCCESS,
BOOKMARK_COLLECTIONS_REMOVE_REQUEST,
2020-12-16 00:31:30 +00:00
} from '../actions/bookmarks'
import { Map as ImmutableMap, List as ImmutableList, fromJS } from 'immutable'
const initialState = ImmutableMap({
items: ImmutableList(),
isLoading: false,
isFetched: false,
isError: false,
})
export default function bookmark_collections(state = initialState, action) {
switch(action.type) {
case BOOKMARK_COLLECTIONS_FETCH_REQUEST:
2020-12-16 00:31:30 +00:00
return state.withMutations((map) => {
map.set('isLoading', true)
map.set('isFetched', false)
map.set('isError', false)
})
case BOOKMARK_COLLECTIONS_FETCH_SUCCESS:
2020-12-16 00:31:30 +00:00
return state.withMutations((map) => {
map.set('items', fromJS(action.bookmarkCollections))
2020-12-16 00:31:30 +00:00
map.set('isLoading', false)
map.set('isFetched', true)
map.set('isError', false)
})
case BOOKMARK_COLLECTIONS_FETCH_FAIL:
2020-12-16 00:31:30 +00:00
return state.withMutations((map) => {
map.set('isLoading', false)
map.set('isFetched', true)
map.set('isError', true)
})
case BOOKMARK_COLLECTIONS_CREATE_SUCCESS:
return state.update('items', list => list.push(fromJS(action.bookmarkCollection)))
2020-12-16 00:31:30 +00:00
case BOOKMARK_COLLECTIONS_REMOVE_REQUEST:
return state.update('items', list => list.filterNot((item) => {
return item.get('id') === action.bookmarkCollectionId
2020-12-16 00:31:30 +00:00
}))
default:
return state
}
}