refactor: Rewrite the composable to a helper method

This commit is contained in:
Fayaz Ahmed
2024-08-26 14:28:07 +05:30
parent 73fbfd5782
commit 3901abc009
18 changed files with 52 additions and 108 deletions
@@ -3,7 +3,7 @@ import WidgetHead from './WidgetHead.vue';
import WidgetBody from './WidgetBody.vue';
import WidgetFooter from './WidgetFooter.vue';
import InputRadioGroup from 'dashboard/routes/dashboard/settings/inbox/components/InputRadioGroup.vue';
import { useGlobalConfig } from 'shared/composables/useGlobalConfig';
import { useInstallationName } from 'shared/helpers/installationHelper';
import { mapGetters } from 'vuex';
export default {
@@ -56,12 +56,6 @@ export default {
default: '',
},
},
setup() {
const { useInstallationName } = useGlobalConfig();
return {
useInstallationName,
};
},
data() {
return {
widgetScreens: [
@@ -149,6 +143,7 @@ export default {
this.isWidgetVisible = !this.isWidgetVisible;
this.isDefaultScreen = true;
},
useInstallationName,
},
};
</script>
@@ -1,17 +1,11 @@
<script>
import { mapGetters } from 'vuex';
import { useGlobalConfig } from 'shared/composables/useGlobalConfig';
import { useInstallationName } from 'shared/helpers/installationHelper';
import SettingsHeader from 'dashboard/routes/dashboard/settings/SettingsHeader.vue';
export default {
components: {
SettingsHeader,
},
setup() {
const { useInstallationName } = useGlobalConfig();
return {
useInstallationName,
};
},
computed: {
...mapGetters({
globalConfig: 'globalConfig/get',
@@ -43,6 +37,9 @@ export default {
return '';
},
},
methods: {
useInstallationName,
},
};
</script>
@@ -3,19 +3,13 @@ import ChannelItem from 'dashboard/components/widgets/ChannelItem.vue';
import router from '../../../index';
import PageHeader from '../SettingsSubPageHeader.vue';
import { mapGetters } from 'vuex';
import { useGlobalConfig } from 'shared/composables/useGlobalConfig';
import { useInstallationName } from 'shared/helpers/installationHelper';
export default {
components: {
ChannelItem,
PageHeader,
},
setup() {
const { useInstallationName } = useGlobalConfig();
return {
useInstallationName,
};
},
data() {
return {
enabledFeatures: {},
@@ -61,6 +55,7 @@ export default {
};
router.push({ name: 'settings_inboxes_page_channel', params });
},
useInstallationName,
},
};
</script>
@@ -1,14 +1,8 @@
<script>
import { mapGetters } from 'vuex';
import { useGlobalConfig } from 'shared/composables/useGlobalConfig';
import { useInstallationName } from 'shared/helpers/installationHelper';
export default {
setup() {
const { useInstallationName } = useGlobalConfig();
return {
useInstallationName,
};
},
computed: {
...mapGetters({
globalConfig: 'globalConfig/get',
@@ -23,6 +17,9 @@ export default {
}));
},
},
methods: {
useInstallationName,
},
};
</script>
@@ -4,7 +4,7 @@ import { useAlert } from 'dashboard/composables';
import { useAdmin } from 'dashboard/composables/useAdmin';
import { useAccount } from 'dashboard/composables/useAccount';
import Settings from './Settings.vue';
import { useGlobalConfig } from 'shared/composables/useGlobalConfig';
import { useInstallationName } from 'shared/helpers/installationHelper';
export default {
components: {
@@ -13,11 +13,9 @@ export default {
setup() {
const { isAdmin } = useAdmin();
const { accountScopedUrl } = useAccount();
const { useInstallationName } = useGlobalConfig();
return {
isAdmin,
accountScopedUrl,
useInstallationName,
};
},
data() {
@@ -56,6 +54,7 @@ export default {
},
},
methods: {
useInstallationName,
twilioChannelName(item) {
const { medium = '' } = item;
if (medium === 'whatsapp') return 'WhatsApp';
@@ -10,7 +10,7 @@ import { mapGetters } from 'vuex';
import ChannelApi from '../../../../../api/channels';
import PageHeader from '../../SettingsSubPageHeader.vue';
import router from '../../../../index';
import { useGlobalConfig } from 'shared/composables/useGlobalConfig';
import { useInstallationName } from 'shared/helpers/installationHelper';
import { loadScript } from 'dashboard/helper/DOMHelpers';
import * as Sentry from '@sentry/browser';
@@ -21,10 +21,8 @@ export default {
PageHeader,
},
setup() {
const { useInstallationName } = useGlobalConfig();
const { accountId } = useAccount();
return {
useInstallationName,
accountId,
v$: useVuelidate(),
};
@@ -75,6 +73,7 @@ export default {
},
methods: {
useInstallationName,
async startLogin() {
this.hasLoginStarted = true;
try {
@@ -2,7 +2,7 @@
import { mapGetters } from 'vuex';
import { frontendURL } from '../../../../helper/URLHelper';
import { useAlert } from 'dashboard/composables';
import { useGlobalConfig } from 'shared/composables/useGlobalConfig';
import { useInstallationName } from 'shared/helpers/installationHelper';
export default {
props: {
@@ -17,12 +17,6 @@ export default {
actionButtonText: { type: String, default: '' },
deleteConfirmationText: { type: Object, default: () => ({}) },
},
setup() {
const { useInstallationName } = useGlobalConfig();
return {
useInstallationName,
};
},
data() {
return {
showDeleteConfirmationPopup: false,
@@ -35,6 +29,7 @@ export default {
}),
},
methods: {
useInstallationName,
frontendURL,
openDeletePopup() {
this.showDeleteConfirmationPopup = true;
@@ -3,7 +3,7 @@ import { computed } from 'vue';
import { useStoreGetters } from 'dashboard/composables/store';
import { useI18n } from 'dashboard/composables/useI18n';
import { frontendURL } from 'dashboard/helper/URLHelper';
import { useGlobalConfig } from 'shared/composables/useGlobalConfig';
import { useInstallationName } from 'shared/helpers/installationHelper';
const props = defineProps({
id: {
@@ -24,7 +24,6 @@ const props = defineProps({
},
});
const { useInstallationName } = useGlobalConfig();
const getters = useStoreGetters();
const accountId = getters.getCurrentAccountId;
const globalConfig = getters['globalConfig/get'];
@@ -1,7 +1,7 @@
<script>
import { mapGetters } from 'vuex';
import { useAlert } from 'dashboard/composables';
import { useGlobalConfig } from 'shared/composables/useGlobalConfig';
import { useInstallationName } from 'shared/helpers/installationHelper';
import messageFormatterMixin from 'shared/mixins/messageFormatterMixin';
export default {
@@ -12,12 +12,6 @@ export default {
default: true,
},
},
setup() {
const { useInstallationName } = useGlobalConfig();
return {
useInstallationName,
};
},
data() {
return { selectedChannelId: '', availableChannels: [] };
},
@@ -33,6 +27,7 @@ export default {
},
},
methods: {
useInstallationName,
async fetchChannels() {
try {
this.availableChannels = await this.$store.dispatch(
@@ -3,7 +3,7 @@ import { mapGetters } from 'vuex';
import { useAlert } from 'dashboard/composables';
import NewWebhook from './NewWebHook.vue';
import EditWebhook from './EditWebHook.vue';
import { useGlobalConfig } from 'shared/composables/useGlobalConfig';
import { useInstallationName } from 'shared/helpers/installationHelper';
import WebhookRow from './WebhookRow.vue';
export default {
@@ -12,12 +12,6 @@ export default {
EditWebhook,
WebhookRow,
},
setup() {
const { useInstallationName } = useGlobalConfig();
return {
useInstallationName,
};
},
data() {
return {
loading: {},
@@ -38,6 +32,7 @@ export default {
this.$store.dispatch('webhooks/get');
},
methods: {
useInstallationName,
openAddPopup() {
this.showAddPopup = true;
},
@@ -1,6 +1,6 @@
<script>
import { useAlert } from 'dashboard/composables';
import { useGlobalConfig } from 'shared/composables/useGlobalConfig';
import { useInstallationName } from 'shared/helpers/installationHelper';
import { mapGetters } from 'vuex';
import WebhookForm from './WebhookForm.vue';
@@ -12,12 +12,6 @@ export default {
required: true,
},
},
setup() {
const { useInstallationName } = useGlobalConfig();
return {
useInstallationName,
};
},
computed: {
...mapGetters({
globalConfig: 'globalConfig/get',
@@ -25,6 +19,7 @@ export default {
}),
},
methods: {
useInstallationName,
async onSubmit(webhook) {
try {
await this.$store.dispatch('webhooks/create', { webhook });
@@ -4,7 +4,7 @@ import { useAlert } from 'dashboard/composables';
import { useUISettings } from 'dashboard/composables/useUISettings';
import { clearCookiesOnLogout } from 'dashboard/store/utils/api.js';
import { copyTextToClipboard } from 'shared/helpers/clipboard';
import { useGlobalConfig } from 'shared/composables/useGlobalConfig';
import { useInstallationName } from 'shared/helpers/installationHelper';
import UserProfilePicture from './UserProfilePicture.vue';
import UserBasicDetails from './UserBasicDetails.vue';
import MessageSignature from './MessageSignature.vue';
@@ -28,7 +28,6 @@ export default {
AccessToken,
},
setup() {
const { useInstallationName } = useGlobalConfig();
const { uiSettings, updateUISettings, isEditorHotKeyEnabled } =
useUISettings();
@@ -36,7 +35,6 @@ export default {
uiSettings,
updateUISettings,
isEditorHotKeyEnabled,
useInstallationName,
};
},
data() {
@@ -87,6 +85,7 @@ export default {
}
},
methods: {
useInstallationName,
initializeUser() {
this.name = this.currentUser.name;
this.email = this.currentUser.email;
@@ -1,5 +1,5 @@
<script>
import { useGlobalConfig } from 'shared/composables/useGlobalConfig';
import { useInstallationName } from 'shared/helpers/installationHelper';
const {
LOGO_THUMBNAIL: logoThumbnail,
@@ -14,12 +14,6 @@ export default {
default: false,
},
},
setup() {
const { useInstallationName } = useGlobalConfig();
return {
useInstallationName,
};
},
data() {
return {
globalConfig: {
@@ -46,6 +40,9 @@ export default {
return '';
},
},
methods: {
useInstallationName,
},
};
</script>
@@ -1,18 +0,0 @@
/*
* Composable for using the installation name in the application.
* @param {string} str - The string to be processed.
* @param {string} installationName - The installation name.
* @returns {string} The processed string with the installation name.
*/
export const useGlobalConfig = () => {
const useInstallationName = (str, installationName) => {
if (str && installationName) {
return str.replace(/Chatwoot/g, installationName);
}
return str;
};
return {
useInstallationName,
};
};
@@ -0,0 +1,14 @@
/**
* Replaces occurrences of "Chatwoot" with the provided installation name in a given string.
*
* @param {string} str - The original string that may contain "Chatwoot".
* @param {string} installationName - The name to replace "Chatwoot" with.
* @returns {string} The modified string with "Chatwoot" replaced by the installation name,
* or the original string if either input is falsy.
*/
export const useInstallationName = (str, installationName) => {
if (str && installationName) {
return str.replace(/Chatwoot/g, installationName);
}
return str;
};
@@ -1,10 +1,9 @@
import { useGlobalConfig } from '../useGlobalConfig';
import { useInstallationName } from '../installationNameHelper';
describe('useGlobalConfig', () => {
describe('installationNameHelper', () => {
it('should return the string with the installation name', () => {
const str = 'Chatwoot is awesome';
const installationName = 'Acme Inc';
const { useInstallationName } = useGlobalConfig();
expect(useInstallationName(str, installationName)).toBe(
'Acme Inc is awesome'
);
@@ -13,7 +12,6 @@ describe('useGlobalConfig', () => {
it('should return the string without the installation name', () => {
const str = 'Chatwoot is awesome';
const installationName = '';
const { useInstallationName } = useGlobalConfig();
expect(useInstallationName(str, installationName)).toBe(
'Chatwoot is awesome'
);
@@ -22,7 +20,6 @@ describe('useGlobalConfig', () => {
it('should handle null installation name properly', () => {
const str = 'Chatwoot is super cool';
const installationName = null;
const { useInstallationName } = useGlobalConfig();
expect(useInstallationName(str, installationName)).toBe(
'Chatwoot is super cool'
);
@@ -31,7 +28,6 @@ describe('useGlobalConfig', () => {
it('should handle undefined installation name properly', () => {
const str = 'Chatwoot is super cool';
const installationName = undefined;
const { useInstallationName } = useGlobalConfig();
expect(useInstallationName(str, installationName)).toBe(
'Chatwoot is super cool'
);
@@ -3,7 +3,7 @@ import { useVuelidate } from '@vuelidate/core';
import { mapGetters } from 'vuex';
import { useAlert } from 'dashboard/composables';
import { required, minLength, email } from '@vuelidate/validators';
import { useGlobalConfig } from 'shared/composables/useGlobalConfig';
import { useInstallationName } from 'shared/helpers/installationHelper';
import FormInput from '../../../../components/Form/Input.vue';
import { resetPassword } from '../../../../api/auth';
import SubmitButton from '../../../../components/Button/SubmitButton.vue';
@@ -11,8 +11,7 @@ import SubmitButton from '../../../../components/Button/SubmitButton.vue';
export default {
components: { FormInput, SubmitButton },
setup() {
const { useInstallationName } = useGlobalConfig();
return { v$: useVuelidate(), useInstallationName };
return { v$: useVuelidate() };
},
data() {
return {
@@ -37,6 +36,7 @@ export default {
},
},
methods: {
useInstallationName,
showAlertMessage(message) {
// Reset loading, current selected agent
this.resetPassword.showLoading = false;
@@ -1,6 +1,6 @@
<script>
import { mapGetters } from 'vuex';
import { useGlobalConfig } from 'shared/composables/useGlobalConfig';
import { useInstallationName } from 'shared/helpers/installationHelper';
import SignupForm from './components/Signup/Form.vue';
import Testimonials from './components/Testimonials/Index.vue';
import Spinner from 'shared/components/Spinner.vue';
@@ -11,12 +11,6 @@ export default {
Spinner,
Testimonials,
},
setup() {
const { useInstallationName } = useGlobalConfig();
return {
useInstallationName,
};
},
data() {
return { isLoading: false };
},
@@ -30,6 +24,7 @@ export default {
this.isLoading = this.isAChatwootInstance;
},
methods: {
useInstallationName,
resizeContainers() {
this.isLoading = false;
},