import { registerComponent, Components } from 'meteor/vulcan:lib'; import { intlShape, FormattedMessage } from 'meteor/vulcan:i18n'; import React from 'react'; import PropTypes from 'prop-types'; import classNames from 'classnames'; import moment from 'moment'; import Button from 'react-bootstrap/lib/Button'; import { Link } from 'react-router'; const getLabel = (field, fieldName, collection, intl) => { const schema = collection.simpleSchema()._schema; const fieldSchema = schema[fieldName]; if (fieldSchema) { return intl.formatMessage({id: `${collection._name}.${fieldName}`, defaultMessage: fieldSchema.label}); } else { return fieldName; } } const getTypeName = (field, fieldName, collection) => { const schema = collection.simpleSchema()._schema; const fieldSchema = schema[fieldName]; if (fieldSchema) { const type = fieldSchema.type.singleType; const typeName = typeof type === 'function' ? type.name : type; return typeName; } else { return typeof field; } } const parseImageUrl = value => { const isImage = ['.png', '.jpg', '.gif'].indexOf(value.substr(-4)) !== -1 || ['.webp', '.jpeg' ].indexOf(value.substr(-5)) !== -1; return isImage ? {value}/ : parseUrl(value); } const parseUrl = value => { return value.slice(0,4) === 'http' ? : ; } const LimitedString = ({ string }) =>
{string.indexOf(' ') === -1 && string.length > 30 ? {string.substr(0,30)}… : {(string)} }
export const getFieldValue = (value, typeName) => { if (typeof value === 'undefined' || value === null) { return '' } if (Array.isArray(value)) { typeName = 'Array'; } if (typeof typeName === 'undefined') { typeName = typeof value; } switch (typeName) { case 'Boolean': case 'boolean': case 'Number': case 'number': case 'SimpleSchema.Integer': return {value.toString()}; case 'Array': return
    {value.map((item, index) =>
  1. {getFieldValue(item, typeof item)}
  2. )}
case 'Object': case 'object': return getObject(value); case 'Date': return moment(new Date(value)).format('dddd, MMMM Do YYYY, h:mm:ss'); default: return parseImageUrl(value); } } const getObject = object => { if (object.__typename === 'User') { const user = object; return (
{user.displayName}
) } else { return ( {_.without(Object.keys(object), '__typename').map(key => )}
{key} {getFieldValue(object[key], typeof object[key])}
) } } const CardItem = ({label, value, typeName}) => {label} {getFieldValue(value, typeName)} const CardEdit = (props, context) => }> CardEdit.contextTypes = { intl: intlShape }; const CardEditForm = ({ collection, document, closeModal }) => { closeModal(); }} /> const Card = ({className, collection, document, currentUser, fields, showEdit = true}, {intl}) => { const fieldNames = fields ? fields : _.without(_.keys(document), '__typename'); const canEdit = showEdit && currentUser && collection.options.mutations.edit.check(currentUser, document); return (
{canEdit ? : null} {fieldNames.map((fieldName, index) => )}
); }; Card.displayName = "Card"; Card.propTypes = { className: PropTypes.string, collection: PropTypes.object, document: PropTypes.object, currentUser: PropTypes.object, fields: PropTypes.array, showEdit: PropTypes.bool } Card.contextTypes = { intl: intlShape } registerComponent('Card', Card);