Update widget colors based on the widgetConfig (#276)

This commit is contained in:
Pranav Raj S
2019-11-26 00:10:27 +05:30
committed by Sojan Jose
parent 9c6a101768
commit 5d2efdc7fc
11 changed files with 85 additions and 21 deletions

View File

@@ -9,6 +9,7 @@ module.exports = {
rules: { rules: {
'prettier/prettier': ['error'], 'prettier/prettier': ['error'],
camelcase: 'off', camelcase: 'off',
'no-param-reassign': 'off',
'import/no-extraneous-dependencies': 'off', 'import/no-extraneous-dependencies': 'off',
'import/prefer-default-export': 'off', 'import/prefer-default-export': 'off',
'import/no-named-as-default': 'off', 'import/no-named-as-default': 'off',

View File

@@ -153,11 +153,11 @@ const IFrameHelper = {
if (message.event === 'loaded') { if (message.event === 'loaded') {
Cookies.set('cw_conversation', message.config.authToken); Cookies.set('cw_conversation', message.config.authToken);
IFrameHelper.sendMessage('config-set', {}); IFrameHelper.sendMessage('config-set', {});
IFrameHelper.onLoad(); IFrameHelper.onLoad(message.config.channelConfig);
} }
}; };
}, },
onLoad: () => { onLoad: ({ widget_color: widgetColor }) => {
const iframe = IFrameHelper.getAppFrame(); const iframe = IFrameHelper.getAppFrame();
iframe.style.visibility = ''; iframe.style.visibility = '';
iframe.setAttribute('id', `chatwoot_live_chat_widget`); iframe.setAttribute('id', `chatwoot_live_chat_widget`);
@@ -167,20 +167,23 @@ const IFrameHelper = {
loadCSS(); loadCSS();
createBubbleHolder(); createBubbleHolder();
bubbleHolder.appendChild( const chatIcon = createBubbleIcon({
createBubbleIcon({ className: 'woot-widget-bubble',
className: 'woot-widget-bubble', src: bubbleImg,
src: bubbleImg, target: chatBubble,
target: chatBubble, });
})
); const closeIcon = createBubbleIcon({
bubbleHolder.appendChild( className: 'woot-widget-bubble woot--close woot--hide',
createBubbleIcon({ src: closeImg,
className: 'woot-widget-bubble woot--close woot--hide', target: closeBubble,
src: closeImg, });
target: closeBubble,
}) chatIcon.style.background = widgetColor;
); closeIcon.style.background = widgetColor;
bubbleHolder.appendChild(chatIcon);
bubbleHolder.appendChild(closeIcon);
bubbleHolder.appendChild(createNotificationBubble()); bubbleHolder.appendChild(createNotificationBubble());
onClickChatBubble(); onClickChatBubble();
}, },

View File

@@ -22,6 +22,7 @@ export default {
name: 'App', name: 'App',
methods: { methods: {
...mapActions('appConfig', ['setWidgetColor']),
...mapActions('conversation', ['fetchOldConversations']), ...mapActions('conversation', ['fetchOldConversations']),
scrollConversationToBottom() { scrollConversationToBottom() {
const container = this.$el.querySelector('.conversation-wrap'); const container = this.$el.querySelector('.conversation-wrap');
@@ -35,10 +36,12 @@ export default {
event: 'loaded', event: 'loaded',
config: { config: {
authToken: window.authToken, authToken: window.authToken,
channelConfig: window.chatwootWebChannel,
}, },
}); });
setHeader('X-Auth-Token', window.authToken); setHeader('X-Auth-Token', window.authToken);
} }
this.setWidgetColor(window.chatwootWebChannel);
window.addEventListener('message', e => { window.addEventListener('message', e => {
if ( if (

View File

@@ -1,5 +1,5 @@
<template> <template>
<header class="header-expanded"> <header class="header-expanded" :style="{ background: widgetColor }">
<div> <div>
<h2 class="title"> <h2 class="title">
{{ introHeading }} {{ introHeading }}
@@ -12,8 +12,15 @@
</template> </template>
<script> <script>
import { mapGetters } from 'vuex';
export default { export default {
name: 'ChatHeaderExpanded', name: 'ChatHeaderExpanded',
computed: {
...mapGetters({
widgetColor: 'appConfig/getWidgetColor',
}),
},
props: { props: {
introHeading: { introHeading: {
type: String, type: String,

View File

@@ -1,12 +1,22 @@
<template> <template>
<div class="chat-bubble user" v-html="formatMessage(message)"></div> <div
class="chat-bubble user"
:style="{ background: widgetColor }"
v-html="formatMessage(message)"
></div>
</template> </template>
<script> <script>
import messageFormatterMixin from 'shared/mixins/messageFormatterMixin'; import messageFormatterMixin from 'shared/mixins/messageFormatterMixin';
import { mapGetters } from 'vuex';
export default { export default {
name: 'UserMessageBubble', name: 'UserMessageBubble',
computed: {
...mapGetters({
widgetColor: 'appConfig/getWidgetColor',
}),
},
mixins: [messageFormatterMixin], mixins: [messageFormatterMixin],
props: { props: {
message: String, message: String,
@@ -14,7 +24,6 @@ export default {
}; };
</script> </script>
<!-- Add "scoped" attribute to limit CSS to this component only -->
<style lang="scss"> <style lang="scss">
@import '~widget/assets/scss/variables.scss'; @import '~widget/assets/scss/variables.scss';
@import '~widget/assets/scss/mixins.scss'; @import '~widget/assets/scss/mixins.scss';

View File

@@ -1,11 +1,13 @@
import Vue from 'vue'; import Vue from 'vue';
import Vuex from 'vuex'; import Vuex from 'vuex';
import conversation from 'widget/store/modules/conversation'; import conversation from 'widget/store/modules/conversation';
import appConfig from 'widget/store/modules/appConfig';
Vue.use(Vuex); Vue.use(Vuex);
export default new Vuex.Store({ export default new Vuex.Store({
modules: { modules: {
appConfig,
conversation, conversation,
}, },
}); });

View File

@@ -0,0 +1,29 @@
import { SET_WIDGET_COLOR } from '../types';
const state = {
widgetColor: '',
};
const getters = {
getWidgetColor: $state => $state.widgetColor,
};
const actions = {
setWidgetColor({ commit }, data) {
commit(SET_WIDGET_COLOR, data);
},
};
const mutations = {
[SET_WIDGET_COLOR]($state, data) {
$state.widgetColor = data.widget_color;
},
};
export default {
namespaced: true,
state,
getters,
actions,
mutations,
};

View File

@@ -0,0 +1 @@
export const SET_WIDGET_COLOR = 'SET_WIDGET_COLOR';

View File

@@ -5,7 +5,10 @@
<%= csrf_meta_tags %> <%= csrf_meta_tags %>
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=0" /> <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=0" />
<script> <script>
window.chatwootWebChannel = '<%= @web_widget.website_name %>' window.chatwootWebChannel = {
website_name: '<%= @web_widget.website_name %>',
widget_color: '<%= @web_widget.widget_color %>'
}
window.chatwootPubsubToken = '<%= @contact.pubsub_token %>' window.chatwootPubsubToken = '<%= @contact.pubsub_token %>'
window.authToken = '<%= @token %>' window.authToken = '<%= @token %>'
</script> </script>

View File

@@ -0,0 +1,5 @@
class AddWidgetColorToWebWidget < ActiveRecord::Migration[6.1]
def change
add_column :channel_web_widgets, :widget_color, :string, default: '#1f93ff'
end
end

View File

@@ -10,7 +10,7 @@
# #
# It's strongly recommended that you check this file into your version control system. # It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 2019_11_16_073924) do ActiveRecord::Schema.define(version: 2019_11_24_091232) do
# These are extensions that must be enabled in order to support this database # These are extensions that must be enabled in order to support this database
enable_extension "plpgsql" enable_extension "plpgsql"
@@ -62,6 +62,7 @@ ActiveRecord::Schema.define(version: 2019_11_16_073924) do
t.datetime "created_at", null: false t.datetime "created_at", null: false
t.datetime "updated_at", null: false t.datetime "updated_at", null: false
t.string "website_token" t.string "website_token"
t.string "widget_color", default: "#1f93ff"
t.index ["website_token"], name: "index_channel_web_widgets_on_website_token", unique: true t.index ["website_token"], name: "index_channel_web_widgets_on_website_token", unique: true
end end