Passed
Pull Request — master (#2)
by
unknown
03:55
created

src/components/MyEMS/AuxiliarySystem/DistributionSystem.js   A

Complexity

Total Complexity 5
Complexity/F 0

Size

Lines of Code 209
Function Count 0

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 5
eloc 179
mnd 5
bc 5
fnc 0
dl 0
loc 209
rs 10
bpm 0
cpm 0
noi 0
c 0
b 0
f 0
1
import React, { createRef, Fragment, useState, useEffect } from 'react';
2
import {
3
  Breadcrumb,
4
  BreadcrumbItem,
5
  Button,
6
  ButtonGroup,
7
  Card,
8
  CardBody,
9
  Col,
10
  CustomInput,
11
  Form,
12
  FormGroup,
13
  Input,
14
  Label,
15
  Row,
16
} from 'reactstrap';
17
import Cascader from 'rc-cascader';
18
import RealtimeChart from './RealtimeChart';
19
import LightBoxGallery from '../../common/LightBoxGallery';
20
import img1 from './distribution.svg';
21
import { getCookieValue, createCookie } from '../../../helpers/utils';
22
import withRedirect from '../../../hoc/withRedirect';
23
import { withTranslation } from 'react-i18next';
24
import { toast } from 'react-toastify';
25
import { APIBaseURL } from '../../../config';
26
27
28
const DistributionSystem = ({ setRedirect, setRedirectUrl, t }) => {
29
30
  useEffect(() => {
31
    let is_logged_in = getCookieValue('is_logged_in');
32
    let user_name = getCookieValue('user_name');
33
    let user_display_name = getCookieValue('user_display_name');
34
    let user_uuid = getCookieValue('user_uuid');
35
    let token = getCookieValue('token');
36
    if (is_logged_in === null || !is_logged_in) {
37
      setRedirectUrl(`/authentication/basic/login`);
38
      setRedirect(true);
39
    } else {
40
      //update expires time of cookies
41
      createCookie('is_logged_in', true, 1000 * 60 * 60 * 8);
42
      createCookie('user_name', user_name, 1000 * 60 * 60 * 8);
43
      createCookie('user_display_name', user_display_name, 1000 * 60 * 60 * 8);
44
      createCookie('user_uuid', user_uuid, 1000 * 60 * 60 * 8);
45
      createCookie('token', token, 1000 * 60 * 60 * 8);
46
    }
47
  });
48
  let table = createRef();
49
  // State
50
  // Query Parameters
51
  const [distributionSystemList, setDistributionSystemList] = useState([]);
52
  const [selectedDistributionSystem, setSelectedDistributionSystem] = useState(undefined);
53
  const [isDisabled, setIsDisabled] = useState(true);
54
  //Results
55
  const [realtimeChartOptions, setRealtimeChartOptions] = useState([]);
56
  const [realtimeChartData, setRealtimeChartData] = useState([]);
57
  const [images, setImages] = useState([]);
58
59
  useEffect(() => {
60
    let isResponseOK = false;
61
    fetch(APIBaseURL + '/distributionsystems', {
62
      method: 'GET',
63
      headers: {
64
        "Content-type": "application/json",
65
        "User-UUID": getCookieValue('user_uuid'),
66
        "Token": getCookieValue('token')
67
      },
68
      body: null,
69
70
    }).then(response => {
71
      console.log(response);
72
      if (response.ok) {
73
        isResponseOK = true;
74
      }
75
      return response.json();
76
    }).then(json => {
77
      console.log(json);
78
      if (isResponseOK) {
79
        // rename keys 
80
        json = JSON.parse(JSON.stringify(json).split('"id":').join('"value":').split('"name":').join('"label":'));
81
        console.log(json);
82
        setDistributionSystemList(json);
83
        setSelectedDistributionSystem([json[0]].map(o => o.value));
84
        setIsDisabled(false);
85
      } else {
86
        toast.error(json.description);
87
      }
88
    }).catch(err => {
89
      console.log(err);
90
    });
91
92
  }, []);
93
94
  const labelClasses = 'ls text-uppercase text-600 font-weight-semi-bold mb-0';
95
96
  
97
98
99
  // Handler
100
  const handleSubmit = e => {
101
    e.preventDefault();
102
    console.log('handleSubmit');
103
    console.log(selectedDistributionSystem);
104
105
    let isResponseOK = false;
106
    fetch(APIBaseURL + '/reports/auxiliarysystemdistributionsystem?' +
107
      'distributionsystemid=' + selectedDistributionSystem, {
108
      method: 'GET',
109
      headers: {
110
        "Content-type": "application/json",
111
        "User-UUID": getCookieValue('user_uuid'),
112
        "Token": getCookieValue('token')
113
      },
114
      body: null,
115
116
    }).then(response => {
117
      if (response.ok) {
118
        isResponseOK = true;
119
      }
120
      return response.json();
121
    }).then(json => {
122
      if (isResponseOK) {
123
        console.log(json)
124
        
125
        setRealtimeChartOptions([
126
          { value: 'a', label: '主进线' },
127
          { value: 'b', label: '地源热泵空调总表' },
128
          { value: 'c', label: '消防栓机械泵2路' },
129
          { value: 'd', label: '一层南' },
130
          { value: 'e', label: '一层北' }
131
        ]);
132
133
        setRealtimeChartData([]);
134
135
        setImages([img1]);
136
        
137
      } else {
138
        toast.error(json.description)
139
      }
140
    }).catch(err => {
141
      console.log(err);
142
    });
143
  };
144
145
  return (
146
    <Fragment>
147
      <div>
148
        <Breadcrumb>
149
          <BreadcrumbItem>{t('Auxiliary System')}</BreadcrumbItem><BreadcrumbItem active>{t('Distribution System')}</BreadcrumbItem>
150
        </Breadcrumb>
151
      </div>
152
      <Card className="bg-light mb-3">
153
        <CardBody className="p-3">
154
          <Form onSubmit={handleSubmit}>
155
            <Row form>
156
              <Col xs="auto">
157
                <FormGroup>
158
                  <Label className={labelClasses} for="distributionSystemSelect">
159
                    {t('Distribution System')}
160
                  </Label>
161
                  <CustomInput type="select" id="distributionSystemSelect" name="distributionSystemSelect"
162
                    value={selectedDistributionSystem} onChange={({ target }) => setSelectedDistributionSystem(target.value)}
163
                  >
164
                    {distributionSystemList.map((distributionSystem, index) => (
165
                      <option value={distributionSystem.value} key={index}>
166
                        {distributionSystem.label}
167
                      </option>
168
                    ))}
169
                  </CustomInput>
170
                </FormGroup>
171
              </Col>
172
              <Col xs="auto">
173
                <FormGroup>
174
                  <br></br>
175
                  <ButtonGroup id="submit">
176
                    <Button color="success" disabled={isDisabled} >{t('Submit')}</Button>
177
                  </ButtonGroup>
178
                </FormGroup>
179
              </Col>
180
            </Row>
181
          </Form>
182
        </CardBody>
183
      </Card>
184
      <Row noGutters>
185
        <Col lg="3" className="pr-lg-2">
186
          <RealtimeChart options={realtimeChartOptions} />
187
        </Col>
188
        <Col lg="9" className="pr-lg-2">
189
          <LightBoxGallery images={images}>
190
            {openImgIndex => (
191
              <img
192
                className="rounded w-100 cursor-pointer"
193
                src={images[0]}
194
                alt=""
195
                onClick={() => {
196
                  openImgIndex(0);
197
                }}
198
              />
199
            )}
200
          </LightBoxGallery>
201
        </Col>
202
203
      </Row>
204
    </Fragment>
205
  );
206
};
207
208
export default withTranslation()(withRedirect(DistributionSystem));
209