Skip to content

Commit

Permalink
feat: import and export polls
Browse files Browse the repository at this point in the history
Signed-off-by: Maksim Sukharev <[email protected]>
  • Loading branch information
Antreesy committed Oct 8, 2024
1 parent acd558e commit d4523c1
Showing 1 changed file with 58 additions and 0 deletions.
58 changes: 58 additions & 0 deletions src/components/NewMessage/NewMessagePollEditor.vue
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,18 @@
{{ t('spreed', 'Question') }}
</p>
<NcTextField :value.sync="pollForm.question" :label="t('spreed', 'Ask a question')" v-on="$listeners" />
<!--native file picker, hidden -->
<input id="poll-upload"
ref="pollImport"
type="file"
class="hidden-visually"
@change="importPoll">
<NcButton class="poll-editor__button"
type="secondary"
wide
@click="triggerImport">
{{ t('spreed', 'Import poll from file') }}
</NcButton>

<!-- Poll options -->
<p class="poll-editor__caption">
Expand Down Expand Up @@ -58,6 +70,12 @@
<NcButton type="tertiary" @click="dismissEditor">
{{ t('spreed', 'Dismiss') }}
</NcButton>
<NcButton v-if="isFilled"
type="secondary"
:href="exportPollBlob"
:download="exportPollFileName">
{{ t('spreed', 'Export') }}
</NcButton>
<NcButton type="primary" :disabled="!isFilled" @click="createPoll">
{{ t('spreed', 'Create poll') }}
</NcButton>
Expand Down Expand Up @@ -111,10 +129,23 @@ export default {
})
const isFilled = computed(() => !!pollForm.question || pollForm.options.some(option => option))
const exportPollBlob = computed(() => {
if (!isFilled.value) {
return null
}
const jsonString = JSON.stringify(pollForm, null, 2)
const blob = new Blob([jsonString], { type: 'application/json' })
return URL.createObjectURL(blob)
})
const exportPollFileName = `Talk Poll ${new Date().toISOString().slice(0, 10)}`
return {
pollsStore: usePollsStore(),
pollForm,
isFilled,
exportPollBlob,
exportPollFileName,
}
},
Expand Down Expand Up @@ -149,6 +180,29 @@ export default {
}
},
triggerImport() {
this.$refs.pollImport.click()
},
importPoll(event) {
if (!event?.target?.files?.[0]) {
return
}
const reader = new FileReader()
reader.onload = (e) => {
try {
const jsonObject = JSON.parse(e.target.result)
for (const key of Object.keys(this.pollForm)) {
this.pollForm[key] = jsonObject[key]
}
} catch (error) {
console.error('Error while parsing JSON:', error)
}
}
reader.readAsText(event.target.files[0])
},
},
}
</script>
Expand All @@ -162,6 +216,10 @@ export default {
color: var(--color-primary-element);
}
&__button {
margin-block: 8px;
}
&__option {
display: flex;
align-items: flex-end;
Expand Down

0 comments on commit d4523c1

Please sign in to comment.