1
1
import React , { useState , useEffect , useRef } from 'react' ;
2
2
import { Link } from 'react-router-dom' ;
3
3
import humanizeDuration from 'humanize-duration' ;
4
+ import { DocumentTextIcon } from '@heroicons/react/outline' ;
4
5
import { useGeocodeContext } from '../components/GeocodeContext.js' ;
5
6
6
7
const percentFormatter = new Intl . NumberFormat ( 'en-US' , { style : 'percent' } ) ;
7
8
8
9
export default function Geocoding ( ) {
9
- const [ status , setStatus ] = useState ( {
10
+ const [ stats , setStats ] = useState ( {
10
11
rowsProcessed : 0 ,
11
12
totalRows : 0 ,
12
13
activeMatchRate : 0 ,
13
14
averageScore : 0 ,
15
+ status : 'idle' ,
14
16
} ) ;
15
17
const startTime = useRef ( new Date ( ) ) ;
16
18
const abortController = useRef ( new AbortController ( ) ) ;
17
19
const geocodeContext = useGeocodeContext ( ) [ 0 ] ;
20
+ const draggable = useRef ( null ) ;
21
+
22
+ const onDragStart = ( event ) => {
23
+ event . preventDefault ( ) ;
24
+ window . ugrc . startDrag ( 'ugrc_geocode_results.csv' ) ;
25
+ } ;
18
26
19
27
useEffect ( ( ) => {
20
28
window . ugrc . onGeocodingUpdate ( ( _ , data ) => {
21
- setStatus ( data ) ;
29
+ setStats ( data ) ;
22
30
} ) ;
23
31
24
32
window . ugrc . geocode ( {
@@ -29,10 +37,10 @@ export default function Geocoding() {
29
37
} ) ;
30
38
} , [ geocodeContext ] ) ;
31
39
32
- const progress = status . rowsProcessed / status . totalRows || 0 ;
40
+ const progress = stats . rowsProcessed / stats . totalRows || 0 ;
33
41
const elapsedTime = new Date ( ) . getTime ( ) - startTime . current . getTime ( ) ;
34
- const timePerRow = elapsedTime / status . rowsProcessed ;
35
- const estimatedTimeRemaining = timePerRow * ( status . totalRows - status . rowsProcessed ) ;
42
+ const timePerRow = elapsedTime / stats . rowsProcessed ;
43
+ const estimatedTimeRemaining = timePerRow * ( stats . totalRows - stats . rowsProcessed ) ;
36
44
37
45
const cancel = ( ) => {
38
46
window . ugrc . cancelGeocode ( ) ;
@@ -51,21 +59,21 @@ export default function Geocoding() {
51
59
< dl >
52
60
< div className = "px-4 py-5 bg-gray-50 sm:grid sm:grid-cols-3 sm:gap-4 sm:px-6" >
53
61
< dt className = "font-medium text-gray-500" > Rows processed</ dt >
54
- < dd className = "mt-1 text-gray-900 sm:mt-0 sm:col-span-2" > { status . rowsProcessed } </ dd >
62
+ < dd className = "mt-1 text-gray-900 sm:mt-0 sm:col-span-2" > { stats . rowsProcessed } </ dd >
55
63
</ div >
56
64
< div className = "px-4 py-5 bg-white sm:grid sm:grid-cols-3 sm:gap-4 sm:px-6" >
57
65
< dt className = "font-medium text-gray-500" > Total Rows</ dt >
58
- < dd className = "mt-1 text-gray-900 sm:mt-0 sm:col-span-2" > { status . totalRows } </ dd >
66
+ < dd className = "mt-1 text-gray-900 sm:mt-0 sm:col-span-2" > { stats . totalRows } </ dd >
59
67
</ div >
60
68
< div className = "px-4 py-5 bg-gray-50 sm:grid sm:grid-cols-3 sm:gap-4 sm:px-6" >
61
69
< dt className = "font-medium text-gray-500" > Active match rate</ dt >
62
70
< dd className = "mt-1 text-gray-900 sm:mt-0 sm:col-span-2" >
63
- { percentFormatter . format ( status . activeMatchRate ) }
71
+ { percentFormatter . format ( stats . activeMatchRate ) }
64
72
</ dd >
65
73
</ div >
66
74
< div className = "px-4 py-5 bg-white sm:grid sm:grid-cols-3 sm:gap-4 sm:px-6" >
67
75
< dt className = "font-medium text-gray-500" > Average match score</ dt >
68
- < dd className = "mt-1 text-gray-900 sm:mt-0 sm:col-span-2" > { status . averageScore } </ dd >
76
+ < dd className = "mt-1 text-gray-900 sm:mt-0 sm:col-span-2" > { stats . averageScore } </ dd >
69
77
</ div >
70
78
< div className = "px-4 py-5 bg-gray-50 sm:grid sm:grid-cols-3 sm:gap-4 sm:px-6" >
71
79
< dt className = "font-medium text-gray-500" > Time elapsed</ dt >
@@ -80,14 +88,28 @@ export default function Geocoding() {
80
88
</ dd >
81
89
</ div >
82
90
</ dl >
91
+ { stats . status === 'complete' && (
92
+ < div
93
+ className = "p-12 mx-auto text-center bg-gray-100 border rounded-lg shadow cursor-grab"
94
+ ref = { draggable }
95
+ draggable = { true }
96
+ onDragStart = { onDragStart }
97
+ >
98
+ < p > Drag and drop this file to save the results.</ p >
99
+ < DocumentTextIcon className = "w-32 mx-auto" />
100
+ < span className = "mx-auto font-mono text-sm" > ugrc_geocode_results.csv</ span >
101
+ </ div >
102
+ ) }
83
103
</ section >
84
- < button
85
- type = "button"
86
- onClick = { cancel }
87
- disabled = { status . rowsProcessed > 0 && status . totalRows === status . rowsProcessed }
88
- >
89
- Cancel
90
- </ button >
104
+ { stats . status === 'running' ? (
105
+ < button
106
+ type = "button"
107
+ onClick = { cancel }
108
+ disabled = { stats . rowsProcessed > 0 && stats . totalRows === stats . rowsProcessed }
109
+ >
110
+ Cancel
111
+ </ button >
112
+ ) : null }
91
113
</ article >
92
114
) ;
93
115
}
0 commit comments