From a817cc7cb464d804ccd8af2bbe22e9b738959a3a Mon Sep 17 00:00:00 2001 From: taehoon Date: Wed, 13 Feb 2019 12:05:23 -0500 Subject: [PATCH] Wire up ui to real blocks api data --- src/components/user_settings/user_settings.js | 4 ++-- src/hocs/with_load_more/with_load_more.js | 2 +- src/modules/users.js | 17 +++++++++++++++++ src/services/api/api.service.js | 17 +++++++++++++++++ .../backend_interactor_service.js | 2 ++ .../entity_normalizer.service.js | 1 + 6 files changed, 40 insertions(+), 3 deletions(-) diff --git a/src/components/user_settings/user_settings.js b/src/components/user_settings/user_settings.js index 34ab99c7..5d8541f0 100644 --- a/src/components/user_settings/user_settings.js +++ b/src/components/user_settings/user_settings.js @@ -10,8 +10,8 @@ import withLoadMore from '../../hocs/with_load_more/with_load_more' const BlockListWithLoadMore = withLoadMore( UserList, - (props, $store) => $store.dispatch('addFriends', $store.state.users.currentUser.id), - (props, $store) => get($store.getters.userById($store.state.users.currentUser.id), 'friends', []) + (props, $store) => $store.dispatch('fetchBlocks'), + (props, $store) => get($store.state.users.currentUser, 'blocks', []) ) const UserSettings = { diff --git a/src/hocs/with_load_more/with_load_more.js b/src/hocs/with_load_more/with_load_more.js index 0f72ffd6..14d4303d 100644 --- a/src/hocs/with_load_more/with_load_more.js +++ b/src/hocs/with_load_more/with_load_more.js @@ -48,7 +48,7 @@ const withLoadMore = (Component, fetchEntries, getEntries) => { fetchEntries(this.$props, this.$store).then((newEntries) => { this.error = false this.loading = false - this.bottomedOut = newEntries.length === 0 + this.bottomedOut = !newEntries || newEntries.length === 0 }).catch(() => { this.error = true this.loading = false diff --git a/src/modules/users.js b/src/modules/users.js index 000cfd72..6ea4e0c9 100644 --- a/src/modules/users.js +++ b/src/modules/users.js @@ -85,6 +85,15 @@ export const mutations = { addNewUsers (state, users) { each(users, (user) => mergeOrAdd(state.users, state.usersObject, user)) }, + addBlocks (state, { blocks, page }) { + const user = state.currentUser + each(blocks, block => { + if (!find(user.blocks, { id: block.id })) { + user.blocks.push(block) + } + }) + user.blocksPage = page + 1 + }, setUserForStatus (state, status) { status.user = state.usersObject[status.user.id] }, @@ -137,6 +146,14 @@ const users = { store.rootState.api.backendInteractor.fetchUser({ id }) .then((user) => store.commit('addNewUsers', [user])) }, + fetchBlocks (store) { + const page = store.state.currentUser.blocksPage || 1 + return store.rootState.api.backendInteractor.fetchBlocks({ page }) + .then((blocks) => { + store.commit('addBlocks', { blocks, page }) + return blocks + }) + }, addFriends ({ rootState, commit }, fetchBy) { return new Promise((resolve, reject) => { const user = rootState.users.usersObject[fetchBy] diff --git a/src/services/api/api.service.js b/src/services/api/api.service.js index 13d31d91..908edf7c 100644 --- a/src/services/api/api.service.js +++ b/src/services/api/api.service.js @@ -18,6 +18,7 @@ const MENTIONS_URL = '/api/statuses/mentions.json' const DM_TIMELINE_URL = '/api/statuses/dm_timeline.json' const FOLLOWERS_URL = '/api/statuses/followers.json' const FRIENDS_URL = '/api/statuses/friends.json' +const BLOCKS_URL = '/api/statuses/blocks.json' const FOLLOWING_URL = '/api/friendships/create.json' const UNFOLLOWING_URL = '/api/friendships/destroy.json' const QVITTER_USER_PREF_URL = '/api/qvitter/set_profile_pref.json' @@ -519,6 +520,21 @@ const fetchMutes = ({credentials}) => { }).then((data) => data.json()) } +const fetchBlocks = ({page, credentials}) => { + let url = BLOCKS_URL + if (page) { + url = url + `?page=${page}` + } + return fetch(url, { + headers: authHeaders(credentials) + }).then((data) => { + if (data.ok) { + return data.json() + } + throw new Error('Error fetching blocks', data) + }) +} + const suggestions = ({credentials}) => { return fetch(SUGGESTIONS_URL, { headers: authHeaders(credentials) @@ -560,6 +576,7 @@ const apiService = { fetchAllFollowing, setUserMute, fetchMutes, + fetchBlocks, register, getCaptcha, updateAvatar, diff --git a/src/services/backend_interactor_service/backend_interactor_service.js b/src/services/backend_interactor_service/backend_interactor_service.js index 80c5cc5e..43c914d9 100644 --- a/src/services/backend_interactor_service/backend_interactor_service.js +++ b/src/services/backend_interactor_service/backend_interactor_service.js @@ -63,6 +63,7 @@ const backendInteractorService = (credentials) => { } const fetchMutes = () => apiService.fetchMutes({credentials}) + const fetchBlocks = (params) => apiService.fetchBlocks({credentials, ...params}) const fetchFollowRequests = () => apiService.fetchFollowRequests({credentials}) const getCaptcha = () => apiService.getCaptcha() @@ -94,6 +95,7 @@ const backendInteractorService = (credentials) => { startFetching, setUserMute, fetchMutes, + fetchBlocks, register, getCaptcha, updateAvatar, diff --git a/src/services/entity_normalizer/entity_normalizer.service.js b/src/services/entity_normalizer/entity_normalizer.service.js index d20ce77f..1192b6cc 100644 --- a/src/services/entity_normalizer/entity_normalizer.service.js +++ b/src/services/entity_normalizer/entity_normalizer.service.js @@ -120,6 +120,7 @@ export const parseUser = (data) => { if (data.pleroma) { output.follow_request_count = data.pleroma.follow_request_count } + output.blocks = [] return output }