Message: Send/reply form refactoring - refs BT#21648

pull/5544/head
Angel Fernando Quiroz Campos 6 months ago
parent ef9548abd3
commit 48cae77b15
  1. 185
      assets/vue/components/message/Form.vue
  2. 129
      assets/vue/views/message/MessageCreate.vue
  3. 210
      assets/vue/views/message/MessageReply.vue

@ -3,11 +3,43 @@
<div class="col-span-2">
<BaseInputText
id="item_title"
v-model="item.title"
v-model="messagePayload.title"
:label="t('Title')"
/>
<BaseAutocomplete
id="to"
v-model="usersTo"
:label="t('To')"
:search="asyncFind"
is-multiple
/>
<BaseAutocomplete
id="cc"
v-model="usersCc"
:label="t('Cc')"
:search="asyncFind"
is-multiple
/>
<BaseTinyEditor
v-model="messagePayload.content"
:full-page="false"
editor-id="message"
required
/>
<slot></slot>
<BaseButton
:disabled="!canSubmitMessage"
:label="t('Send')"
icon="plus"
type="primary"
class="mb-2"
@click="onSubmit"
/>
</div>
<div class="space-y-4">
@ -17,14 +49,14 @@
</p>
<ul
v-if="attachments && attachments.length > 0"
v-if="resourceFileList && resourceFileList.length > 0"
class="space-y-2"
>
<li
v-for="(attachment, index) in attachments"
v-for="(resourceFile, index) in resourceFileList"
:key="index"
class="text-body-2"
v-text="attachment.originalName"
v-text="resourceFile.originalName"
/>
</ul>
@ -40,33 +72,146 @@
<script setup>
import BaseInputText from "../basecomponents/BaseInputText.vue"
import { useI18n } from "vue-i18n"
import { computed } from "vue"
import { computed, ref, watch } from "vue"
import BaseIcon from "../basecomponents/BaseIcon.vue"
import BaseUploader from "../basecomponents/BaseUploader.vue"
import resourceFileService from "../../services/resourceFileService"
const attachments = defineModel("attachments", {
type: Array,
})
import BaseAutocomplete from "../basecomponents/BaseAutocomplete.vue"
import userService from "../../services/userService"
import { MESSAGE_TYPE_INBOX } from "./constants"
import BaseButton from "../basecomponents/BaseButton.vue"
import { useSecurityStore } from "../../store/securityStore"
import { MESSAGE_REL_USER_TYPE_CC, MESSAGE_REL_USER_TYPE_TO } from "../../constants/entity/messagereluser"
import BaseTinyEditor from "../basecomponents/BaseTinyEditor.vue"
const props = defineProps({
values: {
type: Object,
required: true,
title: {
type: String,
required: false,
default: "",
},
receiversTo: {
type: Array,
required: false,
default: () => [],
},
receiversCc: {
type: Array,
required: false,
default: () => [],
},
errors: {
type: Object,
default: () => {},
content: {
type: String,
required: false,
default: "",
},
initialValues: {
type: Object,
default: () => {},
attachments: {
type: Array,
required: false,
default: () => [],
},
msgType: {
type: Number,
required: false,
default: MESSAGE_TYPE_INBOX,
},
})
const emit = defineEmits(["submit"])
const { t } = useI18n()
const item = computed(() => props.initialValues || props.values)
const securityStore = useSecurityStore()
const messagePayload = ref({
sender: securityStore.user["@id"],
msgType: MESSAGE_TYPE_INBOX,
title: "",
content: "",
receivers: [],
attachments: [],
})
const usersTo = ref([])
const usersCc = ref([])
const resourceFileList = ref([])
watch(
() => props.title,
(newTitle) => (messagePayload.value.title = newTitle),
)
watch(
() => props.content,
(newContent) => (messagePayload.value.content = newContent),
)
watch(
() => props.msgType,
(newMsgType) => (messagePayload.value.msgType = newMsgType),
)
watch(
() => props.receiversTo,
(newReceiversTo) => {
usersTo.value.push(
...newReceiversTo.map((messageRelUser) => ({
name: messageRelUser.fullName,
value: messageRelUser["@id"],
})),
)
},
)
watch(
() => props.receiversCc,
(newReceiversCc) => {
usersTo.value.push(
...newReceiversCc.map((messageRelUser) => ({
name: messageRelUser.fullName,
value: messageRelUser["@id"],
})),
)
},
)
async function asyncFind(query) {
const { items } = await userService.findBySearchTerm(query)
return items.map((member) => ({
name: member.fullName,
value: member["@id"],
}))
}
function onUploadSuccess({ response }) {
resourceFileList.value.push(response)
}
const canSubmitMessage = computed(() => {
return (
(usersTo.value.length > 0 || usersCc.value.length > 0) &&
messagePayload.value.title.trim() !== "" &&
messagePayload.value.content.trim() !== ""
)
})
function onSubmit() {
messagePayload.value.receivers = [
...usersTo.value.map((userTo) => ({
receiver: userTo.value,
receiverType: MESSAGE_REL_USER_TYPE_TO,
})),
...usersCc.value.map((userCc) => ({
receiver: userCc.value,
receiverType: MESSAGE_REL_USER_TYPE_CC,
})),
]
messagePayload.value.attachments = resourceFileList.value.map((resourceFile) => ({
resourceFileToAttach: resourceFile["@id"],
}))
const onUploadSuccess = ({ response }) => attachments.value.push(response)
emit("submit", messagePayload.value)
}
</script>

@ -1,73 +1,24 @@
<template>
<MessageForm
v-model:attachments="attachments"
:values="item"
>
<div
v-if="sendToUser"
class="field space-x-4"
>
<span v-t="'To'" />
<MessageCommunicationParty
:username="sendToUser.username"
:full-name="sendToUser.fullName"
:profile-image-url="sendToUser.illustrationUrl"
/>
</div>
<BaseAutocomplete
v-else
id="to"
v-model="usersTo"
:label="t('To')"
:search="asyncFind"
is-multiple
/>
<BaseAutocomplete
v-if="!sendToUser"
id="cc"
v-model="usersCc"
:label="t('Cc')"
:search="asyncFind"
is-multiple
/>
<BaseTinyEditor
v-model="item.content"
editor-id="message"
required
/>
<BaseButton
:label="t('Send')"
:disabled="!canSubmitMessage"
icon="plus"
type="primary"
class="mb-2"
@click="onSubmit"
/>
</MessageForm>
:content="message.content"
:receivers-to="message.receiversTo"
:title="message.title"
@submit="onSubmit"
/>
<Loading :visible="isLoading || isLoadingUser" />
</template>
<script setup>
import MessageForm from "../../components/message/Form.vue"
import Loading from "../../components/Loading.vue"
import { computed, onMounted, ref } from "vue"
import BaseAutocomplete from "../../components/basecomponents/BaseAutocomplete.vue"
import BaseButton from "../../components/basecomponents/BaseButton.vue"
import { onMounted, ref } from "vue"
import { useI18n } from "vue-i18n"
import { useRoute, useRouter } from "vue-router"
import { MESSAGE_TYPE_INBOX } from "../../components/message/constants"
import userService from "../../services/userService"
import { useNotification } from "../../composables/notification"
import { capitalize } from "lodash"
import BaseTinyEditor from "../../components/basecomponents/BaseTinyEditor.vue"
import { useSecurityStore } from "../../store/securityStore"
import { messageService } from "../../services/message"
import MessageCommunicationParty from "./MessageCommunicationParty.vue"
import { MESSAGE_REL_USER_TYPE_CC, MESSAGE_REL_USER_TYPE_TO } from "../../constants/entity/messagereluser"
const securityStore = useSecurityStore()
const router = useRouter()
@ -76,67 +27,19 @@ const { t } = useI18n()
const notification = useNotification()
const asyncFind = async (query) => {
const { items } = await userService.findBySearchTerm(query)
return items.map((member) => ({
name: member.fullName,
value: member["@id"],
}))
}
const item = ref({
sender: securityStore.user["@id"],
receivers: [],
msgType: MESSAGE_TYPE_INBOX,
const message = ref({
title: "",
content: "",
attachments: [],
})
const attachments = ref([])
const usersTo = ref([])
const usersCc = ref([])
const receiversTo = computed(() =>
usersTo.value.map((userTo) => ({
receiver: userTo.value,
receiverType: MESSAGE_REL_USER_TYPE_TO,
})),
)
const receiversCc = computed(() =>
usersCc.value.map((userCc) => ({
receiver: userCc.value,
receiverType: MESSAGE_REL_USER_TYPE_CC,
})),
)
const canSubmitMessage = computed(() => {
return (
(usersTo.value.length > 0 || usersCc.value.length > 0) &&
item.value.title.trim() !== "" &&
item.value.content.trim() !== ""
)
receiversTo: [],
})
const isLoading = ref(false)
const onSubmit = async () => {
if (!canSubmitMessage.value) {
return
}
item.value.attachments = attachments.value.map((attachment) => ({
resourceFileToAttach: attachment["@id"],
}))
item.value.receivers = [...receiversTo.value, ...receiversCc.value]
const onSubmit = async (messageToSend) => {
isLoading.value = true
try {
await messageService.create(item.value)
await messageService.create(messageToSend)
} catch (error) {
notification.showErrorNotification(error)
} finally {
@ -150,7 +53,6 @@ const onSubmit = async () => {
}
const isLoadingUser = ref(false)
const sendToUser = ref()
onMounted(async () => {
if (route.query.send_to_user) {
@ -158,18 +60,13 @@ onMounted(async () => {
try {
let user = await userService.findById(route.query.send_to_user)
sendToUser.value = user
usersTo.value.push({
name: user.fullName,
value: user["@id"],
})
message.value.receiversTo = [user]
if (route.query.prefill) {
const prefill = capitalize(route.query.prefill)
item.value.title = t(prefill + "Title")
item.value.content = t(prefill + "Content", [
message.value.title = t(prefill + "Title")
message.value.content = t(prefill + "Content", [
user.firstname,
securityStore.user.firstname,
securityStore.user.firstname,

@ -1,69 +1,29 @@
<template>
<MessageForm
ref="createForm"
:errors="violations"
:values="item"
>
<div
v-if="item.originalSender"
class="field space-x-4"
>
<span v-t="'To'" />
<MessageCommunicationParty
:username="item.originalSender.username"
:full-name="item.originalSender.fullName"
:profile-image-url="item.originalSender.illustrationUrl"
/>
</div>
<div
v-if="item.receiversCc"
class="field space-x-4"
>
<span v-t="'Cc'" />
<MessageCommunicationParty
v-for="receiver in item.receiversCc"
:key="receiver.receiver.id"
:username="receiver.receiver.username"
:full-name="receiver.receiver.fullName"
:profile-image-url="receiver.receiver.illustrationUrl"
/>
</div>
<BaseTinyEditor
v-model="item.content"
:full-page="false"
editor-id="message"
required
/>
<BaseButton
:label="t('Send')"
icon="plus"
type="primary"
@click="onReplyMessageForm"
/>
</MessageForm>
v-if="replyMessage"
:title="replyMessage.title"
:content="replyMessage.content"
:receivers-to="replyMessage.receiversTo"
:receivers-cc="replyMessage.receiversCc"
@submit="onReplyMessageForm"
/>
<Loading :visible="isLoading" />
</template>
<script setup>
import { useStore } from "vuex"
import { computed, onMounted, ref } from "vue"
import { ref } from "vue"
import MessageForm from "../../components/message/Form.vue"
import Loading from "../../components/Loading.vue"
import isEmpty from "lodash/isEmpty"
import { useRoute, useRouter } from "vue-router"
import { MESSAGE_TYPE_INBOX } from "../../components/message/constants"
import BaseButton from "../../components/basecomponents/BaseButton.vue"
import { useI18n } from "vue-i18n"
import { useSecurityStore } from "../../store/securityStore"
import { useNotification } from "../../composables/notification"
import { formatDateTimeFromISO } from "../../utils/dates"
import BaseTinyEditor from "../../components/basecomponents/BaseTinyEditor.vue"
import MessageCommunicationParty from "./MessageCommunicationParty.vue"
import { useFormatDate } from "../../composables/formatDate"
import userService from "../../services/userService"
const item = ref({})
const store = useStore()
const securityStore = useSecurityStore()
const route = useRoute()
@ -72,106 +32,84 @@ const router = useRouter()
const { t } = useI18n()
const notification = useNotification()
const { abbreviatedDatetime } = useFormatDate()
let id = isEmpty(route.params.id) ? route.query.id : route.params.id
let replyAll = "1" === route.query.all
onMounted(async () => {
const response = await store.dispatch("message/load", id)
item.value = await response
const originalUserInfo = await store.dispatch("user/load", "/api/users/" + item.value.sender.id)
const originalSenderName = originalUserInfo.fullName
const originalSenderEmail = originalUserInfo.email
const formattedDate = formatDateTimeFromISO(item.value.sendDate)
const translatedHeader = t("Email reply header", [
formattedDate,
originalSenderName,
`<a href="mailto:${originalSenderEmail}">${originalSenderEmail}</a>`,
])
delete item.value["@id"]
delete item.value["id"]
delete item.value["firstReceiver"]
//delete item.value['receivers'];
delete item.value["sendDate"]
const originalMessage = ref(null)
const originalSenderInfo = ref(null)
item.value["originalSender"] = item.value["sender"]
// New sender.
item.value["sender"] = securityStore.user["@id"]
function filterReceiver(messageRelUser) {
// Dont' add original sender.
if (originalMessage.value.sender["@id"] === messageRelUser.receiver["@id"]) {
return false
}
// Set new receivers, will be loaded by onSendMessageForm()
if (replyAll) {
item.value.receiversTo.forEach((user) => {
// Dont' add original sender.
if (item.value["originalSender"]["@id"] === user.receiver["@id"]) {
return
}
// Dont' add the current user.
if (securityStore.user["@id"] === user.receiver["@id"]) {
return
}
item.value.receiversCc.push(user)
})
// Don't add the current user.
if (securityStore.user["@id"] === messageRelUser.receiver["@id"]) {
return false
}
return true
}
const replyMessage = ref({})
store.dispatch("message/load", id).then((messagePayload) => {
originalMessage.value = messagePayload
userService
.find(messagePayload.sender["@id"])
.then((senderPayload) => {
originalSenderInfo.value = senderPayload
// Check that the original sender is not already in the Cc.
item.value.receiversCc.forEach(function (user, index, obj) {
if (item.value["originalSender"]["@id"] === user.receiver["@id"]) {
obj.splice(index, 1)
}
setReplyMessage()
})
.finally(() => (isLoading.value = false))
})
function setReplyMessage() {
const sendDate = abbreviatedDatetime(originalMessage.value.sendDate)
const senderFullName = originalSenderInfo.value.fullName
const sendeEmail = originalSenderInfo.value.email
const replyHeader = t("Email reply header", [
sendDate,
senderFullName,
`<a href="mailto:${sendeEmail}">${sendeEmail}</a>`,
])
/*item.value.receiversTo.forEach(function (user, index, obj) {
if (securityStore.user['@id'] === user.receiver['@id']) {
obj.splice(index, 1);
}
});*/
} else {
item.value["receivers"] = []
item.value["receiversTo"] = null
item.value["receiversCc"] = null
item.value["receivers"][0] = item.value["originalSender"]
replyMessage.value = {
receiversTo: [],
receiversCc: [],
msgType: MESSAGE_TYPE_INBOX,
title: t("Re:") + " " + originalMessage.value.title,
content: `<br /><br /><hr /><blockquote>${replyHeader}<hr />${originalMessage.value.content}</blockquote>`,
attachments: [],
}
/*console.log('-----------------------');
console.log(item.value.receiversCc);
if (item.value.receiversCc) {
item.value.receiversCc.forEach(user => {
console.log(user);
// Send to inbox
usersCc.value.push(user.receiver);
});
}*/
// Set reply content.
item.value.title = t("Re:") + " " + item.value.title
item.value.content = `<br /><br /><hr /><blockquote>${translatedHeader}<hr />${item.value.content}</blockquote>`
})
if (replyAll) {
replyMessage.value.receiversTo = originalMessage.value.receiversTo
.filter(filterReceiver)
.map((messageRelUser) => messageRelUser.receiver)
const isLoading = computed(() => store.state.message.isLoading)
const violations = computed(() => store.state.message.violations)
const createForm = ref()
const onReplyMessageForm = async () => {
let users = []
users.push({
receiver: createForm.value.v$.item.$model.originalSender["@id"],
receiverType: 1,
})
if (createForm.value.v$.item.$model.receiversCc) {
createForm.value.v$.item.$model.receiversCc.forEach((user) => {
// Send to inbox
users.push({ receiver: user.receiver["@id"], receiverType: 2 })
})
replyMessage.value.receiversCc = originalMessage.value.receiversCc
.filter(filterReceiver)
.map((messageRelUser) => messageRelUser.receiver)
}
createForm.value.v$.item.$model.sender = "/api/users/" + securityStore.user.id
createForm.value.v$.item.$model.receiversTo = null
createForm.value.v$.item.$model.receiversCc = null
createForm.value.v$.item.$model.receivers = users
createForm.value.v$.item.$model.msgType = MESSAGE_TYPE_INBOX
replyMessage.value.receiversTo.push(originalMessage.value.sender)
}
const isLoading = ref(true)
async function onReplyMessageForm(messageToSend) {
isLoading.value = true
try {
await store.dispatch("message/create", createForm.value.v$.item.$model)
await store.dispatch("message/create", messageToSend)
notification.showSuccessNotification("Message sent")
@ -180,6 +118,8 @@ const onReplyMessageForm = async () => {
})
} catch (e) {
notification.showErrorNotification(e)
} finally {
isLoading.value = false
}
}
</script>

Loading…
Cancel
Save