From c0c758c10362e369e3a40f1ed3b9cb3d5fb4c9e2 Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Sat, 26 Mar 2022 16:20:45 -0500 Subject: [PATCH] Typescript: reducers/polls.ts --- app/soapbox/reducers/polls.js | Bin 785 -> 0 bytes app/soapbox/reducers/polls.ts | 39 +++++++++++++++++++++++++++++++ app/soapbox/reducers/statuses.ts | 3 ++- app/soapbox/types/entities.ts | 2 ++ 4 files changed, 43 insertions(+), 1 deletion(-) delete mode 100644 app/soapbox/reducers/polls.js create mode 100644 app/soapbox/reducers/polls.ts diff --git a/app/soapbox/reducers/polls.js b/app/soapbox/reducers/polls.js deleted file mode 100644 index f9f220edcde720119b099ff20419170de33f60b4..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 785 zcmZWn%WA_g5WM><_7)q0d{0QB^g#m&4vh=F6~$iHqPAotxlJ4L?_EibexFY2qBq4>;IW3=U0bL+?SIiaS6`!ofwqD z3f{OFNDik*6B97Pqra*WE8aI2s$TkY2oXXaqKQeDIfRQz{3s0tBAcPa?01KuUU1sZ fbY%;&HfEfR?J~2WgHP{s43_@z{0x1_=p6k5(On9c diff --git a/app/soapbox/reducers/polls.ts b/app/soapbox/reducers/polls.ts new file mode 100644 index 0000000000..ff6e25567e --- /dev/null +++ b/app/soapbox/reducers/polls.ts @@ -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; + +// 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 => { + const status = { poll }; + return normalizeStatus(status).poll; +}; + +const importPolls = (state: State, polls: Array) => { + 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; + } +} diff --git a/app/soapbox/reducers/statuses.ts b/app/soapbox/reducers/statuses.ts index 3c4f2ce6f0..37d78e2c7f 100644 --- a/app/soapbox/reducers/statuses.ts +++ b/app/soapbox/reducers/statuses.ts @@ -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; diff --git a/app/soapbox/types/entities.ts b/app/soapbox/types/entities.ts index 65a5bf0579..cecdf235d4 100644 --- a/app/soapbox/types/entities.ts +++ b/app/soapbox/types/entities.ts @@ -27,6 +27,7 @@ type PollOption = ReturnType; type Status = ReturnType; // Utility types +type APIEntity = Record; type EmbeddedEntity = null | string | ReturnType>; export { @@ -43,5 +44,6 @@ export { Status, // Utility types + APIEntity, EmbeddedEntity, };