clientjs/packages/dashboard-components/src/reducers/documents.js
changeset 0 5f4fcbc80b37
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/clientjs/packages/dashboard-components/src/reducers/documents.js	Fri Sep 14 17:57:34 2018 +0200
@@ -0,0 +1,52 @@
+import _ from 'lodash';
+
+import {
+  RECEIVE_ANNOTATIONS,
+} from '../actions';
+
+function documents(
+  state = {
+    documents: {},
+  },
+  action,
+) {
+  switch (action.type) {
+    case RECEIVE_ANNOTATIONS:
+      return Object.assign({}, state, {
+        documents: _(action.annotations)
+          .groupBy('uri')
+          .map((annotList, url) => {
+            const users = _(annotList).map(a => a.user).uniq().value();
+            const tagsCall = _(annotList)
+              .reduce(
+                (res, annot) => res.concat(annot.tags.filter(t => t.toLowerCase().startsWith('need:'))),
+                [],
+              );
+            const commentsNb = _(annotList).reduce((res, annot) => {
+              let fres = res;
+              if (annot.references && annot.references.length) {
+                fres += 1;
+              }
+              return fres;
+            }, 0);
+            return [
+              url,
+              {
+                uri: url,
+                annotations: annotList.length,
+                users: users.length,
+                calls: tagsCall.length,
+                comments: commentsNb,
+                updated: annotList[0].updated,
+              },
+            ];
+          }).fromPairs()
+          .value(),
+      });
+
+    default:
+      return state;
+  }
+}
+
+export default documents;