Convert polls to Typescript / Immutable Records (#29789)
This commit is contained in:
parent
e4e35ab134
commit
ded799f913
15 changed files with 272 additions and 186 deletions
|
@ -27,7 +27,7 @@ import { notificationPolicyReducer } from './notification_policy';
|
|||
import { notificationRequestsReducer } from './notification_requests';
|
||||
import notifications from './notifications';
|
||||
import { pictureInPictureReducer } from './picture_in_picture';
|
||||
import polls from './polls';
|
||||
import { pollsReducer } from './polls';
|
||||
import push_notifications from './push_notifications';
|
||||
import { relationshipsReducer } from './relationships';
|
||||
import search from './search';
|
||||
|
@ -70,7 +70,7 @@ const reducers = {
|
|||
filters,
|
||||
conversations,
|
||||
suggestions: suggestionsReducer,
|
||||
polls,
|
||||
polls: pollsReducer,
|
||||
trends,
|
||||
markers: markersReducer,
|
||||
picture_in_picture: pictureInPictureReducer,
|
||||
|
|
|
@ -1,45 +0,0 @@
|
|||
import { Map as ImmutableMap, fromJS } from 'immutable';
|
||||
|
||||
import { POLLS_IMPORT } from 'mastodon/actions/importer';
|
||||
|
||||
import { normalizePollOptionTranslation } from '../actions/importer/normalizer';
|
||||
import { STATUS_TRANSLATE_SUCCESS, STATUS_TRANSLATE_UNDO } from '../actions/statuses';
|
||||
|
||||
const importPolls = (state, polls) => state.withMutations(map => polls.forEach(poll => map.set(poll.id, fromJS(poll))));
|
||||
|
||||
const statusTranslateSuccess = (state, pollTranslation) => {
|
||||
return state.withMutations(map => {
|
||||
if (pollTranslation) {
|
||||
const poll = state.get(pollTranslation.id);
|
||||
|
||||
pollTranslation.options.forEach((item, index) => {
|
||||
map.setIn([pollTranslation.id, 'options', index, 'translation'], fromJS(normalizePollOptionTranslation(item, poll)));
|
||||
});
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
const statusTranslateUndo = (state, id) => {
|
||||
return state.withMutations(map => {
|
||||
const options = map.getIn([id, 'options']);
|
||||
|
||||
if (options) {
|
||||
options.forEach((item, index) => map.deleteIn([id, 'options', index, 'translation']));
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
const initialState = ImmutableMap();
|
||||
|
||||
export default function polls(state = initialState, action) {
|
||||
switch(action.type) {
|
||||
case POLLS_IMPORT:
|
||||
return importPolls(state, action.polls);
|
||||
case STATUS_TRANSLATE_SUCCESS:
|
||||
return statusTranslateSuccess(state, action.translation.poll);
|
||||
case STATUS_TRANSLATE_UNDO:
|
||||
return statusTranslateUndo(state, action.pollId);
|
||||
default:
|
||||
return state;
|
||||
}
|
||||
}
|
67
app/javascript/mastodon/reducers/polls.ts
Normal file
67
app/javascript/mastodon/reducers/polls.ts
Normal file
|
@ -0,0 +1,67 @@
|
|||
import type { Reducer } from '@reduxjs/toolkit';
|
||||
import { Map as ImmutableMap } from 'immutable';
|
||||
|
||||
import { importPolls } from 'mastodon/actions/importer/polls';
|
||||
import { makeEmojiMap } from 'mastodon/models/custom_emoji';
|
||||
import { createPollOptionTranslationFromServerJSON } from 'mastodon/models/poll';
|
||||
import type { Poll } from 'mastodon/models/poll';
|
||||
|
||||
import {
|
||||
STATUS_TRANSLATE_SUCCESS,
|
||||
STATUS_TRANSLATE_UNDO,
|
||||
} from '../actions/statuses';
|
||||
|
||||
const initialState = ImmutableMap<string, Poll>();
|
||||
type PollsState = typeof initialState;
|
||||
|
||||
const statusTranslateSuccess = (
|
||||
state: PollsState,
|
||||
pollTranslation: Poll | undefined,
|
||||
) => {
|
||||
if (!pollTranslation) return state;
|
||||
|
||||
return state.withMutations((map) => {
|
||||
const poll = state.get(pollTranslation.id);
|
||||
|
||||
if (!poll) return;
|
||||
|
||||
const emojiMap = makeEmojiMap(poll.emojis);
|
||||
|
||||
pollTranslation.options.forEach((item, index) => {
|
||||
map.setIn(
|
||||
[pollTranslation.id, 'options', index, 'translation'],
|
||||
createPollOptionTranslationFromServerJSON(item, emojiMap),
|
||||
);
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
const statusTranslateUndo = (state: PollsState, id: string) => {
|
||||
return state.withMutations((map) => {
|
||||
const options = map.get(id)?.options;
|
||||
|
||||
if (options) {
|
||||
options.forEach((item, index) =>
|
||||
map.deleteIn([id, 'options', index, 'translation']),
|
||||
);
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
export const pollsReducer: Reducer<PollsState> = (
|
||||
state = initialState,
|
||||
action,
|
||||
) => {
|
||||
if (importPolls.match(action)) {
|
||||
return state.withMutations((polls) => {
|
||||
action.payload.polls.forEach((poll) => polls.set(poll.id, poll));
|
||||
});
|
||||
} else if (action.type === STATUS_TRANSLATE_SUCCESS)
|
||||
return statusTranslateSuccess(
|
||||
state,
|
||||
(action.translation as { poll?: Poll }).poll,
|
||||
);
|
||||
else if (action.type === STATUS_TRANSLATE_UNDO)
|
||||
return statusTranslateUndo(state, action.pollId as string);
|
||||
else return state;
|
||||
};
|
Loading…
Add table
Add a link
Reference in a new issue