2020-04-02 04:17:21 +01:00
|
|
|
import {
|
|
|
|
GIFS_CLEAR_RESULTS,
|
|
|
|
GIF_SET_SELECTED,
|
2020-05-02 07:25:55 +01:00
|
|
|
GIF_CLEAR_SELECTED,
|
2020-04-02 04:17:21 +01:00
|
|
|
GIF_CHANGE_SEARCH_TEXT,
|
|
|
|
GIF_RESULTS_FETCH_REQUEST,
|
|
|
|
GIF_RESULTS_FETCH_SUCCESS,
|
|
|
|
GIF_RESULTS_FETCH_FAIL,
|
|
|
|
GIF_CATEGORIES_FETCH_REQUEST,
|
|
|
|
GIF_CATEGORIES_FETCH_SUCCESS,
|
2020-04-08 02:06:59 +01:00
|
|
|
GIF_CATEGORIES_FETCH_FAIL,
|
2020-04-02 04:17:21 +01:00
|
|
|
} from '../actions/tenor'
|
|
|
|
import { Map as ImmutableMap } from 'immutable'
|
|
|
|
|
|
|
|
const initialState = ImmutableMap({
|
|
|
|
categories: [],
|
|
|
|
results: [],
|
|
|
|
searchText: '',
|
2020-04-17 06:35:46 +01:00
|
|
|
next: 0,
|
2020-04-02 04:17:21 +01:00
|
|
|
loading: false,
|
|
|
|
error: false,
|
2020-05-02 07:25:55 +01:00
|
|
|
selectedGif: ImmutableMap({
|
|
|
|
id: '',
|
|
|
|
title: '',
|
|
|
|
src: '',
|
|
|
|
})
|
2020-04-02 04:17:21 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
export default function (state = initialState, action) {
|
|
|
|
switch (action.type) {
|
2020-04-08 02:06:59 +01:00
|
|
|
case GIF_RESULTS_FETCH_REQUEST:
|
|
|
|
case GIF_CATEGORIES_FETCH_REQUEST:
|
|
|
|
return state.set('loading', true)
|
|
|
|
case GIF_RESULTS_FETCH_SUCCESS:
|
|
|
|
return state.withMutations(map => {
|
2020-04-17 06:35:46 +01:00
|
|
|
map.set('results', action.data.results);
|
|
|
|
map.set('next', action.data.next);
|
2020-04-08 02:06:59 +01:00
|
|
|
map.set('error', false);
|
|
|
|
map.set('loading', false);
|
|
|
|
});
|
|
|
|
case GIF_CATEGORIES_FETCH_SUCCESS:
|
|
|
|
return state.withMutations(map => {
|
|
|
|
map.set('categories', action.categories);
|
|
|
|
map.set('error', false);
|
|
|
|
map.set('loading', false);
|
|
|
|
});
|
|
|
|
case GIF_RESULTS_FETCH_FAIL:
|
|
|
|
case GIF_CATEGORIES_FETCH_FAIL:
|
|
|
|
return state.withMutations(map => {
|
|
|
|
map.set('error', !!action.error);
|
|
|
|
map.set('loading', false);
|
|
|
|
});
|
|
|
|
case GIFS_CLEAR_RESULTS:
|
|
|
|
return state.set('results', [])
|
|
|
|
case GIF_SET_SELECTED:
|
2020-05-02 07:25:55 +01:00
|
|
|
return state.set('selectedGif', ImmutableMap({
|
|
|
|
id: action.result.id,
|
|
|
|
title: action.result.title,
|
|
|
|
src: action.result.media[0].gif.url,
|
|
|
|
}))
|
|
|
|
case GIF_CLEAR_SELECTED:
|
|
|
|
return state.set('selectedGif', ImmutableMap())
|
2020-04-08 02:06:59 +01:00
|
|
|
case GIF_CHANGE_SEARCH_TEXT:
|
|
|
|
return state.set('searchText', action.text.trim());
|
|
|
|
default:
|
|
|
|
return state
|
2020-04-02 04:17:21 +01:00
|
|
|
}
|
|
|
|
}
|