Compare commits
5
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b605362f09 | ||
|
|
c697964110 | ||
|
|
53216766b8 | ||
|
|
a0153daca4 | ||
|
|
eb9adb4810 |
@@ -8,7 +8,7 @@ import availabilityMixin from 'widget/mixins/availability';
|
||||
import { getLocale } from './helpers/urlParamsHelper';
|
||||
import { isEmptyObject } from 'widget/helpers/utils';
|
||||
import Spinner from 'shared/components/Spinner.vue';
|
||||
import routerMixin from './mixins/routerMixin';
|
||||
import { useReplaceRoute } from 'widget/composables/useReplaceRoute';
|
||||
import {
|
||||
getExtraSpaceToScroll,
|
||||
loadedEventConfig,
|
||||
@@ -27,7 +27,7 @@ export default {
|
||||
components: {
|
||||
Spinner,
|
||||
},
|
||||
mixins: [availabilityMixin, configMixin, routerMixin, darkModeMixin],
|
||||
mixins: [availabilityMixin, configMixin, darkModeMixin],
|
||||
data() {
|
||||
return {
|
||||
isMobile: false,
|
||||
@@ -141,15 +141,15 @@ export default {
|
||||
this.setUnreadView();
|
||||
});
|
||||
emitter.on(ON_UNREAD_MESSAGE_CLICK, () => {
|
||||
this.replaceRoute('messages').then(() => this.unsetUnreadView());
|
||||
useReplaceRoute('messages').then(() => this.unsetUnreadView());
|
||||
});
|
||||
},
|
||||
registerCampaignEvents() {
|
||||
emitter.on(ON_CAMPAIGN_MESSAGE_CLICK, () => {
|
||||
if (this.shouldShowPreChatForm) {
|
||||
this.replaceRoute('prechat-form');
|
||||
useReplaceRoute('prechat-form');
|
||||
} else {
|
||||
this.replaceRoute('messages');
|
||||
useReplaceRoute('messages');
|
||||
emitter.emit('execute-campaign', {
|
||||
campaignId: this.activeCampaign.id,
|
||||
});
|
||||
@@ -160,7 +160,7 @@ export default {
|
||||
const { customAttributes, campaignId } = campaignDetails;
|
||||
const { websiteToken } = window.chatwootWebChannel;
|
||||
this.executeCampaign({ campaignId, websiteToken, customAttributes });
|
||||
this.replaceRoute('messages');
|
||||
useReplaceRoute('messages');
|
||||
});
|
||||
emitter.on('snooze-campaigns', () => {
|
||||
const expireBy = addHours(new Date(), 1);
|
||||
@@ -176,7 +176,7 @@ export default {
|
||||
!messageCount &&
|
||||
!shouldSnoozeCampaign;
|
||||
if (this.isIFrame && isCampaignReadyToExecute) {
|
||||
this.replaceRoute('campaigns').then(() => {
|
||||
useReplaceRoute('campaigns').then(() => {
|
||||
this.setIframeHeight(true);
|
||||
IFrameHelper.sendMessage({ event: 'setUnreadMode' });
|
||||
});
|
||||
@@ -191,7 +191,7 @@ export default {
|
||||
unreadMessageCount > 0 &&
|
||||
!this.isWidgetOpen
|
||||
) {
|
||||
this.replaceRoute('unread-messages').then(() => {
|
||||
useReplaceRoute('unread-messages').then(() => {
|
||||
this.setIframeHeight(true);
|
||||
IFrameHelper.sendMessage({ event: 'setUnreadMode' });
|
||||
});
|
||||
@@ -298,12 +298,12 @@ export default {
|
||||
['unread-messages', 'campaigns'].includes(this.$route.name);
|
||||
|
||||
if (shouldShowMessageView) {
|
||||
this.replaceRoute('messages');
|
||||
useReplaceRoute('messages');
|
||||
}
|
||||
if (shouldShowHomeView) {
|
||||
this.$store.dispatch('conversation/setUserLastSeen');
|
||||
this.unsetUnreadView();
|
||||
this.replaceRoute('home');
|
||||
useReplaceRoute('home');
|
||||
}
|
||||
if (!message.isOpen) {
|
||||
this.resetCampaign();
|
||||
|
||||
@@ -6,7 +6,7 @@ import FooterReplyTo from 'widget/components/FooterReplyTo.vue';
|
||||
import ChatInputWrap from 'widget/components/ChatInputWrap.vue';
|
||||
import { BUS_EVENTS } from 'shared/constants/busEvents';
|
||||
import { sendEmailTranscript } from 'widget/api/conversation';
|
||||
import routerMixin from 'widget/mixins/routerMixin';
|
||||
import { useReplaceRoute } from 'widget/composables/useReplaceRoute';
|
||||
import { IFrameHelper } from '../helpers/utils';
|
||||
import { CHATWOOT_ON_START_CONVERSATION } from '../constants/sdkEvents';
|
||||
|
||||
@@ -16,7 +16,6 @@ export default {
|
||||
CustomButton,
|
||||
FooterReplyTo,
|
||||
},
|
||||
mixins: [routerMixin],
|
||||
data() {
|
||||
return {
|
||||
inReplyTo: null,
|
||||
@@ -85,7 +84,7 @@ export default {
|
||||
startNewConversation() {
|
||||
this.clearConversations();
|
||||
this.clearConversationAttributes();
|
||||
this.replaceRoute('prechat-form');
|
||||
useReplaceRoute('prechat-form');
|
||||
IFrameHelper.sendMessage({
|
||||
event: 'onEvent',
|
||||
eventIdentifier: CHATWOOT_ON_START_CONVERSATION,
|
||||
|
||||
@@ -3,7 +3,7 @@ import availabilityMixin from 'widget/mixins/availability';
|
||||
import nextAvailabilityTime from 'widget/mixins/nextAvailabilityTime';
|
||||
import FluentIcon from 'shared/components/FluentIcon/Index.vue';
|
||||
import HeaderActions from './HeaderActions.vue';
|
||||
import routerMixin from 'widget/mixins/routerMixin';
|
||||
import { useReplaceRoute } from 'widget/composables/useReplaceRoute';
|
||||
import darkMixin from 'widget/mixins/darkModeMixin.js';
|
||||
|
||||
export default {
|
||||
@@ -12,7 +12,7 @@ export default {
|
||||
FluentIcon,
|
||||
HeaderActions,
|
||||
},
|
||||
mixins: [nextAvailabilityTime, availabilityMixin, routerMixin, darkMixin],
|
||||
mixins: [nextAvailabilityTime, availabilityMixin, darkMixin],
|
||||
props: {
|
||||
avatarUrl: {
|
||||
type: String,
|
||||
@@ -48,7 +48,7 @@ export default {
|
||||
},
|
||||
methods: {
|
||||
onBackButtonClick() {
|
||||
this.replaceRoute('home');
|
||||
useReplaceRoute('home');
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
@@ -6,7 +6,6 @@ import { getContrastingTextColor } from '@chatwoot/utils';
|
||||
import { isEmptyObject } from 'widget/helpers/utils';
|
||||
import { getRegexp } from 'shared/helpers/Validators';
|
||||
import messageFormatterMixin from 'shared/mixins/messageFormatterMixin';
|
||||
import routerMixin from 'widget/mixins/routerMixin';
|
||||
import darkModeMixin from 'widget/mixins/darkModeMixin';
|
||||
import configMixin from 'widget/mixins/configMixin';
|
||||
|
||||
@@ -15,7 +14,7 @@ export default {
|
||||
CustomButton,
|
||||
Spinner,
|
||||
},
|
||||
mixins: [routerMixin, darkModeMixin, messageFormatterMixin, configMixin],
|
||||
mixins: [darkModeMixin, messageFormatterMixin, configMixin],
|
||||
props: {
|
||||
options: {
|
||||
type: Object,
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
import { useRouter, useRoute } from 'dashboard/composables/route';
|
||||
|
||||
/**
|
||||
* Composable for replacing the current route with a new one if it's different.
|
||||
* @param {string} name - The name of the route to replace with.
|
||||
* @param {Object} params - The params to pass to the new route.
|
||||
* @returns {Promise|undefined} A promise that resolves when the navigation is complete, or undefined if no navigation occurs.
|
||||
*/
|
||||
export const useReplaceRoute = async (name, params = {}) => {
|
||||
const router = useRouter();
|
||||
const route = useRoute();
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
if (route.name !== name) {
|
||||
router.replace({ name, params }).then(resolve).catch(reject);
|
||||
}
|
||||
resolve(undefined);
|
||||
});
|
||||
};
|
||||
|
||||
export default useReplaceRoute;
|
||||
@@ -1,10 +0,0 @@
|
||||
export default {
|
||||
methods: {
|
||||
async replaceRoute(name, params = {}) {
|
||||
if (this.$route.name !== name) {
|
||||
return this.$router.replace({ name, params });
|
||||
}
|
||||
return undefined;
|
||||
},
|
||||
},
|
||||
};
|
||||
@@ -0,0 +1,51 @@
|
||||
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
||||
import { useReplaceRoute } from 'widget/composables/useReplaceRoute';
|
||||
|
||||
const mockReplace = vi.fn();
|
||||
const mockRoute = { name: 'initialRoute' };
|
||||
|
||||
vi.mock('dashboard/composables/route', () => ({
|
||||
useRouter: () => ({
|
||||
replace: mockReplace,
|
||||
}),
|
||||
useRoute: () => mockRoute,
|
||||
}));
|
||||
|
||||
describe('useReplaceRoute', () => {
|
||||
let replaceRoute;
|
||||
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
mockRoute.name = 'initialRoute';
|
||||
replaceRoute = useReplaceRoute();
|
||||
});
|
||||
|
||||
it('should replace route when current route is different', async () => {
|
||||
mockReplace.mockResolvedValue(undefined);
|
||||
|
||||
await replaceRoute('newRoute', { id: 1 });
|
||||
|
||||
expect(mockReplace).toHaveBeenCalledWith({
|
||||
name: 'newRoute',
|
||||
params: { id: 1 },
|
||||
});
|
||||
});
|
||||
|
||||
it('should not replace route when current route is the same', async () => {
|
||||
mockRoute.name = 'sameRoute';
|
||||
|
||||
const result = await replaceRoute('sameRoute');
|
||||
|
||||
expect(mockReplace).not.toHaveBeenCalled();
|
||||
expect(result).toBeUndefined();
|
||||
});
|
||||
|
||||
it('should handle router replace rejection', async () => {
|
||||
const error = new Error('Navigation aborted');
|
||||
mockReplace.mockRejectedValue(error);
|
||||
|
||||
await expect(replaceRoute('newRoute')).rejects.toThrow(
|
||||
'Navigation aborted'
|
||||
);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,59 @@
|
||||
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
||||
import { useReplaceRoute } from '../../composables/useReplaceRoute';
|
||||
|
||||
const mockRouter = {
|
||||
currentRoute: { name: 'initialRoute' },
|
||||
replace: vi.fn(),
|
||||
};
|
||||
|
||||
vi.mock('../../composables/useReplaceRoute', () => ({
|
||||
useReplaceRoute: () => ({
|
||||
replaceRoute: async (name, params = {}) => {
|
||||
if (mockRouter.currentRoute.name !== name) {
|
||||
return mockRouter.replace({ name, params });
|
||||
}
|
||||
return undefined;
|
||||
},
|
||||
}),
|
||||
}));
|
||||
|
||||
describe('useReplaceRoute', () => {
|
||||
let routerHelper;
|
||||
|
||||
beforeEach(() => {
|
||||
vi.resetAllMocks();
|
||||
mockRouter.currentRoute = { name: 'initialRoute' };
|
||||
routerHelper = useReplaceRoute();
|
||||
});
|
||||
|
||||
describe('replaceRoute', () => {
|
||||
it('should replace route when current route is different', async () => {
|
||||
mockRouter.replace.mockResolvedValue(undefined);
|
||||
|
||||
await routerHelper.replaceRoute('newRoute', { id: 1 });
|
||||
|
||||
expect(mockRouter.replace).toHaveBeenCalledWith({
|
||||
name: 'newRoute',
|
||||
params: { id: 1 },
|
||||
});
|
||||
});
|
||||
|
||||
it('should not replace route when current route is the same', async () => {
|
||||
mockRouter.currentRoute.name = 'sameRoute';
|
||||
|
||||
const result = await routerHelper.replaceRoute('sameRoute');
|
||||
|
||||
expect(mockRouter.replace).not.toHaveBeenCalled();
|
||||
expect(result).toBeUndefined();
|
||||
});
|
||||
|
||||
it('should handle router replace rejection', async () => {
|
||||
const error = new Error('Navigation aborted');
|
||||
mockRouter.replace.mockRejectedValue(error);
|
||||
|
||||
await expect(routerHelper.replaceRoute('newRoute')).rejects.toThrow(
|
||||
'Navigation aborted'
|
||||
);
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -5,7 +5,7 @@ import ArticleCardSkeletonLoader from 'widget/components/ArticleCardSkeletonLoad
|
||||
|
||||
import { mapGetters } from 'vuex';
|
||||
import darkModeMixin from 'widget/mixins/darkModeMixin';
|
||||
import routerMixin from 'widget/mixins/routerMixin';
|
||||
import { useReplaceRoute } from 'widget/composables/useReplaceRoute';
|
||||
import configMixin from 'widget/mixins/configMixin';
|
||||
|
||||
export default {
|
||||
@@ -15,7 +15,7 @@ export default {
|
||||
TeamAvailability,
|
||||
ArticleCardSkeletonLoader,
|
||||
},
|
||||
mixins: [configMixin, routerMixin, darkModeMixin],
|
||||
mixins: [configMixin, darkModeMixin],
|
||||
computed: {
|
||||
...mapGetters({
|
||||
availableAgents: 'agent/availableAgents',
|
||||
@@ -63,9 +63,9 @@ export default {
|
||||
methods: {
|
||||
startConversation() {
|
||||
if (this.preChatFormEnabled && !this.conversationSize) {
|
||||
return this.replaceRoute('prechat-form');
|
||||
return useReplaceRoute('prechat-form');
|
||||
}
|
||||
return this.replaceRoute('messages');
|
||||
return useReplaceRoute('messages');
|
||||
},
|
||||
openArticleInArticleViewer(link) {
|
||||
let linkToOpen = `${link}?show_plain_layout=true`;
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
<script>
|
||||
import PreChatForm from '../components/PreChat/Form.vue';
|
||||
import configMixin from '../mixins/configMixin';
|
||||
import routerMixin from '../mixins/routerMixin';
|
||||
import { useReplaceRoute } from 'widget/composables/useReplaceRoute';
|
||||
import { isEmptyObject } from 'widget/helpers/utils';
|
||||
import { ON_CONVERSATION_CREATED } from '../constants/widgetBusEvents';
|
||||
|
||||
@@ -9,11 +9,11 @@ export default {
|
||||
components: {
|
||||
PreChatForm,
|
||||
},
|
||||
mixins: [configMixin, routerMixin],
|
||||
mixins: [configMixin],
|
||||
mounted() {
|
||||
this.$emitter.on(ON_CONVERSATION_CREATED, () => {
|
||||
// Redirect to messages page after conversation is created
|
||||
this.replaceRoute('messages');
|
||||
useReplaceRoute('messages');
|
||||
});
|
||||
},
|
||||
methods: {
|
||||
|
||||
Reference in New Issue
Block a user