add comments!
This commit is contained in:
parent
c1de266b2d
commit
69dd14dc0d
@ -12,15 +12,19 @@ import API_URL from "../Constants/constants.js";
|
|||||||
const Home = ({isInvalid, setIsInvalid, isLoading, setIsLoading, isSubmit, setIsSubmit, }) => {
|
const Home = ({isInvalid, setIsInvalid, isLoading, setIsLoading, isSubmit, setIsSubmit, }) => {
|
||||||
const { register, handleSubmit } = useForm();
|
const { register, handleSubmit } = useForm();
|
||||||
|
|
||||||
|
// submit login form, add the token to the localstorage
|
||||||
const onSubmit = (data) => {
|
const onSubmit = (data) => {
|
||||||
localStorage.setItem("token", data.pkToken);
|
localStorage.setItem("token", data.pkToken);
|
||||||
logIn();
|
logIn();
|
||||||
};
|
};
|
||||||
|
|
||||||
function logIn() {
|
function logIn() {
|
||||||
|
|
||||||
|
// make sure the token is not set to invalid and add a funny little spinner to indicate loading
|
||||||
setIsInvalid(false);
|
setIsInvalid(false);
|
||||||
setIsLoading(true);
|
setIsLoading(true);
|
||||||
|
|
||||||
|
// then fetch the system data with the token stored in localstorage
|
||||||
fetch(`${API_URL}s/`, {
|
fetch(`${API_URL}s/`, {
|
||||||
method: "GET",
|
method: "GET",
|
||||||
headers: {
|
headers: {
|
||||||
@ -30,6 +34,9 @@ const { register, handleSubmit } = useForm();
|
|||||||
),
|
),
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
// put all the system data in localstorage
|
||||||
|
// TODO: remove this from localstorage? since we know how to pass stuff along components now
|
||||||
|
// then push the user to the dash!
|
||||||
.then((res) => res.json())
|
.then((res) => res.json())
|
||||||
.then((data) => {
|
.then((data) => {
|
||||||
localStorage.setItem("user", JSON.stringify(data));
|
localStorage.setItem("user", JSON.stringify(data));
|
||||||
@ -37,6 +44,8 @@ const { register, handleSubmit } = useForm();
|
|||||||
setIsLoading(false);
|
setIsLoading(false);
|
||||||
history.push("/pk-webs/dash");
|
history.push("/pk-webs/dash");
|
||||||
})
|
})
|
||||||
|
// remove the token and user data from localstorage if there's an error, also set the token as invalid
|
||||||
|
// TODO: an error doesn't mean the token is invalid, change this depending on what error is thrown
|
||||||
.catch((error) => {
|
.catch((error) => {
|
||||||
console.log(error);
|
console.log(error);
|
||||||
setIsInvalid(true);
|
setIsInvalid(true);
|
||||||
@ -46,12 +55,16 @@ const { register, handleSubmit } = useForm();
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// when the homepage loads, check if there's a token, if there is, check if it's still valid
|
||||||
|
// removing the dependency array causes a rerender loop, so just ignore ESlint here
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (localStorage.getItem('token')) {
|
if (localStorage.getItem('token')) {
|
||||||
checkLogIn();
|
checkLogIn();
|
||||||
}
|
}
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
|
// very similar to LogIn(), only difference is that it doesn't push the user afterwards
|
||||||
|
// TODO: useless double code that probably could be refactored somehow
|
||||||
function checkLogIn() {
|
function checkLogIn() {
|
||||||
setIsInvalid(false);
|
setIsInvalid(false);
|
||||||
setIsLoading(true);
|
setIsLoading(true);
|
||||||
@ -77,6 +90,7 @@ const { register, handleSubmit } = useForm();
|
|||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
|
{/* if the page is loading, just show the loading component */}
|
||||||
{isLoading ? (
|
{isLoading ? (
|
||||||
<Loading />
|
<Loading />
|
||||||
) : (
|
) : (
|
||||||
@ -88,6 +102,7 @@ const { register, handleSubmit } = useForm();
|
|||||||
</BS.Card.Title>
|
</BS.Card.Title>
|
||||||
</BS.Card.Header>
|
</BS.Card.Header>
|
||||||
<BS.Card.Body>
|
<BS.Card.Body>
|
||||||
|
{/* if the login form has been submitted, and there's no user object, show a generic error */}
|
||||||
{isSubmit && !localStorage.getItem("user") ? (
|
{isSubmit && !localStorage.getItem("user") ? (
|
||||||
<BS.Alert variant="danger">
|
<BS.Alert variant="danger">
|
||||||
Something went wrong, please try again.
|
Something went wrong, please try again.
|
||||||
@ -95,12 +110,15 @@ const { register, handleSubmit } = useForm();
|
|||||||
) : (
|
) : (
|
||||||
""
|
""
|
||||||
)}
|
)}
|
||||||
|
{/* if the inserted token is invalid, show invalid error!
|
||||||
|
this also shows if the token used in checkLogIn() is invalid */}
|
||||||
{isInvalid ? (
|
{isInvalid ? (
|
||||||
<BS.Alert variant="danger">Invalid token.</BS.Alert>
|
<BS.Alert variant="danger">Invalid token.</BS.Alert>
|
||||||
) : (
|
) : (
|
||||||
""
|
""
|
||||||
)}
|
)}
|
||||||
{localStorage.getItem("user") && localStorage.getItem("token") ? (
|
{ // if there's a user object in localstorage, and there's a token in localstorage, the user is logged in already
|
||||||
|
localStorage.getItem("user") && localStorage.getItem("token") ? (
|
||||||
<>
|
<>
|
||||||
<p>
|
<p>
|
||||||
You are logged in already, click here to continue to the dash.
|
You are logged in already, click here to continue to the dash.
|
||||||
@ -113,6 +131,7 @@ const { register, handleSubmit } = useForm();
|
|||||||
</BS.Button>
|
</BS.Button>
|
||||||
</>
|
</>
|
||||||
) : (
|
) : (
|
||||||
|
// otherwise, show login form
|
||||||
<BS.Form onSubmit={handleSubmit(onSubmit)}>
|
<BS.Form onSubmit={handleSubmit(onSubmit)}>
|
||||||
<BS.Form.Row>
|
<BS.Form.Row>
|
||||||
<BS.Col className="mb-1" xs={12} lg={10}>
|
<BS.Col className="mb-1" xs={12} lg={10}>
|
||||||
|
Loading…
Reference in New Issue
Block a user