89 lines
2.7 KiB
React
Raw Normal View History

2018-04-30 20:08:18 +02:00
import React from 'react'
import { Helmet } from 'react-helmet'
import { Link } from 'react-router-dom'
2018-05-01 13:03:40 +02:00
import { history } from '../../../index'
2018-04-30 20:08:18 +02:00
export default function AdminPage(props) {
const { data, detailLink, target } = props
const { error } = data
const results = data.data
const tbKeys = []
if (results.length > 0) {
Object.keys(results[0])
.filter(key => key.charAt(0) !== '_')
.map(key => tbKeys.push(key))
2018-04-30 20:08:18 +02:00
}
const title = target.charAt(0).toUpperCase() + target.slice(1)
return (
<div>
<Helmet>
<title>mpwo - Admin</title>
</Helmet>
<h1 className="page-title">
Administration - {title}
</h1>
{error ? (
<code>{error}</code>
) : (
<div className="container">
<div className="row">
<div className="col-md-2" />
<div className="col-md-8 card">
2018-04-30 21:38:09 +02:00
<div className="card-body">
<table className="table">
<thead>
<tr>
{tbKeys.map(
tbKey => <th key={tbKey} scope="col">{tbKey}</th>
)}
2018-04-30 20:08:18 +02:00
</tr>
2018-04-30 21:38:09 +02:00
</thead>
<tbody>
{ results.map((result, idx) => (
// eslint-disable-next-line react/no-array-index-key
2018-04-30 21:38:09 +02:00
<tr key={idx}>
{ Object.keys(result)
.filter(key => key.charAt(0) !== '_')
.map(key => {
2018-04-30 21:38:09 +02:00
if (key === 'id') {
return (
<th key={key} scope="row">
<Link to={`/admin/${detailLink}/${result[key]}`}>
{result[key]}
</Link>
</th>
)
}
return <td key={key}>{result[key]}</td>
})
}
</tr>
))}
</tbody>
</table>
2018-05-01 13:03:40 +02:00
<input
type="submit"
className="btn btn-primary btn-lg btn-block"
onClick={() => history.push(`/admin/${target}/add`)}
value="Add a new item"
/>
<input
type="submit"
className="btn btn-secondary btn-lg btn-block"
onClick={() => history.push('/admin/')}
value="Back"
/>
2018-04-30 21:38:09 +02:00
</div>
2018-04-30 20:08:18 +02:00
</div>
<div className="col-md-2" />
</div>
</div>
)}
</div>
)
}