2020-10-02 21:18:53 +02:00
|
|
|
// @flow
|
|
|
|
import { Lbryio } from 'lbryinc';
|
|
|
|
import * as ACTIONS from 'constants/action_types';
|
|
|
|
import * as REACTION_TYPES from 'constants/reactions';
|
2020-10-05 20:31:51 +02:00
|
|
|
import { makeSelectMyReactionForUri } from 'redux/selectors/reactions';
|
2021-10-17 10:36:14 +02:00
|
|
|
import { makeSelectClaimForUri } from 'redux/selectors/claims';
|
2020-10-02 21:18:53 +02:00
|
|
|
|
|
|
|
export const doFetchReactions = (claimId: string) => (dispatch: Dispatch) => {
|
|
|
|
dispatch({ type: ACTIONS.REACTIONS_LIST_STARTED });
|
|
|
|
|
|
|
|
return Lbryio.call('reaction', 'list', { claim_ids: claimId }, 'post')
|
|
|
|
.then((reactions: Array<number>) => {
|
|
|
|
dispatch({ type: ACTIONS.REACTIONS_LIST_COMPLETED, data: { claimId, reactions } });
|
|
|
|
})
|
2021-10-17 10:36:14 +02:00
|
|
|
.catch((error) => {
|
2020-10-02 21:18:53 +02:00
|
|
|
dispatch({ type: ACTIONS.REACTIONS_LIST_FAILED, data: error });
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
export const doReactionLike = (uri: string) => (dispatch: Dispatch, getState: GetState) => {
|
|
|
|
const state = getState();
|
|
|
|
const myReaction = makeSelectMyReactionForUri(uri)(state);
|
|
|
|
const claim = makeSelectClaimForUri(uri)(state);
|
|
|
|
const claimId = claim.claim_id;
|
|
|
|
const shouldRemove = myReaction === REACTION_TYPES.LIKE;
|
|
|
|
|
|
|
|
return Lbryio.call(
|
|
|
|
'reaction',
|
|
|
|
'react',
|
|
|
|
{
|
|
|
|
claim_ids: claimId,
|
|
|
|
type: REACTION_TYPES.LIKE,
|
|
|
|
clear_types: REACTION_TYPES.DISLIKE,
|
|
|
|
...(shouldRemove ? { remove: true } : {}),
|
|
|
|
},
|
|
|
|
'post'
|
|
|
|
)
|
|
|
|
.then(() => {
|
|
|
|
dispatch({ type: ACTIONS.REACTIONS_LIKE_COMPLETED, data: { claimId, shouldRemove } });
|
|
|
|
})
|
2021-10-17 10:36:14 +02:00
|
|
|
.catch((error) => {
|
2020-10-02 21:18:53 +02:00
|
|
|
dispatch({ type: ACTIONS.REACTIONS_NEW_FAILED, data: error });
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
export const doReactionDislike = (uri: string) => (dispatch: Dispatch, getState: GetState) => {
|
|
|
|
const state = getState();
|
|
|
|
const myReaction = makeSelectMyReactionForUri(uri)(state);
|
|
|
|
const claim = makeSelectClaimForUri(uri)(state);
|
|
|
|
const claimId = claim.claim_id;
|
|
|
|
const shouldRemove = myReaction === REACTION_TYPES.DISLIKE;
|
|
|
|
|
|
|
|
return Lbryio.call(
|
|
|
|
'reaction',
|
|
|
|
'react',
|
|
|
|
{
|
|
|
|
claim_ids: claimId,
|
|
|
|
type: REACTION_TYPES.DISLIKE,
|
|
|
|
clear_types: REACTION_TYPES.LIKE,
|
|
|
|
...(shouldRemove ? { remove: true } : {}),
|
|
|
|
},
|
|
|
|
'post'
|
|
|
|
)
|
|
|
|
.then(() => {
|
|
|
|
dispatch({ type: ACTIONS.REACTIONS_DISLIKE_COMPLETED, data: { claimId, shouldRemove } });
|
|
|
|
})
|
2021-10-17 10:36:14 +02:00
|
|
|
.catch((error) => {
|
2020-10-02 21:18:53 +02:00
|
|
|
dispatch({ type: ACTIONS.REACTIONS_NEW_FAILED, data: error });
|
|
|
|
});
|
|
|
|
};
|