From dbfe1e4be6fb46c7374275a2465f4386798516cd Mon Sep 17 00:00:00 2001
From: Eugen Rochko <eugen@zeonfederated.com>
Date: Sun, 13 Nov 2016 20:42:54 +0100
Subject: Infinite scroll for followers/following lists

---
 .../javascripts/components/reducers/user_lists.jsx | 25 +++++++++++++++++++---
 1 file changed, 22 insertions(+), 3 deletions(-)

(limited to 'app/assets/javascripts/components/reducers/user_lists.jsx')

diff --git a/app/assets/javascripts/components/reducers/user_lists.jsx b/app/assets/javascripts/components/reducers/user_lists.jsx
index 4c201f927..de5c85bba 100644
--- a/app/assets/javascripts/components/reducers/user_lists.jsx
+++ b/app/assets/javascripts/components/reducers/user_lists.jsx
@@ -1,6 +1,8 @@
 import {
   FOLLOWERS_FETCH_SUCCESS,
-  FOLLOWING_FETCH_SUCCESS
+  FOLLOWERS_EXPAND_SUCCESS,
+  FOLLOWING_FETCH_SUCCESS,
+  FOLLOWING_EXPAND_SUCCESS
 } from '../actions/accounts';
 import { SUGGESTIONS_FETCH_SUCCESS } from '../actions/suggestions';
 import {
@@ -17,12 +19,29 @@ const initialState = Immutable.Map({
   favourited_by: Immutable.Map()
 });
 
+const normalizeList = (state, type, id, accounts, prev) => {
+  return state.setIn([type, id], Immutable.Map({
+    prev,
+    items: Immutable.List(accounts.map(item => item.id))
+  }));
+};
+
+const appendToList = (state, type, id, accounts, prev) => {
+  return state.updateIn([type, id], map => {
+    return map.set('prev', prev).update('items', list => list.push(...accounts.map(item => item.id)));
+  });
+};
+
 export default function userLists(state = initialState, action) {
   switch(action.type) {
     case FOLLOWERS_FETCH_SUCCESS:
-      return state.setIn(['followers', action.id], Immutable.List(action.accounts.map(item => item.id)));
+      return normalizeList(state, 'followers', action.id, action.accounts, action.prev);
+    case FOLLOWERS_EXPAND_SUCCESS:
+      return appendToList(state, 'followers', action.id, action.accounts, action.prev);
     case FOLLOWING_FETCH_SUCCESS:
-      return state.setIn(['following', action.id], Immutable.List(action.accounts.map(item => item.id)));
+      return normalizeList(state, 'following', action.id, action.accounts, action.prev);
+    case FOLLOWING_EXPAND_SUCCESS:
+      return appendToList(state, 'following', action.id, action.accounts, action.prev);
     case SUGGESTIONS_FETCH_SUCCESS:
       return state.set('suggestions', Immutable.List(action.accounts.map(item => item.id)));
     case REBLOGS_FETCH_SUCCESS:
-- 
cgit 

