diff -r 000000000000 -r 5f4fcbc80b37 clientjs/packages/dashboard-components/src/actions.js --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/clientjs/packages/dashboard-components/src/actions.js Fri Sep 14 17:57:34 2018 +0200 @@ -0,0 +1,94 @@ +/* eslint-disable func-names */ +import fetch from 'isomorphic-fetch'; +import _ from 'lodash'; + +import { getTerms, toBase64 } from './utils'; + + +export const REQUEST_ANNOTATIONS = 'REQUEST_ANNOTATIONS'; +export function requestAnnotations(url) { + return { + type: REQUEST_ANNOTATIONS, + url, + }; +} + +export const RECEIVE_ANNOTATIONS = 'RECEIVE_ANNOTATIONS'; +export function receiveAnnotations(url, annotations, terms) { + return { + type: RECEIVE_ANNOTATIONS, + url, + annotations, + terms, + receivedAt: Date.now(), + }; +} + +export const RECEIVE_MESSAGES_COUNT = 'RECEIVE_MESSAGES_COUNT'; +export function receiveMessagesCount(url, terms, terms64, messagesCount) { + return { + type: RECEIVE_MESSAGES_COUNT, + terms, + terms64, + messagesCount, + }; +} + +export const REQUEST_MESSAGES_COUNT = 'REQUEST_MESSAGES_COUNT'; +export function requestMessagesCount(url, terms, terms64) { + return { + type: REQUEST_MESSAGES_COUNT, + url, + terms, + terms64, + }; +} + +export const FETCH_MESSAGES_COUNT = 'FETCH_MESSAGES_COUNT'; +export function fetchMessagesCount(terms, discussionUrl) { + const url = `${discussionUrl}count`; + const terms64 = _.map(terms, toBase64); + + return function (dispatch) { + dispatch(requestMessagesCount(url, terms, terms64)); + + if (!discussionUrl) { + return dispatch(receiveMessagesCount(url, terms, terms64, {})); + } + + return fetch(url, { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + }, + body: JSON.stringify(terms64), + }) + .then( + response => response.json(), + error => console.log('An error occured.', error), // eslint-disable-line no-console + ) + .then( + json => dispatch(receiveMessagesCount(url, terms, terms64, json)), + ); + }; +} + +export const FETCH_ANNOTATIONS = 'FETCH_ANNOTATIONS'; +export function fetchAnnotations(url, discussionUrl) { + return function (dispatch) { + dispatch(requestAnnotations(url)); + + return fetch(url) + .then( + response => response.json(), + error => console.log('An error occured.', error), // eslint-disable-line no-console + ) + .then((json) => { + const annotations = json.rows; + const terms = getTerms(annotations); + + dispatch(fetchMessagesCount(Object.keys(terms), discussionUrl)); + dispatch(receiveAnnotations(url, annotations, terms)); + }); + }; +}