Commit 7e5493fb authored by Nastassia Danilova's avatar Nastassia Danilova

updateUserStatus action, userId params in getStatusLists thunk to update...

updateUserStatus action, userId params in getStatusLists thunk to update status in user profile timeline
parent c67cba6e
import { reduce } from 'lodash'
import { reduce, cloneDeep } from 'lodash'
import { emojify, emojifyStatus } from '../utils/parse_utils'
import { addStatuses } from '../utils/status_utils'
import { addIdsToList } from '../utils/common_utils'
......@@ -64,6 +64,31 @@ const addUserStatuses = (state, { userId, statuses }) => {
}
}
const updateUserStatus = (state, { userId, status }) => {
const user = state.usersByIds[userId]
if (!user) return state
const statusIndex = user.statuses ? user.statuses.findIndex(item => status.id === item.id) : -1
const newUser = cloneDeep(user)
if (statusIndex === -1) {
newUser.statuses = addStatuses(newUser.statuses || [],
[{
...status,
...emojifyStatus(status, {})
}])
} else {
newUser.statuses[statusIndex] = emojifyStatus(status, user.statuses[statusIndex])
}
return {
...state,
usersByIds: {
...state.usersByIds,
[userId]: newUser
}
}
}
const addUserList = (state, { userId, listName, items }) => {
const oldUser = state.usersByIds[userId] || {}
const user = {
......@@ -124,6 +149,7 @@ const reducers = {
setCurrentUser,
updateCurrentUser,
addUserStatuses,
updateUserStatus,
addUserFollowers,
addUserFollowing,
deleteUserStatus,
......@@ -161,6 +187,10 @@ const actions = {
payload: { userId, statuses }
}
},
updateUserStatus: ({ userId, status }) => ({
type: 'updateUserStatus',
payload: { userId, status }
}),
addUserFollowers: ({ userId, followers }) => {
return {
type: 'addUserFollowers',
......
......@@ -165,8 +165,12 @@ const statusesThunks = {
if (result[1].data.length) {
status.reblogs_count = result[1].data.length
}
if (params.userId) {
await dispatch(Users.actions.updateUserStatus({ status, userId: params.userId }))
} else {
await dispatch(Statuses.actions.addStatus({ status }))
}
await dispatch(Statuses.actions.addStatus({ status }))
return getState()
}
},
......
......@@ -135,6 +135,36 @@ describe('User reducers', () => {
})
})
describe(`update user status`, () => {
it('should update user status', () => {
const status = { id: '2', content: 'content', spoiler_text: 'header' }
const user = {
id: '1',
statuses: [status]
}
const resultState = Users.reducer(
{ usersByIds: { '1': user } },
Users.actions.updateUserStatus({ userId: '1', status: { ...status, followed_by: [{ id: 'f1' }] } })
)
expect(resultState.usersByIds['1']).toEqual({ id: '1', statuses: [{...status, followed_by: [{ id: 'f1' }] }] })
})
it(`should set status, if it wasn't exist`, () => {
const status = { id: '2', content: 'content', spoiler_text: 'header' }
const user = {
id: '1'
}
const resultState = Users.reducer(
{ usersByIds: { '1': user } },
Users.actions.updateUserStatus({ userId: '1', status: { ...status, followed_by: [{ id: 'f1' }] } })
)
expect(resultState.usersByIds['1']).toEqual({ id: '1', statuses: [{...status, followed_by: [{ id: 'f1' }] }] })
})
})
describe(`adding a user's followers`, () => {
it('add followersIds no a new user', () => {
const followers = [{ id: 2, acct: 'b' }, { id: 1, acct: 'a' }]
......
......@@ -539,12 +539,67 @@ describe('Status thunks', () => {
content: 'Status content',
spoiler_text: '',
favourited_by: favouritedByList,
reblogged_by: rebloggedByList
reblogged_by: rebloggedByList,
reblogs_count: 2,
favourites_count: 2
}
expect(state.statuses.statusesByIds)
.toEqual({ 1: result })
})
it(`fetches a status' lists in user timeline`, async () => {
const store = { state: { users: {
usersByIds: {
'1': {
id: '1',
acct: 'nd',
statuses: [
{ id: '1', content: 'Status content', spoiler_text: '' }
]
}
}}
}
}
const favouritedByList = [{ id: 1 }, { id: 2 }]
const rebloggedByList = [{ id: 3 }, { id: 4 }]
fetch.mockReset()
fetch
.mockImplementationOnce(
fetchMocker(
favouritedByList,
{
expectedUrl: `https://pleroma.soykaf.com/api/v1/statuses/1/favourited_by`
})
)
.mockImplementationOnce(
fetchMocker(
rebloggedByList,
{
expectedUrl: `https://pleroma.soykaf.com/api/v1/statuses/1/reblogged_by`
})
)
let state = await statusesThunks.getStatusLists({ config, params: { id: '1', userId: 1 } })(dispatch(store), getState(store))
const result = {
id: '1',
acct: 'nd',
statuses: [
{
id: '1',
content: 'Status content',
spoiler_text: '',
favourited_by: favouritedByList,
reblogged_by: rebloggedByList,
reblogs_count: 2,
favourites_count: 2
}
]
}
expect(state.users.usersByIds)
.toEqual({ 1: result })
})
it('delete status', async () => {
const status = {
......
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