2024-08-28 04:41:08 -07:00
|
|
|
import { isLoggedIn } from 'pl-fe/utils/auth';
|
2022-06-19 11:38:51 -07:00
|
|
|
|
2024-08-04 07:09:52 -07:00
|
|
|
import { getClient } from '../api';
|
2022-06-19 11:38:51 -07:00
|
|
|
|
2024-08-06 10:52:36 -07:00
|
|
|
import { importFetchedStatus } from './importer';
|
2022-06-19 11:38:51 -07:00
|
|
|
import { favourite, unfavourite } from './interactions';
|
|
|
|
|
2024-08-11 01:48:58 -07:00
|
|
|
import type { EmojiReaction, Status } from 'pl-api';
|
2024-08-28 04:41:08 -07:00
|
|
|
import type { AppDispatch, RootState } from 'pl-fe/store';
|
2022-06-19 11:38:51 -07:00
|
|
|
|
2024-08-11 01:48:58 -07:00
|
|
|
const EMOJI_REACT_REQUEST = 'EMOJI_REACT_REQUEST' as const;
|
|
|
|
const EMOJI_REACT_SUCCESS = 'EMOJI_REACT_SUCCESS' as const;
|
|
|
|
const EMOJI_REACT_FAIL = 'EMOJI_REACT_FAIL' as const;
|
2022-06-19 11:38:51 -07:00
|
|
|
|
2024-08-11 01:48:58 -07:00
|
|
|
const UNEMOJI_REACT_REQUEST = 'UNEMOJI_REACT_REQUEST' as const;
|
|
|
|
const UNEMOJI_REACT_SUCCESS = 'UNEMOJI_REACT_SUCCESS' as const;
|
|
|
|
const UNEMOJI_REACT_FAIL = 'UNEMOJI_REACT_FAIL' as const;
|
2022-06-19 11:38:51 -07:00
|
|
|
|
|
|
|
const noOp = () => () => new Promise(f => f(undefined));
|
|
|
|
|
2024-08-11 01:48:58 -07:00
|
|
|
const simpleEmojiReact = (status: Pick<Status, 'id' | 'emoji_reactions' | 'favourited'>, emoji: string, custom?: string) =>
|
2022-06-19 11:38:51 -07:00
|
|
|
(dispatch: AppDispatch) => {
|
2024-08-11 01:48:58 -07:00
|
|
|
const emojiReacts: Array<EmojiReaction> = status.emoji_reactions || [];
|
2022-06-19 11:38:51 -07:00
|
|
|
|
|
|
|
if (emoji === '👍' && status.favourited) return dispatch(unfavourite(status));
|
|
|
|
|
2024-08-11 01:48:58 -07:00
|
|
|
const undo = emojiReacts.filter(e => e.me === true && e.name === emoji).length > 0;
|
2022-06-19 11:38:51 -07:00
|
|
|
if (undo) return dispatch(unEmojiReact(status, emoji));
|
|
|
|
|
|
|
|
return Promise.all([
|
|
|
|
...emojiReacts
|
2023-10-29 15:00:48 -07:00
|
|
|
.filter((emojiReact) => emojiReact.me === true)
|
2024-08-11 01:48:58 -07:00
|
|
|
.map(emojiReact => dispatch(unEmojiReact(status, emojiReact.name))),
|
2022-06-19 11:38:51 -07:00
|
|
|
status.favourited && dispatch(unfavourite(status)),
|
|
|
|
]).then(() => {
|
|
|
|
if (emoji === '👍') {
|
|
|
|
dispatch(favourite(status));
|
|
|
|
} else {
|
2023-03-17 16:07:18 -07:00
|
|
|
dispatch(emojiReact(status, emoji, custom));
|
2022-06-19 11:38:51 -07:00
|
|
|
}
|
|
|
|
}).catch(err => {
|
|
|
|
console.error(err);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2024-08-11 01:48:58 -07:00
|
|
|
const emojiReact = (status: Pick<Status, 'id'>, emoji: string, custom?: string) =>
|
2022-06-19 11:38:51 -07:00
|
|
|
(dispatch: AppDispatch, getState: () => RootState) => {
|
|
|
|
if (!isLoggedIn(getState)) return dispatch(noOp());
|
|
|
|
|
2024-08-11 01:48:58 -07:00
|
|
|
dispatch(emojiReactRequest(status.id, emoji, custom));
|
2022-06-19 11:38:51 -07:00
|
|
|
|
2024-08-06 10:52:36 -07:00
|
|
|
return getClient(getState).statuses.createStatusReaction(status.id, emoji).then((response) => {
|
|
|
|
dispatch(importFetchedStatus(response));
|
2024-08-11 01:48:58 -07:00
|
|
|
dispatch(emojiReactSuccess(response, emoji));
|
2024-05-13 10:00:42 -07:00
|
|
|
}).catch((error) => {
|
2024-08-11 01:48:58 -07:00
|
|
|
dispatch(emojiReactFail(status.id, emoji, error));
|
2024-05-11 14:37:37 -07:00
|
|
|
});
|
2022-06-19 11:38:51 -07:00
|
|
|
};
|
|
|
|
|
2024-08-11 01:48:58 -07:00
|
|
|
const unEmojiReact = (status: Pick<Status, 'id'>, emoji: string) =>
|
2022-06-19 11:38:51 -07:00
|
|
|
(dispatch: AppDispatch, getState: () => RootState) => {
|
|
|
|
if (!isLoggedIn(getState)) return dispatch(noOp());
|
|
|
|
|
2024-08-11 01:48:58 -07:00
|
|
|
dispatch(unEmojiReactRequest(status.id, emoji));
|
2022-06-19 11:38:51 -07:00
|
|
|
|
2024-08-06 10:52:36 -07:00
|
|
|
return getClient(getState).statuses.deleteStatusReaction(status.id, emoji).then(response => {
|
|
|
|
dispatch(importFetchedStatus(response));
|
2024-08-11 01:48:58 -07:00
|
|
|
dispatch(unEmojiReactSuccess(response, emoji));
|
2024-05-11 14:37:37 -07:00
|
|
|
}).catch(error => {
|
2024-08-11 01:48:58 -07:00
|
|
|
dispatch(unEmojiReactFail(status.id, emoji, error));
|
2024-05-11 14:37:37 -07:00
|
|
|
});
|
2022-06-19 11:38:51 -07:00
|
|
|
};
|
|
|
|
|
2024-08-11 01:48:58 -07:00
|
|
|
const emojiReactRequest = (statusId: string, emoji: string, custom?: string) => ({
|
2022-06-19 11:38:51 -07:00
|
|
|
type: EMOJI_REACT_REQUEST,
|
2024-08-11 01:48:58 -07:00
|
|
|
statusId,
|
2022-06-19 11:38:51 -07:00
|
|
|
emoji,
|
2023-03-17 16:07:18 -07:00
|
|
|
custom,
|
2022-06-19 11:38:51 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
const emojiReactSuccess = (status: Status, emoji: string) => ({
|
|
|
|
type: EMOJI_REACT_SUCCESS,
|
|
|
|
status,
|
2024-08-11 01:48:58 -07:00
|
|
|
statusId: status.id,
|
2022-06-19 11:38:51 -07:00
|
|
|
emoji,
|
|
|
|
});
|
|
|
|
|
2024-08-11 01:48:58 -07:00
|
|
|
const emojiReactFail = (statusId: string, emoji: string, error: unknown) => ({
|
2022-06-19 11:38:51 -07:00
|
|
|
type: EMOJI_REACT_FAIL,
|
2024-08-11 01:48:58 -07:00
|
|
|
statusId,
|
2022-06-19 11:38:51 -07:00
|
|
|
emoji,
|
|
|
|
error,
|
|
|
|
});
|
|
|
|
|
2024-08-11 01:48:58 -07:00
|
|
|
const unEmojiReactRequest = (statusId: string, emoji: string) => ({
|
2022-06-19 11:38:51 -07:00
|
|
|
type: UNEMOJI_REACT_REQUEST,
|
2024-08-11 01:48:58 -07:00
|
|
|
statusId,
|
2022-06-19 11:38:51 -07:00
|
|
|
emoji,
|
|
|
|
});
|
|
|
|
|
|
|
|
const unEmojiReactSuccess = (status: Status, emoji: string) => ({
|
|
|
|
type: UNEMOJI_REACT_SUCCESS,
|
|
|
|
status,
|
2024-08-11 01:48:58 -07:00
|
|
|
statusId: status.id,
|
2022-06-19 11:38:51 -07:00
|
|
|
emoji,
|
|
|
|
});
|
|
|
|
|
2024-08-11 01:48:58 -07:00
|
|
|
const unEmojiReactFail = (statusId: string, emoji: string, error: unknown) => ({
|
2022-06-19 11:38:51 -07:00
|
|
|
type: UNEMOJI_REACT_FAIL,
|
2024-08-11 01:48:58 -07:00
|
|
|
statusId,
|
2022-06-19 11:38:51 -07:00
|
|
|
emoji,
|
|
|
|
error,
|
|
|
|
});
|
|
|
|
|
|
|
|
export {
|
|
|
|
EMOJI_REACT_REQUEST,
|
|
|
|
EMOJI_REACT_SUCCESS,
|
|
|
|
EMOJI_REACT_FAIL,
|
|
|
|
UNEMOJI_REACT_REQUEST,
|
|
|
|
UNEMOJI_REACT_SUCCESS,
|
|
|
|
UNEMOJI_REACT_FAIL,
|
|
|
|
simpleEmojiReact,
|
|
|
|
emojiReact,
|
|
|
|
unEmojiReact,
|
|
|
|
emojiReactRequest,
|
|
|
|
emojiReactSuccess,
|
|
|
|
emojiReactFail,
|
|
|
|
unEmojiReactRequest,
|
|
|
|
unEmojiReactSuccess,
|
|
|
|
unEmojiReactFail,
|
|
|
|
};
|