From a94d0f1f77160da9448e26d0e7082d0865ef383d Mon Sep 17 00:00:00 2001 From: Maksim Sukharev Date: Mon, 14 Oct 2024 20:42:13 +0200 Subject: [PATCH] feat: create new poll from drafts Signed-off-by: Maksim Sukharev --- src/components/NewMessage/NewMessage.vue | 14 +++++++++++++ .../NewMessage/NewMessagePollEditor.vue | 21 +++++++++++++++++++ .../PollViewer/PollDraftHandler.vue | 18 +++++++++++++++- 3 files changed, 52 insertions(+), 1 deletion(-) diff --git a/src/components/NewMessage/NewMessage.vue b/src/components/NewMessage/NewMessage.vue index 29efd484c0c..3162828d18e 100644 --- a/src/components/NewMessage/NewMessage.vue +++ b/src/components/NewMessage/NewMessage.vue @@ -148,6 +148,7 @@ @@ -545,6 +546,7 @@ export default { EventBus.on('upload-discard', this.handleUploadSideEffects) EventBus.on('retry-message', this.handleRetryMessage) EventBus.on('smart-picker-open', this.handleOpenTributeMenu) + EventBus.on('poll-editor-open', this.fillPollEditorFromDraft) EventBus.on('poll-drafts-open', this.togglePollDraftHandler) if (!this.$store.getters.areFileTemplatesInitialised) { @@ -558,6 +560,7 @@ export default { EventBus.off('upload-discard', this.handleUploadSideEffects) EventBus.off('retry-message', this.handleRetryMessage) EventBus.off('smart-picker-open', this.handleOpenTributeMenu) + EventBus.off('poll-editor-open', this.fillPollEditorFromDraft) EventBus.off('poll-drafts-open', this.togglePollDraftHandler) }, @@ -901,6 +904,17 @@ export default { this.showPollEditor = !this.showPollEditor }, + fillPollEditorFromDraft(id) { + this.showPollEditor = true + this.$nextTick(() => { + if (id) { + this.$refs.pollEditor?.fillPollEditorFromDraft(id) + } + // Wait for editor to be mounted and filled before unmounting drafts dialog + this.togglePollDraftHandler() + }) + }, + togglePollDraftHandler() { this.showPollDraftHandler = !this.showPollDraftHandler }, diff --git a/src/components/NewMessage/NewMessagePollEditor.vue b/src/components/NewMessage/NewMessagePollEditor.vue index 9bcb0b5397b..9db6aebf639 100644 --- a/src/components/NewMessage/NewMessagePollEditor.vue +++ b/src/components/NewMessage/NewMessagePollEditor.vue @@ -113,6 +113,9 @@ const props = defineProps<{ const emit = defineEmits<{ (event: 'close'): void, }>() +defineExpose({ + fillPollEditorFromDraft, +}) const supportPollDrafts = hasTalkFeature(props.token, 'talk-polls-drafts') @@ -180,6 +183,24 @@ async function createPoll() { } } +/** + * Pre-fills form from the draft + * @param id poll draft ID + */ +function fillPollEditorFromDraft(id: number) { + fillPollForm(pollsStore.drafts[props.token][id]) +} + +/** + * Insert data into form fields + * @param payload data to fill with + */ +function fillPollForm(payload: createPollParams) { + for (const key of Object.keys(pollForm)) { + pollForm[key] = payload[key] + } +} + /** * Saves a poll draft for this conversation */ diff --git a/src/components/PollViewer/PollDraftHandler.vue b/src/components/PollViewer/PollDraftHandler.vue index 556cd8c6408..a7aa44f98f5 100644 --- a/src/components/PollViewer/PollDraftHandler.vue +++ b/src/components/PollViewer/PollDraftHandler.vue @@ -24,8 +24,14 @@ :key="item.id" :token="token" :name="item.question" - draft /> + draft + @click="openPollEditor(item.id)" /> + @@ -36,6 +42,7 @@ import IconPoll from 'vue-material-design-icons/Poll.vue' import { t } from '@nextcloud/l10n' +import NcButton from '@nextcloud/vue/dist/Components/NcButton.js' import NcDialog from '@nextcloud/vue/dist/Components/NcDialog.js' import EmptyView from '../EmptyView.vue' @@ -43,6 +50,7 @@ import Poll from '../MessagesList/MessagesGroup/Message/MessagePart/Poll.vue' import { useStore } from '../../composables/useStore.js' import { hasTalkFeature } from '../../services/CapabilitiesManager.ts' +import { EventBus } from '../../services/EventBus.js' import { usePollsStore } from '../../stores/polls.ts' const props = defineProps<{ @@ -63,6 +71,14 @@ if (supportPollDrafts && isModerator.value) { pollsStore.getPollDrafts(props.token) } const pollDrafts = computed(() => supportPollDrafts ? pollsStore.getDrafts(props.token) : []) + +/** + * Opens poll editor pre-filled from the draft + * @param id poll draft ID + */ +function openPollEditor(id) { + EventBus.emit('poll-editor-open', id) +}