1 import React from 'react'
2 import styles from './KeyValueTable.scss'
3 import {Section} from 'features/shared/components'
4 import {Link} from 'react-router'
5 import {size, sample, isArray, isObject, toPairs} from 'lodash'
6 import {withNamespaces} from 'react-i18next'
8 class KeyValueTable extends React.Component {
10 if (item.pre) return true
12 return item.value != null && (typeof item.value == 'object')
16 if (isObject(value) && size(value) == 1) {
17 // Random sample will always be the lone value here
18 let sampled = sample(value)
20 if (!isObject(sampled)) {
21 if (isArray(value)) return JSON.stringify(value)
23 // Manually construct single-key object stringify for better formatting
24 const pair = toPairs(value)[0]
25 return `{${JSON.stringify(pair[0])}: ${JSON.stringify(pair[1])}}`
29 return JSON.stringify(value, null, ' ')
33 let value = item.value
34 if (this.shouldUsePre(item)) {
35 value = <pre className={styles.pre}>{this.stringify(item.value)}</pre>
38 value = <Link to={item.link}>{value}</Link>
41 if (value === undefined || value === null || value === '') {
49 const t = this.props.t
52 title={this.props.title}
53 actions={this.props.actions}>
54 <table className={styles.table}>
56 {this.props.items.map((item) => {
57 return <tr key={item.label}>
58 <td className={styles.label}>{item.label}</td>
59 <td className={styles.value}>{this.renderValue(item)}
61 {item.editUrl && <Link to={item.editUrl} className={styles.edit}>
63 className={`${styles.pencil} glyphicon glyphicon-pencil`}></span>{t('form.edit')}
65 {item.program && <button onClick={item.program} className={`${styles.detail} ${styles.edit} btn btn-link`}>
66 { t('commonWords.program')}
78 export default withNamespaces('translations') (KeyValueTable)