The code powering m.abunchtell.com https://m.abunchtell.com
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 

103 linhas
2.7 KiB

  1. import { Map as ImmutableMap, List as ImmutableList } from 'immutable';
  2. import {
  3. CONVERSATIONS_MOUNT,
  4. CONVERSATIONS_UNMOUNT,
  5. CONVERSATIONS_FETCH_REQUEST,
  6. CONVERSATIONS_FETCH_SUCCESS,
  7. CONVERSATIONS_FETCH_FAIL,
  8. CONVERSATIONS_UPDATE,
  9. CONVERSATIONS_READ,
  10. } from '../actions/conversations';
  11. import compareId from '../compare_id';
  12. const initialState = ImmutableMap({
  13. items: ImmutableList(),
  14. isLoading: false,
  15. hasMore: true,
  16. mounted: false,
  17. });
  18. const conversationToMap = item => ImmutableMap({
  19. id: item.id,
  20. unread: item.unread,
  21. accounts: ImmutableList(item.accounts.map(a => a.id)),
  22. last_status: item.last_status ? item.last_status.id : null,
  23. });
  24. const updateConversation = (state, item) => state.update('items', list => {
  25. const index = list.findIndex(x => x.get('id') === item.id);
  26. const newItem = conversationToMap(item);
  27. if (index === -1) {
  28. return list.unshift(newItem);
  29. } else {
  30. return list.set(index, newItem);
  31. }
  32. });
  33. const expandNormalizedConversations = (state, conversations, next) => {
  34. let items = ImmutableList(conversations.map(conversationToMap));
  35. return state.withMutations(mutable => {
  36. if (!items.isEmpty()) {
  37. mutable.update('items', list => {
  38. list = list.map(oldItem => {
  39. const newItemIndex = items.findIndex(x => x.get('id') === oldItem.get('id'));
  40. if (newItemIndex === -1) {
  41. return oldItem;
  42. }
  43. const newItem = items.get(newItemIndex);
  44. items = items.delete(newItemIndex);
  45. return newItem;
  46. });
  47. list = list.concat(items);
  48. return list.sortBy(x => x.get('last_status'), (a, b) => {
  49. if(a === null || b === null) {
  50. return -1;
  51. }
  52. return compareId(a, b) * -1;
  53. });
  54. });
  55. }
  56. if (!next) {
  57. mutable.set('hasMore', false);
  58. }
  59. mutable.set('isLoading', false);
  60. });
  61. };
  62. export default function conversations(state = initialState, action) {
  63. switch (action.type) {
  64. case CONVERSATIONS_FETCH_REQUEST:
  65. return state.set('isLoading', true);
  66. case CONVERSATIONS_FETCH_FAIL:
  67. return state.set('isLoading', false);
  68. case CONVERSATIONS_FETCH_SUCCESS:
  69. return expandNormalizedConversations(state, action.conversations, action.next);
  70. case CONVERSATIONS_UPDATE:
  71. return updateConversation(state, action.conversation);
  72. case CONVERSATIONS_MOUNT:
  73. return state.update('mounted', count => count + 1);
  74. case CONVERSATIONS_UNMOUNT:
  75. return state.update('mounted', count => count - 1);
  76. case CONVERSATIONS_READ:
  77. return state.update('items', list => list.map(item => {
  78. if (item.get('id') === action.id) {
  79. return item.set('unread', false);
  80. }
  81. return item;
  82. }));
  83. default:
  84. return state;
  85. }
  86. };