| Conditions | 5 |
| Total Lines | 61 |
| Code Lines | 35 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | import {choiceSection} from './helpers/vote'; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * Builds the configuration form. |
||
| 49 | * |
||
| 50 | * @param {object} poll - Current state of poll |
||
| 51 | * @param {object} poll.author - User that submitted the poll |
||
| 52 | * @param {string} poll.topic - Topic of poll |
||
| 53 | * @param {string[]} poll.choices - Text of choices to display to users |
||
| 54 | * @param {object} poll.votes - Map of cast votes keyed by choice index |
||
| 55 | * @param {object} poll.choiceCreator - Map of cast votes keyed by choice index |
||
| 56 | * @param {boolean} poll.anon - Is anonymous?(will save voter name or not) |
||
| 57 | * @param {boolean} poll.optionable - Can other user add other option? |
||
| 58 | * @returns {object} card |
||
| 59 | */ |
||
| 60 | export function buildVoteCard(poll) { |
||
| 61 | const sections = []; |
||
| 62 | const state = JSON.stringify(poll); |
||
| 63 | |||
| 64 | const votes: Array<Array<object>> = Object.values(poll.votes); |
||
| 65 | const totalVotes: number = votes.reduce((sum, vote) => sum + vote.length, 0); |
||
| 66 | for (let i = 0; i < poll.choices.length; ++i) { |
||
| 67 | const creator = poll.choiceCreator?.[i]; |
||
| 68 | const section = choiceSection(i, poll, totalVotes, state, creator); |
||
| 69 | sections.push(section); |
||
| 70 | } |
||
| 71 | if (poll.optionable) { |
||
| 72 | sections.push( |
||
| 73 | { |
||
| 74 | 'widgets': [ |
||
| 75 | { |
||
| 76 | 'buttonList': { |
||
| 77 | 'buttons': [ |
||
| 78 | { |
||
| 79 | 'text': 'Add Option', |
||
| 80 | 'onClick': { |
||
| 81 | 'action': { |
||
| 82 | 'function': 'add_option_form', |
||
| 83 | 'interaction': 'OPEN_DIALOG', |
||
| 84 | 'parameters': [], |
||
| 85 | }, |
||
| 86 | }, |
||
| 87 | }, |
||
| 88 | ], |
||
| 89 | }, |
||
| 90 | }, |
||
| 91 | ], |
||
| 92 | }); |
||
| 93 | } |
||
| 94 | const card: chatV1.Schema$CardWithId = { |
||
| 95 | 'cardId': 'unique-card-id', |
||
| 96 | 'card': { |
||
| 97 | sections, |
||
| 98 | }, |
||
| 99 | }; |
||
| 100 | if (poll.topic.length > 40) { |
||
| 101 | const widgetHeader = sectionHeader(poll.topic, poll.author.displayName); |
||
| 102 | card.card.sections = [widgetHeader, ...sections]; |
||
| 103 | } else { |
||
| 104 | card.card.header = cardHeader(poll.topic, poll.author.displayName); |
||
| 105 | } |
||
| 106 | return card; |
||
| 107 | } |
||
| 108 |