-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(network): previews of datasets with JSON bodies now load
issue here was making adjustments to body data structure (adding row numbers) was happening regardless of weather the body is JSON or CSV, which would crash some JSON data structures. The better solution is to decide weather a body is JSON or CSV as early as possible, delegating all details to format-specific components.
- Loading branch information
Showing
2 changed files
with
99 additions
and
90 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import React from 'react' | ||
|
||
import Dataset, { Body } from '../models/dataset' | ||
import { TypeLabel } from './TwoDSchemaLayout' | ||
|
||
interface BodyPreviewProps { | ||
data: Dataset | ||
} | ||
|
||
// BodyTablePreview is a lightweight version of BodyTable that we're hoping to | ||
// merge into BodyTable in the near future | ||
// TODO (b5) - merge BodyTablePreview functionality into BodyTable | ||
const BodyTablePreview: React.FunctionComponent<BodyPreviewProps> = ({ data }) => { | ||
const { body, structure } = data | ||
|
||
if (!body) return null | ||
|
||
const bdy = addRowNumbers(body) | ||
|
||
const tableRows = bdy.map((row: any[], i: number) => { | ||
return ( | ||
<tr key={i}> | ||
{row.map((d: any, j: number) => { | ||
const isFirstColumn = j === 0 | ||
if (isFirstColumn) d = parseInt(d) + 1 | ||
return ( | ||
<td key={j} className={isFirstColumn ? 'first-column' : ''}> | ||
<div className='cell'>{typeof d === 'boolean' ? JSON.stringify(d) : d}</div> | ||
</td> | ||
) | ||
})} | ||
</tr> | ||
) | ||
}) | ||
|
||
let headers: Array<{ type: string, title: string}> = [] | ||
if (structure && structure.schema) { | ||
structure.schema.items.items.forEach((column: { type: string, title: string}) => { | ||
headers.push(column) | ||
}) | ||
} | ||
|
||
return (<div className='table-container'> | ||
<table style={{ display: 'table' }}> | ||
<thead> | ||
<tr> | ||
<th> | ||
<div className='cell'>#</div> | ||
</th> | ||
{headers.length > 0 && headers.map((d: any, j: number) => { | ||
return ( | ||
<th key={j} > | ||
<div className='cell' > | ||
<TypeLabel type={d.type} showLabel={false}/> {d.title} | ||
</div> | ||
</th> | ||
) | ||
})} | ||
</tr> | ||
</thead> | ||
<tbody> | ||
{tableRows} | ||
</tbody> | ||
</table> | ||
</div>) | ||
} | ||
|
||
// adds row numbers to an array of arrays or object | ||
const addRowNumbers = (body: Body): any[][] | any[] => { | ||
if (!body) return body | ||
if (Array.isArray(body)) { | ||
return body.map((row: any[], i): any[] => { return [i, ...row] }) | ||
} | ||
|
||
return Object.keys(body).map((key: string): Record<any, any> => ({ | ||
' ': key, | ||
...body[key] | ||
})) | ||
} | ||
|
||
export default BodyTablePreview |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters