working group chats

This commit is contained in:
erik-sn committed 2020-11-20 18:40:51 -05:00
1 parent 681ce16912
commit 5a78b67946
6 files changed
+36 -37

No files matched your search

@@ -16,12 +16,14 @@ const MessageInput = ({ messageGroupId }: IProps) => {
const handleSubmit = (event: FormEvent) => { const handleSubmit = (event: FormEvent) => {
event.preventDefault(); event.preventDefault();
if (message.trim()) { // don't allow the user to submit white space
Nui.send('phone:sendMessage', { Nui.send('phone:sendMessage', {
groupId: messageGroupId, groupId: messageGroupId,
message message
}); });
setMessage(''); setMessage('');
} }
}
if (!messageGroupId) return null; if (!messageGroupId) return null;
@@ -1,4 +1,4 @@
import React from 'react' import React, { useEffect } from 'react'
import { List, ListItem, ListItemText, ListItemAvatar, Avatar as MuiAvatar } from '@material-ui/core'; import { List, ListItem, ListItemText, ListItemAvatar, Avatar as MuiAvatar } from '@material-ui/core';
import { MessageGroup } from '../../../../common/interfaces/messages'; import { MessageGroup } from '../../../../common/interfaces/messages';
@@ -12,6 +12,10 @@ const MessagesList = (): any => {
const { messageGroups } = useMessages(); const { messageGroups } = useMessages();
const { setActiveMessageGroup } = useModals(); const { setActiveMessageGroup } = useModals();
useEffect(() => {
Nui.send('phone:fetchMessageGroups');
}, []);
if (!messageGroups) return null; if (!messageGroups) return null;
const handleClick = (messageGroup: MessageGroup) => () => { const handleClick = (messageGroup: MessageGroup) => () => {
@@ -7,6 +7,6 @@ export const useMessagesService = () => {
const setMessageGroups = useSetRecoilState(messageState.messageGroups); const setMessageGroups = useSetRecoilState(messageState.messageGroups);
const setMessages = useSetRecoilState(messageState.messages); const setMessages = useSetRecoilState(messageState.messages);
useNuiEvent('MESSAGES', 'phone:getMessageGroupsSuccess', setMessageGroups); useNuiEvent('MESSAGES', 'phone:fetchMessageGroupsSuccess', setMessageGroups);
useNuiEvent('MESSAGES', 'phone:fetchMessagesSuccess', setMessages); useNuiEvent('MESSAGES', 'phone:fetchMessagesSuccess', setMessages);
} }
+12 -13
View File
@@ -7,22 +7,22 @@ import { sendMessageEvent } from '../utils/messages';
/** /**
* Messages get message groups * Messages get message groups
*/ */
RegisterNuiCallbackType(events.MESSAGES_GET_MESSAGE_GROUPS); RegisterNuiCallbackType(events.MESSAGES_FETCH_MESSAGE_GROUPS);
on(`__cfx_nui:${events.MESSAGES_GET_MESSAGE_GROUPS}`, (): void => { on(`__cfx_nui:${events.MESSAGES_FETCH_MESSAGE_GROUPS}`, (): void => {
emitNet(events.MESSAGES_GET_MESSAGE_GROUPS); emitNet(events.MESSAGES_FETCH_MESSAGE_GROUPS);
}); });
onNet(events.MESSAGES_GET_MESSAGE_GROUPS_SUCCESS, (messageGroups: MessageGroup[]): void => { onNet(events.MESSAGES_FETCH_MESSAGE_GROUPS_SUCCESS, (messageGroups: MessageGroup[]): void => {
sendMessageEvent(events.MESSAGES_GET_MESSAGE_GROUPS_SUCCESS, messageGroups); sendMessageEvent(events.MESSAGES_FETCH_MESSAGE_GROUPS_SUCCESS, messageGroups);
emitNet(events.MESSAGES_GET_MESSAGE_GROUPS_SUCCESS); emitNet(events.MESSAGES_FETCH_MESSAGE_GROUPS_SUCCESS);
}); });
onNet(events.MESSAGES_CREATE_MESSAGE_GROUP_FAILED, () => { onNet(events.MESSAGES_FETCH_MESSAGE_GROUPS_FAILED, () => {
sendMessageEvent(events.MESSAGES_CREATE_MESSAGE_GROUP_FAILED); sendMessageEvent(events.MESSAGES_FETCH_MESSAGE_GROUPS_FAILED);
}); });
// fetch message groups on client launch // fetch message groups on client launch
emitNet(events.MESSAGES_GET_MESSAGE_GROUPS); emitNet(events.MESSAGES_FETCH_MESSAGE_GROUPS);
/** /**
@@ -33,10 +33,9 @@ on(`__cfx_nui:${events.MESSAGES_CREATE_MESSAGE_GROUP}`, ({ phoneNumbers }: any):
emitNet(events.MESSAGES_CREATE_MESSAGE_GROUP, phoneNumbers); emitNet(events.MESSAGES_CREATE_MESSAGE_GROUP, phoneNumbers);
}); });
onNet(events.MESSAGES_CREATE_MESSAGE_GROUP_SUCCESS, (result: any): void => { onNet(events.MESSAGES_CREATE_MESSAGE_GROUP_SUCCESS, (): void => {
console.log(result); sendMessageEvent(events.MESSAGES_CREATE_MESSAGE_GROUP_SUCCESS);
sendMessageEvent(events.MESSAGES_CREATE_MESSAGE_GROUP_SUCCESS, result); emitNet(events.MESSAGES_FETCH_MESSAGE_GROUPS);
emitNet(events.MESSAGES_CREATE_MESSAGE_GROUP_SUCCESS);
}); });
onNet(events.MESSAGES_CREATE_MESSAGE_GROUP_FAILED, (): void => { onNet(events.MESSAGES_CREATE_MESSAGE_GROUP_FAILED, (): void => {
+13 -19
View File
@@ -40,14 +40,11 @@ async function getMessageGroups(userIdentifier: string): Promise<UnformattedMess
npwd_messages_groups.group_id, npwd_messages_groups.group_id,
npwd_messages_groups.participant_identifier, npwd_messages_groups.participant_identifier,
users.phone_number, users.phone_number,
MAX(npwd_messages.updatedAt) AS updatedAt,
npwd_phone_contacts.display npwd_phone_contacts.display
FROM npwd_messages_groups FROM npwd_messages_groups
LEFT OUTER JOIN users on users.identifier = npwd_messages_groups.participant_identifier LEFT OUTER JOIN users on users.identifier = npwd_messages_groups.participant_identifier
LEFT OUTER JOIN npwd_phone_contacts on npwd_phone_contacts.number = users.phone_number LEFT OUTER JOIN npwd_phone_contacts on npwd_phone_contacts.number = users.phone_number
LEFT OUTER JOIN npwd_messages on npwd_messages.group_id = npwd_messages_groups.group_id
WHERE npwd_messages_groups.user_identifier = ? AND npwd_messages_groups.participant_identifier != ? WHERE npwd_messages_groups.user_identifier = ? AND npwd_messages_groups.participant_identifier != ?
ORDER BY updatedAt DESC
` `
const [results] = await pool.query(query, [ userIdentifier, userIdentifier]); const [results] = await pool.query(query, [ userIdentifier, userIdentifier]);
return <UnformattedMessageGroup[]>results; return <UnformattedMessageGroup[]>results;
@@ -164,18 +161,26 @@ async function createMessageGroupsFromPhoneNumbers(userIdentifier: string, phone
return groupId; return groupId;
} }
onNet(events.MESSAGES_FETCH_MESSAGE_GROUPS, async () => {
onNet(events.MESSAGES_GET_MESSAGE_GROUPS, async () => {
try { try {
const _identifier = await useIdentifier(); const _identifier = await useIdentifier();
const messageGroups = await getFormattedMessageGroups(_identifier); const messageGroups = await getFormattedMessageGroups(_identifier);
emitNet(events.MESSAGES_GET_MESSAGE_GROUPS_SUCCESS, getSource(), messageGroups); emitNet(events.MESSAGES_FETCH_MESSAGE_GROUPS_SUCCESS, getSource(), messageGroups);
} catch (e) { } catch (e) {
emitNet(events.MESSAGES_GET_MESSAGE_GROUPS_FAILED, getSource()); emitNet(events.MESSAGES_FETCH_MESSAGE_GROUPS_FAILED, getSource());
} }
}); });
onNet(events.MESSAGES_CREATE_MESSAGE_GROUP, async (phoneNumbers: string[]) => {
try {
console.log(phoneNumbers);
const _identifier = await useIdentifier();
await createMessageGroupsFromPhoneNumbers(_identifier, phoneNumbers)
emitNet(events.MESSAGES_CREATE_MESSAGE_GROUP_SUCCESS, getSource());
} catch (e) {
emitNet(events.MESSAGES_CREATE_MESSAGE_GROUP_FAILED, getSource());
}
});
onNet(events.MESSAGES_FETCH_MESSAGES, async (groupId: string) => { onNet(events.MESSAGES_FETCH_MESSAGES, async (groupId: string) => {
try { try {
@@ -196,14 +201,3 @@ onNet(events.MESSAGES_SEND_MESSAGE, async (groupId: string, message: string) =>
emitNet(events.MESSAGES_SEND_MESSAGE_FAILED, getSource()); emitNet(events.MESSAGES_SEND_MESSAGE_FAILED, getSource());
} }
}); });
onNet(events.MESSAGES_CREATE_MESSAGE_GROUP, async (phoneNumbers: string[]) => {
try {
console.log(phoneNumbers);
const _identifier = await useIdentifier();
const groupId = await createMessageGroupsFromPhoneNumbers(_identifier, phoneNumbers)
emitNet(events.MESSAGES_CREATE_MESSAGE_GROUP_SUCCESS, getSource(), groupId);
} catch (e) {
emitNet(events.MESSAGES_CREATE_MESSAGE_GROUP_FAILED, getSource());
}
});
+3 -3
View File
@@ -54,9 +54,9 @@ export default {
BANK_GET_CREDENTIALS: 'phone:getBankCredentials', BANK_GET_CREDENTIALS: 'phone:getBankCredentials',
//messages //messages
MESSAGES_GET_MESSAGE_GROUPS: 'phone:getMessageGroups', MESSAGES_FETCH_MESSAGE_GROUPS: 'phone:fetchMessageGroups',
MESSAGES_GET_MESSAGE_GROUPS_SUCCESS: 'phone:getMessageGroupsSuccess', MESSAGES_FETCH_MESSAGE_GROUPS_SUCCESS: 'phone:fetchMessageGroupsSuccess',
MESSAGES_GET_MESSAGE_GROUPS_FAILED: 'phone:getMessageGroupsFailed', MESSAGES_FETCH_MESSAGE_GROUPS_FAILED: 'phone:fetchMessageGroupsFailed',
MESSAGES_CREATE_MESSAGE_GROUP: 'phone:createMessageGroup', MESSAGES_CREATE_MESSAGE_GROUP: 'phone:createMessageGroup',
MESSAGES_CREATE_MESSAGE_GROUP_SUCCESS: 'phone:createMessageGroupSuccess', MESSAGES_CREATE_MESSAGE_GROUP_SUCCESS: 'phone:createMessageGroupSuccess',
MESSAGES_CREATE_MESSAGE_GROUP_FAILED: 'phone:createMessageGroupFailed', MESSAGES_CREATE_MESSAGE_GROUP_FAILED: 'phone:createMessageGroupFailed',