-
Notifications
You must be signed in to change notification settings - Fork 63
/
ScanWrapper.jsx
137 lines (131 loc) · 4.37 KB
/
ScanWrapper.jsx
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import React, { Component } from 'react'
import { translate } from 'cozy-ui/transpiled/react/I18n'
import { ActionMenuItem } from 'cozy-ui/transpiled/react/ActionMenu'
import Icon from 'cozy-ui/transpiled/react/Icon'
import { Scanner, SCANNER_DONE, SCANNER_UPLOADING } from 'cozy-scanner'
import toolbarContainer from '../toolbar'
import PortaledQueue from './PortaledQueue'
import MuiCozyTheme from 'cozy-ui/transpiled/react/MuiCozyTheme'
import {
startMediaBackup,
cancelMediaBackup
} from 'drive/mobile/modules/mediaBackup/duck'
import { getTracker } from 'cozy-ui/transpiled/react/helpers/tracker'
import { connect } from 'react-redux'
/**
* ScanMenItem Display the "Scan" item in the "More Menu"
*/
const ScanMenuItem = translate()(({ status, onClick, t, online }) => {
const offlineMessage = () => {
return alert(t('Scan.error.offline'))
}
const uploadingMessage = () => {
return alert(t('Scan.error.uploading'))
}
const actionOnClick = (() => {
if (status === SCANNER_UPLOADING) return uploadingMessage
if (!online) return offlineMessage
return onClick
})()
const trackEvent = () => {
const tracker = getTracker()
if (tracker) {
tracker.push(['trackEvent', 'Drive', 'Scanner', 'Scan Click'])
}
}
return (
<ActionMenuItem
left={<Icon icon="camera" />}
onClick={() => {
trackEvent()
return actionOnClick()
}}
>
{t('Scan.scan_a_doc')}
</ActionMenuItem>
)
})
/**
* ScanWrapper is a wrapper of Scanner. It has the responsability to decide of :
* - generating the filename
* - Dispatching some events before and after the scan
* - call the component to render
*/
class ScanWrapper extends Component {
render() {
const { displayedFolder, stopMediaBackup, startMediaBackup } = this.props
// The ActionMenu needs to stay open during the scan, so we prevent the click event from bubbling
return (
<MuiCozyTheme>
<div onClick={e => e.stopPropagation()}>
<Scanner
dirId={displayedFolder.id} //Pour savoir où uploader
pluginConfig={{
sourceType: 1 // Camera
}}
generateName={() => {
const date = new Date()
//We had to replace : by - since the Cordova File plugin doesn't support : in the filename
//https://github.com/apache/cordova-plugin-file/issues/289#issuecomment-477954331
return `Scan_${date.toISOString().replace(/:/g, '-')}.jpg`
}}
onConflict={'rename'}
//We need to cancel the MediaBackup before doing the upload since the scanned file will be
//inserted we don't know where in the queue resulting in a non uploaded file if the queue is
//big enough
onBeforeUpload={() => stopMediaBackup()}
onFinish={() => {
const tracker = getTracker()
if (tracker) {
tracker.push(['trackEvent', 'Drive', 'Scanner', 'Finished'])
}
startMediaBackup()
}}
>
{({ status, error, startScanner, filename, onClear, online }) => {
if (error || !filename) {
return (
<ScanMenuItem
status={status}
onClick={startScanner}
online={online}
/>
)
}
return (
<>
<ScanMenuItem
status={status}
onClick={startScanner}
online={online}
/>
<PortaledQueue
file={{
file: {
name: filename,
isDirectory: false,
status
}
}}
successCount={status === SCANNER_DONE ? 1 : 0}
doneCount={1}
key={filename}
onClear={onClear}
/>
</>
)
}}
</Scanner>
</div>
</MuiCozyTheme>
)
}
}
const mapDispatchToProps = dispatch => ({
stopMediaBackup: () => dispatch(cancelMediaBackup()),
startMediaBackup: () => dispatch(startMediaBackup())
})
export default connect(
null,
mapDispatchToProps
)(toolbarContainer(ScanWrapper))