Advertisement
Guest User

Untitled

a guest
Jul 18th, 2019
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.35 KB | None | 0 0
  1. import {
  2. ClubModuleState,
  3. initialClubModuleState,
  4. ClubsState,
  5. SortState
  6. } from './clubs.state';
  7. import {
  8. ClubAction,
  9. ClubsActionType,
  10. RespondClubsAction,
  11. SortClubsAction
  12. } from './clubs.actions';
  13.  
  14. export function clubReducer(
  15. oldState: ClubModuleState = initialClubModuleState,
  16. action: ClubAction
  17. ): ClubModuleState {
  18. switch (action.type) {
  19. case ClubsActionType.RESPOND_CLUBS: {
  20. const clubs: ClubsState[] = (action as RespondClubsAction).payload.clubs;
  21.  
  22. const newState = {
  23. ...oldState,
  24. clubs
  25. };
  26.  
  27. return newState;
  28. }
  29. case ClubsActionType.SORT_CLUBS: {
  30. const sort: SortState = (action as SortClubsAction).payload.sort;
  31. const clubs: ClubsState[] = oldState.clubs.slice().sort((a, b) => {
  32. const clubA =
  33. sort.field !== 'capacity'
  34. ? a[sort.field].toUpperCase()
  35. : a[sort.field];
  36. const clubB =
  37. sort.field !== 'capacity'
  38. ? b[sort.field].toUpperCase()
  39. : b[sort.field];
  40.  
  41. if (sort.order === 'ASC') {
  42. return clubA > clubB ? 1 : clubA < clubB ? -1 : 0;
  43. } else {
  44. return clubA < clubB ? 1 : clubA > clubB ? -1 : 0;
  45. }
  46. });
  47.  
  48. const newState = {
  49. ...oldState,
  50. sort,
  51. clubs
  52. };
  53.  
  54. return newState;
  55. }
  56. default:
  57. return oldState;
  58. }
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement