All files / src/containers LoginContainer.jsx

82.35% Statements 14/17
87.5% Branches 7/8
83.33% Functions 5/6
82.35% Lines 14/17
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                        5x 5x           5x 5x 1x   4x 1x     3x               3x 3x     3x 1x             3x 1x              
import React, { Component } from "react";
import LoginPage from "../components/LoginPage";
import LoadingUserPage from "../components/LoadingUserPage";
import { Redirect } from "react-router-dom";
import { connect } from "react-redux";
import { loginSuccess } from "../redux/actions";
import { publicurl, decodeHash } from "../spotify-api/auth";
import { isAuth } from "../redux/middleware";
import TemplatePage from "../components/TemplatePage";
 
export class LoginContainer extends Component {
  componentDidMount() {
    const { location } = this.props;
    Iif (location && isCallback(location)) {
      const hash = decodeHash(location.hash);
      this.props.setToken(hash.access_token, hash.expirationTime);
    }
  }
  render() {
    const { isAuth, isLoading } = this.props;
    if (isAuth) {
      return <Redirect to={process.env.PUBLIC_URL + "/"} />;
    }
    if (isLoading) {
      return <LoadingUserPage />;
    }
 
    return (
      <TemplatePage>
        <LoginPage login_url={publicurl} />
      </TemplatePage>
    );
  }
}
 
const isCallback = location => {
  return location.pathname.endsWith("/callback");
};
 
const mapDispatchToProps = dispatch => {
  return {
    setToken: (auth_token, expiration) => {
      dispatch(loginSuccess(auth_token, undefined, expiration));
    }
  };
};
 
const mapStateToProps = (state, ownProps) => {
  return {
    isAuth: isAuth(state),
    isLoading: isCallback(ownProps.location)
  };
};
 
export default connect(mapStateToProps, mapDispatchToProps)(LoginContainer);