Skip to content

Instantly share code, notes, and snippets.

@bigslycat
Forked from Bitaru/findify_challenge.md
Last active January 24, 2018 13:33
Show Gist options
  • Save bigslycat/673bf3fb7165421ede81da134f3818a9 to your computer and use it in GitHub Desktop.
Save bigslycat/673bf3fb7165421ede81da134f3818a9 to your computer and use it in GitHub Desktop.
Findify Challenge

TASK:

Implement a "withModal" high order component (HOC) that will accept two arguments [modalName: String] and [modalComponent: React.Component]. It should render a "modalComponent" into the portal. In addition, this HOC should inject 'showModal' and 'hideModal' handlers into a new collection of props that are passed to the base component. For instance:

import React, { Fragment } from 'react'
import { createPortal } from 'react-dom'

import {
  withStateHandlers,
  withProps,
  mapProps,
} from 'recompose'

const Modal = mapProps(({ close, title, ...props }) => ({
  ...props,
  children: `Hide ${title} modal`,
  onClick: close,
}))('button')

const ModalButton = withProps({
  children: 'Open modal',
})('button')

const withModal =
  (modalNode, modalName, ModalComponent) =>
    ButtonComponent => withStateHandlers({
      isOpen: false,
    }, {
      open: () => () => ({ isOpen: true }),
      close: () => () => ({ isOpen: false }),
    })(({ isOpen, open, close, buttonProps, ...modalProps }) => (
      <Fragment>
        {isOpen && createPortal(
          <ModalComponent {...modalProps} title={modalName} close={close} />,
          modalNode,
        )}
        <ButtonComponent {...buttonProps} onClick={open} />
      </Fragment>
    ))

export const PricingModal = withModal(
  document.getElementById('modals'),
  'pricing',
  Modal,
)(ModalButton)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment