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: preserve message text in the input field #537

Merged
Show file tree
Hide file tree
Changes from 5 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
23 changes: 20 additions & 3 deletions src/components/AChat/AChatForm.vue
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,20 @@ export default {
showDivider: {
type: Boolean,
default: false
},
clearInputValueOnSend: {
type: Boolean,
default: true
},
/**
* Message validator.
*/
validator: {
type: Function,
required: true
}
},
emits: ['message', 'esc'],
emits: ['message', 'esc', 'error'],
data: () => ({
message: ''
}),
Expand Down Expand Up @@ -118,8 +129,14 @@ export default {
},
methods: {
submitMessage() {
this.$emit('message', this.message)
this.message = ''
const error = this.validator(this.message)
if (error === false) {
this.$emit('message', this.message)
this.message = ''
} else {
this.$emit('error', error)
}

// Fix textarea height to 1 row after miltiline message send
this.calculateInputHeight()
this.focus()
Expand Down
50 changes: 34 additions & 16 deletions src/components/Chat/Chat.vue
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,9 @@
:label="chatFormLabel"
:message-text="$route.query.messageText"
@message="onMessage"
@error="onMessageError"
@esc="replyMessageId = -1"
:validator="messageValidator.bind(this)"
>
<template #prepend>
<chat-menu
Expand Down Expand Up @@ -256,7 +258,7 @@ import { detect } from 'detect-browser'
import Visibility from 'visibilityjs'
import copyToClipboard from 'copy-to-clipboard'

import { Cryptos } from '@/lib/constants'
import { Cryptos, Fees } from '@/lib/constants'
import EmojiPicker from '@/components/EmojiPicker.vue'

import {
Expand Down Expand Up @@ -302,34 +304,36 @@ function getUserMeta(userId) {
return user
}

const validationErrors = {
bludnic marked this conversation as resolved.
Show resolved Hide resolved
emptyMessage: 'EMPTY_MESSAGE',
notEnoughFunds: 'NON_ENOUGH_FUNDS',
notEnoughFundsNewAccount: 'NON_ENOUGH_FUNDS_NEW_ACCOUNT',
messageTooLong: 'MESSAGE_LENGTH_EXCEED'
}
/**
* Validate message before sending.
* @param {string} message
* @returns {boolean}
* @returns {string | false} If `false` then validation passed without errors.
*/
function validateMessage(message) {
// Ensure that message contains at least one non-whitespace character
if (!message.trim().length) {
return false
return validationErrors.emptyMessage
}

if (this.$store.state.balance < 0.001) {
if (this.$store.state.balance < Fees.NOT_ADM_TRANSFER) {
if (this.$store.getters.isAccountNew()) {
this.showFreeTokensDialog = true
return validationErrors.notEnoughFundsNewAccount
} else {
this.$store.dispatch('snackbar/show', { message: this.$t('chats.no_money') })
return validationErrors.notEnoughFunds
}
return false
}

if (message.length * 1.5 > 20000) {
this.$store.dispatch('snackbar/show', {
message: this.$t('chats.too_long')
})
return false
return validationErrors.messageTooLong
}

return true
return false
}

export default {
Expand Down Expand Up @@ -476,11 +480,25 @@ export default {
}
},
methods: {
messageValidator: validateMessage,
onMessage(message) {
if (validateMessage.call(this, message)) {
this.sendMessage(message)
nextTick(() => this.$refs.chat.scrollToBottom())
this.replyMessageId = -1
this.sendMessage(message)
nextTick(() => this.$refs.chat.scrollToBottom())
this.replyMessageId = -1
},
onMessageError(error) {
switch (error) {
case validationErrors.notEnoughFundsNewAccount:
this.showFreeTokensDialog = true
return
case validationErrors.notEnoughFunds:
this.$store.dispatch('snackbar/show', { message: this.$t('chats.no_money') })
return
case validationErrors.messageTooLong:
this.$store.dispatch('snackbar/show', {
message: this.$t('chats.too_long')
})
return
}
},
sendMessage(message) {
Expand Down
Loading