Skip to content

Commit

Permalink
feat: Ability to add label for contact page (chatwoot#2350)
Browse files Browse the repository at this point in the history
* feat: Ability to add label for contact page

Co-authored-by: Muhsin Keloth <muhsinkeramam@gmail.com>
Co-authored-by: Pranav Raj S <pranav@chatwoot.com>
Co-authored-by: Nithin David Thomas <webofnithin@gmail.com>
  • Loading branch information
4 people committed Jun 14, 2021
1 parent fe2af37 commit d21c1c7
Show file tree
Hide file tree
Showing 17 changed files with 562 additions and 32 deletions.
8 changes: 8 additions & 0 deletions app/javascript/dashboard/api/contacts.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@ class ContactAPI extends ApiClient {
return axios.get(`${this.url}/${contactId}/contactable_inboxes`);
}

getContactLabels(contactId) {
return axios.get(`${this.url}/${contactId}/labels`);
}

updateContactLabels(contactId, labels) {
return axios.post(`${this.url}/${contactId}/labels`, { labels });
}

search(search = '', page = 1, sortAttr = 'name') {
return axios.get(
`${this.url}/search?q=${search}&page=${page}&sort=${sortAttr}`
Expand Down
19 changes: 19 additions & 0 deletions app/javascript/dashboard/api/specs/contacts.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,25 @@ describe('#ContactsAPI', () => {
'/api/v1/contacts/1/contactable_inboxes'
);
});

it('#getContactLabels', () => {
contactAPI.getContactLabels(1);
expect(context.axiosMock.get).toHaveBeenCalledWith(
'/api/v1/contacts/1/labels'
);
});

it('#updateContactLabels', () => {
const labels = ['support-query'];
contactAPI.updateContactLabels(1, labels);
expect(context.axiosMock.post).toHaveBeenCalledWith(
'/api/v1/contacts/1/labels',
{
labels,
}
);
});

it('#search', () => {
contactAPI.search('leads', 1, 'date');
expect(context.axiosMock.get).toHaveBeenCalledWith(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import { action } from '@storybook/addon-actions';
import LabelSelector from './LabelSelector';

export default {
title: 'Components/Label/Contact Label',
component: LabelSelector,
argTypes: {
contactId: {
control: {
type: 'text ,number',
},
},
},
};

const Template = (args, { argTypes }) => ({
props: Object.keys(argTypes),
components: { LabelSelector },
template:
'<label-selector v-bind="$props" @add="onAdd" @remove="onRemove"></label-selector>',
});

export const ContactLabel = Template.bind({});
ContactLabel.args = {
onAdd: action('Added'),
onRemove: action('Removed'),
allLabels: [
{
id: '1',
title: 'sales',
description: '',
color: '#0a5dd1',
},
{
id: '2',
title: 'refund',
description: '',
color: '#8442f5',
},
{
id: '3',
title: 'testing',
description: '',
color: '#f542f5',
},
{
id: '4',
title: 'scheduled',
description: '',
color: '#42d1f5',
},
],
savedLabels: [
{
id: '2',
title: 'refund',
description: '',
color: '#8442f5',
},
{
id: '4',
title: 'scheduled',
description: '',
color: '#42d1f5',
},
],
};
116 changes: 116 additions & 0 deletions app/javascript/dashboard/components/widgets/LabelSelector.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
<template>
<div>
<h6 class="text-block-title">
<i class="title-icon ion-pricetags" />
{{ $t('CONTACT_PANEL.LABELS.CONTACT.TITLE') }}
</h6>
<div v-on-clickaway="closeDropdownLabel" class="label-wrap">
<add-label @add="toggleLabels" />
<woot-label
v-for="label in savedLabels"
:key="label.id"
:title="label.title"
:description="label.description"
:show-close="true"
:bg-color="label.color"
@click="removeItem"
/>
<div class="dropdown-wrap">
<div
:class="{ 'dropdown-pane--open': showSearchDropdownLabel }"
class="dropdown-pane"
>
<label-dropdown
v-if="showSearchDropdownLabel"
:account-labels="allLabels"
:selected-labels="selectedLabels"
@add="addItem"
@remove="removeItem"
/>
</div>
</div>
</div>
</div>
</template>

<script>
import AddLabel from 'shared/components/ui/dropdown/AddLabel';
import LabelDropdown from 'shared/components/ui/label/LabelDropdown';
import { mixin as clickaway } from 'vue-clickaway';
export default {
components: {
AddLabel,
LabelDropdown,
},
mixins: [clickaway],
props: {
allLabels: {
type: Array,
default: () => [],
},
savedLabels: {
type: Array,
default: () => [],
},
},
data() {
return {
showSearchDropdownLabel: false,
};
},
computed: {
selectedLabels() {
return this.savedLabels.map(label => label.title);
},
},
methods: {
addItem(label) {
this.$emit('add', label);
},
removeItem(label) {
this.$emit('remove', label);
},
toggleLabels() {
this.showSearchDropdownLabel = !this.showSearchDropdownLabel;
},
closeDropdownLabel() {
this.showSearchDropdownLabel = false;
},
},
};
</script>

<style lang="scss" scoped>
.title-icon {
margin-right: var(--space-smaller);
}
.label-wrap {
position: relative;
margin-left: var(--space-two);
line-height: var(--space-medium);
.dropdown-wrap {
display: flex;
position: absolute;
margin-right: var(--space-medium);
top: var(--space-medium);
width: 100%;
left: -1px;
.dropdown-pane {
width: 100%;
box-sizing: border-box;
}
}
}
</style>
19 changes: 7 additions & 12 deletions app/javascript/dashboard/i18n/locale/en/contact.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,14 @@
"TITLE": "Previous Conversations"
},
"LABELS": {
"TITLE": "Conversation Labels",
"MODAL": {
"TITLE": "Labels for",
"ACTIVE_LABELS": "Labels added to the conversation",
"INACTIVE_LABELS": "Labels available in the account",
"REMOVE": "Click on X icon to remove the label",
"ADD": "Click on + icon to add the label",
"ADD_BUTTON": "Add Labels",
"UPDATE_BUTTON": "Update labels",
"UPDATE_ERROR": "Couldn't update labels, try again."
"CONTACT": {
"TITLE": "Contact Labels",
"ERROR": "Couldn't update labels"
},
"CONVERSATION": {
"TITLE": "Conversation Labels",
"ADD_BUTTON": "Add Labels"
},
"NO_LABELS_TO_ADD": "There are no more labels defined in the account.",
"NO_AVAILABLE_LABELS": "There are no labels added to this conversation.",
"LABEL_SELECT": {
"TITLE": "Add Labels",
"PLACEHOLDER": "Search labels",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
v-if="hasContactAttributes"
:custom-attributes="contact.custom_attributes"
/>
<contact-label :contact-id="contact.id" class="contact-labels" />
<contact-conversations
v-if="contact.id"
:contact-id="contact.id"
Expand All @@ -20,12 +21,14 @@
import ContactConversations from 'dashboard/routes/dashboard/conversation/ContactConversations';
import ContactInfo from 'dashboard/routes/dashboard/conversation/contact/ContactInfo';
import ContactCustomAttributes from 'dashboard/routes/dashboard/conversation/ContactCustomAttributes';
import ContactLabel from 'dashboard/routes/dashboard/contacts/components/ContactLabels.vue';
export default {
components: {
ContactCustomAttributes,
ContactConversations,
ContactInfo,
ContactLabel,
},
props: {
contact: {
Expand Down Expand Up @@ -61,6 +64,10 @@ export default {
position: relative;
border-left: 1px solid var(--color-border);
padding: var(--space-medium) var(--space-two);
.contact-labels {
padding-bottom: var(--space-normal);
}
}
.close-button {
Expand All @@ -79,10 +86,6 @@ export default {
padding: 0 var(--space-normal);
}
.contact-conversation--panel {
height: 100%;
}
.contact--mute {
color: var(--r-400);
display: block;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<template>
<label-selector
:all-labels="allLabels"
:saved-labels="savedLabels"
@add="addItem"
@remove="removeItem"
/>
</template>

<script>
import { mapGetters } from 'vuex';
import LabelSelector from 'dashboard/components/widgets/LabelSelector.vue';
import alertMixin from 'shared/mixins/alertMixin';
export default {
components: { LabelSelector },
mixins: [alertMixin],
props: {
contactId: {
type: [String, Number],
required: true,
},
},
computed: {
savedLabels() {
const result = this.$store.getters['contactLabels/getContactLabels'](
this.contactId
);
return result.map(value => {
return this.allLabels.find(label => label.title === value);
});
},
...mapGetters({
labelUiFlags: 'contactLabels/getUIFlags',
allLabels: 'labels/getLabels',
}),
},
watch: {
contactId(newContactId, prevContactId) {
if (newContactId && newContactId !== prevContactId) {
this.fetchLabels(newContactId);
}
},
},
mounted() {
const { contactId } = this;
this.fetchLabels(contactId);
},
methods: {
async onUpdateLabels(selectedLabels) {
try {
await this.$store.dispatch('contactLabels/update', {
contactId: this.contactId,
labels: selectedLabels,
});
} catch (error) {
this.showAlert(this.$t('CONTACT_PANEL.LABELS.CONTACT.ERROR'));
}
},
addItem(value) {
const result = this.savedLabels.map(item => item.title);
result.push(value.title);
this.onUpdateLabels(result);
},
removeItem(value) {
const result = this.savedLabels
.map(label => label.title)
.filter(label => label !== value);
this.onUpdateLabels(result);
},
async fetchLabels(contactId) {
if (!contactId) {
return;
}
this.$store.dispatch('contactLabels/get', contactId);
},
},
};
</script>

<style></style>
Loading

0 comments on commit d21c1c7

Please sign in to comment.