-
-
Notifications
You must be signed in to change notification settings - Fork 2
Added "Unlock" Feature #40
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 4 commits
6023d4d
1046fed
64918e2
6bbdcc6
1b47bf3
1fdaf34
b3bedd0
e36a1cd
48decf4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -34,23 +34,8 @@ export class Poll { | |
|
|
||
| const titleBlock = Static.buildSectionBlock(mrkdwnValue); | ||
| message.push(titleBlock, Static.buildContextBlock(`Asked by: ${author}`)); | ||
| const actionBlocks: ActionsBlock[] = [{ type: "actions", elements: [] }]; | ||
| let actionBlockCount = 0; | ||
| // Construct all the buttons | ||
| const start = titleBlock.text!.text === parameters[0] ? 1 : 2; | ||
| for (let i = start; i < parameters.length; i++) { | ||
| if (i % 5 === 0) { | ||
| const newActionBlock: ActionsBlock = { type: "actions", elements: [] }; | ||
| actionBlocks.push(newActionBlock); | ||
| actionBlockCount++; | ||
| } | ||
| // Remove special characters, should be able to remove this once slack figures itself out | ||
| parameters[i] = parameters[i].replace("&", "+").replace("<", "greater than ") | ||
| .replace(">", "less than "); | ||
| // We set value to empty string so that it is always defined | ||
| const button: Button = { type: "button", value: " ", text: Static.buildTextElem(parameters[i]) }; | ||
| actionBlocks[actionBlockCount].elements.push(button); | ||
| } | ||
| const actionBlocks = Static.buildVoteOptions(parameters,start); | ||
| // The various poll options | ||
| const selection: StaticSelect = { | ||
| type: "static_select", | ||
|
|
@@ -83,8 +68,8 @@ export class Poll { | |
| // Since its databaseless the way we know if it is anonymous or multiple is by parsing the title | ||
| this.multiple = this.checkIfMsgContains("(Multiple Answers)"); | ||
| this.anonymous = this.checkIfMsgContains("(Anonymous)"); | ||
| // If there's no buttons then the poll is locked | ||
| this.isLocked = this.message[3].type === "divider"; | ||
| //if the is a lock symbol right below the divider, the poll is locked | ||
| this.isLocked = (this.message.length-1 === this.getDividerId())? false : ((this.message[this.getDividerId()+1] as SectionBlock).text as MrkdwnElement).text === ":lock:"; | ||
|
zapdos26 marked this conversation as resolved.
Outdated
|
||
| } | ||
|
|
||
| public getBlocks(): KnownBlock[] { | ||
|
|
@@ -104,6 +89,23 @@ export class Poll { | |
| return { votes, userIdIndex: votes.indexOf(userId) }; | ||
| } | ||
|
|
||
| private getDynamicSelect(): ActionsBlock[] { | ||
| const selection: StaticSelect = { | ||
| type: "static_select", | ||
| placeholder: Static.buildTextElem("Poll Options"), | ||
| options: [ | ||
| Static.buildSelectOption("Reset your vote", "reset"), | ||
| this.isLocked ? Static.buildSelectOption(":unlock: Unlock poll", "unlock") : Static.buildSelectOption(":lock: Lock poll", "lock"), | ||
| Static.buildSelectOption("Move to bottom", "bottom"), | ||
| Static.buildSelectOption("Delete poll", "delete") | ||
| ] | ||
| }; | ||
| if (this.anonymous) { | ||
| selection.options!.push(Static.buildSelectOption("Collect Results", "collect")); | ||
| } | ||
| return [{ type: "actions", elements: [selection] }]; | ||
| } | ||
|
|
||
| public resetVote(userId: string): void { | ||
| this.processButtons(this.message.length, button => { | ||
| const { votes, userIdIndex } = this.getVotesAndUserIndex(button, userId); | ||
|
|
@@ -117,6 +119,7 @@ export class Poll { | |
| } | ||
|
|
||
| public vote(buttonText: string, userId: string): void { | ||
| if (this.isLocked) return; | ||
|
zapdos26 marked this conversation as resolved.
|
||
| this.processButtons(this.message.length, button => { | ||
| const { votes, userIdIndex } = this.getVotesAndUserIndex(button, userId); | ||
| if (!this.multiple && userIdIndex > -1 && button.text.text !== buttonText) { | ||
|
|
@@ -131,13 +134,17 @@ export class Poll { | |
| } | ||
|
|
||
| public lockPoll(): void { | ||
| if (this.isLocked) return; | ||
| this.isLocked = true; | ||
| this.generateVoteResults(); | ||
| this.message = this.message.slice(0, 2).concat(this.message.slice(this.getDividerId() - 1)); | ||
| this.message = this.message.slice(0, this.getDividerId()-1).concat(this.getDynamicSelect()).concat(this.message.slice(this.getDividerId())); | ||
| // ((this.message[2] as ActionsBlock).elements[0] as StaticSelect).options!.splice(0, 2); | ||
| } | ||
|
|
||
| public unlockpoll(): void { | ||
| this.isLocked = false; | ||
| this.message = this.message.slice(0,this.getDividerId()-1).concat(this.getDynamicSelect()).concat({ type: "divider" }).concat(this.message.slice(this.getDividerId()+2)); | ||
| } | ||
|
|
||
| // Creates the message that will be sent to the poll author with the final results | ||
| public collectResults(): KnownBlock[] { | ||
| const results = this.generateResults(true); | ||
|
|
@@ -162,8 +169,6 @@ export class Poll { | |
| private buildVoteTally(overrideAnon: boolean, votes: any, key: string): SectionBlock | null { | ||
| const users: string[] = votes[key].split(","); | ||
| users.splice(0, 1); | ||
| // Don"t bother with empty votes | ||
| if (users.length === 0) return null; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does this mean we're now going to show an option if it has 0 votes?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Originally, yes. However since there appears to be a want for anonymous polling, we can re-add it.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Well even with non anonymous polling we don't want to show votes with 0 people in the results block.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We need to though, otherwise how will we know what the vote options are?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. But it looks like you kept the buttons but disabled voting.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There were previous iterations (see previous commits) where the buttons were deleted. However I re-added the buttons since they store information in regards to who voted. Since we kept the buttons, this will be re-added. |
||
| // When anonymous we don"t display the user"s names | ||
| const names = !this.anonymous || overrideAnon ? users.map((k: string) => `<@${k}>`).join(",") : "~HIDDEN~"; | ||
| return Static.buildSectionBlock(`*${users.length}* ${key} » ${names}`); | ||
|
|
@@ -183,7 +188,7 @@ export class Poll { | |
| } | ||
|
|
||
| private generateVoteResults(): void { | ||
| // We throw out the old vote response and construct them again | ||
| // We throw out the old vote response and construct them again | ||
| this.message = this.message.slice(0, this.getDividerId() + 1).concat(this.generateResults(false)); | ||
| } | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| import { | ||
| SectionBlock, ContextBlock, PlainTextElement, Option | ||
| SectionBlock, ContextBlock, PlainTextElement, Option, ActionsBlock, Button | ||
| } from "@slack/types"; | ||
|
|
||
| export class Static { | ||
|
|
@@ -8,18 +8,37 @@ export class Static { | |
| } | ||
|
|
||
| public static buildSectionBlock(mrkdwnValue: string): SectionBlock { | ||
| return { type: "section", text: { type: "mrkdwn", text: mrkdwnValue } }; | ||
| return {type: "section", text: {type: "mrkdwn", text: mrkdwnValue}}; | ||
| } | ||
|
|
||
| public static buildContextBlock(mrkdwnValue: string): ContextBlock { | ||
| return { type: "context", elements: [ { type: "mrkdwn", text: mrkdwnValue } ] }; | ||
| return {type: "context", elements: [{type: "mrkdwn", text: mrkdwnValue}]}; | ||
|
zapdos26 marked this conversation as resolved.
|
||
| } | ||
|
|
||
| public static buildSelectOption(text: string, value: string): Option { | ||
| return { text: this.buildTextElem(text), value: value }; | ||
| return {text: this.buildTextElem(text), value: value}; | ||
| } | ||
|
|
||
| public static buildTextElem(text: string): PlainTextElement { | ||
| return { type: "plain_text", text, emoji: true }; | ||
| return {type: "plain_text", text, emoji: true}; | ||
| } | ||
| public static buildVoteOptions(parameters: string [], start: number ): ActionsBlock[] { | ||
| const actionBlocks: ActionsBlock[] = [{ type: "actions", elements: [] }]; | ||
| let actionBlockCount = 0; | ||
| // Construct all the buttons | ||
| for (let i = start; i < parameters.length; i++) { | ||
| if (i % 5 === 0 && i != 0) { | ||
| const newActionBlock: ActionsBlock = { type: "actions", elements: [] }; | ||
| actionBlocks.push(newActionBlock); | ||
|
Comment on lines
+31
to
+32
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should just make this one line if we're not going to re-use the defined constant again. |
||
| actionBlockCount++; | ||
| } | ||
| // Remove special characters, should be able to remove this once slack figures itself out | ||
| parameters[i] = parameters[i].replace("&", "+").replace("<", "greater than ") | ||
|
zapdos26 marked this conversation as resolved.
Outdated
|
||
| .replace(">", "less than "); | ||
| // We set value to empty string so that it is always defined | ||
| const button: Button = { type: "button", value: " ", text: Static.buildTextElem(parameters[i]) }; | ||
| actionBlocks[actionBlockCount].elements.push(button); | ||
|
Comment on lines
+39
to
+40
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same as above. |
||
| } | ||
| return actionBlocks; | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.