Advertisement
famiev

WizardComponent

Jun 20th, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import React, { Component } from 'react'
  2. //  libs
  3. import { connect } from 'react-redux'
  4. import { compose } from 'react-apollo'
  5. import { hideModal } from 'reducers/modals'
  6. import { object, func } from 'prop-types'
  7. //  components
  8. import Modal from 'components/UI-elements/Modal'
  9.  
  10. export default ({ modalType, onSubmit, hocs, mapStateToProps, mapDispatchToProps, initialValue, Children }) => {
  11.   class Wizard extends Component {
  12.     static propTypes = {
  13.       hideModal: func,
  14.       modalData: object
  15.     }
  16.  
  17.     state = {
  18.       page: 1
  19.     }
  20.  
  21.     onSubmit = onSubmit.bind(this)
  22.  
  23.     onHideModal = () => {
  24.       this.props.hideModal({ modalType })
  25.       this.setState({
  26.         page: 1
  27.       })
  28.     }
  29.  
  30.     nextPage = e => {
  31.       e.preventDefault()
  32.  
  33.       this.setState(({ page }) => ({
  34.         page: page + 1
  35.       }))
  36.     }
  37.  
  38.     previousPage = e => {
  39.       e.preventDefault()
  40.       this.setState({ page: this.state.page - 1 })
  41.     }
  42.  
  43.     render () {
  44.       const { modalData } = this.props
  45.       return (
  46.         <Modal
  47.           isOpen={modalData.isOpen}
  48.           width={modalData.width}
  49.           hideModal={this.onHideModal}
  50.           withCloseButton
  51.           grayMask
  52.         >
  53.           <Children
  54.             {...this.props}
  55.             initialValues={typeof initialValue === 'function' ? initialValue(modalData) : initialValue || {}}
  56.             onSubmit={this.onSubmit}
  57.             page={this.state.page}
  58.             nextPage={this.nextPage}
  59.             previousPage={this.previousPage}
  60.           />
  61.         </Modal>
  62.       )
  63.     }
  64.   }
  65.  
  66.   const defaultMapStateToProps = ({ modals }) => ({
  67.     modalData: modals[modalType]
  68.   })
  69.  
  70.   const defaultMapDispatchToProps = {
  71.     hideModal
  72.   }
  73.  
  74.   return compose(
  75.     connect(mapStateToProps || defaultMapStateToProps, mapDispatchToProps || defaultMapDispatchToProps),
  76.     ...hocs || []
  77.   )(Wizard)
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement