diff --git a/PollApp.ts b/PollApp.ts index 8922cf1..5675bc6 100644 --- a/PollApp.ts +++ b/PollApp.ts @@ -15,7 +15,7 @@ import { UIKitViewSubmitInteractionContext, } from '@rocket.chat/apps-engine/definition/uikit'; -import { createPollMessage } from './src/lib/createPollMessage'; +import { checkDeleteChoice, createPollMessage } from './src/lib/createPollMessage'; import { createPollModal } from './src/lib/createPollModal'; import { finishPollMessage } from './src/lib/finishPollMessage'; import { votePoll } from './src/lib/votePoll'; @@ -85,11 +85,23 @@ export class PollApp extends App implements IUIKitInteractionHandler { } case 'addChoice': { + const value = parseInt(String(data.value), 10); + checkDeleteChoice(value); const modal = await createPollModal({ id: data.container.id, data, persistence, modify, options: parseInt(String(data.value), 10) }); return context.getInteractionResponder().updateModalViewResponse(modal); } + case `deleteChoice`: { + let value = parseInt(String(data.value), 10); + if (value < 4) { + value = 4; + } + checkDeleteChoice(value - 2); + const modal = await createPollModal({ id: data.container.id, data, persistence, modify, options: value - 2 }); + return context.getInteractionResponder().updateModalViewResponse(modal); + } + case 'finish': { try { await finishPollMessage({ data, read, persistence, modify }); diff --git a/src/lib/createPollMessage.ts b/src/lib/createPollMessage.ts index bdd6b2e..47fe1f1 100644 --- a/src/lib/createPollMessage.ts +++ b/src/lib/createPollMessage.ts @@ -8,6 +8,11 @@ import { import { IPoll } from '../IPoll'; import { createPollBlocks } from './createPollBlocks'; +let totalChoices = 2; +export const checkDeleteChoice = (value) => { + totalChoices = value; +} + export async function createPollMessage(data: IUIKitViewSubmitIncomingInteraction, read: IRead, modify: IModify, persistence: IPersistence, uid: string) { const { view: { id } } = data; const { state }: { @@ -25,6 +30,7 @@ export async function createPollMessage(data: IUIKitViewSubmitIncomingInteractio const options = Object.entries(state.poll || {}) .filter(([key]) => key !== 'question') + .filter(([param]) => param < `option-${totalChoices}`) .map(([, option]) => option) .filter((option) => option.trim() !== ''); diff --git a/src/lib/createPollModal.ts b/src/lib/createPollModal.ts index 88f410f..c2f3ee8 100644 --- a/src/lib/createPollModal.ts +++ b/src/lib/createPollModal.ts @@ -37,6 +37,24 @@ export async function createPollModal({ id = '', question, persistence, data, mo }); } + block + .addActionsBlock({ + blockId: 'config', + elements: [ + block.newButtonElement({ + actionId: 'deleteChoice', + text: block.newPlainTextObject('Delete Choice'), + value: String(options + 1), + }), + block.newButtonElement({ + actionId: 'addChoice', + text: block.newPlainTextObject('Add a choice'), + value: String(options + 1), + }), + ], + }) + .addDividerBlock(); + block .addActionsBlock({ blockId: 'config', @@ -56,11 +74,14 @@ export async function createPollModal({ id = '', question, persistence, data, mo }, ], }), - block.newButtonElement({ - actionId: 'addChoice', - text: block.newPlainTextObject('Add a choice'), - value: String(options + 1), - }), + ], + }) + .addDividerBlock(); + + block + .addActionsBlock({ + blockId: 'config', + elements: [ block.newStaticSelectElement({ placeholder: block.newPlainTextObject('Open vote'), actionId: 'visibility',