Rename some options and add comments to HOCs

This commit is contained in:
taehoon 2019-02-13 23:25:21 -05:00
parent 339373b495
commit 6d4d705c51
4 changed files with 20 additions and 9 deletions

View File

@ -15,7 +15,7 @@ const BlockList = compose(
withSubscription({ withSubscription({
fetch: (props, $store) => $store.dispatch('fetchBlocks'), fetch: (props, $store) => $store.dispatch('fetchBlocks'),
select: (props, $store) => get($store.state.users.currentUser, 'blockIds', []), select: (props, $store) => get($store.state.users.currentUser, 'blockIds', []),
contentPropName: 'entries' childPropName: 'entries'
}), }),
withList({ getEntryProps: userId => ({ userId }) }) withList({ getEntryProps: userId => ({ userId }) })
)(BlockCard) )(BlockCard)
@ -24,7 +24,7 @@ const MuteList = compose(
withSubscription({ withSubscription({
fetch: (props, $store) => $store.dispatch('fetchMutes'), fetch: (props, $store) => $store.dispatch('fetchMutes'),
select: (props, $store) => get($store.state.users.currentUser, 'muteIds', []), select: (props, $store) => get($store.state.users.currentUser, 'muteIds', []),
contentPropName: 'entries' childPropName: 'entries'
}), }),
withList({ getEntryProps: userId => ({ userId }) }) withList({ getEntryProps: userId => ({ userId }) })
)(MuteCard) )(MuteCard)

View File

@ -4,7 +4,10 @@ import map from 'lodash/map'
const defaultEntryPropsGetter = entry => ({ entry }) const defaultEntryPropsGetter = entry => ({ entry })
const defaultKeyGetter = entry => entry.id const defaultKeyGetter = entry => entry.id
const withList = ({ getEntryProps = defaultEntryPropsGetter, getKey = defaultKeyGetter }) => (ItemComponent) => ( const withList = ({
getEntryProps = defaultEntryPropsGetter, // function to accept entry and index values and return props to be passed into the item component
getKey = defaultKeyGetter // funciton to accept entry and index values and return key prop value
}) => (ItemComponent) => (
Vue.component('withList', { Vue.component('withList', {
render (createElement) { render (createElement) {
return ( return (

View File

@ -3,7 +3,11 @@ import filter from 'lodash/filter'
import isEmpty from 'lodash/isEmpty' import isEmpty from 'lodash/isEmpty'
import './with_load_more.scss' import './with_load_more.scss'
const withLoadMore = ({ fetch, select, entriesPropName = 'entries' }) => (WrappedComponent) => { const withLoadMore = ({
fetch, // function to fetch entries and return a promise
select, // function to select data from store
childPropName = 'entries' // name of the prop to be passed into the wrapped component
}) => (WrappedComponent) => {
const originalProps = WrappedComponent.props || [] const originalProps = WrappedComponent.props || []
const props = filter(originalProps, v => v !== 'entries') const props = filter(originalProps, v => v !== 'entries')
@ -12,7 +16,7 @@ const withLoadMore = ({ fetch, select, entriesPropName = 'entries' }) => (Wrappe
const props = { const props = {
props: { props: {
...this.$props, ...this.$props,
[entriesPropName]: this.entries [childPropName]: this.entries
}, },
on: this.$listeners on: this.$listeners
} }

View File

@ -4,8 +4,12 @@ import isEmpty from 'lodash/isEmpty'
import omit from 'lodash/omit' import omit from 'lodash/omit'
import './with_subscription.scss' import './with_subscription.scss'
const withSubscription = ({ fetch, select, contentPropName = 'content' }) => (WrapperComponent) => { const withSubscription = ({
const originalProps = WrapperComponent.props || [] fetch, // function to fetch entries and return a promise
select, // function to select data from store
childPropName = 'content' // name of the prop to be passed into the wrapped component
}) => (WrappedComponent) => {
const originalProps = WrappedComponent.props || []
const props = reject(originalProps, v => v === 'content') const props = reject(originalProps, v => v === 'content')
return Vue.component('withSubscription', { return Vue.component('withSubscription', {
@ -13,13 +17,13 @@ const withSubscription = ({ fetch, select, contentPropName = 'content' }) => (Wr
const props = { const props = {
props: { props: {
...omit(this.$props, 'refresh'), ...omit(this.$props, 'refresh'),
[contentPropName]: this.fetchedData [childPropName]: this.fetchedData
}, },
on: this.$listeners on: this.$listeners
} }
return ( return (
<div class="with-subscription"> <div class="with-subscription">
{!this.error && !this.loading && <WrapperComponent {...props} />} {!this.error && !this.loading && <WrappedComponent {...props} />}
<div class="with-subscription-footer"> <div class="with-subscription-footer">
{this.error && <a onClick={this.fetchData} class="alert error">{this.$t('general.generic_error')}</a>} {this.error && <a onClick={this.fetchData} class="alert error">{this.$t('general.generic_error')}</a>}
{!this.error && this.loading && <i class="icon-spin3 animate-spin"/>} {!this.error && this.loading && <i class="icon-spin3 animate-spin"/>}