Skip to content

Commit

Permalink
[IMPROVE] Reduce complexity in some functions (#23387)
Browse files Browse the repository at this point in the history
  • Loading branch information
tassoevan authored Nov 10, 2021
1 parent f50609d commit 8650aa7
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 120 deletions.
113 changes: 36 additions & 77 deletions app/utils/lib/getURL.tests.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
/* eslint-disable complexity */
/* eslint-env mocha */
import 'babel-polyfill';
import assert from 'assert';
Expand Down Expand Up @@ -89,12 +88,6 @@ const testCases = (options) => {
}
}
} else if (options._cdn_prefix === '') {
if (options.full && !options.cdn && !options.cloud) {
it('should return with host if full: true', () => {
testPaths(options, (path) => _site_url + path);
});
}

if (!options.full && options.cdn) {
it('should return with cloud host if cdn: true', () => {
testPaths(options, (path) => getCloudUrl(_site_url, path));
Expand All @@ -106,88 +99,54 @@ const testCases = (options) => {
testPaths(options, (path) => getCloudUrl(_site_url, path));
});
}

if (options.full && options.cdn && !options.cloud) {
it('should return with host if full: true and cdn: true', () => {
testPaths(options, (path) => _site_url + path);
});
}
} else {
if (options.full && !options.cdn && !options.cloud) {
it('should return with host if full: true', () => {
testPaths(options, (path) => _site_url + path);
});
}

if (!options.full && options.cdn && !options.cloud) {
it('should return with cdn prefix if cdn: true', () => {
testPaths(options, (path) => options._cdn_prefix + path);
});
}

if (!options.full && !options.cdn) {
it('should return with cloud host if full: fase and cdn: false', () => {
testPaths(options, (path) => getCloudUrl(_site_url, path));
});
}

if (options.full && options.cdn && !options.cloud) {
it('should return with host if full: true and cdn: true', () => {
testPaths(options, (path) => options._cdn_prefix + path);
});
}
} else if (!options.full && !options.cdn) {
it('should return with cloud host if full: fase and cdn: false', () => {
testPaths(options, (path) => getCloudUrl(_site_url, path));
});
}
};

const testOptions = (options) => {
testCases({ ...options, cdn: false, full: false, cloud: false });
testCases({ ...options, cdn: true, full: false, cloud: false });
testCases({ ...options, cdn: false, full: true, cloud: false });
testCases({ ...options, cdn: false, full: false, cloud: true });
testCases({ ...options, cdn: true, full: true, cloud: false });
testCases({ ...options, cdn: false, full: true, cloud: true });
testCases({ ...options, cdn: true, full: false, cloud: true });
testCases({ ...options, cdn: true, full: true, cloud: true });
const testCasesForOptions = (description, options) => {
describe(description, () => {
testCases({ ...options, cdn: false, full: false, cloud: false });
testCases({ ...options, cdn: true, full: false, cloud: false });
testCases({ ...options, cdn: false, full: true, cloud: false });
testCases({ ...options, cdn: false, full: false, cloud: true });
testCases({ ...options, cdn: true, full: true, cloud: false });
testCases({ ...options, cdn: false, full: true, cloud: true });
testCases({ ...options, cdn: true, full: false, cloud: true });
testCases({ ...options, cdn: true, full: true, cloud: true });
});
};

describe('getURL', () => {
describe('getURL with no CDN, no PREFIX for http://localhost:3000/', () => {
testOptions({
_cdn_prefix: '',
_root_url_path_prefix: '',
_site_url: 'http://localhost:3000/',
});
describe.only('getURL', () => {
testCasesForOptions('getURL with no CDN, no PREFIX for http://localhost:3000/', {
_cdn_prefix: '',
_root_url_path_prefix: '',
_site_url: 'http://localhost:3000/',
});

describe('getURL with no CDN, no PREFIX for http://localhost:3000', () => {
testOptions({
_cdn_prefix: '',
_root_url_path_prefix: '',
_site_url: 'http://localhost:3000',
});
testCasesForOptions('getURL with no CDN, no PREFIX for http://localhost:3000', {
_cdn_prefix: '',
_root_url_path_prefix: '',
_site_url: 'http://localhost:3000',
});

describe('getURL with CDN, no PREFIX for http://localhost:3000/', () => {
testOptions({
_cdn_prefix: 'https://cdn.com',
_root_url_path_prefix: '',
_site_url: 'http://localhost:3000/',
});
testCasesForOptions('getURL with CDN, no PREFIX for http://localhost:3000/', {
_cdn_prefix: 'https://cdn.com',
_root_url_path_prefix: '',
_site_url: 'http://localhost:3000/',
});

describe('getURL with CDN, PREFIX for http://localhost:3000/', () => {
testOptions({
_cdn_prefix: 'https://cdn.com',
_root_url_path_prefix: 'sub',
_site_url: 'http://localhost:3000/',
});
testCasesForOptions('getURL with CDN, PREFIX for http://localhost:3000/', {
_cdn_prefix: 'https://cdn.com',
_root_url_path_prefix: 'sub',
_site_url: 'http://localhost:3000/',
});

describe('getURL with CDN, PREFIX for https://localhost:3000/', () => {
testOptions({
_cdn_prefix: 'https://cdn.com',
_root_url_path_prefix: 'sub',
_site_url: 'https://localhost:3000/',
});
testCasesForOptions('getURL with CDN, PREFIX for https://localhost:3000/', {
_cdn_prefix: 'https://cdn.com',
_root_url_path_prefix: 'sub',
_site_url: 'https://localhost:3000/',
});
});
77 changes: 36 additions & 41 deletions client/views/omnichannel/departments/EditDepartment.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
/* eslint-disable complexity */
import {
FieldGroup,
Field,
Expand Down Expand Up @@ -33,19 +32,17 @@ import DepartmentsAgentsTable from './DepartmentsAgentsTable';

function EditDepartment({ data, id, title, reload, allowedToForwardData }) {
const t = useTranslation();
const agentsRoute = useRoute('omnichannel-departments');
const eeForms = useSubscription(formsSubscription);
const initialAgents = useRef((data && data.agents) || []);

const router = useRoute('omnichannel-departments');
const departmentsRoute = useRoute('omnichannel-departments');

const {
useEeNumberInput = () => {},
useEeTextInput = () => {},
useEeTextAreaInput = () => {},
useDepartmentForwarding = () => {},
useDepartmentBusinessHours = () => {},
} = eeForms;
} = useSubscription(formsSubscription);

const initialAgents = useRef((data && data.agents) || []);

const MaxChats = useEeNumberInput();
const VisitorInactivity = useEeNumberInput();
Expand All @@ -57,29 +54,23 @@ function EditDepartment({ data, id, title, reload, allowedToForwardData }) {

const { department } = data || { department: {} };

const [tags, setTags] = useState((department && department.chatClosingTags) || []);
const [tagsText, setTagsText] = useState();
const [[tags, tagsText], setTagsState] = useState(() => [department?.chatClosingTags ?? [], '']);

const { values, handlers, hasUnsavedChanges } = useForm({
name: (department && department.name) || '',
email: (department && department.email) || '',
description: (department && department.description) || '',
enabled: !!(department && department.enabled),
maxNumberSimultaneousChat: (department && department.maxNumberSimultaneousChat) || undefined,
showOnRegistration: !!(department && department.showOnRegistration),
showOnOfflineForm: !!(department && department.showOnOfflineForm),
abandonedRoomsCloseCustomMessage:
(department && department.abandonedRoomsCloseCustomMessage) || '',
requestTagBeforeClosingChat: (department && department.requestTagBeforeClosingChat) || false,
offlineMessageChannelName: (department && department.offlineMessageChannelName) || '',
visitorInactivityTimeoutInSeconds:
(department && department.visitorInactivityTimeoutInSeconds) || undefined,
waitingQueueMessage: (department && department.waitingQueueMessage) || '',
name: department?.name || '',
email: department?.email || '',
description: department?.description || '',
enabled: !!department?.enabled,
maxNumberSimultaneousChat: department?.maxNumberSimultaneousChat || undefined,
showOnRegistration: !!department?.showOnRegistration,
showOnOfflineForm: !!department?.showOnOfflineForm,
abandonedRoomsCloseCustomMessage: department?.abandonedRoomsCloseCustomMessage || '',
requestTagBeforeClosingChat: department?.requestTagBeforeClosingChat || false,
offlineMessageChannelName: department?.offlineMessageChannelName || '',
visitorInactivityTimeoutInSeconds: department?.visitorInactivityTimeoutInSeconds || undefined,
waitingQueueMessage: department?.waitingQueueMessage || '',
departmentsAllowedToForward:
(allowedToForwardData &&
allowedToForwardData.departments &&
allowedToForwardData.departments.map((dep) => ({ label: dep.name, value: dep._id }))) ||
[],
allowedToForwardData?.departments?.map((dep) => ({ label: dep.name, value: dep._id })) || [],
});
const {
handleName,
Expand Down Expand Up @@ -119,20 +110,25 @@ function EditDepartment({ data, id, title, reload, allowedToForwardData }) {
const { phase: roomsPhase, items: roomsItems, itemCount: roomsTotal } = useRecordList(RoomsList);

const handleTagChipClick = (tag) => () => {
setTags((tags) => tags.filter((_tag) => _tag !== tag));
setTagsState(([tags, tagsText]) => [tags.filter((_tag) => _tag !== tag), tagsText]);
};

const handleTagTextSubmit = useMutableCallback(() => {
if (!tags.includes(tagsText)) {
setTags([...tags, tagsText]);
setTagsText('');
}
});
setTagsState((state) => {
const [tags, tagsText] = state;

if (tags.includes(tagsText)) {
return state;
}

const handleTagTextChange = useMutableCallback((e) => {
setTagsText(e.target.value);
return [[...tags, tagsText], ''];
});
});

const handleTagTextChange = (e) => {
setTagsState(([tags]) => [tags, e.target.value]);
};

const saveDepartmentInfo = useMethod('livechat:saveDepartment');
const saveDepartmentAgentsInfoOnEdit = useEndpoint('POST', `livechat/department/${id}/agents`);

Expand Down Expand Up @@ -197,8 +193,7 @@ function EditDepartment({ data, id, title, reload, allowedToForwardData }) {
visitorInactivityTimeoutInSeconds,
abandonedRoomsCloseCustomMessage,
waitingQueueMessage,
departmentsAllowedToForward:
departmentsAllowedToForward && departmentsAllowedToForward.map((dep) => dep.value).join(),
departmentsAllowedToForward: departmentsAllowedToForward?.map((dep) => dep.value).join(),
};

const agentListPayload = {
Expand Down Expand Up @@ -227,14 +222,14 @@ function EditDepartment({ data, id, title, reload, allowedToForwardData }) {
}
dispatchToastMessage({ type: 'success', message: t('Saved') });
reload();
agentsRoute.push({});
departmentsRoute.push({});
} catch (error) {
dispatchToastMessage({ type: 'error', message: error });
}
});

const handleReturn = useMutableCallback(() => {
router.push({});
departmentsRoute.push({});
});

const invalidForm =
Expand Down Expand Up @@ -438,7 +433,7 @@ function EditDepartment({ data, id, title, reload, allowedToForwardData }) {
</Button>
</Field.Row>
<Field.Hint>{t('Conversation_closing_tags_description')}</Field.Hint>
{tags && tags.length > 0 && (
{tags?.length > 0 && (
<Field.Row justifyContent='flex-start'>
{tags.map((tag, i) => (
<Chip key={i} onClick={handleTagChipClick(tag)} mie='x8'>
Expand All @@ -451,7 +446,7 @@ function EditDepartment({ data, id, title, reload, allowedToForwardData }) {
)}
{DepartmentBusinessHours && (
<Field>
<DepartmentBusinessHours bhId={department && department.businessHourId} />
<DepartmentBusinessHours bhId={department?.businessHourId} />
</Field>
)}
<Divider mb='x16' />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
/* eslint-disable complexity */
import { Box } from '@rocket.chat/fuselage';
import React, { useMemo } from 'react';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
/* eslint-disable complexity */
import { Box } from '@rocket.chat/fuselage';
import React from 'react';

Expand Down

0 comments on commit 8650aa7

Please sign in to comment.