Split table into component

This commit is contained in:
2022-03-13 16:19:52 +03:00
parent 9e216f821f
commit 648af4f060
8 changed files with 108 additions and 50 deletions

18
app/pages/home/ItemRow.js Normal file
View File

@@ -0,0 +1,18 @@
import React from "react"
export default class ItemRow extends React.Component {
constructor ({ scanResult }) {
super()
this.scanResult = scanResult
}
render () {
return (
<tr>
<td>{this.scanResult.name}</td>
<td>{this.scanResult.partsPrice}</td>
<td>{this.scanResult.setPrice}</td>
</tr>
)
}
}

25
app/pages/home/Table.js Normal file
View File

@@ -0,0 +1,25 @@
import React from "react"
import ItemRow from './ItemRow'
export default class Table extends React.Component {
constructor ({ scanResults }) {
super()
this.scanResults = scanResults
}
render () {
return (
<table>
<thead></thead>
<tbody>
{this.scanResults.map((scanResult) =>
<ItemRow
key={scanResult._id}
scanResult={scanResult}
/>
)}
</tbody>
</table>
)
}
}

39
app/pages/home/index.js Normal file
View File

@@ -0,0 +1,39 @@
import React from "react"
import dbConnect from '../../lib/dbConnect'
import { models } from 'shared-stuff'
import Table from './Table'
export default class Home extends React.Component {
constructor ({ scanResults }) {
super()
this.scanResults = scanResults
}
render () {
return (
<div>
<Table
scanResults={this.scanResults}
/>
</div>
)
}
}
export async function getServerSideProps() {
try {
await dbConnect()
const scanResults = await models.ScanResult.find({})
return {
props: {
scanResults: JSON.parse(JSON.stringify(scanResults))
},
}
} catch (e) {
console.error(e)
return {
props: {},
}
}
}