1
|
|
|
import React, { Fragment, useState, useEffect } from 'react'; |
2
|
|
|
import { |
3
|
|
|
Breadcrumb, |
4
|
|
|
BreadcrumbItem, |
5
|
|
|
Button, |
6
|
|
|
ButtonGroup, |
7
|
|
|
Card, |
8
|
|
|
CardBody, |
9
|
|
|
Col, |
10
|
|
|
CustomInput, |
11
|
|
|
Row, |
12
|
|
|
Form, |
13
|
|
|
FormGroup, |
14
|
|
|
Input, |
15
|
|
|
Label, |
16
|
|
|
Spinner, |
17
|
|
|
} from 'reactstrap'; |
18
|
|
|
import Loader from '../../common/Loader'; |
19
|
|
|
import useFakeFetch from '../../../hooks/useFakeFetch'; |
20
|
|
|
import { isIterableArray } from '../../../helpers/utils'; |
21
|
|
|
import Flex from '../../common/Flex'; |
22
|
|
|
import Cascader from 'rc-cascader'; |
23
|
|
|
import classNames from 'classnames'; |
24
|
|
|
import EquipmentList from './EquipmentList'; |
25
|
|
|
import EquipmentFooter from './EquipmentFooter'; |
26
|
|
|
import usePagination from '../../../hooks/usePagination'; |
27
|
|
|
import equipments from './equipments'; |
28
|
|
|
import { getCookieValue, createCookie } from '../../../helpers/utils'; |
29
|
|
|
import withRedirect from '../../../hoc/withRedirect'; |
30
|
|
|
import { withTranslation } from 'react-i18next'; |
31
|
|
|
import { toast } from 'react-toastify'; |
32
|
|
|
import { APIBaseURL } from '../../../config'; |
33
|
|
|
|
34
|
|
|
|
35
|
|
|
const SpaceEquipments = ({ setRedirect, setRedirectUrl, t }) => { |
36
|
|
|
useEffect(() => { |
37
|
|
|
let is_logged_in = getCookieValue('is_logged_in'); |
38
|
|
|
let user_name = getCookieValue('user_name'); |
39
|
|
|
let user_display_name = getCookieValue('user_display_name'); |
40
|
|
|
let user_uuid = getCookieValue('user_uuid'); |
41
|
|
|
let token = getCookieValue('token'); |
42
|
|
|
if (is_logged_in === null || !is_logged_in) { |
43
|
|
|
setRedirectUrl(`/authentication/basic/login`); |
44
|
|
|
setRedirect(true); |
45
|
|
|
} else { |
46
|
|
|
//update expires time of cookies |
47
|
|
|
createCookie('is_logged_in', true, 1000 * 60 * 60 * 8); |
48
|
|
|
createCookie('user_name', user_name, 1000 * 60 * 60 * 8); |
49
|
|
|
createCookie('user_display_name', user_display_name, 1000 * 60 * 60 * 8); |
50
|
|
|
createCookie('user_uuid', user_uuid, 1000 * 60 * 60 * 8); |
51
|
|
|
createCookie('token', token, 1000 * 60 * 60 * 8); |
52
|
|
|
} |
53
|
|
|
}); |
54
|
|
|
// State |
55
|
|
|
const [selectedSpaceName, setSelectedSpaceName] = useState(undefined); |
56
|
|
|
const [selectedSpaceID, setSelectedSpaceID] = useState(undefined); |
57
|
|
|
const [equipmentIds, setEquipmentIds] = useState([]); |
58
|
|
|
const [cascaderOptions, setCascaderOptions] = useState(undefined); |
59
|
|
|
|
60
|
|
|
// button |
61
|
|
|
const [submitButtonDisabled, setSubmitButtonDisabled] = useState(true); |
62
|
|
|
const [spinnerHidden, setSpinnerHidden] = useState(true); |
63
|
|
|
|
64
|
|
|
useEffect(() => { |
65
|
|
|
let isResponseOK = false; |
66
|
|
|
fetch(APIBaseURL + '/spaces/tree', { |
67
|
|
|
method: 'GET', |
68
|
|
|
headers: { |
69
|
|
|
"Content-type": "application/json", |
70
|
|
|
"User-UUID": getCookieValue('user_uuid'), |
71
|
|
|
"Token": getCookieValue('token') |
72
|
|
|
}, |
73
|
|
|
body: null, |
74
|
|
|
|
75
|
|
|
}).then(response => { |
76
|
|
|
console.log(response); |
77
|
|
|
if (response.ok) { |
78
|
|
|
isResponseOK = true; |
79
|
|
|
} |
80
|
|
|
return response.json(); |
81
|
|
|
}).then(json => { |
82
|
|
|
console.log(json); |
83
|
|
|
if (isResponseOK) { |
84
|
|
|
// rename keys |
85
|
|
|
json = JSON.parse(JSON.stringify([json]).split('"id":').join('"value":').split('"name":').join('"label":')); |
86
|
|
|
setCascaderOptions(json); |
87
|
|
|
setSelectedSpaceName([json[0]].map(o => o.label)); |
88
|
|
|
setSelectedSpaceID([json[0]].map(o => o.value)); |
89
|
|
|
} else { |
90
|
|
|
toast.error(json.description); |
91
|
|
|
} |
92
|
|
|
}).catch(err => { |
93
|
|
|
console.log(err); |
94
|
|
|
}); |
95
|
|
|
|
96
|
|
|
}, []); |
97
|
|
|
|
98
|
|
|
const labelClasses = 'ls text-uppercase text-600 font-weight-semi-bold mb-0'; |
99
|
|
|
|
100
|
|
|
const sliderSettings = { |
101
|
|
|
infinite: true, |
102
|
|
|
speed: 500, |
103
|
|
|
slidesToShow: 1, |
104
|
|
|
slidesToScroll: 1 |
105
|
|
|
}; |
106
|
|
|
|
107
|
|
|
let onSpaceCascaderChange = (value, selectedOptions) => { |
108
|
|
|
console.log(value, selectedOptions); |
109
|
|
|
setSelectedSpaceName(selectedOptions.map(o => o.label).join('/')); |
110
|
|
|
setSelectedSpaceID(value[value.length - 1]); |
111
|
|
|
} |
112
|
|
|
// Hook |
113
|
|
|
const { loading } = useFakeFetch(equipments); |
114
|
|
|
const { data: paginationData, meta: paginationMeta, handler: paginationHandler } = usePagination(equipmentIds, 4); |
115
|
|
|
const { total, itemsPerPage, from, to } = paginationMeta; |
116
|
|
|
const { perPage } = paginationHandler; |
117
|
|
|
|
118
|
|
|
const isList = true; |
119
|
|
|
const isGrid = false; |
120
|
|
|
|
121
|
|
|
useEffect(() => { |
122
|
|
|
setEquipmentIds(equipments.map(equipment => equipment.id)); |
123
|
|
|
}, []); |
124
|
|
|
|
125
|
|
|
// Handler |
126
|
|
|
const handleSubmit = e => { |
127
|
|
|
e.preventDefault(); |
128
|
|
|
console.log('handleSubmit'); |
129
|
|
|
console.log(selectedSpaceID); |
130
|
|
|
// // disable submit button |
131
|
|
|
// setSubmitButtonDisabled(true); |
132
|
|
|
// // show spinner |
133
|
|
|
// setSpinnerHidden(false); |
134
|
|
|
|
135
|
|
|
// // enable submit button |
136
|
|
|
// setSubmitButtonDisabled(false); |
137
|
|
|
// // hide spinner |
138
|
|
|
// setSpinnerHidden(true); |
139
|
|
|
}; |
140
|
|
|
|
141
|
|
|
return ( |
142
|
|
|
<Fragment> |
143
|
|
|
<div> |
144
|
|
|
<Breadcrumb> |
145
|
|
|
<BreadcrumbItem>{t('Monitoring')}</BreadcrumbItem><BreadcrumbItem active>{t('Space Equipments')}</BreadcrumbItem> |
146
|
|
|
</Breadcrumb> |
147
|
|
|
</div> |
148
|
|
|
<Card className="bg-light mb-3"> |
149
|
|
|
<CardBody className="p-3"> |
150
|
|
|
<Form onSubmit={handleSubmit}> |
151
|
|
|
<Row form> |
152
|
|
|
<Col xs="auto"> |
153
|
|
|
<FormGroup className="form-group"> |
154
|
|
|
<Label className={labelClasses} for="space"> |
155
|
|
|
{t('Space')} |
156
|
|
|
</Label> |
157
|
|
|
<br /> |
158
|
|
|
<Cascader options={cascaderOptions} |
159
|
|
|
onChange={onSpaceCascaderChange} |
160
|
|
|
changeOnSelect |
161
|
|
|
expandTrigger="hover"> |
162
|
|
|
<Input value={selectedSpaceName || ''} readOnly /> |
163
|
|
|
</Cascader> |
164
|
|
|
</FormGroup> |
165
|
|
|
</Col> |
166
|
|
|
<Col xs="auto"> |
167
|
|
|
<FormGroup> |
168
|
|
|
<br></br> |
169
|
|
|
<ButtonGroup id="submit"> |
170
|
|
|
<Button color="success" disabled={submitButtonDisabled} >{t('Submit')}</Button> |
171
|
|
|
</ButtonGroup> |
172
|
|
|
</FormGroup> |
173
|
|
|
</Col> |
174
|
|
|
<Col xs="auto"> |
175
|
|
|
<FormGroup> |
176
|
|
|
<br></br> |
177
|
|
|
<Spinner color="primary" hidden={spinnerHidden} /> |
178
|
|
|
</FormGroup> |
179
|
|
|
</Col> |
180
|
|
|
</Row> |
181
|
|
|
</Form> |
182
|
|
|
</CardBody> |
183
|
|
|
</Card> |
184
|
|
|
|
185
|
|
|
|
186
|
|
|
<Card> |
187
|
|
|
<CardBody className={classNames({ 'p-0 overflow-hidden': isList, 'pb-0': isGrid })}> |
188
|
|
|
{loading ? ( |
189
|
|
|
<Loader /> |
190
|
|
|
) : ( |
191
|
|
|
<Row noGutters={isList}> |
192
|
|
|
{isIterableArray(equipments) && |
193
|
|
|
equipments |
194
|
|
|
.filter(equipment => paginationData.includes(equipment.id)) |
195
|
|
|
.map((equipment, index) => <EquipmentList {...equipment} sliderSettings={sliderSettings} key={equipment.id} index={index} />)} |
196
|
|
|
</Row> |
197
|
|
|
)} |
198
|
|
|
</CardBody> |
199
|
|
|
<EquipmentFooter meta={paginationMeta} handler={paginationHandler} /> |
200
|
|
|
</Card> |
201
|
|
|
<Card className="mb-3"> |
202
|
|
|
<CardBody> |
203
|
|
|
<Row className="justify-content-between align-items-center"> |
204
|
|
|
<Col sm="auto" className="mb-2 mb-sm-0" tag={Flex} align="center"> |
205
|
|
|
<h6 className="mb-0 text-nowrap ml-2"> |
206
|
|
|
{t('Show Up to')} |
207
|
|
|
</h6> |
208
|
|
|
<CustomInput |
209
|
|
|
id="itemsPerPage" |
210
|
|
|
type="select" |
211
|
|
|
bsSize="sm" |
212
|
|
|
value={itemsPerPage} |
213
|
|
|
onChange={({ target }) => perPage(Number(target.value))} |
214
|
|
|
> |
215
|
|
|
<option value={2}>2</option> |
216
|
|
|
<option value={4}>4</option> |
217
|
|
|
<option value={6}>6</option> |
218
|
|
|
<option value={total}>{t('All')}</option> |
219
|
|
|
</CustomInput> |
220
|
|
|
<h6 className="mb-0 text-nowrap ml-2"> |
221
|
|
|
{t('FROM - TO of TOTAL Equipments', { 'FROM': from, 'TO': to, 'TOTAL': total })} |
222
|
|
|
</h6> |
223
|
|
|
</Col> |
224
|
|
|
|
225
|
|
|
</Row> |
226
|
|
|
</CardBody> |
227
|
|
|
</Card> |
228
|
|
|
</Fragment> |
229
|
|
|
); |
230
|
|
|
}; |
231
|
|
|
|
232
|
|
|
export default withTranslation()(withRedirect(SpaceEquipments)); |
233
|
|
|
|