Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import { useEffect } from "react";
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
import {
faFileCirclePlus,
faFilePen,
faUserGear,
faUserPlus,
faRightFromBracket,
} from "@fortawesome/free-solid-svg-icons";
import { useNavigate, Link, useLocation } from "react-router-dom";
import { useSendLogoutMutation } from "../features/auth/authApiSlice";
const DASH_REGEX = /^\/dash(\/)?$/;
const NOTES_REGEX = /^\/dash\/todos(\/)?$/;
const USERS_REGEX = /^\/dash\/users(\/)?$/;
const DashHeader = () => {
const navigate = useNavigate();
const { pathname } = useLocation();
const [sendLogout, { isLoading, isSuccess, isError, error }] =
useSendLogoutMutation();
useEffect(() => {
if (isSuccess) navigate("/");
}, [isSuccess, navigate]);
const onNewTodoClicked = () => navigate("/dash/todos/new");
const onNewUserClicked = () => navigate("/dash/users/new");
const onTodosClicked = () => navigate("/dash/todos");
const onUsersClicked = () => navigate("/dash/users");
if (isLoading) return <p>Logging Out...</p>;
if (isError) return <p>Error: {error.data?.message}</p>;
let dashClass = null;
if (
!DASH_REGEX.test(pathname) &&
!NOTES_REGEX.test(pathname) &&
!USERS_REGEX.test(pathname)
) {
dashClass = "dash-header__container--small";
}
let newTodoButton = null;
if (NOTES_REGEX.test(pathname)) {
newTodoButton = (
<button
className="icon-button"
title="New Todo"
onClick={onNewTodoClicked}
>
<FontAwesomeIcon icon={faFileCirclePlus} />
</button>
);
}
let newUserButton = null;
if (USERS_REGEX.test(pathname)) {
newUserButton = (
<button
className="icon-button"
title="New User"
onClick={onNewUserClicked}
>
<FontAwesomeIcon icon={faUserPlus} />
</button>
);
}
let userButton = null;
if (!USERS_REGEX.test(pathname) && pathname.includes("/dash")) {
userButton = (
<button className="icon-button" title="Users" onClick={onUsersClicked}>
<FontAwesomeIcon icon={faUserGear} />
</button>
);
}
let todosButton = null;
if (!NOTES_REGEX.test(pathname) && pathname.includes("/dash")) {
todosButton = (
<button className="icon-button" title="Todos" onClick={onTodosClicked}>
<FontAwesomeIcon icon={faFilePen} />
</button>
);
}
const logoutButton = (
<button className="icon-button" title="Logout" onClick={sendLogout}>
<FontAwesomeIcon icon={faRightFromBracket} />
</button>
);
const errClass = isError ? "errmsg" : "offscreen";
let buttonContent;
if (isLoading) {
buttonContent = <p>Logging Out...</p>;
} else {
buttonContent = (
<>
{newTodoButton}
{newUserButton}
{todosButton}
{userButton}
{logoutButton}
</>
);
}
const content = (
<>
<p className={errClass}>{error?.data?.message}</p>
<header className="dash-header">
<div className={`dash-header__container ${dashClass}`}>
<Link to="/dash">
<h1 className="dash-header__title">TODO-APP API</h1>
</Link>
<nav className="dash-header__nav">{buttonContent}</nav>
</div>
</header>
</>
);
return content;
};
export default DashHeader;