sforkowany z mirror/soapbox
Statuses: optimistic reply counter
rodzic
4c1b3dd88b
commit
a354fd325d
|
@ -1,8 +1,41 @@
|
|||
import reducer from '../statuses';
|
||||
import { Map as ImmutableMap } from 'immutable';
|
||||
import { Map as ImmutableMap, fromJS } from 'immutable';
|
||||
import {
|
||||
STATUS_CREATE_REQUEST,
|
||||
STATUS_CREATE_FAIL,
|
||||
} from 'soapbox/actions/statuses';
|
||||
|
||||
|
||||
describe('statuses reducer', () => {
|
||||
it('should return the initial state', () => {
|
||||
expect(reducer(undefined, {})).toEqual(ImmutableMap());
|
||||
});
|
||||
|
||||
describe('STATUS_CREATE_REQUEST', () => {
|
||||
it('increments the replies_count of its parent', () => {
|
||||
const state = fromJS({ '123': { replies_count: 4 } });
|
||||
|
||||
const action = {
|
||||
type: STATUS_CREATE_REQUEST,
|
||||
params: { in_reply_to_id: '123' },
|
||||
};
|
||||
|
||||
const result = reducer(state, action).getIn(['123', 'replies_count']);
|
||||
expect(result).toEqual(5);
|
||||
});
|
||||
});
|
||||
|
||||
describe('STATUS_CREATE_FAIL', () => {
|
||||
it('decrements the replies_count of its parent', () => {
|
||||
const state = fromJS({ '123': { replies_count: 5 } });
|
||||
|
||||
const action = {
|
||||
type: STATUS_CREATE_FAIL,
|
||||
params: { in_reply_to_id: '123' },
|
||||
};
|
||||
|
||||
const result = reducer(state, action).getIn(['123', 'replies_count']);
|
||||
expect(result).toEqual(4);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
|
@ -6,6 +6,8 @@ import {
|
|||
FAVOURITE_FAIL,
|
||||
} from '../actions/interactions';
|
||||
import {
|
||||
STATUS_CREATE_REQUEST,
|
||||
STATUS_CREATE_FAIL,
|
||||
STATUS_MUTE_SUCCESS,
|
||||
STATUS_UNMUTE_SUCCESS,
|
||||
STATUS_REVEAL,
|
||||
|
@ -33,6 +35,22 @@ const deleteStatus = (state, id, references) => {
|
|||
return state.delete(id);
|
||||
};
|
||||
|
||||
const importPendingStatus = (state, { in_reply_to_id }) => {
|
||||
if (in_reply_to_id) {
|
||||
return state.updateIn([in_reply_to_id, 'replies_count'], 0, count => count + 1);
|
||||
} else {
|
||||
return state;
|
||||
}
|
||||
};
|
||||
|
||||
const deletePendingStatus = (state, { in_reply_to_id }) => {
|
||||
if (in_reply_to_id) {
|
||||
return state.updateIn([in_reply_to_id, 'replies_count'], 0, count => Math.max(0, count - 1));
|
||||
} else {
|
||||
return state;
|
||||
}
|
||||
};
|
||||
|
||||
const initialState = ImmutableMap();
|
||||
|
||||
export default function statuses(state = initialState, action) {
|
||||
|
@ -41,6 +59,10 @@ export default function statuses(state = initialState, action) {
|
|||
return importStatus(state, action.status);
|
||||
case STATUSES_IMPORT:
|
||||
return importStatuses(state, action.statuses);
|
||||
case STATUS_CREATE_REQUEST:
|
||||
return importPendingStatus(state, action.params);
|
||||
case STATUS_CREATE_FAIL:
|
||||
return deletePendingStatus(state, action.params);
|
||||
case FAVOURITE_REQUEST:
|
||||
return state.update(action.status.get('id'), status =>
|
||||
status
|
||||
|
|
Ładowanie…
Reference in New Issue