This repository was archived by the owner on Sep 19, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathSavedIndicator.jsx
251 lines (214 loc) · 6.43 KB
/
SavedIndicator.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
import React from 'react'
import PropTypes from 'prop-types'
import { connect } from 'react-redux'
import classnames from 'classnames'
import i18n from 'util/i18n'
import { saveSection } from 'components/SavedIndicator/persistence-helpers'
import { formIsLocked } from 'validators'
class SavedIndicator extends React.Component {
constructor(props) {
super(props)
this.state = {
interval: props.interval || 1000,
elapsed: props.elapsed || 0,
hover: false,
animate: false,
}
this.save = this.save.bind(this)
this.tick = this.tick.bind(this)
this.mouseEnter = this.mouseEnter.bind(this)
this.mouseLeave = this.mouseLeave.bind(this)
}
componentDidMount() {
const { interval } = this.state
this.timer = window.setInterval(this.tick, interval)
}
componentWillReceiveProps() {
this.setState({ elapsed: 0 })
}
componentWillUnmount() {
window.clearInterval(this.timer)
}
isRoute = route => (window.location.pathname || '').indexOf(route) !== -1
save() {
const { app, section, dispatch } = this.props
// So, this is fun, it was determined the save button is not required on
// `form/package/print` but should still be available on any other section
// found in "Review and submit". The crux is saving within this section does
// nothing since it was declared signatures should **only** be saved when
// the form is **submitted**.
//
// So what we have here is a reset of the elapsed time when the save button
// is clicked within the "Review and submit" section to provide comfort to
// the end user.
if (this.isRoute('form/package') && !this.isRoute('form/package/comments')) {
this.setState({ elapsed: 0 })
return
}
this.setState({ animate: true })
saveSection(app, section.section, section.subsection, dispatch)
.then(() => {
this.setState({ animate: false })
})
.catch((error) => {
this.setState({ animate: false })
alert(error)
})
}
mouseEnter() {
this.setState({ hover: true })
}
mouseLeave() {
this.setState({ hover: false })
}
tick() {
const { saved } = this.props
const currentTick = new Date().getTime()
let s = currentTick
if (this.props && saved) {
s = saved.getTime()
}
this.setState({ elapsed: currentTick - s })
}
calculateTime() {
const { elapsed } = this.state
// If there has been no time elapsed...
if (!elapsed) {
return i18n.t('saved.now')
}
// Get the time in seconds
let timespan = elapsed / 1000
// Determine the unit of measurement and then adjust the timespan
// rounding the value
let unit = ''
if (timespan / (24 * 60 * 60) >= 1) {
timespan /= 24 * 60 * 60
timespan = Math.round(timespan)
unit = timespan > 1 ? i18n.t('saved.days') : i18n.t('saved.day')
} else if (timespan / (60 * 60) >= 1) {
timespan /= 60 * 60
timespan = Math.round(timespan)
unit = timespan > 1 ? i18n.t('saved.hours') : i18n.t('saved.hour')
} else if (timespan / 60 >= 1) {
timespan /= 60
timespan = Math.round(timespan)
unit = timespan > 1 ? i18n.t('saved.minutes') : i18n.t('saved.minute')
} else {
timespan = Math.round(timespan)
if (timespan < 1) {
return i18n.t('saved.now')
}
unit = timespan === 1 ? i18n.t('saved.second') : i18n.t('saved.seconds')
}
return `${timespan} ${unit} ${i18n.t('saved.ago')}`
}
allowed() {
const { app } = this.props
return !formIsLocked(app) && !this.isRoute('form/package/print')
}
render() {
if (!this.allowed()) {
return null
}
const { saveError } = this.props
const { animate, hover } = this.state
const buttonClasses = classnames(
'saved-indicator',
{
active: animate,
error: !animate && saveError,
}
)
const circleClasses = classnames(
'spinner-icon',
{ spin: animate }
)
const iconClasses = classnames(
'fa', 'fa-floppy-o',
{ invert: animate }
)
// Determine the appropriate response for screen readers.
let talkback = null
if (!animate) {
if (saveError) {
talkback = i18n.t('saved.error.message')
} else if (hover) {
talkback = i18n.t('saved.action')
} else {
talkback = `${i18n.t('saved.saved')} ${this.calculateTime()}. ${i18n.t(
'saved.action'
)}?`
}
} else {
talkback = i18n.t('saved.saving')
}
return (
<div className="saved-indicator-container">
<button
className={buttonClasses}
aria-label={talkback}
title={talkback}
onClick={this.save}
onMouseEnter={this.mouseEnter}
onMouseLeave={this.mouseLeave}
type="button"
>
<div className="spinner">
<div className={circleClasses} />
<i className={iconClasses} aria-hidden="true" />
</div>
<span className="spinner-label">
{(animate || (hover && !saveError)) && (
<strong className="one-line">
{animate ? i18n.t('saved.saving') : i18n.t('saved.action')}
</strong>
)}
{!animate && !hover && !saveError && (
<span>
<strong>{i18n.t('saved.saved')}</strong>
<span className="time">{this.calculateTime()}</span>
</span>
)}
{!animate && saveError && (
<span>
<strong>{i18n.t('saved.error.title')}</strong>
<span className="time">{i18n.t('saved.error.subtitle')}</span>
</span>
)}
</span>
</button>
</div>
)
}
}
SavedIndicator.propTypes = {
interval: PropTypes.number,
elapsed: PropTypes.number,
saveError: PropTypes.bool,
app: PropTypes.object,
saved: PropTypes.object,
section: PropTypes.object,
dispatch: PropTypes.func,
}
SavedIndicator.defaultProps = {
interval: 1000,
elapsed: 0,
saveError: false,
app: {},
saved: {},
section: {},
dispatch: () => {},
}
function mapStateToProps(state) {
const section = state.section || {}
const app = state.application || {}
const settings = app.Settings || {}
return {
section,
app,
saved: settings.saved || new Date(),
saveError: settings.saveError,
}
}
export { SavedIndicator }
export default connect(mapStateToProps)(SavedIndicator)