Commit fef82f22 authored by Nastassia Danilova's avatar Nastassia Danilova

loadOlderUserList thunk, removed dublicated code

parent 8c6f1c2d
Pipeline #29974 passed with stages
in 5 minutes and 8 seconds
......@@ -64,12 +64,12 @@ const addUserStatuses = (state, { userId, statuses }) => {
}
}
const addUserFollowers = (state, { userId, followers }) => {
const addUserList = (state, { userId, listName, items }) => {
const oldUser = state.usersByIds[userId] || {}
const user = {
...oldUser,
followers: addIdsToList(oldUser.followers || [],
followers ? followers.map(account => account.id) : [])
[listName]: addIdsToList(oldUser[listName] || [],
items ? items.map(account => account.id) : [])
}
return {
......@@ -81,21 +81,12 @@ const addUserFollowers = (state, { userId, followers }) => {
}
}
const addUserFollowing = (state, { userId, following }) => {
const oldUser = state.usersByIds[userId] || {}
const user = {
...oldUser,
following: addIdsToList(oldUser.following || [],
following ? following.map(account => account.id) : [])
}
const addUserFollowers = (state, { userId, followers }) => {
return addUserList(state, { userId, listName: 'followers', items: followers })
}
return {
...state,
usersByIds: {
...state.usersByIds,
[userId]: user
}
}
const addUserFollowing = (state, { userId, following }) => {
return addUserList(state, { userId, listName: 'following', items: following })
}
const deleteUserStatus = (state, { userId, statusId }) => {
......
......@@ -6,7 +6,7 @@ import usersThunks from './users_thunks.js'
import has from 'lodash/has'
import pollsThunks from './polls_thunks.js'
import conversationsThunks from './conversations_thunks.js'
import { ENTITIES } from './api_thunks_entities_config'
import { ENTITIES, LOAD_OLDER_USER_FIELDS_CONFIG } from './api_thunks_entities_config'
import tagsThunks from './tags_thunks.js'
import { apiErrorCatcher, getConfig } from '../utils/api_utils'
import reducers from '../reducers'
......@@ -159,29 +159,16 @@ const generateApiThunks = () => {
}
},
loadOlderUserFollowers: ({ params, queries }) => {
return async (dispatch, getState) => {
const userFollowers = getState().api.userFollowers || {}
const config = getState().api.config
const fullUrl = (userFollowers.next || {}).url
return dispatch(usersThunks.fetchUserFollowers({
older: true,
config,
fullUrl,
queries,
params
}))
}
},
loadOlderUserList: ({ params, queries, entity }) => {
const loadOlderEntityConfig = LOAD_OLDER_USER_FIELDS_CONFIG[entity]
loadOlderUserFollowing: ({ params, queries }) => {
if (!entity || !loadOlderEntityConfig) return
return async (dispatch, getState) => {
const userFollowing = getState().api.userFollowing || {}
const items = getState().api[loadOlderEntityConfig.reducerField] || {}
const config = getState().api.config
const fullUrl = (userFollowing.next || {}).url
const fullUrl = (items.next || {}).url
return dispatch(usersThunks.fetchUserFollowing({
return dispatch(usersThunks[loadOlderEntityConfig.thunk]({
older: true,
config,
fullUrl,
......
......@@ -26,3 +26,14 @@ export const ENTITIES = {
clearThunk: 'clear'
}
}
export const LOAD_OLDER_USER_FIELDS_CONFIG = {
followers: {
reducerField: 'userFollowers',
thunk: 'fetchUserFollowers'
},
following: {
reducerField: 'userFollowing',
thunk: 'fetchUserFollowing'
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment