All files / src/apps/authentication LoginPage.tsx

77.78% Statements 14/18
37.5% Branches 3/8
100% Functions 5/5
76.47% Lines 13/17

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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                                        1x                                 1x               1x   1x 1x   1x 1x       1x 1x         1x             1x                                                                                               1x             1x            
import React, { useEffect } from "react";
import { Redirect } from "react-router-dom";
import { connect } from "react-redux";
import { Container, Grid, Typography } from "@mui/material";
import { makeStyles } from "@mui/styles";
import { Login } from "./components";
import { authErrorsSelector, authLoadingSelector } from "./redux/reducer";
import { clearNextPageAction, loginAction } from "./redux";
import { AppState } from "../../redux/types";
import { BUILD, TRADITIONAL_OCL_URL } from "../../utils";
 
interface Props {
  isLoggedIn: boolean;
  login: (...args: Parameters<typeof loginAction>) => void;
  loading: boolean;
  errors?: any;
  nextPage?: string;
  clearNextPage: (...args: Parameters<typeof clearNextPageAction>) => void;
}
 
const useStyles = makeStyles({
  loginPage: {
    display: "flex",
    height: "100vh"
  },
  header: {
    textAlign: "center",
    margin: "-6vh 0 5vh 0"
  },
  title: {
    marginBottom: "1.3rem"
  },
  footerText: {
    textAlign: "center"
  }
});
 
const LoginPage: React.FC<Props> = ({
  isLoggedIn,
  login,
  loading,
  errors,
  nextPage,
  clearNextPage
}: Props) => {
  const classes = useStyles();
 
  useEffect(() => {
    document.title = `Login | OpenMRS Dictionary Manager`;
 
    return () => {
      document.title = "OpenMRS Dictionary Manager";
    };
  }, []);
 
  useEffect(() => {
    Iif (isLoggedIn && nextPage) {
      clearNextPage();
    }
  }, [isLoggedIn, nextPage, clearNextPage]);
 
  Iif (isLoggedIn) {
    if (nextPage) {
      return <Redirect to={nextPage} />;
    }
 
    return <Redirect to="/" />;
  } else {
    return (
      <>
        <Grid
          container
          justifyContent="center"
          alignItems="center"
          className={classes.loginPage}
          component={Container}
        >
          <Grid
            xs={5}
            container
            item
            component="div"
            justifyContent="center"
            alignItems="center"
            className={classes.header}
          >
            <Typography variant="h3" component="h3" className={classes.title}>
              OpenMRS Dictionary Manager
            </Typography>
            <Typography variant="subtitle1" component="span">
              Use the shared{" "}
              <a
                href={TRADITIONAL_OCL_URL}
                target="_blank"
                rel="external noopener noreferrer"
              >
                Open Concept Lab
              </a>{" "}
              environment to create OpenMRS dictionary by mixing expert-defined
              content with your own custom concepts.
            </Typography>
            <Login onSubmit={login} loading={loading} status={errors} />
          </Grid>
        </Grid>
        <Typography
          variant="caption"
          className={classes.footerText}
          component="div"
        >
          OpenMRS Dictionary Manager: {BUILD}
        </Typography>
      </>
    );
  }
};
 
const mapStateToProps = (state: AppState) => ({
  isLoggedIn: state.auth.isLoggedIn,
  loading: authLoadingSelector(state),
  errors: authErrorsSelector(state),
  nextPage: state.auth.nextPage
});
 
const mapActionsToProps = {
  login: loginAction,
  clearNextPage: clearNextPageAction
};
 
export default connect(mapStateToProps, mapActionsToProps)(LoginPage);