url.js 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. import {
  2. ADD_URL,
  3. UPDATE_URL_LIST,
  4. LIST_URLS,
  5. DELETE_URL,
  6. UNAUTH_USER,
  7. } from '../actions/actionTypes';
  8. const initialState = {
  9. list: [],
  10. isShortened: false,
  11. count: 10,
  12. countAll: 0,
  13. page: 1,
  14. search: '',
  15. };
  16. const url = (state = initialState, action) => {
  17. const { count, page, search } = action.payload || {};
  18. const isSearch = typeof search !== 'undefined';
  19. switch (action.type) {
  20. case ADD_URL:
  21. return {
  22. ...state,
  23. isShortened: true,
  24. list: [action.payload, ...state.list],
  25. };
  26. case UPDATE_URL_LIST:
  27. return Object.assign({}, state, count && { count }, page && { page }, isSearch && { search });
  28. case LIST_URLS:
  29. return {
  30. ...state,
  31. list: action.payload.list,
  32. countAll: action.payload.countAll,
  33. isShortened: false,
  34. };
  35. case DELETE_URL:
  36. return {
  37. ...state,
  38. list: state.list.filter(item => item.id !== action.payload),
  39. };
  40. case UNAUTH_USER:
  41. return initialState;
  42. default:
  43. return state;
  44. }
  45. };
  46. export default url;