Typescript: reducers/polls.ts

virtualized-window
Alex Gleason 3 years ago
parent 1ce340c1c5
commit c0c758c103
No known key found for this signature in database
GPG Key ID: 7211D1F99744FBB7

@ -1,28 +0,0 @@
import { Map as ImmutableMap } from 'immutable';
import { POLLS_IMPORT } from 'soapbox/actions/importer';
import { normalizeStatus } from 'soapbox/normalizers/status';
// HOTFIX: Convert the poll into a fake status to normalize it...
// TODO: get rid of POLLS_IMPORT and use STATUS_IMPORT here.
const normalizePoll = poll => {
const status = { poll };
return normalizeStatus(status).poll;
};
const importPolls = (state, polls) => {
return state.withMutations(map => {
return polls.forEach(poll => map.set(poll.id, normalizePoll(poll)));
});
};
const initialState = ImmutableMap();
export default function polls(state = initialState, action) {
switch(action.type) {
case POLLS_IMPORT:
return importPolls(state, action.polls);
default:
return state;
}
}

@ -0,0 +1,39 @@
import { Map as ImmutableMap } from 'immutable';
import { POLLS_IMPORT } from 'soapbox/actions/importer';
import { normalizeStatus } from 'soapbox/normalizers/status';
import type { AnyAction } from 'redux';
import type { Poll, APIEntity, EmbeddedEntity } from 'soapbox/types/entities';
type State = ImmutableMap<string, Poll>;
// HOTFIX: Convert the poll into a fake status to normalize it...
// TODO: get rid of POLLS_IMPORT and use STATUS_IMPORT here.
const normalizePoll = (poll: any): EmbeddedEntity<Poll> => {
const status = { poll };
return normalizeStatus(status).poll;
};
const importPolls = (state: State, polls: Array<APIEntity>) => {
return state.withMutations(map => {
return polls.forEach(poll => {
const normalPoll = normalizePoll(poll);
if (normalPoll && typeof normalPoll === 'object') {
map.set(normalPoll.id, normalPoll);
}
});
});
};
const initialState: State = ImmutableMap();
export default function polls(state: State = initialState, action: AnyAction): State {
switch(action.type) {
case POLLS_IMPORT:
return importPolls(state, action.polls);
default:
return state;
}
}

@ -1,6 +1,5 @@
import escapeTextContentForBrowser from 'escape-html';
import { Map as ImmutableMap, List as ImmutableList } from 'immutable';
import { AnyAction } from 'redux';
import emojify from 'soapbox/features/emoji/emoji';
import { normalizeStatus } from 'soapbox/normalizers';
@ -32,6 +31,8 @@ import {
} from '../actions/statuses';
import { TIMELINE_DELETE } from '../actions/timelines';
import type { AnyAction } from 'redux';
const domParser = new DOMParser();
type StatusRecord = ReturnType<typeof normalizeStatus>;

@ -27,6 +27,7 @@ type PollOption = ReturnType<typeof PollOptionRecord>;
type Status = ReturnType<typeof StatusRecord>;
// Utility types
type APIEntity = Record<string, any>;
type EmbeddedEntity<T extends object> = null | string | ReturnType<ImmutableRecord.Factory<T>>;
export {
@ -43,5 +44,6 @@ export {
Status,
// Utility types
APIEntity,
EmbeddedEntity,
};

Loading…
Cancel
Save