-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path_devicesList.tsx
More file actions
66 lines (61 loc) · 2.01 KB
/
Copy path_devicesList.tsx
File metadata and controls
66 lines (61 loc) · 2.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import React, {useState, useEffect} from 'react';
import Link from 'next/link'
import {useRouter} from 'next/router'
export function DevicesList({listFilter, onError, isLoading}:
{listFilter: string | [],
onError: (error: string) => void,
isLoading: (loading: boolean) => void})
{
const [data, setData] = useState<any[] | null>(null)
const itemType = 'device'
const router = useRouter()
const deviceId = router.query['deviceId'] || ''
const itemKey = (item: any) => `${item.deviceId}_${item.key}`
useEffect(() => {
isLoading(true)
fetch(`/api/devices/${listFilter || '' }`)
.then((res) => res.json())
.then((data) => {
if(Array.isArray(data)) {
setData(data)
}
if(data.error) {
onError(data.error)
}
isLoading(false)
})
}, [listFilter])
function localTime(time: any) {
const date = new Date(time)
return date.toLocaleString()
}
const style = `
.link-
`
return (
<ul className='list-group vh-100 overflow-scroll'>
{ Array.isArray(data) ?
data.map((item, i) => (
<li className={`list-group-item d-flex justify-content-between align-items-start
${deviceId.includes(item.deviceId) && 'active'}`}
key={`${itemKey(item)}${i}`}>
<div className="ms-2 me-auto">
<div className="fw-bold">
{deviceId.includes(item.deviceId) ?
(item.deviceId || 'Unknown ID') :
<Link href={`/devices/${encodeURIComponent(item.deviceId)}`}>
<button className='btn btn-primary'>
{item.deviceId || 'Unknown ID'}
</button>
</Link>
}
</div>
<small className='mt-1'>{`Updated: ${localTime(item.updatedAt)}`}</small>
</div>
</li>
)
) : <p>No devices found</p>
}
</ul>
)
}