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

src/components/MyEMS/auth/LogoutContent.js   A

Complexity

Total Complexity 2
Complexity/F 0

Size

Lines of Code 74
Function Count 0

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 2
eloc 62
mnd 2
bc 2
fnc 0
dl 0
loc 74
rs 10
bpm 0
cpm 0
noi 0
c 0
b 0
f 0
1
import React, { Fragment, useState, useEffect } from 'react';
2
import PropTypes from 'prop-types';
3
import { Button } from 'reactstrap';
4
import { Link } from 'react-router-dom';
5
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
6
import { toast } from 'react-toastify';
7
import { createCookie, getCookieValue } from '../../../helpers/utils';
8
import rocket from '../../../assets/img/illustrations/rocket.png';
9
import { withTranslation } from 'react-i18next';
10
import { APIBaseURL } from '../../../config';
11
12
13
const LogoutContent = ({ layout, titleTag: TitleTag, t }) => {
14
15
  useEffect(() => {
16
    let isResponseOK = false;
17
    fetch(APIBaseURL + '/users/logout', {
18
      method: 'PUT',
19
      headers: {
20
        "Content-type": "application/json",
21
        "User-UUID": getCookieValue('user_uuid'),
22
        "Token": getCookieValue('token')
23
      },
24
      body: null,
25
    }).then(response => {
26
      console.log(response)
27
      if (response.ok) {
28
        isResponseOK = true;
29
      }
30
      return response.json();
31
    }).then(json => {
32
      console.log(json)
33
      if (isResponseOK) {
34
        createCookie('user_name', '', 0);
35
        createCookie('user_display_name', '', 0);
36
        createCookie('user_uuid', '', 0);
37
        createCookie('token', '', 0);
38
        createCookie('is_logged_in', false, 0);
39
      } else {
40
        toast.error(json.description)
41
      }
42
    }).catch(err => {
43
      console.log(err);
44
    });
45
46
  });
47
48
  return (
49
    <Fragment>
50
      <img className="d-block mx-auto mb-4" src={rocket} alt="shield" width={70} />
51
      <TitleTag>{t('Thanks for using MyEMS!')}</TitleTag>
52
      <p>
53
        {t('You are now successfully signed out.')}
54
      </p>
55
      <Button tag={Link} color="primary" size="sm" className="mt-3" to={`/authentication/${layout}/login`}>
56
        <FontAwesomeIcon icon="chevron-left" transform="shrink-4 down-1" className="mr-1" />
57
        {t('Return to Login')}
58
      </Button>
59
    </Fragment>
60
  );
61
};
62
63
LogoutContent.propTypes = {
64
  layout: PropTypes.string,
65
  titleTag: PropTypes.string
66
};
67
68
LogoutContent.defaultProps = {
69
  layout: 'basic',
70
  titleTag: 'h4'
71
};
72
73
export default withTranslation()(LogoutContent);
74