1
1
import React , { useState , useEffect } from 'react' ;
2
+ import dynamic from 'next/dynamic'
2
3
3
- export default function Device ( { device, deviceData, error, isLoading } :
4
- { device : any , deviceData : any , error : string | null , isLoading : boolean }
5
- ) {
4
+ /* Wrap DevicePlot with a dynamic import to disable server-side rendering.
5
+ * Prevents `self is undefined` errors caused by Giraffe when rendered
6
+ * with Node.
7
+ */
8
+ const DynamicDevicePlotWithNoSSR = dynamic (
9
+ ( ) => import ( './_devicePlot' ) ,
10
+ { ssr : false }
11
+ )
6
12
7
- function writeSimulatedData ( ) {
13
+ export default function Device ( { deviceId } : { deviceId : string } ) {
14
+ const [ error , setError ] = useState < string | null > ( null )
15
+ const [ isLoading , setIsLoading ] = useState ( false )
16
+ const [ device , setDevice ] = useState < any [ ] | null > ( null )
17
+ const [ data , setData ] = useState < any | null > ( null )
18
+
19
+ const getDeviceData = ( deviceId : string ) => {
20
+ setIsLoading ( true )
21
+ setError ( null )
22
+ fetch ( `/api/devices/${ deviceId } /measurements` ,
23
+ { headers : { "Accept" : "application/csv" } }
24
+ )
25
+ . then ( ( res ) => {
26
+ if ( res . ok ) {
27
+ return res . text ( )
28
+ } else {
29
+ setError ( `Device data ${ res . statusText } ` )
30
+ setIsLoading ( false )
31
+ }
32
+ } )
33
+ . then ( ( data ) => {
34
+ setData ( data )
35
+ setIsLoading ( false )
36
+ } )
37
+ }
38
+
39
+ const getDevice = ( deviceId : string ) => {
40
+ setIsLoading ( true )
41
+ fetch ( `/api/devices/${ deviceId } ` )
42
+ . then ( ( res ) => res . json ( ) )
43
+ . then ( ( data ) => {
44
+ if ( Array . isArray ( data ) ) {
45
+ setDevice ( data )
46
+ }
47
+ if ( data . error ) {
48
+ setError ( data . error )
49
+ }
50
+ setIsLoading ( false )
51
+ } )
52
+ }
8
53
54
+ useEffect ( ( ) => {
55
+ deviceId &&
56
+ getDevice ( deviceId )
57
+ getDeviceData ( deviceId )
58
+ } , [ deviceId ] )
59
+
60
+ function writeSimulatedData ( ) {
61
+ fetch ( '/api/devices/generate' , {
62
+ method : 'POST' ,
63
+ body : JSON . stringify ( { deviceIds : [ deviceId ] } ) ,
64
+ headers : { "Content-Type" : "application/json" }
65
+ } )
66
+ . then ( ( res ) => {
67
+ if ( res . ok ) {
68
+ getDeviceData ( deviceId )
69
+ } else {
70
+ setError ( res . statusText )
71
+ }
72
+ } )
9
73
10
74
}
11
75
12
76
return (
13
- < >
14
- < div className = 'card' >
15
- < div className = "alert" >
16
- { isLoading && < span > Loading...</ span > }
17
- { error &&
18
- < span className = "alert-danger" > { error } </ span >
19
- }
20
- </ div >
21
- < h2 > Device</ h2 >
22
- < p > { device . deviceId } </ p >
23
- < p > { JSON . stringify ( deviceData ) } </ p >
24
-
77
+ deviceId ?
78
+ < div className = 'card' >
79
+ < div className = "alert" >
80
+ { isLoading && < span > Loading...</ span > }
81
+ { error &&
82
+ < span className = "alert-danger" > { error } </ span >
83
+ }
25
84
</ div >
26
- </ >
85
+ < h2 > Device</ h2 >
86
+ < p > { deviceId } </ p >
87
+ < p > < button onClick = { writeSimulatedData } > Generate data for this device</ button > </ p >
88
+ { data &&
89
+ < div > < DynamicDevicePlotWithNoSSR csv = { data } title = { 'Measurements' } lastUpdated = { '' } /> </ div >
90
+ }
91
+ </ div > : < > </ >
27
92
)
28
93
}
0 commit comments