diff --git a/src/views/components/SendMessage.js b/src/views/components/SendMessage.js new file mode 100644 index 0000000..574b471 --- /dev/null +++ b/src/views/components/SendMessage.js @@ -0,0 +1,119 @@ +export default { + name: 'SendMessage', + data() { + return { + type: 'user', + phone: '', + text: '', + reply_id: '', + loading: false, + } + }, + computed: { + phone_id() { + return this.type === 'user' ? `${this.phone}@${window.TYPEUSER}` : `${this.phone}@${window.TYPEGROUP}` + } + }, + methods: { + openModal() { + $('#modalSendMessage').modal({ + onApprove: function () { + return false; + } + }).modal('show'); + }, + async handleSubmit() { + try { + let response = await this.sendApi() + showSuccessInfo(response) + $('#modalSendMessage').modal('hide'); + } catch (err) { + showErrorInfo(err) + } + }, + async sendApi() { + this.loading = true; + try { + const payload = { + phone: this.phone_id, + message: this.text, + } + if (this.reply_id !== '') { + payload.reply_id = this.reply_id; + } + + let response = await http.post(`/send/message`, payload) + this.handleReset(); + return response.data.message; + } catch (error) { + if (error.response) { + throw new Error(error.response.data.message); + } else { + throw new Error(error.message); + } + } finally { + this.loading = false; + } + }, + handleReset() { + this.phone = ''; + this.text = ''; + this.type = 'user'; + this.reply_id = ''; + }, + }, + template: ` +