From b1da3dc7cf9443b2fe2acf64eab4a0acacc62844 Mon Sep 17 00:00:00 2001
From: Sivin Varghese <64252451+iamsivin@users.noreply.github.com>
Date: Mon, 12 Aug 2024 18:26:07 +0530
Subject: [PATCH] feat: Replace `attributeMixin` within the component (#9919)
# Pull Request Template
## Description
This PR will replace the usage of `attributeMixin` within the component
itself. And moved the component from option API to composition API and
updated the styles and related component
Fixes
https://linear.app/chatwoot/issue/CW-3444/rewrite-attributemixin-mixin-to-a-composable
## Type of change
- [x] New feature (non-breaking change which adds functionality)
## How Has This Been Tested?
**Test cases**
1. Add custom attributes for both conversation and contact from the
settings
2. See all attributes are showing based on the conversation and contact
in both conversation and contact sidebar.
3. Try all CRUD operations like EDIT, DELETE.
4. Check whether styles are properly showing or not (Background color
based on odd/even)
## Checklist:
- [x] My code follows the style guidelines of this project
- [x] I have performed a self-review of my code
- [x] I have commented on my code, particularly in hard-to-understand
areas
- [ ] I have made corresponding changes to the documentation
- [x] My changes generate no new warnings
- [x] I have added tests that prove my fix is effective or that my
feature works
- [x] New and existing unit tests pass locally with my changes
- [ ] Any dependent changes have been merged and published in downstream
modules
---
.../dashboard/mixins/attributeMixin.js | 49 ---
.../mixins/specs/attributeMixin.spec.js | 145 --------
.../contacts/components/ContactInfoPanel.vue | 2 -
.../dashboard/conversation/ContactPanel.vue | 2 -
.../conversation/ConversationInfo.vue | 50 ++-
.../customAttributes/CustomAttributes.vue | 326 +++++++++---------
6 files changed, 192 insertions(+), 382 deletions(-)
delete mode 100644 app/javascript/dashboard/mixins/attributeMixin.js
delete mode 100644 app/javascript/dashboard/mixins/specs/attributeMixin.spec.js
diff --git a/app/javascript/dashboard/mixins/attributeMixin.js b/app/javascript/dashboard/mixins/attributeMixin.js
deleted file mode 100644
index 5bc05a4ae..000000000
--- a/app/javascript/dashboard/mixins/attributeMixin.js
+++ /dev/null
@@ -1,49 +0,0 @@
-import { mapGetters } from 'vuex';
-import { isValidURL } from '../helper/URLHelper';
-export default {
- computed: {
- ...mapGetters({
- currentChat: 'getSelectedChat',
- accountId: 'getCurrentAccountId',
- }),
- attributes() {
- return this.$store.getters['attributes/getAttributesByModel'](
- this.attributeType
- );
- },
- customAttributes() {
- if (this.attributeType === 'conversation_attribute')
- return this.currentChat.custom_attributes || {};
- return this.contact.custom_attributes || {};
- },
- contactIdentifier() {
- return (
- this.currentChat.meta?.sender?.id ||
- this.$route.params.contactId ||
- this.contactId
- );
- },
- contact() {
- return this.$store.getters['contacts/getContact'](this.contactIdentifier);
- },
- conversationId() {
- return this.currentChat.id;
- },
- },
- methods: {
- isAttributeNumber(attributeValue) {
- return (
- Number.isInteger(Number(attributeValue)) && Number(attributeValue) > 0
- );
- },
- attributeDisplayType(attributeValue) {
- if (this.isAttributeNumber(attributeValue)) {
- return 'number';
- }
- if (isValidURL(attributeValue)) {
- return 'link';
- }
- return 'text';
- },
- },
-};
diff --git a/app/javascript/dashboard/mixins/specs/attributeMixin.spec.js b/app/javascript/dashboard/mixins/specs/attributeMixin.spec.js
deleted file mode 100644
index 2484cad4b..000000000
--- a/app/javascript/dashboard/mixins/specs/attributeMixin.spec.js
+++ /dev/null
@@ -1,145 +0,0 @@
-import { shallowMount, createLocalVue } from '@vue/test-utils';
-import attributeMixin from '../attributeMixin';
-import Vuex from 'vuex';
-
-const localVue = createLocalVue();
-localVue.use(Vuex);
-
-describe('attributeMixin', () => {
- let getters;
- let actions;
- let store;
-
- beforeEach(() => {
- actions = { updateUISettings: vi.fn(), toggleSidebarUIState: vi.fn() };
- getters = {
- getSelectedChat: () => ({
- id: 7165,
- custom_attributes: {
- product_id: 2021,
- },
- meta: {
- sender: {
- id: 1212,
- },
- },
- }),
- getCurrentAccountId: () => 1,
- attributeType: () => 'conversation_attribute',
- };
- store = new Vuex.Store({ actions, getters });
- });
-
- it('returns currently selected conversation id', () => {
- const Component = {
- render() {},
- title: 'TestComponent',
- mixins: [attributeMixin],
- };
- const wrapper = shallowMount(Component, { store, localVue });
- expect(wrapper.vm.conversationId).toEqual(7165);
- });
-
- it('return display type if attribute passed', () => {
- const Component = {
- render() {},
- title: 'TestComponent',
- mixins: [attributeMixin],
- };
- const wrapper = shallowMount(Component, { store, localVue });
- expect(wrapper.vm.attributeDisplayType('date')).toBe('text');
- expect(
- wrapper.vm.attributeDisplayType('https://www.chatwoot.com/pricing')
- ).toBe('link');
- expect(wrapper.vm.attributeDisplayType(9988)).toBe('number');
- });
-
- it('return true if number is passed', () => {
- const Component = {
- render() {},
- title: 'TestComponent',
- mixins: [attributeMixin],
- };
- const wrapper = shallowMount(Component, { store, localVue });
- expect(wrapper.vm.isAttributeNumber(9988)).toBe(true);
- });
-
- it('returns currently selected contact', () => {
- const Component = {
- render() {},
- title: 'TestComponent',
- mixins: [attributeMixin],
- computed: {
- contact() {
- return {
- id: 7165,
- custom_attributes: {
- product_id: 2021,
- },
- };
- },
- },
- };
- const wrapper = shallowMount(Component, { store, localVue });
- expect(wrapper.vm.contact).toEqual({
- id: 7165,
- custom_attributes: {
- product_id: 2021,
- },
- });
- });
-
- it('returns currently selected contact id', () => {
- const Component = {
- render() {},
- title: 'TestComponent',
- mixins: [attributeMixin],
- };
- const wrapper = shallowMount(Component, { store, localVue });
- expect(wrapper.vm.contactIdentifier).toEqual(1212);
- });
-
- it('returns currently selected conversation custom attributes', () => {
- const Component = {
- render() {},
- title: 'TestComponent',
- mixins: [attributeMixin],
- computed: {
- contact() {
- return {
- id: 7165,
- custom_attributes: {
- product_id: 2021,
- },
- };
- },
- },
- };
- const wrapper = shallowMount(Component, { store, localVue });
- expect(wrapper.vm.customAttributes).toEqual({
- product_id: 2021,
- });
- });
-
- it('returns currently selected contact custom attributes', () => {
- const Component = {
- render() {},
- title: 'TestComponent',
- mixins: [attributeMixin],
- computed: {
- contact() {
- return {
- id: 7165,
- custom_attributes: {
- cloudCustomer: true,
- },
- };
- },
- },
- };
- const wrapper = shallowMount(Component, { store, localVue });
- expect(wrapper.vm.customAttributes).toEqual({
- cloudCustomer: true,
- });
- });
-});
diff --git a/app/javascript/dashboard/routes/dashboard/contacts/components/ContactInfoPanel.vue b/app/javascript/dashboard/routes/dashboard/contacts/components/ContactInfoPanel.vue
index 3b0133048..0c9a1ec5b 100644
--- a/app/javascript/dashboard/routes/dashboard/contacts/components/ContactInfoPanel.vue
+++ b/app/javascript/dashboard/routes/dashboard/contacts/components/ContactInfoPanel.vue
@@ -115,13 +115,11 @@ export default {
diff --git a/app/javascript/dashboard/routes/dashboard/conversation/ContactPanel.vue b/app/javascript/dashboard/routes/dashboard/conversation/ContactPanel.vue
index ff61d994d..8d82e5465 100644
--- a/app/javascript/dashboard/routes/dashboard/conversation/ContactPanel.vue
+++ b/app/javascript/dashboard/routes/dashboard/conversation/ContactPanel.vue
@@ -218,8 +218,6 @@ export default {
>
-
-
+
- {{ referer }}
-
-
+
+ {{ referer }}
+
+
+
-
-
diff --git a/app/javascript/dashboard/routes/dashboard/conversation/customAttributes/CustomAttributes.vue b/app/javascript/dashboard/routes/dashboard/conversation/customAttributes/CustomAttributes.vue
index 86ef0f91e..3c127b857 100644
--- a/app/javascript/dashboard/routes/dashboard/conversation/customAttributes/CustomAttributes.vue
+++ b/app/javascript/dashboard/routes/dashboard/conversation/customAttributes/CustomAttributes.vue
@@ -1,153 +1,187 @@
-
+
-