Advertisement
Guest User

Untitled

a guest
Jun 17th, 2019
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.26 KB | None | 0 0
  1. const mapStateToProps = state => {
  2. return {
  3. profile: state.profileReducer.profile
  4. }
  5. }
  6.  
  7. const mapDispatchToProps = dispatch => {
  8. return {
  9. dispatch: action => {
  10. dispatch(action)
  11. }
  12. }
  13. }
  14.  
  15. ...
  16.  
  17. handleConnection = () => {
  18. const { profile } = this.props
  19.  
  20. this.props.dispatch({ type: 'CONNECT_USER' })
  21.  
  22. }
  23.  
  24. ...
  25.  
  26. export default connect(mapStateToProps, mapDispatchToProps)(LoginPage)
  27.  
  28. import io from 'socket.io-client'
  29.  
  30. const host = [SERVER_URL]
  31.  
  32. const socketConnection = io.connect(host, {path: [PATH], secure: true})
  33.  
  34. const initialState = {
  35. profile: {
  36. token: null,
  37. username: '',
  38. password: ''
  39. }
  40. }
  41.  
  42. function profileReducer(state = initialState, action) {
  43. switch(action.type) {
  44.  
  45. ...
  46.  
  47. case 'CONNECT_USER':
  48. let tempProfile = {...state.profile}
  49.  
  50. socketConnection.emit('login', tempProfile.username + ';' + tempProfile.password)
  51.  
  52. socketConnection.on('check', msg => {
  53. if (msg !== null && msg !== '')
  54. tempProfile.token = msg
  55.  
  56. return {
  57. ...state,
  58. profile: tempProfile
  59. }
  60. })
  61.  
  62. return state
  63.  
  64. ...
  65. }
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement