-
Notifications
You must be signed in to change notification settings - Fork 51
/
index.js
336 lines (322 loc) · 10.1 KB
/
index.js
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
import React, { Component } from 'react'
import PropTypes from 'prop-types'
import { connect } from 'react-redux'
import Helmet from 'react-helmet'
import { isUndefined, size, map } from 'lodash'
import ReactList from 'react-list'
import { Icon, LoaderText, Select, Notification } from '../../components/'
import { Button, Row } from 'react-bootstrap'
import {
glossaryDeleteTerm,
glossaryResetTerm,
glossarySelectTerm,
glossaryUpdateField,
glossaryUpdateTerm,
glossaryGoFirstPage,
glossaryGoLastPage,
glossaryGoNextPage,
glossaryGoPreviousPage,
glossaryInitialLoad,
glossaryUpdatePageSize,
PAGE_SIZE_DEFAULT,
PAGE_SIZE_SELECTION
} from '../../actions/glossary-actions'
import ViewHeader from './ViewHeader'
import Entry from './Entry'
/**
* Root component for Glossary page
*/
class Glossary extends Component {
static propTypes = {
/**
* Object of glossary id with term
*/
terms: PropTypes.object,
project: PropTypes.object,
params: PropTypes.object,
termIds: PropTypes.array,
termCount: PropTypes.number,
termsLoading: PropTypes.bool,
transLocales: PropTypes.array,
srcLocale: PropTypes.object,
filterText: PropTypes.string,
selectedTerm: PropTypes.object,
selectedTransLocale: PropTypes.string,
permission: PropTypes.object,
location: PropTypes.object,
saving: PropTypes.object,
deleting: PropTypes.object,
notification: PropTypes.object,
goPreviousPage: PropTypes.func,
goFirstPage: PropTypes.func,
goLastPage: PropTypes.func,
goNextPage: PropTypes.func,
handleInitLoad: PropTypes.func,
handleSelectTerm: PropTypes.func,
handleTermFieldUpdate: PropTypes.func,
handleDeleteTerm: PropTypes.func,
handleResetTerm: PropTypes.func,
handleUpdateTerm: PropTypes.func,
handlePageSizeChange: PropTypes.func,
page: PropTypes.string,
gotoPreviousPage: PropTypes.func,
gotoFirstPage: PropTypes.func,
gotoLastPage: PropTypes.func,
gotoNextPage: PropTypes.func,
pageSize: PropTypes.string
}
componentDidMount () {
const paramProjectSlug = this.props.params.projectSlug
const projectSlug = (!paramProjectSlug || paramProjectSlug === 'undefined')
? undefined : paramProjectSlug
this.props.handleInitLoad(projectSlug)
}
/**
* Force component to update when changes between project glossary to glossary
*/
componentDidUpdate (prevProps, prevState) {
const projectSlug = this.props.params.projectSlug
if (prevProps.params.projectSlug !== projectSlug) {
this.props.handleInitLoad(projectSlug)
}
}
/**
* Function passed to react-list to delegate rendering of an item.
*
* Arguments are the overall index of the item, and the local (page?) index of
* the item.
*
* This is not in the API for the latest version of react-list,which is
* now released under a stable version number).
*
* Best practice is to use an arrow function here so that React will auto-bind
* this function, but this stops working properly unless binding is done
* inline in the JSX. Hope you have a good garbage collector.
*/
renderItem (index, key) {
const {
handleSelectTerm,
handleTermFieldUpdate,
handleDeleteTerm,
handleResetTerm,
handleUpdateTerm,
termsLoading,
termIds,
terms,
selectedTransLocale,
selectedTerm,
permission,
saving,
deleting
} = this.props
const entryId = termIds[index]
const selected = entryId === selectedTerm.id
const isSaving = !isUndefined(saving[entryId])
let entry
if (isSaving && entryId) {
entry = saving[entryId]
} else if (selected) {
entry = selectedTerm
} else if (entryId) {
entry = terms[entryId]
}
const isDeleting = !isUndefined(deleting[entryId])
return (
<Entry {...{
key,
entry,
index,
selected,
isDeleting,
isSaving,
permission,
selectedTransLocale,
termsLoading,
handleSelectTerm,
handleTermFieldUpdate,
handleDeleteTerm,
handleResetTerm,
handleUpdateTerm
}} />
)
}
render () {
const {
terms,
termsLoading,
termCount,
notification,
gotoPreviousPage,
gotoFirstPage,
gotoLastPage,
gotoNextPage,
page,
pageSize,
handlePageSizeChange,
project
} = this.props
const intPageSize = pageSize ? parseInt(pageSize) : PAGE_SIZE_DEFAULT
const totalPage = Math.floor(termCount / intPageSize) +
(termCount % intPageSize > 0 ? 1 : 0)
const currentPage = page ? parseInt(page) : 1
const displayPaging = totalPage > 1
const pageSizeOption = map(PAGE_SIZE_SELECTION, (size) => {
return {label: size, value: size}
})
const headerTitle = project ? 'Project Glossary' : 'Glossary'
let list
/* eslint-disable react/jsx-no-bind */
if (termsLoading) {
list = (<div className='loader-loadingContainer'>
<LoaderText loading loadingText='Loading' />
</div>)
} else if (!termsLoading && termCount) {
list = (<ReactList
useTranslate3d
itemRenderer={::this.renderItem}
length={size(terms)}
type='uniform'
className='reactList'
ref={(c) => { this.list = c }} />)
} else {
list = (<div className='loader-loadingContainer'>
<span className='u-textLoadingMuted'>
<Icon name='glossary' />
</span>
<p className='glossaryText-muted'>No content</p>
</div>)
}
return (
<div>
{notification &&
(<Notification severity={notification.severity}
message={notification.message}
details={notification.details}
show={!!notification} />
)
}
<Helmet title={headerTitle} />
<div className='wideView glossary'>
<ViewHeader title={headerTitle} />
<div className='glossaryHeader'>
<Row>
{termCount > 0 &&
<Row>
<span className='hidden-lesm glossaryRow'>Show</span>
<Select options={pageSizeOption}
placeholder='Terms per page'
value={intPageSize}
name='glossary-page'
className='glossarySelect'
searchable={false}
clearable={false}
onChange={handlePageSizeChange} />
</Row>
}
{displayPaging &&
<div className='u-pullRight glossaryPaging'>
<Button bsStyle='link' disabled={currentPage <= 1}
title='First page'
onClick={() => { gotoFirstPage(currentPage, totalPage) }}>
<Icon name='previous' className='s1' />
</Button>
<Button bsStyle='link' disabled={currentPage <= 1}
title='Previous page'
onClick={
() => { gotoPreviousPage(currentPage, totalPage) }}>
<Icon name='chevron-left' className='s1' />
</Button>
<span className='u-textNeutral-top'>
{currentPage} of {totalPage}
</span>
<Button bsStyle='link' disabled={currentPage === totalPage}
title='Next page'
onClick={() => { gotoNextPage(currentPage, totalPage) }}>
<Icon name='chevron-right' className='s1' />
</Button>
<Button bsStyle='link' disabled={currentPage === totalPage}
title='Last page'
onClick={() => { gotoLastPage(currentPage, totalPage) }}>
<Icon name='next' className='s1' />
</Button>
<span className='textNeutralTotal'
title='Total glossary terms'>
<Row>
<Icon name='glossary' className='s1' /> {termCount}
</Row>
</span>
</div>
}
</Row>
</div>
<div className='glossaryList'>
{list}
</div>
</div>
</div>
)
/* eslint-enable react/jsx-no-bind */
}
}
const mapStateToProps = (state, { location }) => {
const {
selectedTerm,
stats,
terms,
termIds,
filter,
permission,
termsLoading,
termCount,
saving,
deleting,
notification,
project
} = state.glossary
const query = location.query
return {
terms,
termIds,
termCount,
termsLoading,
transLocales: stats.transLocales,
srcLocale: stats.srcLocale,
filterText: filter,
selectedTerm: selectedTerm,
selectedTransLocale: query.locale,
permission,
location: state.routing.location,
saving,
deleting,
notification,
page: query.page,
pageSize: query.size,
project
}
}
const mapDispatchToProps = (dispatch) => {
return {
handleInitLoad: (projectSlug) => {
dispatch(glossaryInitialLoad(projectSlug))
},
handleSelectTerm: (termId) => dispatch(glossarySelectTerm(termId)),
handleTermFieldUpdate: (field, event) => {
dispatch(glossaryUpdateField({ field, value: event.target.value || '' }))
},
handleDeleteTerm: (termId) => dispatch(glossaryDeleteTerm(termId)),
handleResetTerm: (termId) => dispatch(glossaryResetTerm(termId)),
handleUpdateTerm: (term, needRefresh) =>
dispatch(glossaryUpdateTerm(term, needRefresh)),
handlePageSizeChange: (size) =>
dispatch(glossaryUpdatePageSize(size.value)),
gotoFirstPage: (currentPage, totalPage) =>
dispatch(glossaryGoFirstPage(currentPage, totalPage)),
gotoPreviousPage: (currentPage, totalPage) =>
dispatch(glossaryGoPreviousPage(currentPage, totalPage)),
gotoNextPage: (currentPage, totalPage) =>
dispatch(glossaryGoNextPage(currentPage, totalPage)),
gotoLastPage: (currentPage, totalPage) =>
dispatch(glossaryGoLastPage(currentPage, totalPage))
}
}
export default connect(mapStateToProps, mapDispatchToProps)(Glossary)