mirror of https://github.com/glitch-soc/mastodon
58 lines
1.6 KiB
TypeScript
58 lines
1.6 KiB
TypeScript
import type { Reducer } from '@reduxjs/toolkit';
|
|
|
|
import { importPolls } from 'flavours/glitch/actions/importer/polls';
|
|
import { makeEmojiMap } from 'flavours/glitch/models/custom_emoji';
|
|
import { createPollOptionTranslationFromServerJSON } from 'flavours/glitch/models/poll';
|
|
import type { Poll } from 'flavours/glitch/models/poll';
|
|
|
|
import {
|
|
STATUS_TRANSLATE_SUCCESS,
|
|
STATUS_TRANSLATE_UNDO,
|
|
} from '../actions/statuses';
|
|
|
|
const initialState: Record<string, Poll> = {};
|
|
type PollsState = typeof initialState;
|
|
|
|
const statusTranslateSuccess = (state: PollsState, pollTranslation?: Poll) => {
|
|
if (!pollTranslation) return;
|
|
|
|
const poll = state[pollTranslation.id];
|
|
|
|
if (!poll) return;
|
|
|
|
const emojiMap = makeEmojiMap(poll.emojis);
|
|
|
|
pollTranslation.options.forEach((item, index) => {
|
|
const option = poll.options[index];
|
|
if (!option) return;
|
|
|
|
option.translation = createPollOptionTranslationFromServerJSON(
|
|
item,
|
|
emojiMap,
|
|
);
|
|
});
|
|
};
|
|
|
|
const statusTranslateUndo = (state: PollsState, id: string) => {
|
|
state[id]?.options.forEach((option) => {
|
|
option.translation = null;
|
|
});
|
|
};
|
|
|
|
export const pollsReducer: Reducer<PollsState> = (
|
|
draft = initialState,
|
|
action,
|
|
) => {
|
|
if (importPolls.match(action)) {
|
|
action.payload.polls.forEach((poll) => {
|
|
draft[poll.id] = poll;
|
|
});
|
|
} else if (action.type === STATUS_TRANSLATE_SUCCESS)
|
|
statusTranslateSuccess(draft, (action.translation as { poll?: Poll }).poll);
|
|
else if (action.type === STATUS_TRANSLATE_UNDO) {
|
|
statusTranslateUndo(draft, action.pollId as string);
|
|
}
|
|
|
|
return draft;
|
|
};
|