Skip to content

Commit 84c4154

Browse files
authored
Merge pull request KelvinTegelaar#40 from KelvinTegelaar/dev
2 parents b9a0e95 + 33735d4 commit 84c4154

File tree

10 files changed

+26582
-14402
lines changed

10 files changed

+26582
-14402
lines changed

src/components/tables/CellGenericFormat.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,14 @@ export const cellGenericFormatter =
7676
return CellBoolean({ cell, warning, reverse, colourless, noDataIsFalse })
7777
}
7878
if (typeof cell === 'string') {
79+
if (cell.toLowerCase() === 'failed') {
80+
return <CBadge color="danger">{CellTip('Failed to retrieve from API')}</CBadge>
81+
}
7982
return CellTip(cell)
8083
}
84+
if (typeof cell === 'number') {
85+
return <CBadge color="info">{CellTip(cell)}</CBadge>
86+
}
8187
if (Array.isArray(cell) || typeof cell === 'object') {
8288
return CellTip(JSON.stringify(cell))
8389
}

src/components/tables/CellTable.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import React from 'react'
2+
import { CButton } from '@coreui/react'
3+
import { ModalService } from '../utilities'
4+
import { CBadge } from '@coreui/react'
5+
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'
6+
import { faCheckCircle } from '@fortawesome/free-solid-svg-icons'
7+
import { cellGenericFormatter } from './CellGenericFormat'
8+
9+
export default function cellTable(row, column, propertyName) {
10+
const handleTable = ({ row }) => {
11+
const QueryColumns = []
12+
const columns = Object.keys(row[propertyName][0]).map((key) => {
13+
QueryColumns.push({
14+
name: key,
15+
selector: (row) => row[key], // Accessing the property using the key
16+
sortable: true,
17+
exportSelector: key,
18+
cell: cellGenericFormatter(),
19+
})
20+
})
21+
ModalService.open({
22+
data: row[propertyName],
23+
componentType: 'table',
24+
componentProps: {
25+
columns: QueryColumns,
26+
keyField: 'SKU',
27+
},
28+
title: `Data`,
29+
size: 'lg',
30+
})
31+
}
32+
33+
if (!row[propertyName] || !Array.isArray(row[propertyName]) || row.length === 0) {
34+
return <FontAwesomeIcon icon={faCheckCircle} className="text-success" />
35+
}
36+
37+
return (
38+
<CButton className="btn-danger" key={row} size="sm" onClick={() => handleTable({ row })}>
39+
{row[propertyName].length} Items
40+
</CButton>
41+
)
42+
}
43+
44+
export const cellTableFormatter = (propertyName) => (row, index, column, id) => {
45+
return cellTable(row, column, propertyName)
46+
}

src/components/tables/CippTable.js

Lines changed: 45 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -163,9 +163,12 @@ export default function CippTable({
163163
}
164164
}
165165
const [resetPaginationToggle, setResetPaginationToggle] = React.useState(false)
166-
const filteredItems = data.filter(
167-
(item) => JSON.stringify(item).toLowerCase().indexOf(filterText.toLowerCase()) !== -1,
168-
)
166+
const filteredItems = Array.isArray(data)
167+
? data.filter(
168+
(item) => JSON.stringify(item).toLowerCase().indexOf(filterText.toLowerCase()) !== -1,
169+
)
170+
: []
171+
169172
const applyFilter = (e) => {
170173
setFilterText(e.target.value)
171174
}
@@ -336,32 +339,35 @@ export default function CippTable({
336339
return null
337340
})
338341

339-
const filtered = data.map((obj) =>
340-
// eslint-disable-next-line no-sequences
341-
/* keys.reduce((acc, curr) => ((acc[curr] = obj[curr]), acc), {}),*/
342-
keys.reduce((acc, curr) => {
343-
const key = curr.split('/')
344-
if (key.length > 1) {
345-
var property = obj
346-
for (var x = 0; x < key.length; x++) {
347-
if (property.hasOwnProperty(key[x]) && property[key[x]] !== null) {
348-
property = property[key[x]]
349-
} else {
350-
property = 'n/a'
351-
break
352-
}
353-
}
354-
acc[curr] = property
355-
} else {
356-
if (typeof exportFormatter[curr] === 'function') {
357-
acc[curr] = exportFormatter[curr]({ cell: obj[curr] })
358-
} else {
359-
acc[curr] = obj[curr]
360-
}
361-
}
362-
return acc
363-
}, {}),
364-
)
342+
const filtered =
343+
Array.isArray(data) && data.length > 0
344+
? data.map((obj) =>
345+
// eslint-disable-next-line no-sequences
346+
/* keys.reduce((acc, curr) => ((acc[curr] = obj[curr]), acc), {}),*/
347+
keys.reduce((acc, curr) => {
348+
const key = curr.split('/')
349+
if (key.length > 1) {
350+
var property = obj
351+
for (var x = 0; x < key.length; x++) {
352+
if (property.hasOwnProperty(key[x]) && property[key[x]] !== null) {
353+
property = property[key[x]]
354+
} else {
355+
property = 'n/a'
356+
break
357+
}
358+
}
359+
acc[curr] = property
360+
} else {
361+
if (typeof exportFormatter[curr] === 'function') {
362+
acc[curr] = exportFormatter[curr]({ cell: obj[curr] })
363+
} else {
364+
acc[curr] = obj[curr]
365+
}
366+
}
367+
return acc
368+
}, {}),
369+
)
370+
: []
365371

366372
if (!disablePDFExport) {
367373
if (dynamicColumns === true) {
@@ -499,7 +505,16 @@ export default function CippTable({
499505
{(massResults.length >= 1 || loopRunning) && (
500506
<CCallout color="info">
501507
{massResults.map((message, idx) => {
502-
return <li key={idx}>{message.data.Results}</li>
508+
return (
509+
<li key={idx}>
510+
{
511+
//if message.data.results is an array, join, else just show the message
512+
message.data?.Results?.length > 1
513+
? message.data?.Results?.join(', ')
514+
: message.data?.Results
515+
}
516+
</li>
517+
)
503518
})}
504519
{loopRunning && (
505520
<li>

0 commit comments

Comments
 (0)