Guest User

Untitled

a guest
May 19th, 2018
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.15 KB | None | 0 0
  1. import React,{Component} from 'react'
  2. import PropTypes from 'prop-types'
  3. import { compose } from 'recompose'
  4. import {connect} from 'react-redux'
  5. import {
  6. withRouter,
  7. } from 'react-router-dom';
  8.  
  9. import {
  10. Divider, Message,Container,Header
  11. } from 'semantic-ui-react';
  12. import {session,getUserFromJwtTocken} from '../services';
  13. import PublicLayout from "./frames/PublicLayout";
  14. import NearBottom from "./navs/NearBottom";
  15. import Next2Footer from "./navs/Next2Footer";
  16. import './css/nav.css'
  17. import SigninForm from './forms/SigninForm'
  18. /* eslint-disable no-console */
  19.  
  20.  
  21. class SigninPageUI extends Component{
  22.  
  23. onSubmit= (user) => {
  24. const {history:{push},onLogin,retrieveAuthUserFromJWT} = this.props;
  25.  
  26. return onLogin(user).then(res=>res.value.accessToken)
  27. .then(t=>retrieveAuthUserFromJWT(t))
  28. .then(u=>u.value.id)
  29. .then(id=>push(`/users/${id}`))
  30. .catch(() => {
  31.  
  32. console.log("PPPPPP: ",this.props.errors);
  33. });
  34. }
  35.  
  36. render(){
  37. const {history,loading,errors,isVerified,verifiedUser} = this.props
  38.  
  39. const errs =(errors)? errors.message.split(","):'';
  40. return(
  41. <PublicLayout
  42. className="register"
  43. url1="/" link1="Home"
  44. url2="/signup" link2="Sign up"
  45. url3="/htw" link3="How It Works"
  46. >
  47. <Divider section hidden/>
  48. {errors &&
  49. <Container text textAlign='justified'>
  50.  
  51. <Message
  52. error
  53. header='There are some errors with your submission'
  54. list={errs.map(e=>e)}
  55. />
  56. </Container>
  57. }{
  58. isVerified?
  59. <Container text>
  60. <Divider section hidden />
  61. <Message>
  62. <Header as ='h1'> Greetings {verifiedUser.firstname} {verifiedUser.lastname}</Header>
  63. <Header>
  64. Your Account Has Been Verified
  65. </Header>
  66. </Message>
  67. <Divider section hidden/>
  68. <SigninForm
  69. user={verifiedUser}
  70. onSubmit={this.onSubmit}
  71. errors={errors}
  72. loading={loading}
  73. history={history}
  74. />
  75. </Container>
  76. :
  77. <Container text>
  78. <Divider section hidden />
  79. <SigninForm
  80. user={verifiedUser}
  81. onSubmit={this.onSubmit}
  82. errors={errors}
  83. loading={loading}
  84. history={history}
  85. />
  86. </Container>
  87. }
  88.  
  89. <NearBottom/>
  90. <Next2Footer/>
  91. </PublicLayout>
  92. );
  93. }
  94. }
  95.  
  96. SigninPageUI.propTypes={
  97. verifiedUser:PropTypes.shape({}),
  98. isVerified:PropTypes.bool,
  99. loading:PropTypes.bool,
  100. errors:PropTypes.shape({}),
  101. history:PropTypes.shape({
  102. push:PropTypes.func.isRequired
  103. }).isRequired,
  104. onLogin:PropTypes.func.isRequired,
  105. retrieveAuthUserFromJWT:PropTypes.func.isRequired,
  106.  
  107. }
  108. SigninPageUI.defaultProps={
  109. isVerified:false,
  110. verifiedUser:null,
  111. errors:{},
  112. loading:false
  113. }
  114.  
  115. /**
  116. * To use connect(), you need to define a special function called mapStateToProps
  117. * that tells how to transform the current Redux store state into the props you want
  118. * to pass to a presentational component you are wrapping.
  119. */
  120. const mapStateToProps =(state) =>({
  121. authUser:state.sessionState.user,
  122.  
  123. errors: state.sessionState.isError ,
  124. loading:state.sessionState.isLoading,
  125. user:state.userState.data||{
  126. email:'',password:'',id:null
  127. },
  128. isVerified:state.authManagementState.isFinished,
  129. verifiedUser:state.authManagementState.data||{
  130. email:'',password:'',id:null
  131. }
  132. })
  133.  
  134. /**
  135. * In addition to reading the state, container components can dispatch actions.
  136. * In a similar fashion, you can define a function called mapDispatchToProps() that receives the dispatch() method
  137. * and returns callback props that you want to inject into the presentational component.
  138. */
  139.  
  140. // SERVICES_MESSAGES_FIND
  141.  
  142.  
  143.  
  144. const mapDispatchToProps = (dispatch) => ({
  145. // Sign in with credentials
  146. onLogin: (user) =>dispatch(session.authenticate({ strategy: 'local', ...user})),
  147. retrieveAuthUserFromJWT : jwt=> dispatch({
  148. type: 'RETRIEVE_AUTH_USER_FROMJWT',
  149. // payload:authUser
  150. payload: getUserFromJwtTocken(jwt)
  151.  
  152. }),
  153.  
  154. });
  155.  
  156.  
  157.  
  158. const SigninPage=compose(
  159. withRouter,
  160. connect(
  161. mapStateToProps,
  162. mapDispatchToProps
  163. ),
  164. )(SigninPageUI)
  165. export default SigninPage;
Add Comment
Please, Sign In to add comment