Make emoji reacts more responsive

merge-requests/18/head
Alex Gleason 4 years ago
parent 29bcc4a0d1
commit e04ab557ac
No known key found for this signature in database
GPG Key ID: 7211D1F99744FBB7

@ -10,9 +10,13 @@ import {
STATUS_REVEAL,
STATUS_HIDE,
} from '../actions/statuses';
import {
EMOJI_REACT_REQUEST,
} from '../actions/emoji_reacts';
import { TIMELINE_DELETE } from '../actions/timelines';
import { STATUS_IMPORT, STATUSES_IMPORT } from '../actions/importer';
import { Map as ImmutableMap, fromJS } from 'immutable';
import { simulateEmojiReact } from 'gabsocial/utils/emoji_reacts';
const importStatus = (state, status) => state.set(status.id, fromJS(status));
@ -37,6 +41,10 @@ export default function statuses(state = initialState, action) {
return importStatuses(state, action.statuses);
case FAVOURITE_REQUEST:
return state.setIn([action.status.get('id'), 'favourited'], true);
case EMOJI_REACT_REQUEST:
const path = [action.status.get('id'), 'pleroma', 'emoji_reactions'];
const emojiReacts = state.getIn(path);
return state.setIn(path, simulateEmojiReact(emojiReacts, action.emoji));
case FAVOURITE_FAIL:
return state.get(action.status.get('id')) === undefined ? state : state.setIn([action.status.get('id'), 'favourited'], false);
case REBLOG_REQUEST:

@ -5,6 +5,7 @@ import {
oneEmojiPerAccount,
reduceEmoji,
getReactForStatus,
simulateEmojiReact,
} from '../emoji_reacts';
import { fromJS } from 'immutable';
@ -179,3 +180,28 @@ describe('getReactForStatus', () => {
expect(getReactForStatus(status)).toEqual(undefined);
});
});
describe('simulateEmojiReact', () => {
it('adds the emoji to the list', () => {
const emojiReacts = fromJS([
{ 'count': 2, 'me': false, 'name': '👍' },
{ 'count': 2, 'me': false, 'name': '❤' },
]);
expect(simulateEmojiReact(emojiReacts, '❤')).toEqual(fromJS([
{ 'count': 2, 'me': false, 'name': '👍' },
{ 'count': 3, 'me': true, 'name': '❤' },
]));
});
it('creates the emoji if it didn\'t already exist', () => {
const emojiReacts = fromJS([
{ 'count': 2, 'me': false, 'name': '👍' },
{ 'count': 2, 'me': false, 'name': '❤' },
]);
expect(simulateEmojiReact(emojiReacts, '😯')).toEqual(fromJS([
{ 'count': 2, 'me': false, 'name': '👍' },
{ 'count': 2, 'me': false, 'name': '❤' },
{ 'count': 1, 'me': true, 'name': '😯' },
]));
});
});

@ -83,3 +83,20 @@ export const getReactForStatus = status => {
).filter(e => e.get('me') === true)
.getIn([0, 'name']);
};
export const simulateEmojiReact = (emojiReacts, emoji) => {
const idx = emojiReacts.findIndex(e => e.get('name') === emoji);
if (idx > -1) {
const emojiReact = emojiReacts.get(idx);
return emojiReacts.set(idx, emojiReact.merge({
count: emojiReact.get('count') + 1,
me: true,
}));
} else {
return emojiReacts.push(ImmutableMap({
count: 1,
me: true,
name: emoji,
}));
}
};

Loading…
Cancel
Save