From 94e6de11b7f9aebbc0130c836f334921fc70ae81 Mon Sep 17 00:00:00 2001
From: taehoon
Date: Wed, 13 Feb 2019 14:30:12 -0500
Subject: Add withList hoc and remove UserList component
---
src/hocs/with_list/with_list.js | 27 +++++++++++++++++++++++++++
1 file changed, 27 insertions(+)
create mode 100644 src/hocs/with_list/with_list.js
(limited to 'src/hocs/with_list/with_list.js')
diff --git a/src/hocs/with_list/with_list.js b/src/hocs/with_list/with_list.js
new file mode 100644
index 00000000..21aa288b
--- /dev/null
+++ b/src/hocs/with_list/with_list.js
@@ -0,0 +1,27 @@
+import Vue from 'vue'
+import map from 'lodash/map'
+
+const defaultEntryPropsGetter = entry => ({ entry })
+const defaultKeyGetter = entry => entry.id
+
+const withList = (Component, getEntryProps = defaultEntryPropsGetter, getKey = defaultKeyGetter) => {
+ return Vue.component('withList', {
+ render (createElement) {
+ return (
+
+ {map(this.entries, (entry, index) => {
+ const props = {
+ key: getKey(entry, index),
+ ...this.$props.entryProps,
+ ...getEntryProps(entry, index)
+ }
+ return
+ })}
+
+ )
+ },
+ props: ['entries', 'entryProps']
+ })
+}
+
+export default withList
--
cgit v1.2.3-70-g09d2
From 82702748653c7630e4f337433d47863f52c57ee4 Mon Sep 17 00:00:00 2001
From: taehoon
Date: Wed, 13 Feb 2019 20:59:26 -0500
Subject: Update hocs to pass parent-scope bindings to the wrapped component
---
src/hocs/with_list/with_list.js | 11 +++++---
src/hocs/with_load_more/with_load_more.js | 42 +++++++++++++++++++------------
2 files changed, 33 insertions(+), 20 deletions(-)
(limited to 'src/hocs/with_list/with_list.js')
diff --git a/src/hocs/with_list/with_list.js b/src/hocs/with_list/with_list.js
index 21aa288b..5ec37a2b 100644
--- a/src/hocs/with_list/with_list.js
+++ b/src/hocs/with_list/with_list.js
@@ -12,15 +12,18 @@ const withList = (Component, getEntryProps = defaultEntryPropsGetter, getKey = d
{map(this.entries, (entry, index) => {
const props = {
key: getKey(entry, index),
- ...this.$props.entryProps,
- ...getEntryProps(entry, index)
+ props: {
+ ...this.$props.entryProps,
+ ...getEntryProps(entry, index)
+ },
+ on: this.$props.entryListeners
}
- return
+ return
})}
)
},
- props: ['entries', 'entryProps']
+ props: ['entries', 'entryProps', 'entryListeners']
})
}
diff --git a/src/hocs/with_load_more/with_load_more.js b/src/hocs/with_load_more/with_load_more.js
index 14d4303d..8877f8d3 100644
--- a/src/hocs/with_load_more/with_load_more.js
+++ b/src/hocs/with_load_more/with_load_more.js
@@ -1,20 +1,28 @@
import Vue from 'vue'
import filter from 'lodash/filter'
+import isEmpty from 'lodash/isEmpty'
import './with_load_more.scss'
-const withLoadMore = (Component, fetchEntries, getEntries) => {
+const withLoadMore = (Component, fetch, select, entriesPropName = 'entries') => {
const originalProps = Component.props || []
const props = filter(originalProps, v => v !== 'entries')
return Vue.component('withLoadMore', {
render (createElement) {
+ const props = {
+ props: {
+ ...this.$props,
+ [entriesPropName]: this.entries
+ },
+ on: this.$listeners
+ }
return (
-
+
)
@@ -29,30 +37,32 @@ const withLoadMore = (Component, fetchEntries, getEntries) => {
},
computed: {
entries () {
- return getEntries(this.$props, this.$store) || []
+ return select(this.$props, this.$store) || []
}
},
created () {
window.addEventListener('scroll', this.scrollLoad)
if (this.entries.length === 0) {
- this.fetch()
+ this.fetchEntries()
}
},
destroyed () {
window.removeEventListener('scroll', this.scrollLoad)
},
methods: {
- fetch () {
+ fetchEntries () {
if (!this.loading) {
this.loading = true
- fetchEntries(this.$props, this.$store).then((newEntries) => {
- this.error = false
- this.loading = false
- this.bottomedOut = !newEntries || newEntries.length === 0
- }).catch(() => {
- this.error = true
- this.loading = false
- })
+ this.error = false
+ fetch(this.$props, this.$store)
+ .then((newEntries) => {
+ this.loading = false
+ this.bottomedOut = isEmpty(newEntries)
+ })
+ .catch(() => {
+ this.loading = false
+ this.error = true
+ })
}
},
scrollLoad (e) {
@@ -63,7 +73,7 @@ const withLoadMore = (Component, fetchEntries, getEntries) => {
this.$el.offsetHeight > 0 &&
(window.innerHeight + window.pageYOffset) >= (height - 750)
) {
- this.fetch()
+ this.fetchEntries()
}
}
}
--
cgit v1.2.3-70-g09d2
From f81b82b4714643ba396b69ca54b97259a36a6b9f Mon Sep 17 00:00:00 2001
From: taehoon
Date: Wed, 13 Feb 2019 22:52:57 -0500
Subject: Use hoc definitions to be factor of factory
---
src/components/user_settings/user_settings.js | 26 ++++++++++++-------------
src/hocs/with_list/with_list.js | 8 ++++----
src/hocs/with_load_more/with_load_more.js | 6 +++---
src/hocs/with_subscription/with_subscription.js | 6 +++---
4 files changed, 22 insertions(+), 24 deletions(-)
(limited to 'src/hocs/with_list/with_list.js')
diff --git a/src/components/user_settings/user_settings.js b/src/components/user_settings/user_settings.js
index 8114d5e2..21023841 100644
--- a/src/components/user_settings/user_settings.js
+++ b/src/components/user_settings/user_settings.js
@@ -10,21 +10,19 @@ import MuteCard from '../mute_card/mute_card.vue'
import withSubscription from '../../hocs/with_subscription/with_subscription'
import withList from '../../hocs/with_list/with_list'
-const BlockList = withList(BlockCard, userId => ({ userId }))
-const BlockListWithSubscription = withSubscription(
- BlockList,
- (props, $store) => $store.dispatch('fetchBlocks'),
- (props, $store) => get($store.state.users.currentUser, 'blockIds', []),
- 'entries'
-)
+const BlockList = withList({ getEntryProps: userId => ({ userId }) })(BlockCard)
+const BlockListWithSubscription = withSubscription({
+ fetch: (props, $store) => $store.dispatch('fetchBlocks'),
+ select: (props, $store) => get($store.state.users.currentUser, 'blockIds', []),
+ contentPropName: 'entries'
+})(BlockList)
-const MuteList = withList(MuteCard, userId => ({ userId }))
-const MuteListWithSubscription = withSubscription(
- MuteList,
- (props, $store) => $store.dispatch('fetchMutes'),
- (props, $store) => get($store.state.users.currentUser, 'muteIds', []),
- 'entries'
-)
+const MuteList = withList({ getEntryProps: userId => ({ userId }) })(MuteCard)
+const MuteListWithSubscription = withSubscription({
+ fetch: (props, $store) => $store.dispatch('fetchMutes'),
+ select: (props, $store) => get($store.state.users.currentUser, 'muteIds', []),
+ contentPropName: 'entries'
+})(MuteList)
const UserSettings = {
data () {
diff --git a/src/hocs/with_list/with_list.js b/src/hocs/with_list/with_list.js
index 5ec37a2b..c31cdcb1 100644
--- a/src/hocs/with_list/with_list.js
+++ b/src/hocs/with_list/with_list.js
@@ -4,8 +4,8 @@ import map from 'lodash/map'
const defaultEntryPropsGetter = entry => ({ entry })
const defaultKeyGetter = entry => entry.id
-const withList = (Component, getEntryProps = defaultEntryPropsGetter, getKey = defaultKeyGetter) => {
- return Vue.component('withList', {
+const withList = ({ getEntryProps = defaultEntryPropsGetter, getKey = defaultKeyGetter }) => (ItemComponent) => (
+ Vue.component('withList', {
render (createElement) {
return (
@@ -18,13 +18,13 @@ const withList = (Component, getEntryProps = defaultEntryPropsGetter, getKey = d
},
on: this.$props.entryListeners
}
- return
+ return
})}
)
},
props: ['entries', 'entryProps', 'entryListeners']
})
-}
+)
export default withList
diff --git a/src/hocs/with_load_more/with_load_more.js b/src/hocs/with_load_more/with_load_more.js
index 8877f8d3..28c741e3 100644
--- a/src/hocs/with_load_more/with_load_more.js
+++ b/src/hocs/with_load_more/with_load_more.js
@@ -3,8 +3,8 @@ import filter from 'lodash/filter'
import isEmpty from 'lodash/isEmpty'
import './with_load_more.scss'
-const withLoadMore = (Component, fetch, select, entriesPropName = 'entries') => {
- const originalProps = Component.props || []
+const withLoadMore = ({ fetch, select, entriesPropName = 'entries' }) => (WrappedComponent) => {
+ const originalProps = WrappedComponent.props || []
const props = filter(originalProps, v => v !== 'entries')
return Vue.component('withLoadMore', {
@@ -18,7 +18,7 @@ const withLoadMore = (Component, fetch, select, entriesPropName = 'entries') =>
}
return (
-
+