Skip to content

Commit

Permalink
Merge pull request #537 from juliahermak/feat--preserve-message-text-…
Browse files Browse the repository at this point in the history
…in-the-input-field,-if-it-was-not-sent

feat: preserve message text in the input field
  • Loading branch information
bludnic authored Nov 2, 2023
2 parents fdd27d9 + b3b1768 commit 4093d20
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 19 deletions.
19 changes: 16 additions & 3 deletions src/components/AChat/AChatForm.vue
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,16 @@ export default {
showDivider: {
type: Boolean,
default: false
},
/**
* Message validator.
*/
validator: {
type: Function,
required: true
}
},
emits: ['message', 'esc'],
emits: ['message', 'esc', 'error'],
data: () => ({
message: ''
}),
Expand Down Expand Up @@ -118,8 +125,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 @@ -265,7 +267,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 @@ -311,34 +313,36 @@ function getUserMeta(userId) {
return user
}
const validationErrors = {
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 @@ -485,11 +489,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

0 comments on commit 4093d20

Please sign in to comment.