Skip to content

Instantly share code, notes, and snippets.

@Shuumatsu
Created August 29, 2018 03:27
Show Gist options
  • Save Shuumatsu/184b27aaaddf609c0cbca73d3a6a1f6a to your computer and use it in GitHub Desktop.
Save Shuumatsu/184b27aaaddf609c0cbca73d3a6a1f6a to your computer and use it in GitHub Desktop.
import { curry } from 'ramda'
import React from 'react'
import { withStateHandlers } from 'recompose'
import styled, { css } from 'styled-components'
const initialState = { retryCount: 1 }
const stateHandlers = {
reload: ({ retryCount }) => _ => ({ retryCount: retryCount + 1 })
}
const hideComponent = Cmp => styled(Cmp)`
${css`
${props => (props.hide ? 'transition: opacity 0.75s; opacity: 0;' : 'opacity: 1;')};
`};
`
export default curry((errorCondition, RawCmp, RawError) => {
const Error = hideComponent(RawError)
const Cmp = hideComponent(RawCmp)
return withStateHandlers(initialState, stateHandlers)(({ retryCount, reload, ...rest }) => (
<>
<Error hide={!errorCondition(rest)} key={-retryCount} action={reload} />
<Cmp hide={errorCondition(rest)} key={retryCount} />
</>
))
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment