From 8f4e3980c7a497aed2ac8768bf0e0afbfef1b7ae Mon Sep 17 00:00:00 2001 From: hippoz Date: Mon, 4 Oct 2021 22:35:40 +0300 Subject: [PATCH] improvement!: clean up and optimize imports --- src/api/authenticator.js | 14 +++++--------- src/api/gateway/GatewayConnection.js | 2 +- src/api/gateway/globalGatewayConnection.js | 2 +- src/api/request.js | 8 ++++---- src/api/tokenManager.js | 9 +++++---- src/common/util/{Errors.js => errors.js} | 0 src/common/util/{Logger.js => logger.js} | 0 src/components/FullMessage.js | 12 +++++++----- src/components/auth/Create.js | 3 +-- src/components/auth/Login.js | 10 +++++----- src/components/channel/ChannelButton.js | 4 ++-- src/components/channel/ChannelList.js | 4 ++-- src/components/channel/ChannelMessageView.js | 3 ++- src/components/channel/ChannelView.js | 6 +++--- src/components/main/App.js | 6 +++--- src/components/main/LoggedInMount.js | 3 ++- src/components/user/UserView.js | 8 ++++---- 17 files changed, 47 insertions(+), 47 deletions(-) rename src/common/util/{Errors.js => errors.js} (100%) rename src/common/util/{Logger.js => logger.js} (100%) diff --git a/src/api/authenticator.js b/src/api/authenticator.js index 424120d..e98f807 100644 --- a/src/api/authenticator.js +++ b/src/api/authenticator.js @@ -1,14 +1,10 @@ -import Logger from '../common/util/Logger'; +import Logger from '../common/util/logger'; import gateway from "./gateway/globalGatewayConnection"; -import token from "./tokenManager"; +import { getToken } from "./tokenManager"; const { log: authLog } = Logger([ 'Authenticator' ]); -const Authenticator = { - login: async function() { - authLog('Logging in through gateway...'); - return gateway.connect(token.getToken()); - } +export function login() { + authLog('Logging in through gateway...'); + return gateway.connect(getToken()); }; - -export default Authenticator; \ No newline at end of file diff --git a/src/api/gateway/GatewayConnection.js b/src/api/gateway/GatewayConnection.js index 32a5b3f..d239d9d 100644 --- a/src/api/gateway/GatewayConnection.js +++ b/src/api/gateway/GatewayConnection.js @@ -1,4 +1,4 @@ -import logger from "../../common/util/Logger"; +import logger from "../../common/util/logger"; const { log: logGateway } = logger([ "Gateway" ]); const { log: logRtc } = logger([ "Gateway", "RTC" ]); diff --git a/src/api/gateway/globalGatewayConnection.js b/src/api/gateway/globalGatewayConnection.js index 22fa09e..5de06ec 100644 --- a/src/api/gateway/globalGatewayConnection.js +++ b/src/api/gateway/globalGatewayConnection.js @@ -1,7 +1,7 @@ import GatewayConnection from './GatewayConnection'; import config from '../../config'; import store from '../../common/store'; -import logger from '../../common/util/Logger'; +import logger from '../../common/util/logger'; const { log } = logger(["globalGatewayConnection"]); const { warn: experimentsWarn } = logger(["globalGatewayConnection", "Experiments"]); diff --git a/src/api/request.js b/src/api/request.js index 1f946bc..b7a836c 100644 --- a/src/api/request.js +++ b/src/api/request.js @@ -1,7 +1,7 @@ import config from '../config'; -import token from "./tokenManager"; +import { getToken } from "./tokenManager"; -async function APIRequest(endpoint, options) { +export async function APIRequest(endpoint, options) { let res; let json; let isOK = false; @@ -28,7 +28,7 @@ async function APIRequest(endpoint, options) { return { res, json, isOK, err }; } -APIRequest.authenticated = async function(endpoint, options) { +export async function authenticated(endpoint, options) { let res; let json; let isOK = false; @@ -38,7 +38,7 @@ APIRequest.authenticated = async function(endpoint, options) { if (!options.headers) options.headers = {}; options.headers = { - "Authorization": token.getToken(), + "Authorization": getToken(), ...options.headers }; diff --git a/src/api/tokenManager.js b/src/api/tokenManager.js index 87b2199..b552dff 100644 --- a/src/api/tokenManager.js +++ b/src/api/tokenManager.js @@ -1,6 +1,7 @@ -const tokenManager = { - getToken: () => localStorage.getItem("token"), - setToken: (token) => localStorage.setItem("token", token) +export function getToken() { + return localStorage.getItem("token"); }; -export default tokenManager; \ No newline at end of file +export function setToken(token) { + localStorage.setItem("token", token); +}; diff --git a/src/common/util/Errors.js b/src/common/util/errors.js similarity index 100% rename from src/common/util/Errors.js rename to src/common/util/errors.js diff --git a/src/common/util/Logger.js b/src/common/util/logger.js similarity index 100% rename from src/common/util/Logger.js rename to src/common/util/logger.js diff --git a/src/components/FullMessage.js b/src/components/FullMessage.js index 7043a57..27736fe 100644 --- a/src/components/FullMessage.js +++ b/src/components/FullMessage.js @@ -1,11 +1,13 @@ export default function FullMessage({ glyph, title, content }) { - return (
+ return ( +
- { glyph } + {glyph}
- { title } + {title}
- { content } + {content}
-
) +
+ ); } \ No newline at end of file diff --git a/src/components/auth/Create.js b/src/components/auth/Create.js index e980e8a..a457432 100644 --- a/src/components/auth/Create.js +++ b/src/components/auth/Create.js @@ -3,7 +3,7 @@ import { useHistory } from 'react-router-dom'; import Notification from '../Notification'; import APIRequest from '../../api/request'; -import { getSignupMessageFromError } from '../../common/util/Errors' +import { getSignupMessageFromError } from '../../common/util/errors' export default function Create() { const history = useHistory(); @@ -11,7 +11,6 @@ export default function Create() { const [ usernameInput, setUsernameInput ] = useState(); const [ passwordInput, setPasswordInput ] = useState(); const [ specialCodeInput, setSpecialCodeInput ] = useState(); - const [ specialCodePrompt, setSpecialCodePrompt ] = useState(); const [ info, setInfo ] = useState(); diff --git a/src/components/auth/Login.js b/src/components/auth/Login.js index f3cff1b..662f654 100644 --- a/src/components/auth/Login.js +++ b/src/components/auth/Login.js @@ -3,9 +3,9 @@ import { useHistory } from 'react-router-dom'; import Notification from '../Notification'; import APIRequest from '../../api/request'; -import Authenticator from '../../api/authenticator'; -import token from "../../api/tokenManager"; -import { getLoginMessageFromError } from '../../common/util/Errors' +import { login } from '../../api/authenticator'; +import { setToken } from "../../api/tokenManager"; +import { getLoginMessageFromError } from '../../common/util/errors' export default function Login() { const history = useHistory(); @@ -42,8 +42,8 @@ export default function Login() { return; } - token.setToken(json.token); - await Authenticator.login(); + setToken(json.token); + await login(); history.push('/'); } diff --git a/src/components/channel/ChannelButton.js b/src/components/channel/ChannelButton.js index 7748b87..1dcdb98 100644 --- a/src/components/channel/ChannelButton.js +++ b/src/components/channel/ChannelButton.js @@ -1,8 +1,8 @@ +import { useHistory } from 'react-router-dom'; + import ChannelProfile from './ChannelProfileLink'; import gatewayConnection from '../../api/gateway/globalGatewayConnection'; -import { useHistory } from 'react-router-dom'; - export default function ChannelButton({ channel, selected }) { const history = useHistory(); diff --git a/src/components/channel/ChannelList.js b/src/components/channel/ChannelList.js index 4fc5eb5..18fec4f 100644 --- a/src/components/channel/ChannelList.js +++ b/src/components/channel/ChannelList.js @@ -1,8 +1,8 @@ +import { connect } from 'react-redux' + import ChannelListLoader from './ChannelListLoader'; import ChannelButton from './ChannelButton'; -import { connect } from 'react-redux' - function ChannelList({ selectedChannelId, channels }) { if (!channels) { return ( diff --git a/src/components/channel/ChannelMessageView.js b/src/components/channel/ChannelMessageView.js index 27ac1bb..ba31574 100644 --- a/src/components/channel/ChannelMessageView.js +++ b/src/components/channel/ChannelMessageView.js @@ -1,6 +1,7 @@ -import Message from "../Message"; import { useRef, useEffect } from 'react'; +import Message from "../Message"; + export default function ChannelMessageView({messages}) { const invisibleBottomMessageRef = useRef(); diff --git a/src/components/channel/ChannelView.js b/src/components/channel/ChannelView.js index f51b270..2801c2b 100644 --- a/src/components/channel/ChannelView.js +++ b/src/components/channel/ChannelView.js @@ -1,10 +1,10 @@ +import { connect, useDispatch } from 'react-redux'; +import { useState, useRef, useEffect } from 'react'; + import ChannelProfile from './ChannelProfileLink'; import gatewayConnection from '../../api/gateway/globalGatewayConnection'; import ChannelUserList from "./ChannelUserList"; import ProfileLinkLoader from "../ProfileLinkLoader"; - -import { connect, useDispatch } from 'react-redux'; -import { useState, useRef, useEffect } from 'react'; import ChannelMessageView from './ChannelMessageView'; const ChannelView = ({ messages, channel, channelPresenceClientList }) => { diff --git a/src/components/main/App.js b/src/components/main/App.js index 665ec34..3fa7bf0 100644 --- a/src/components/main/App.js +++ b/src/components/main/App.js @@ -2,12 +2,12 @@ import { useEffect, useState } from 'react'; import { connect } from 'react-redux' import { BrowserRouter, Switch, Route } from 'react-router-dom'; +import './../../styles/App.scss'; import Login from '../auth/Login'; import Create from '../auth/Create'; import Root from '../main/Root'; -import Authenticator from '../../api/authenticator'; +import { login } from '../../api/authenticator'; import Notification from '../Notification'; -import './../../styles/App.scss'; import LoggedInMount from './LoggedInMount'; import FullMessage from '../FullMessage'; @@ -16,7 +16,7 @@ function App({ user, fullscreenMessage }) { const [ hasError ] = useState(false); useEffect(() => { - Authenticator.login(); + login(); }, []); if (user === null && !hasError) { diff --git a/src/components/main/LoggedInMount.js b/src/components/main/LoggedInMount.js index 787361f..17395ef 100644 --- a/src/components/main/LoggedInMount.js +++ b/src/components/main/LoggedInMount.js @@ -1,9 +1,10 @@ +import { connect } from "react-redux"; import { useParams } from "react-router-dom"; + import Sidebar from "../Sidebar"; import ChannelView from "../channel/ChannelView"; import GradientBanner from "../GradientBanner"; import UserView from "../user/UserView"; -import { connect } from "react-redux"; function LoggedInMount({ gradientBannerNotificationText }) { const { channelId, userId } = useParams(); diff --git a/src/components/user/UserView.js b/src/components/user/UserView.js index dc59059..452227a 100644 --- a/src/components/user/UserView.js +++ b/src/components/user/UserView.js @@ -1,5 +1,5 @@ import { useEffect, useState } from "react"; -import APIRequest from "../../api/request"; +import { authenticated } from "../../api/request"; import UserProfile from "./UserProfileLink"; import ProfileLinkLoader from "../ProfileLinkLoader"; @@ -7,7 +7,7 @@ export default function UserView({ userId }) { const [userObject, setUserObject] = useState(null); useEffect(() => { - APIRequest.authenticated(`/api/v1/users/user/${userId}/info`, { + authenticated(`/api/v1/users/user/${userId}/info`, { method: 'GET', headers: { "Accept": "application/json" @@ -23,8 +23,8 @@ export default function UserView({ userId }) { if (userObject) { view = <> - {(userObject.role === "ADMIN") && Admin} - {(userObject.role === "USER") && User} + {(userObject.role === "ADMIN") && Admin} + {(userObject.role === "USER") && User} } else { view = <>