PluralKit/src/App.js

147 lines
5.1 KiB
JavaScript
Raw Normal View History

2020-12-13 23:38:10 +01:00
import React, {useEffect, useState, useCallback} from 'react';
2020-12-09 09:15:55 +01:00
import { Router, Switch, Route, Redirect } from 'react-router-dom';
import * as BS from 'react-bootstrap'
import { useForm } from "react-hook-form";
import * as fetch from 'node-fetch';
2020-12-13 23:38:10 +01:00
import Toggle from 'react-toggle'
2020-12-09 09:15:55 +01:00
2020-12-09 17:45:11 +01:00
import './App.scss';
2020-12-09 09:15:55 +01:00
import 'bootstrap/dist/css/bootstrap.min.css';
import { FaLock } from "react-icons/fa";
2020-12-13 23:38:10 +01:00
import { FaCog } from "react-icons/fa";
2020-12-09 09:15:55 +01:00
import Dash from './Components/Dash.js'
import history from "./History.js";
import Loading from "./Components/Loading.js";
import Navigation from "./Components/Navigation.js";
2020-12-12 19:19:22 +01:00
import Footer from './Components/Footer.js'
2020-12-13 23:38:10 +01:00
import Profile from './Components/Profile.js'
2020-12-09 09:15:55 +01:00
import API_URL from "./Constants/constants.js";
export default function App() {
const [isLoading, setIsLoading ] = useState(false);
const [isSubmit, setIsSubmit ] = useState(false);
const [isInvalid, setIsInvalid] = useState(false);
2020-12-13 23:38:10 +01:00
const [, updateState] = useState();
const forceUpdate = useCallback(() => updateState({}), []);
2020-12-09 09:15:55 +01:00
const { register, handleSubmit } = useForm();
useEffect(() => {
if (localStorage.getItem('token')) {
logIn();
}
}, [])
const onSubmit = data => {
localStorage.setItem('token', data.pkToken);
logIn();
};
2020-12-13 23:38:10 +01:00
2020-12-09 09:15:55 +01:00
function logIn() {
setIsInvalid(false);
setIsLoading(true);
fetch(`${API_URL}s/`,{
2020-12-11 00:53:43 +01:00
method: 'GET',
2020-12-09 09:15:55 +01:00
headers: {
'Authorization': JSON.stringify(localStorage.getItem("token")).slice(1, -1)
}}).then ( res => res.json()
).then (data => {
localStorage.setItem('user', JSON.stringify(data));
setIsSubmit(true);
setIsLoading(false);
2020-12-09 10:23:21 +01:00
history.push("/pk-webs/dash");
2020-12-09 09:15:55 +01:00
})
.catch (error => {
console.log(error);
setIsInvalid(true);
localStorage.removeItem('token');
localStorage.removeItem('user');
setIsLoading(false);
})
};
2020-12-09 17:45:11 +01:00
return (
2020-12-13 23:38:10 +01:00
<div className={localStorage.getItem('opendyslexic') ? "opendyslexic" : ""}>
2020-12-09 23:56:35 +01:00
<Router history={history} basename="/pk-webs">
2020-12-11 16:35:25 +01:00
<Navigation/>
2020-12-09 09:15:55 +01:00
<BS.Container>
<Switch>
2020-12-13 23:38:10 +01:00
<Route exact path="/pk-webs/dash" >
2020-12-11 16:35:25 +01:00
{ !localStorage.getItem('token') || isInvalid ? <Redirect to="/pk-webs"/> : <Dash />
2020-12-09 09:15:55 +01:00
}
</Route>
2020-12-09 09:32:20 +01:00
<Route exact path="/pk-webs">
2020-12-09 09:15:55 +01:00
{ isLoading ? <Loading /> :
<BS.Card className="mb-3 mt-3">
<BS.Card.Header className="d-flex align-items-center justify-content-between">
<BS.Card.Title><FaLock className="mr-3" />Login</BS.Card.Title>
</BS.Card.Header>
<BS.Card.Body>
<BS.Form onSubmit={handleSubmit(onSubmit)}>
{ isSubmit && !localStorage.getItem('user') ? <BS.Alert variant="danger">Something went wrong, please try again.</BS.Alert> : ""}
{ isInvalid ? <BS.Alert variant="danger">Invalid token.</BS.Alert> : "" }
<BS.Form.Row>
2020-12-09 17:45:11 +01:00
<BS.Col xs={12} lg={10}>
2020-12-09 09:15:55 +01:00
<BS.Form.Label>Enter your token here. You can get your token by using <b>"pk;token"</b>.</BS.Form.Label>
</BS.Col>
</BS.Form.Row>
<BS.Form.Row>
2020-12-09 17:45:11 +01:00
<BS.Col xs={12} lg={10}>
2020-12-09 09:15:55 +01:00
<BS.Form.Control required name="pkToken" type="text" ref={register} placeholder="token" />
</BS.Col>
<BS.Col>
<BS.Button variant="primary" type="submit" block >Submit</BS.Button>
</BS.Col>
</BS.Form.Row>
</BS.Form>
</BS.Card.Body>
</BS.Card>
}
</Route>
2020-12-13 23:38:10 +01:00
<Route exact path="/pk-webs/profile">
<Profile />
</Route>
<Route exact path="/pk-webs/settings">
<BS.Card>
<BS.Card.Header className="d-flex align-items-center justify-content-between">
<BS.Card.Title><FaCog className="mr-3" />Settings</BS.Card.Title>
</BS.Card.Header>
<BS.Card.Body>
<p>Change how you view and use pk-webs here, changes will be saved after refreshing. You will have to apply them again in different browsers and on different devices.</p>
<hr/>
<BS.Row>
<BS.Col xs={12} lg={4} className="mx-lg-2 d-flex align-items-center row">
{ localStorage.getItem('opendyslexic') ?
<Toggle className="mr-2"
defaultChecked={true}
icons={false}
onChange={() => {
localStorage.removeItem('opendyslexic');
forceUpdate()}} /> :
<Toggle className="mr-2"
defaultChecked={false}
icons={false}
onChange={() => {
localStorage.setItem('opendyslexic', 'true')
forceUpdate()}} /> }
Use opendyslexic?
</BS.Col>
</BS.Row>
</BS.Card.Body>
</BS.Card>
</Route>
2020-12-09 09:15:55 +01:00
</Switch>
</BS.Container>
2020-12-12 19:19:22 +01:00
<Footer />
2020-12-09 09:15:55 +01:00
</Router>
2020-12-13 23:38:10 +01:00
</div>
);
}