Created
March 30, 2020 23:51
-
-
Save ericelliott/d80ce25b3e791b8912618b2a7e9944ca to your computer and use it in GitHub Desktop.
withMagicLink HOC example.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { useEffect } from 'react'; | |
import hoistStatics from '../../HOCs/hoist-statics'; | |
import { connect } from 'react-redux'; | |
import { | |
getShouldSignOut, | |
setShouldSignOut, | |
getDidAuthFail, | |
signInStatusChanged, | |
setMagicUser, | |
} from './ethereum-authentication-reducer'; | |
import { getUser } from '../user-profile/user-profile-reducer'; | |
import useMagicLink from './use-magic-link'; | |
import PropTypes from 'prop-types'; | |
const apiKey = 'pk_live_NOT_A_REAL_KEY_GET_YOUR_OWN'; | |
const mapStateToProps = state => ({ | |
shouldSignOut: getShouldSignOut(state), | |
ethDidAuthFail: getDidAuthFail(state), | |
user: getUser(state), | |
}); | |
const mapDispatchToProps = { | |
setShouldSignOut, | |
signInStatusChanged, | |
setMagicUser, | |
}; | |
const connectComponent = connect(mapStateToProps, mapDispatchToProps); | |
const withMagicLink = Component => { | |
const WithMagicLink = props => { | |
const { | |
shouldSignOut, | |
setShouldSignOut, | |
signInStatusChanged, | |
setMagicUser, | |
} = props; | |
const { signIn, signOut, user } = useMagicLink(apiKey, signInStatusChanged); | |
useEffect(() => { | |
if (shouldSignOut) { | |
setShouldSignOut(false); | |
signOut(); | |
} | |
}, [shouldSignOut]); | |
useEffect(() => { | |
setMagicUser(user); | |
}, [user]); | |
return ( | |
<Component | |
{...props} | |
magicLinkSignIn={signIn} | |
magicLinkSignOut={signOut} | |
isSignedInToMagic={user.isSignedIn} | |
magicUser={user} | |
/> | |
); | |
}; | |
WithMagicLink.propTypes = { | |
shouldSignOut: PropTypes.bool, | |
setShouldSignOut: PropTypes.func, | |
signInStatusChanged: PropTypes.func, | |
setMagicUser: PropTypes.func, | |
}; | |
return connectComponent(WithMagicLink); | |
}; | |
export default hoistStatics(withMagicLink); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment