Skip to content

Commit

Permalink
Merge pull request #3033 from glific/refactoring
Browse files Browse the repository at this point in the history
Moved functions to helper file
  • Loading branch information
kurund committed Aug 20, 2024
2 parents 517fa22 + 817b8eb commit 14a1d1c
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 106 deletions.
104 changes: 0 additions & 104 deletions src/common/constants.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import dayjs from 'dayjs';
import * as Yup from 'yup';
import { getDisplayName, getDisplayNameForSearch } from './utils';

export const OLD_DOMAIN = 'tides.coloredcow.com';
export const NEW_DOMAIN = 'glific.com';
Expand Down Expand Up @@ -263,106 +262,3 @@ export const getVariables = (
...variables,
};
};

export const getConversationForSearchMulti = (
conversation: any,
selectedContactId: any,
groups: boolean
) => {
const chatType: string = groups ? 'waGroup' : 'contact';

let entity = conversation;
let selectedRecord = false;
let timer;
if (selectedContactId == entity.id) {
selectedRecord = true;
}
let entityId: any;
let displayName: string;
let contactIsOrgRead: boolean = false;

if (groups) {
entityId = entity.waGroup?.id || entity.id;
} else {
entityId = entity.contact?.id || entity.id;
}

if (conversation[chatType]) {
entity = conversation[chatType];
if (selectedContactId == conversation[chatType]?.id) {
selectedRecord = true;
}
} else if (conversation.bspStatus && conversation.lastMessageAt) {
contactIsOrgRead = conversation.isOrgRead;
timer = {
time: conversation.lastMessageAt,
contactStatus: conversation.status,
contactBspStatus: conversation.bspStatus,
};
}

displayName = getDisplayNameForSearch(entity);

return {
displayName,
contactIsOrgRead,
selectedRecord,
entityId,
entity,
timer,
};
};

export const getConversation = (
conversation: any,
selectedContactId: any,
selectedCollectionId: any
) => {
let lastMessage = [];
if (conversation.messages && conversation.messages.length > 0) {
[lastMessage] = conversation.messages;
}
let entityId: any;
let displayName = '';
let contactIsOrgRead = false;
let selectedRecord = false;
let timer;
if (conversation.waGroup) {
if (selectedContactId === conversation.waGroup?.id) {
selectedRecord = true;
}
entityId = conversation.waGroup?.id;
displayName = conversation.waGroup?.label;
contactIsOrgRead = false;
} else if (conversation.contact) {
if (selectedContactId === conversation.contact.id) {
selectedRecord = true;
}
entityId = conversation.contact.id;
displayName = getDisplayName(conversation.contact);
contactIsOrgRead = conversation.contact.isOrgRead;
timer = {
contactStatus: conversation.contact.status,
contactBspStatus: conversation.contact.bspStatus,
time: conversation.contact.lastMessageAt,
};
}

if (conversation.group) {
if (selectedCollectionId === conversation.group.id) {
selectedRecord = true;
}
entityId = conversation.group.id;
displayName = conversation.group.label;
contactIsOrgRead = conversation.group.isOrgRead;
}

return {
lastMessage,
entityId,
displayName,
contactIsOrgRead,
selectedRecord,
timer,
};
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
import { getDisplayName, getDisplayNameForSearch } from 'common/utils';

export const getConversationForSearchMulti = (
conversation: any,
selectedContactId: any,
groups: boolean
) => {
const chatType: string = groups ? 'waGroup' : 'contact';

let entity = conversation;
let selectedRecord = false;
let timer;
if (selectedContactId === entity.id) {
selectedRecord = true;
}
let entityId: any;
let displayName: string;
let contactIsOrgRead: boolean = false;

if (groups) {
entityId = entity.waGroup?.id || entity.id;
} else {
entityId = entity.contact?.id || entity.id;
}

if (conversation[chatType]) {
entity = conversation[chatType];
if (selectedContactId === conversation[chatType]?.id) {
selectedRecord = true;
}
} else if (conversation.bspStatus && conversation.lastMessageAt) {
contactIsOrgRead = conversation.isOrgRead;
timer = {
time: conversation.lastMessageAt,
contactStatus: conversation.status,
contactBspStatus: conversation.bspStatus,
};
}

displayName = getDisplayNameForSearch(entity);

return {
displayName,
contactIsOrgRead,
selectedRecord,
entityId,
entity,
timer,
};
};

export const getConversation = (
conversation: any,
selectedContactId: any,
selectedCollectionId: any
) => {
let lastMessage = [];
if (conversation.messages && conversation.messages.length > 0) {
[lastMessage] = conversation.messages;
}
let entityId: any;
let displayName = '';
let contactIsOrgRead = false;
let selectedRecord = false;
let timer;
if (conversation.waGroup) {
if (selectedContactId === conversation.waGroup?.id) {
selectedRecord = true;
}
entityId = conversation.waGroup?.id;
displayName = conversation.waGroup?.label;
contactIsOrgRead = false;
} else if (conversation.contact) {
if (selectedContactId === conversation.contact.id) {
selectedRecord = true;
}
entityId = conversation.contact.id;
displayName = getDisplayName(conversation.contact);
contactIsOrgRead = conversation.contact.isOrgRead;
timer = {
contactStatus: conversation.contact.status,
contactBspStatus: conversation.contact.bspStatus,
time: conversation.contact.lastMessageAt,
};
}

if (conversation.group) {
if (selectedCollectionId === conversation.group.id) {
selectedRecord = true;
}
entityId = conversation.group.id;
displayName = conversation.group.label;
contactIsOrgRead = conversation.group.isOrgRead;
}

return {
lastMessage,
entityId,
displayName,
contactIsOrgRead,
selectedRecord,
timer,
};
};
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,13 @@ import {
GROUP_QUERY_VARIABLES,
GROUP_COLLECTION_SEARCH_QUERY_VARIABLES,
getVariables,
getConversationForSearchMulti,
getConversation,
} from 'common/constants';
import { updateConversations } from 'services/ChatService';
import { updateGroupConversations } from 'services/GroupMessageService';
import { showMessages } from 'common/responsive';
import { addLogs } from 'common/utils';
import ChatConversation from '../ChatConversation/ChatConversation';
import { getConversationForSearchMulti, getConversation } from './ConversationList.helper';
import styles from './ConversationList.module.css';
import { GROUP_SEARCH_MULTI_QUERY, GROUP_SEARCH_QUERY } from 'graphql/queries/WaGroups';
import { useLocation } from 'react-router-dom';
Expand Down

0 comments on commit 14a1d1c

Please sign in to comment.