about summary refs log tree commit diff
path: root/app/assets/javascripts/components/reducers/relationships.jsx
diff options
context:
space:
mode:
authorEugen Rochko <eugen@zeonfederated.com>2016-10-30 15:06:43 +0100
committerEugen Rochko <eugen@zeonfederated.com>2016-10-30 15:06:43 +0100
commite8ff4c8e56650bf061c63a7da3d84b742e618b6a (patch)
treecb9aa48393bc6108655db7490434ae29d3145ee5 /app/assets/javascripts/components/reducers/relationships.jsx
parent7060bdf04bde59aab9addce95f00d6e1075a62ba (diff)
Refactoring redux state into different reducers
Diffstat (limited to 'app/assets/javascripts/components/reducers/relationships.jsx')
-rw-r--r--app/assets/javascripts/components/reducers/relationships.jsx34
1 files changed, 34 insertions, 0 deletions
diff --git a/app/assets/javascripts/components/reducers/relationships.jsx b/app/assets/javascripts/components/reducers/relationships.jsx
new file mode 100644
index 000000000..165b1f3ff
--- /dev/null
+++ b/app/assets/javascripts/components/reducers/relationships.jsx
@@ -0,0 +1,34 @@
+import {
+  ACCOUNT_FOLLOW_SUCCESS,
+  ACCOUNT_UNFOLLOW_SUCCESS,
+  ACCOUNT_BLOCK_SUCCESS,
+  ACCOUNT_UNBLOCK_SUCCESS,
+  RELATIONSHIPS_FETCH_SUCCESS
+} from '../actions/accounts';
+import Immutable from 'immutable';
+
+const normalizeRelationship = (state, relationship) => state.set(relationship.get('id'), relationship);
+
+const normalizeRelationships = (state, relationships) => {
+  relationships.forEach(relationship => {
+    state = normalizeRelationship(state, relationship);
+  });
+
+  return state;
+};
+
+const initialState = Immutable.Map();
+
+export default function relationships(state = initialState, action) {
+  switch(action.type) {
+    case ACCOUNT_FOLLOW_SUCCESS:
+    case ACCOUNT_UNFOLLOW_SUCCESS:
+    case ACCOUNT_BLOCK_SUCCESS:
+    case ACCOUNT_UNBLOCK_SUCCESS:
+      return normalizeRelationship(state, Immutable.fromJS(action.relationship));
+    case RELATIONSHIPS_FETCH_SUCCESS:
+      return normalizeRelationships(state, Immutable.fromJS(action.relationships));
+    default:
+      return state;
+  }
+};