@bigfishtv/cockpit
Version:
147 lines (130 loc) • 3.77 kB
JavaScript
import PropTypes from 'prop-types'
import React, { Component } from 'react'
import { Cell as TableCell } from 'fixed-data-table'
import { showDeletePrompt } from '../../utils/promptUtils'
import { filterDataByQueryWithFields } from '../../utils/tableUtils'
import { get } from '../../api/xhrUtils'
import TableView from '../table/Table'
import FixedDataTableDateCell from '../table/cell/FixedDataTableDateCell'
import Spinner from '../Spinner'
import MainContent from '../container/MainContent'
import Panel from '../container/panel/Panel'
import Bulkhead from '../page/Bulkhead'
import FilterInput from '../input/SearchInput'
import Button from '../button/Button'
const FixedDataTableTitleCell = ({ data, rowIndex, columnKey, ...props }) => (
<TableCell {...props}>
<a href={'/tank/roles/edit/' + data[rowIndex]['id']}>{data[rowIndex][columnKey]}</a>
</TableCell>
)
const fields = [
{
key: 'title',
width: 300,
Cell: FixedDataTableTitleCell,
},
{
key: 'modified',
value: 'Last Modified',
width: 130,
Cell: FixedDataTableDateCell,
},
{
key: 'created',
value: 'Date Created',
width: 130,
Cell: FixedDataTableDateCell,
},
]
/**
* Roles index page template
*/
export default class RolesIndex extends Component {
static propTypes = {
rolesGetUrl: PropTypes.string,
rolesAddUrl: PropTypes.string,
rolesUpdateUrl: PropTypes.string,
}
constructor() {
super()
this.state = {
data: [],
selectedIds: [],
currentuserId: null,
query: '',
loading: true,
negativeHeight: 182,
}
}
componentDidMount() {
get({
quietSuccess: true,
url: this.props.rolesGetUrl,
subject: 'roles',
callback: data => this.setState({ data, loading: false }),
callbackError: () => this.setState({ loading: false }),
})
}
getSubmitUrl(data) {
return data.id ? this.props.rolesUpdateUrl + '/' + data.id + '.json' : this.props.rolesAddUrl
}
handleQueryChange = value => {
const query = typeof value == 'string' ? value : value.target.value
this.setState({ query, selectedIds: [] })
}
handleAdd = () => {
window.location.href = '/tank/roles/add'
}
handleEdit = role => {
window.location.href = '/tank/roles/edit/' + role.id
}
handleDelete = () => {
const { data, selectedIds } = this.state
showDeletePrompt({
subject: 'role',
selectedIds,
data: data.filter(item => selectedIds.indexOf(item.id) >= 0),
queryUrl: this.props.rolesDeleteUrl,
callback: deletedData => {
const deletedIds = deletedData.map(item => item.id)
const data = this.state.data.filter(item => deletedIds.indexOf(item.id) < 0)
this.setState({ data, selectedIds: [] })
},
})
}
render() {
const { data, query, selectedIds, negativeHeight, loading } = this.state
return (
<MainContent>
<Bulkhead
title="Roles"
Toolbar={() => <Button text="New Role" onClick={this.handleAdd} style="primary" size="large" />}
/>
<div className="finder">
<div className="finder-content" ref="finderContent">
<Panel
title={<FilterInput value={query} onChange={this.handleQueryChange} />}
PanelToolbar={() => (
<Button text="Delete" onClick={this.handleDelete} style="error" disabled={!selectedIds.length} />
)}>
{loading ? (
<div className="loader-center margin-top-xlarge">
<Spinner spinnerName="circle" />
</div>
) : (
<TableView
data={filterDataByQueryWithFields(data, fields, query)}
fields={fields}
selectedIds={selectedIds}
onSelect={this.handleEdit}
onSelectionChange={selectedIds => this.setState({ selectedIds })}
negativeHeight={negativeHeight}
/>
)}
</Panel>
</div>
</div>
</MainContent>
)
}
}