clientjs/packages/dashboard-components/src/ui/AnnotationsCards.jsx
author ymh <ymh.work@gmail.com>
Mon, 17 Sep 2018 15:32:33 +0200
changeset 6 819a7ab4e3fb
parent 0 5f4fcbc80b37
permissions -rw-r--r--
aborted async loading test, better split dashboard-components


import _ from 'lodash';

import React from 'react';
import PropTypes from 'prop-types';

import { FormattedMessage } from 'react-intl';

import DocumentAnnotations from './DocumentAnnotations';

const AnnotationsCards = ({
  annotations,
  viaBaseUrl,
  metacategories,
  categories,
  children,
}) => {
  const documents = _(annotations).groupBy('uri');

  const documentAnnotations = documents.map((docAnnotations, uri) => {
    const document = {
      uri,
      title: docAnnotations[0].document.title,
      annotations: docAnnotations,
    };

    return (
      <DocumentAnnotations
        key={document.uri}
        document={document}
        viaBaseUrl={viaBaseUrl}
        metacategories={metacategories}
        categories={categories}
      />
    );
  }).value();

  let noAnnotationContent = <FormattedMessage id="ui.annotationsCards.noAnnotation" defaultMessage="No annotation" />;
  if (React.Children.count(children) > 0) {
    noAnnotationContent = children;
  }

  return (
    <div className="card-columns">
      { annotations.length === 0 ? noAnnotationContent : documentAnnotations }
    </div>
  );
};

AnnotationsCards.propTypes = {
  annotations: PropTypes.arrayOf(PropTypes.object),
  viaBaseUrl: PropTypes.string.isRequired,
  metacategories: PropTypes.arrayOf(PropTypes.object).isRequired,
  categories: PropTypes.arrayOf(PropTypes.string).isRequired,
  children: PropTypes.node,
};

AnnotationsCards.defaultProps = {
  annotations: [],
  children: null,
};

export default AnnotationsCards;