Passed
Push — master ( 669be3...4762c7 )
by Guangyu
19:25 queued 10s
created

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

Complexity

Total Complexity 4
Complexity/F 0

Size

Lines of Code 170
Function Count 0

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 4
eloc 145
mnd 4
bc 4
fnc 0
dl 0
loc 170
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
  Card,
6
  CardBody,
7
  Col,
8
  CustomInput,
9
  Form,
10
  FormGroup,
11
  Label,
12
  Row,
13
  Spinner,
14
} from 'reactstrap';
15
import RealtimeChart from './RealtimeChart';
16
import { getCookieValue, createCookie } from '../../../helpers/utils';
17
import withRedirect from '../../../hoc/withRedirect';
18
import { withTranslation } from 'react-i18next';
19
import uuid from 'uuid/v1';
20
import { toast } from 'react-toastify';
21
import { APIBaseURL } from '../../../config';
22
23
24
const DistributionSystem = ({ setRedirect, setRedirectUrl, t }) => {
25
26
  useEffect(() => {
27
    let is_logged_in = getCookieValue('is_logged_in');
28
    let user_name = getCookieValue('user_name');
29
    let user_display_name = getCookieValue('user_display_name');
30
    let user_uuid = getCookieValue('user_uuid');
31
    let token = getCookieValue('token');
32
    if (is_logged_in === null || !is_logged_in) {
33
      setRedirectUrl(`/authentication/basic/login`);
34
      setRedirect(true);
35
    } else {
36
      //update expires time of cookies
37
      createCookie('is_logged_in', true, 1000 * 60 * 60 * 8);
38
      createCookie('user_name', user_name, 1000 * 60 * 60 * 8);
39
      createCookie('user_display_name', user_display_name, 1000 * 60 * 60 * 8);
40
      createCookie('user_uuid', user_uuid, 1000 * 60 * 60 * 8);
41
      createCookie('token', token, 1000 * 60 * 60 * 8);
42
    }
43
  });
44
  let table = createRef();
45
  // State
46
  // Query Parameters
47
  const [distributionSystemList, setDistributionSystemList] = useState([]);
48
  const [selectedDistributionSystemName, setSelectedDistributionSystemName] = useState(undefined);
49
  const [selectedDistributionSystemID, setSelectedDistributionSystemID] = useState(undefined);
50
  
51
  //Results
52
  const [images, setImages] = useState([]);
53
  const [spinnerHidden, setSpinnerHidden] = useState(false);
54
55
  useEffect(() => {
56
    let isResponseOK = false;
57
    fetch(APIBaseURL + '/distributionsystems', {
58
      method: 'GET',
59
      headers: {
60
        "Content-type": "application/json",
61
        "User-UUID": getCookieValue('user_uuid'),
62
        "Token": getCookieValue('token')
63
      },
64
      body: null,
65
66
    }).then(response => {
67
      console.log(response);
68
      if (response.ok) {
69
        isResponseOK = true;
70
      }
71
      return response.json();
72
    }).then(json => {
73
      console.log(json);
74
      if (isResponseOK) {
75
        // rename keys 
76
        json = JSON.parse(JSON.stringify(json).split('"id":').join('"value":').split('"name":').join('"label":'));
77
        
78
        console.log(json);
79
        setDistributionSystemList(json);
80
        setSelectedDistributionSystemID([json[0]].map(o => o.value));
81
        setSelectedDistributionSystemName([json[0]].map(o => o.label));
82
        
83
        let images = {};
84
        json.forEach((currentValue, index) => {
85
          images[currentValue['value']] = {__html: currentValue['svg']}
86
        });
87
        setImages(images);
88
        setSpinnerHidden(true);
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
  let onDistributionSystemChange = (event) => {
101
    // console.log('onDistributionSystemChange');
102
    // console.log(event.target.value);
103
104
    setSelectedDistributionSystemID(event.target.value);
105
    distributionSystemList.forEach((currentItem, index) => {
106
      if (currentItem['value'] == event.target.value) {
107
        setSelectedDistributionSystemName(currentItem['label']);
108
      }
109
    });
110
    
111
  }
112
113
114
  return (
115
    <Fragment>
116
      <div>
117
        <Breadcrumb>
118
          <BreadcrumbItem>{t('Auxiliary System')}</BreadcrumbItem><BreadcrumbItem active>{t('Distribution System')}</BreadcrumbItem>
119
        </Breadcrumb>
120
      </div>
121
      <Card className="bg-light mb-3">
122
        <CardBody className="p-3">
123
          <Form >
124
            <Row form>
125
              <Col xs="auto">
126
                <FormGroup>
127
                  <Label className={labelClasses} for="distributionSystemSelect">
128
                    {t('Distribution System')}
129
                  </Label>
130
                  <CustomInput type="select" id="distributionSystemSelect" name="distributionSystemSelect"
131
                    value={selectedDistributionSystemID} onChange={onDistributionSystemChange}
132
                  >
133
                    {distributionSystemList.map((distributionSystem, index) => (
134
                      <option value={distributionSystem.value} key={distributionSystem.value}>
135
                        {distributionSystem.label}
136
                      </option>
137
                    ))}
138
                  </CustomInput>
139
                </FormGroup>
140
              </Col>
141
              <Col xs="auto">
142
                <FormGroup>
143
                  <br></br>
144
                  <Spinner color="primary" hidden={spinnerHidden}  />
145
                </FormGroup>
146
              </Col>              
147
            </Row>
148
          </Form>
149
        </CardBody>
150
      </Card>
151
      <Row noGutters>
152
        
153
        <Col lg="4" className="pr-lg-2" key={uuid()}>
154
          <RealtimeChart 
155
            distributionSystemID={selectedDistributionSystemID} 
156
            distributionSystemName={selectedDistributionSystemName}  
157
          />
158
        </Col>
159
        
160
        <Col lg="8" className="pr-lg-2">
161
          <div dangerouslySetInnerHTML={images[selectedDistributionSystemID]} />
162
        </Col>
163
164
      </Row>
165
    </Fragment>
166
  );
167
};
168
169
export default withTranslation()(withRedirect(DistributionSystem));
170