/
policies-table.js
159 lines (144 loc) · 3.88 KB
/
policies-table.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
'use strict'
import React from 'react'
import { PropTypes as T } from 'prop-types'
import { Link } from 'react-router-dom'
import ReactTooltip from 'react-tooltip'
import c from 'classnames'
import { environment } from '../config'
import { LoadingSkeleton } from '../components/loading-skeleton'
import { initializeArrayWithRange } from '../utils/array'
export default class PoliciesTable extends React.PureComponent {
componentDidUpdate () {
ReactTooltip.rebuild()
}
onSort (field, e) {
e.preventDefault()
const { sortDirection, onSort } = this.props
onSort(field, sortDirection === 'asc' ? 'desc' : 'asc')
}
renderTableHeader () {
const headings = [
{
id: 'name',
sortable: true,
title: 'Sort by policy name',
value: 'Policy name'
},
{
id: 'country',
sortable: true,
title: 'Sort by geography',
value: 'Geography'
},
{
id: 'mechanism',
sortable: false,
value: 'Policy Mechanism'
},
{
id: 'status',
sortable: true,
title: 'Sort by status',
value: 'Status'
}
]
return (
<thead>
<tr>
{headings.map(o => {
if (!o.sortable) return <th key={o.id}>{o.value}</th>
const { sortField, sortDirection } = this.props
const klass = c('sort', {
'sort-none': sortField !== o.id,
'sort-asc': sortField === o.id && sortDirection === 'asc',
'sort-desc': sortField === o.id && sortDirection === 'desc'
})
return <th key={o.id}><a href='#' title={o.title} className={klass} onClick={this.onSort.bind(this, o.id)}>{o.value}</a></th>
})}
</tr>
</thead>
)
}
renderLoadingRows () {
return initializeArrayWithRange(10).map(v => (
<tr key={v}>
<td><LoadingSkeleton /></td>
<td><LoadingSkeleton /></td>
<td><LoadingSkeleton /></td>
<td><LoadingSkeleton /></td>
</tr>
))
}
renderRows () {
const renderArrayField = (data) => {
if (!data.length) return 'N/A'
if (data.length === 1) return data[0].name
return (
<div data-for='array-field-tooltip' data-tip={data.map(o => o.name).join(', ')}>
{data[0].name}
{data.length > 1 ? <small>+ {data.length - 1}</small> : null}
</div>
)
}
return this.props.policies.map(policy => {
return (
<tr key={policy.id}>
<td className='cell-policy-name'>
<Link to={`/policies/${policy.id}`} title='Go to policy page'>{policy.name}</Link>
</td>
<td>{renderArrayField(policy.country)}</td>
<td>{renderArrayField(policy.type.mechanism)}</td>
<td>{policy.status.name}</td>
</tr>
)
})
}
renderTooltip () {
const popoverContent = (tipContent) => {
return (
<div className='popover__contents'>
<div className='popover__body'>
{tipContent}
</div>
</div>
)
}
return (
<ReactTooltip
id='array-field-tooltip'
effect='solid'
type='custom'
className='popover'
wrapper='article'
getContent={popoverContent}
/>
)
}
render () {
if (!this.props.loading && !this.props.policies.length) return null
return (
<>
<table className='table policies-table'>
{this.renderTableHeader()}
<tbody>
{
this.props.loading
? this.renderLoadingRows()
: this.renderRows()
}
</tbody>
</table>
{this.renderTooltip()}
</>
)
}
}
if (environment !== 'production') {
PoliciesTable.propTypes = {
sortField: T.string,
sortDirection: T.string,
policies: T.array,
loading: T.bool,
onSort: T.func
}
}