From 814c0890851a6a6983488902b90c72405ae304a7 Mon Sep 17 00:00:00 2001 From: Maksim Sukharev Date: Mon, 27 Jan 2025 17:16:46 +0100 Subject: [PATCH] feat(polls): allow to update drafts Signed-off-by: Maksim Sukharev --- .../Message/MessagePart/Poll.vue | 22 +++++++++++- .../PollViewer/PollDraftHandler.vue | 10 +++--- src/components/PollViewer/PollEditor.vue | 34 ++++++++++++++++--- src/components/PollViewer/PollManager.vue | 5 +-- src/services/EventBus.ts | 2 +- 5 files changed, 61 insertions(+), 12 deletions(-) diff --git a/src/components/MessagesList/MessagesGroup/Message/MessagePart/Poll.vue b/src/components/MessagesList/MessagesGroup/Message/MessagePart/Poll.vue index 0f451d8aeb1..6899940791e 100644 --- a/src/components/MessagesList/MessagesGroup/Message/MessagePart/Poll.vue +++ b/src/components/MessagesList/MessagesGroup/Message/MessagePart/Poll.vue @@ -9,6 +9,15 @@ {{ name }} + + + @@ -73,10 +73,12 @@ const pollDraftsLoaded = computed(() => pollsStore.draftsLoaded(props.token)) /** * Opens poll editor pre-filled from the draft - * @param id poll draft ID + * @param payload method payload + * @param payload.id poll draft ID + * @param payload.action required action ('fill' from draft or 'edit' draft) */ -function openPollEditor(id: number | null) { - EventBus.emit('poll-editor-open', { id, fromDrafts: !props.editorOpened, selector: props.container }) +function openPollEditor({ id, action } : { id: number | null, action?: string }) { + EventBus.emit('poll-editor-open', { id, fromDrafts: !props.editorOpened, action, selector: props.container }) } diff --git a/src/components/PollViewer/PollEditor.vue b/src/components/PollViewer/PollEditor.vue index 9dc65327de0..ff43e17db55 100644 --- a/src/components/PollViewer/PollEditor.vue +++ b/src/components/PollViewer/PollEditor.vue @@ -4,7 +4,7 @@ --> @@ -157,6 +157,7 @@ const store = useStore() const pollsStore = usePollsStore() const isOpenedFromDraft = ref(false) +const editingDraftId = ref(null) const pollOption = ref(null) const pollImport = ref(null) @@ -168,7 +169,14 @@ const pollForm = reactive({ }) const isFilled = computed(() => Boolean(pollForm.question) && pollForm.options.filter(option => Boolean(option)).length >= 2) +const dialogName = computed(() => { + return editingDraftId.value ? t('spreed', 'Edit poll draft') : t('spreed', 'Create new poll') +}) const createPollLabel = computed(() => { + if (editingDraftId.value) { + return t('spreed', 'Save') + } + return store.getters.getToken() !== props.token ? t('spreed', 'Create poll in {name}', { name: store.getters.conversation(props.token).displayName }, undefined, { escape: false, sanitize: false }) @@ -217,7 +225,20 @@ function addOption() { /** * Post a poll into conversation */ -async function createPoll() { +async function handleSubmit() { + if (editingDraftId.value) { + await pollsStore.updatePollDraft({ + token: props.token, + pollId: editingDraftId.value, + form: pollForm, + }) + openPollDraftHandler() + nextTick(() => { + emit('close') + }) + return + } + const poll = await pollsStore.createPoll({ token: props.token, form: pollForm, @@ -231,12 +252,17 @@ async function createPoll() { * Pre-fills form from the draft * @param id poll draft ID * @param fromDrafts whether editor was opened from drafts handler + * @param action required action ('fill' from draft or 'edit' draft) */ -function fillPollEditorFromDraft(id: number | null, fromDrafts: boolean) { +function fillPollEditorFromDraft(id: number | null, fromDrafts: boolean, action?: string) { if (fromDrafts) { // Show 'Back' button, do not reset until closed isOpenedFromDraft.value = true } + if (action === 'edit') { + // Show Edit interface + editingDraftId.value = id + } if (id && pollsStore.drafts[props.token][id]) { fillPollForm(pollsStore.drafts[props.token][id]) diff --git a/src/components/PollViewer/PollManager.vue b/src/components/PollViewer/PollManager.vue index d79bdd80d98..7eb0b9b43a0 100644 --- a/src/components/PollViewer/PollManager.vue +++ b/src/components/PollViewer/PollManager.vue @@ -57,13 +57,14 @@ function openPollDraftHandler({ selector }: Events['poll-drafts-open']) { * @param payload event payload * @param payload.id poll draft ID to fill form with (null for empty form) * @param payload.fromDrafts whether editor was opened from PollDraftHandler dialog + * @param payload.action required action ('fill' from draft or 'edit' draft) * @param [payload.selector] selector to mount dialog to (body by default) */ -function openPollEditor({ id, fromDrafts, selector }: Events['poll-editor-open']) { +function openPollEditor({ id, fromDrafts, action, selector }: Events['poll-editor-open']) { container.value = selector showPollEditor.value = true nextTick(() => { - pollEditorRef.value?.fillPollEditorFromDraft(id, fromDrafts) + pollEditorRef.value?.fillPollEditorFromDraft(id, fromDrafts, action) // Wait for editor to be mounted and filled before unmounting drafts dialog to avoid issues when inserting nodes showPollDraftHandler.value = false }) diff --git a/src/services/EventBus.ts b/src/services/EventBus.ts index 104aa732224..d90a5852a31 100644 --- a/src/services/EventBus.ts +++ b/src/services/EventBus.ts @@ -24,7 +24,7 @@ export type Events = Record & { 'joined-conversation': { token: string }, 'message-height-changed': { heightDiff: number }, 'poll-drafts-open': { selector?: string }, - 'poll-editor-open': { id: number | null, fromDrafts: boolean, selector?: string }, + 'poll-editor-open': { id: number | null, fromDrafts: boolean, action?: string, selector?: string }, 'refresh-peer-list': void, 'retry-message': number, 'route-change': { from: Route, to: Route },