src/components/page/Header.js   A
last analyzed

Complexity

Total Complexity 7
Complexity/F 1.75

Size

Lines of Code 94
Function Count 4

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 7
eloc 76
mnd 3
bc 3
fnc 4
dl 0
loc 94
rs 10
bpm 0.75
cpm 1.75
noi 0
c 0
b 0
f 0

4 Functions

Rating   Name   Duplication   Size   Complexity  
A Header.checkLogin 0 14 2
A Header.render 0 37 3
A Header.showContent 0 7 1
A Header.componentDidMount 0 5 1
1
/*eslint max-len: ["error", { "code": 200 }]*/
2
3
import React, { Component } from 'react';
4
import { NavLink } from 'react-router-dom';
5
import logo from '../../assets/img/logo.jpg';
6
import base from '../../config/api.js';
7
8
let api = base.api();
9
10
class Header extends Component {
11
    constructor(props) {
12
        super(props);
13
        this.state = {
14
            name: "",
15
            course: "",
16
            activeUser: ""
17
        };
18
    }
19
    componentDidMount() {
20
        this.checkLogin();
21
        fetch(api)
22
            .then(res => res.json())
23
            .then(res => this.showContent(res));
24
    }
25
26
    showContent(res) {
27
        let data = res.data.me[0];
28
29
        this.setState({
30
            name: data.name,
31
            course: data.course
32
        });
33
    }
34
35
    checkLogin() {
36
        if (localStorage.getItem("activeUser") === null) {
37
            console.log("Normal menu");
38
        } else {
39
            console.log("Admin menu");
40
            this.setState({
41
                activeUser: [
42
                    <nav className="navbar_main admin-nav" key="main-nav">
43
                        <ul>
44
                            <li><NavLink to="/reports/create" activeClassName="selected">Create</NavLink ></li>
45
                            <li><NavLink to="/reports/edit" activeClassName="selected">Edit</NavLink ></li>
46
                        </ul>
47
                    </nav>
48
                ]
49
            });
50
        }
51
    }
52
53
    render() {
54
        const checkActive = (match, location) => {
55
            if (!location) {
56
                return false;
57
            }
58
            const {pathname} = location;
59
60
            return pathname === "/";
61
        };
62
63
        const checkActiveReport = (match, location) => {
64
            if (!location) {
65
                return false;
66
            }
67
            const {pathname} = location;
68
            var splitLink = pathname.split("/")[1];
69
70
            return splitLink === "reports";
71
        };
72
73
        return (
74
            <header className="site-header">
75
                <img src={logo} className="logo" alt="logo" />
76
                <span className="site-title">{ this.state.course } Me-Sida för { this.state.name }</span>
77
                <nav className="navbar_main">
78
                    <ul>
79
                        <li><NavLink to="/" activeClassName="selected" isActive={checkActive}>Me</NavLink ></li>
80
                        <li><NavLink to={{pathname: "/reports/week/1", state: { kmom: "1" }}} activeClassName="selected" isActive={checkActiveReport}>Redovisning</NavLink></li>
81
                        <li><NavLink to="/chat" activeClassName="selected">Chat</NavLink ></li>
82
                        <li><NavLink to="/register" activeClassName="selected">Register</NavLink ></li>
83
                        <li><NavLink to="/login" activeClassName="selected">Login</NavLink ></li>
84
                        <li><NavLink to="/profile" activeClassName="selected">Profile</NavLink ></li>
85
                    </ul>
86
                </nav>
87
                { this.state.activeUser }
88
            </header>
89
        );
90
    }
91
}
92
93
export default Header;
94