import React, { PropTypes, PureComponent } from 'react'; import FontIcon from 'react-toolbox/lib/font_icon'; import style from './history.scss'; const DIFF_PREFIXES = { A: ' ', E: ' ', D: '-', N: '+', }; const SPADEN_CLASS = { A: style.blue, // array edited E: style.blue, // edited D: style.negative, // deleted N: style.positive, // added }; function getIcon (type) { switch (type) { case 'feature-updated': return 'autorenew'; case 'feature-created': return 'add'; case 'feature-deleted': return 'remove'; case 'feature-archived': return 'archived'; default: return 'star'; } } function buildItemDiff (diff, key) { let change; if (diff.lhs !== undefined) { change = (
- {key}: {JSON.stringify(diff.lhs)}
); } else if (diff.rhs !== undefined) { change = (
+ {key}: {JSON.stringify(diff.rhs)}
); } return change; } function buildDiff (diff, idx) { let change; const key = diff.path.join('.'); if (diff.item) { change = buildItemDiff(diff.item, key); } else if (diff.lhs !== undefined && diff.rhs !== undefined) { change = (
- {key}: {JSON.stringify(diff.lhs)}
+ {key}: {JSON.stringify(diff.rhs)}
); } else { const spadenClass = SPADEN_CLASS[diff.kind]; const prefix = DIFF_PREFIXES[diff.kind]; change = (
{prefix} {key}: {JSON.stringify(diff.rhs || diff.item)}
); } return (
{change}
); } class HistoryItem extends PureComponent { static propTypes () { return { entry: PropTypes.object, }; } renderEventDiff (logEntry) { if (!logEntry.diffs) { return; } const changes = logEntry.diffs.map(buildDiff); return ( {changes.length === 0 ? '(no changes)' : changes} ); } renderFullEventData (logEntry) { const localEventData = JSON.parse(JSON.stringify(logEntry)); delete localEventData.description; delete localEventData.name; delete localEventData.diffs; const prettyPrinted = JSON.stringify(localEventData, null, 2); return ({prettyPrinted}); } render () { const { createdBy, id, type, } = this.props.entry; const createdAt = (new Date(this.props.entry.createdAt)).toLocaleString('nb-NO'); const icon = getIcon(type); const data = this.props.showData ? this.renderFullEventData(this.props.entry) : this.renderEventDiff(this.props.entry); return ( {type}
Id:
{id}
Type:
{type}
Timestamp:
{createdAt}
Username:
{createdBy}
{data} ); } } export default HistoryItem;