Skip to content
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

[FEAT][NEW] Added delete choice button #40

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion PollApp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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 });
Expand Down
6 changes: 6 additions & 0 deletions src/lib/createPollMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 }: {
Expand All @@ -25,6 +30,7 @@ export async function createPollMessage(data: IUIKitViewSubmitIncomingInteractio

const options = Object.entries<any>(state.poll || {})
.filter(([key]) => key !== 'question')
.filter(([param]) => param < `option-${totalChoices}`)
.map(([, option]) => option)
.filter((option) => option.trim() !== '');

Expand Down
31 changes: 26 additions & 5 deletions src/lib/createPollModal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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',
Expand Down