working group texts

This commit is contained in:
erik-sn committed 2020-11-20 19:35:42 -05:00
1 parent 5a78b67946
commit 61eecd5b59
8 files changed
+98 -31

No files matched your search

@@ -1,4 +1,4 @@
import React, { useState } from 'react' import React, { FormEvent, useState } from 'react'
import { useTranslation } from 'react-i18next'; import { useTranslation } from 'react-i18next';
import { TextField, Button } from '@material-ui/core' import { TextField, Button } from '@material-ui/core'
import AddIcon from '@material-ui/icons/Add'; import AddIcon from '@material-ui/icons/Add';
@@ -12,32 +12,55 @@ const NewMessageGroupForm = () => {
const { t } = useTranslation(); const { t } = useTranslation();
const { setShowNewMessageGroup } = useModals(); const { setShowNewMessageGroup } = useModals();
const [ participants, setParticipants ] = useState(''); const [ participants, setParticipants ] = useState('');
const [ label, setLabel ] = useState('');
// handles phone numbers in a csv format and strips all spaces and
// external characters out of them:
// 123-4567, 987-6543, 333-4444
const parts = participants.split(',').map(part => part.replace(/[^0-9]/g, ''));
const isGroupChat = parts.length > 1;
const handleSubmit = () => { const handleSubmit = (event: FormEvent) => {
// handles phone numbers in a csv format and strips all spaces and event.preventDefault();
// external characters out of them: if (parts.length > 0) {
// 123-4567, 987-6543, 333-4444 const labelValue = label.trim();
const parts = participants.split(',').map(part => part.replace(/[^0-9]/g, ''));
Nui.send('phone:createMessageGroup', { phoneNumbers: parts }); Nui.send('phone:createMessageGroup', {
setParticipants(''); phoneNumbers: parts,
setShowNewMessageGroup(false); // close modal after submission label: isGroupChat && labelValue ? labelValue : null,
});
setParticipants('');
setShowNewMessageGroup(false); // close modal after submission
}
} }
return ( return (
<div className={classes.formTop}> <form className={classes.newGroupForm} onSubmit={handleSubmit}>
<TextField <TextField
value={participants} value={participants}
onChange={e => setParticipants(e.target.value)} onChange={e => setParticipants(e.target.value)}
placeholder={t("APPS_MESSAGES_NEW_MESSAGE_GROUP")} placeholder={t("APPS_MESSAGES_NEW_MESSAGE_GROUP")}
className={classes.input} className={classes.newGroupinput}
inputProps={{ inputProps={{
className: classes.messagesInput className: classes.messagesInput
}} }}
multiline
/> />
<Button className={classes.sendButton} onClick={handleSubmit} > {isGroupChat && (
<AddIcon fontSize="large" /> <TextField
value={label}
onChange={e => setLabel(e.target.value)}
placeholder={t("APPS_MESSAGES_GROUP_CHAT_LABEL")}
className={classes.newGroupinput}
inputProps={{
className: classes.messagesInput
}}
/>
)}
<Button variant="contained" className={classes.newGroupSubmitButton} color="primary" type="submit" >
{t("APPS_MESSAGES_NEW_MESSAGE_GROUP_SUBMIT")}
</Button> </Button>
</div> </form>
) )
} }
@@ -12,16 +12,24 @@ const useStyles = makeStyles((theme) => ({
form: { form: {
display: 'flex', display: 'flex',
}, },
formTop: {
position: 'absolute',
top: 65,
display: 'flex',
width: '100%',
},
input: { input: {
width: '100%', width: '100%',
margin: 10 margin: 10
}, },
newGroupForm: {
position: 'absolute',
top: 65,
display: 'flex',
flexFlow: 'column nowrap',
justifyContent: 'flex-start',
width: '100%',
},
newGroupSubmitButton: {
margin: '15px 8px 0px 8px',
},
newGroupinput: {
margin: '0px 8px 15px 8px',
},
messagesInput: { messagesInput: {
fontSize: 20 fontSize: 20
} }
@@ -33,10 +33,10 @@ const MessagesList = (): any => {
button button
> >
<ListItemAvatar> <ListItemAvatar>
<MuiAvatar /> <MuiAvatar src={messageGroup?.avatar} />
</ListItemAvatar> </ListItemAvatar>
<ListItemText> <ListItemText>
{messageGroup.groupDisplay} {messageGroup.label || messageGroup.groupDisplay}
</ListItemText> </ListItemText>
</ListItem> </ListItem>
))} ))}
@@ -7,3 +7,8 @@
#messages-app .Mui-focused:after { #messages-app .Mui-focused:after {
border-bottom-color: #ffa000 !important; border-bottom-color: #ffa000 !important;
} }
/* override form submit button */
#messages-app .MuiButton-containedPrimary {
background-color: #ffa000 !important;
}
+2
View File
@@ -13,5 +13,7 @@ export interface Message {
export interface MessageGroup { export interface MessageGroup {
groupId: string; groupId: string;
groupDisplay: string; groupDisplay: string;
avatar?: string;
label?: string;
updatedAt: string; updatedAt: string;
} }
+3 -1
View File
@@ -54,6 +54,8 @@
"APPS_CONTACT_FORM_AVATAR": "Avatar URL", "APPS_CONTACT_FORM_AVATAR": "Avatar URL",
"APPS_MESSAGES_NEW_MESSAGE": "Message...", "APPS_MESSAGES_NEW_MESSAGE": "Message...",
"APPS_MESSAGES_NEW_MESSAGE_GROUP": "Phone numbers..." "APPS_MESSAGES_NEW_MESSAGE_GROUP": "Phone numbers...",
"APPS_MESSAGES_GROUP_CHAT_LABEL": "Group chat label...",
"APPS_MESSAGES_NEW_MESSAGE_GROUP_SUBMIT": "Submit"
} }
} }
+2 -2
View File
@@ -29,8 +29,8 @@ emitNet(events.MESSAGES_FETCH_MESSAGE_GROUPS);
* Messages create message group * Messages create message group
*/ */
RegisterNuiCallbackType(events.MESSAGES_CREATE_MESSAGE_GROUP); RegisterNuiCallbackType(events.MESSAGES_CREATE_MESSAGE_GROUP);
on(`__cfx_nui:${events.MESSAGES_CREATE_MESSAGE_GROUP}`, ({ phoneNumbers }: any): void => { on(`__cfx_nui:${events.MESSAGES_CREATE_MESSAGE_GROUP}`, ({ phoneNumbers, label }: any): void => {
emitNet(events.MESSAGES_CREATE_MESSAGE_GROUP, phoneNumbers); emitNet(events.MESSAGES_CREATE_MESSAGE_GROUP, phoneNumbers, label);
}); });
onNet(events.MESSAGES_CREATE_MESSAGE_GROUP_SUCCESS, (): void => { onNet(events.MESSAGES_CREATE_MESSAGE_GROUP_SUCCESS, (): void => {
+33 -6
View File
@@ -9,13 +9,17 @@ interface UnformattedMessageGroup {
group_id: string; group_id: string;
participant_identifier: string; participant_identifier: string;
phone_number: string; phone_number: string;
display: string; label?: string;
avatar?: string;
display?: string;
updatedAt: string; updatedAt: string;
} }
interface MessageGroupMapping { interface MessageGroupMapping {
[groupId: string]: { [groupId: string]: {
participants: string[]; participants: string[];
label?: string;
avatar?: string;
updatedAt: string; updatedAt: string;
} }
} }
@@ -39,12 +43,16 @@ async function getMessageGroups(userIdentifier: string): Promise<UnformattedMess
SELECT SELECT
npwd_messages_groups.group_id, npwd_messages_groups.group_id,
npwd_messages_groups.participant_identifier, npwd_messages_groups.participant_identifier,
npwd_messages_labels.label,
users.phone_number, users.phone_number,
npwd_phone_contacts.avatar,
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_messages_labels on npwd_messages_labels.group_id = npwd_messages_groups.group_id
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
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 npwd_messages_groups.createdAt DESC
` `
const [results] = await pool.query(query, [ userIdentifier, userIdentifier]); const [results] = await pool.query(query, [ userIdentifier, userIdentifier]);
return <UnformattedMessageGroup[]>results; return <UnformattedMessageGroup[]>results;
@@ -65,7 +73,7 @@ async function getMessages(userIdentifier: string, groupId: string): Promise<Mes
LEFT OUTER JOIN users on users.identifier = npwd_messages.user_identifier LEFT OUTER JOIN users on users.identifier = npwd_messages.user_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
WHERE npwd_messages.group_id = ? WHERE npwd_messages.group_id = ?
ORDER BY updatedAt ASC; ORDER BY createdAt ASC;
`; `;
const [results] = await pool.query(query, [ groupId]); const [results] = await pool.query(query, [ groupId]);
const messages = <Message[]>results; const messages = <Message[]>results;
@@ -86,7 +94,9 @@ async function getConsolidatedMessageGroups(useIdentifier: string): Promise<Mess
mapping[groupId].participants = mapping[groupId].participants.concat(displayTerm) mapping[groupId].participants = mapping[groupId].participants.concat(displayTerm)
} else { } else {
mapping[groupId] = { mapping[groupId] = {
label: messageGroup.label,
participants: [displayTerm], participants: [displayTerm],
avatar: messageGroup.avatar,
updatedAt: messageGroup.updatedAt ? messageGroup.updatedAt.toString() : null updatedAt: messageGroup.updatedAt ? messageGroup.updatedAt.toString() : null
} }
} }
@@ -101,13 +111,22 @@ async function getFormattedMessageGroups(userIdentifier: string): Promise<Messag
return groupIds.map(groupId => { return groupIds.map(groupId => {
const group = groupMapping[groupId]; const group = groupMapping[groupId];
return { return {
...group,
groupId, groupId,
groupDisplay: group.participants.join(', '), groupDisplay: group.participants.join(', '),
updatedAt: group.updatedAt,
} }
}); });
} }
async function createLabel(userIdentifier: string, groupId: string, label: string): Promise<any> {
const query = `
INSERT INTO npwd_messages_labels
(user_identifier, group_id, label)
VALUES (?, ?, ?)
`;
await pool.query(query, [ userIdentifier, groupId, label ]);
}
async function createMessageGroup(userIdentifier: string, groupId: string, participantIdentifier: string): Promise<any> { async function createMessageGroup(userIdentifier: string, groupId: string, participantIdentifier: string): Promise<any> {
const query = ` const query = `
INSERT INTO npwd_messages_groups INSERT INTO npwd_messages_groups
@@ -133,7 +152,10 @@ async function getIdentifierFromPhoneNumber(phoneNumber: string): Promise<string
return identifiers[0]['identifier'] return identifiers[0]['identifier']
} }
async function createMessageGroupsFromPhoneNumbers(userIdentifier: string, phoneNumbers: string[]): Promise<string> { async function createMessageGroupsFromPhoneNumbers(
userIdentifier: string,
phoneNumbers: string[],
groupLabel: string): Promise<string> {
const groupId = uuidv4(); const groupId = uuidv4();
// we check that each phoneNumber exists before we create the group // we check that each phoneNumber exists before we create the group
@@ -152,6 +174,11 @@ async function createMessageGroupsFromPhoneNumbers(userIdentifier: string, phone
...identifiers.map(identifier => createMessageGroup(userIdentifier, groupId, identifier)) ...identifiers.map(identifier => createMessageGroup(userIdentifier, groupId, identifier))
] ]
// we allow users to attach labels to name their group chats
if (groupLabel) {
queryPromises.push(createLabel(userIdentifier, groupId, groupLabel));
}
// wrap this in a transaction to make sure ALL of these INSERTs succeed // wrap this in a transaction to make sure ALL of these INSERTs succeed
//so we are not left in a situation where only some of the member of the //so we are not left in a situation where only some of the member of the
// group exist while other are left off. // group exist while other are left off.
@@ -171,11 +198,11 @@ onNet(events.MESSAGES_FETCH_MESSAGE_GROUPS, async () => {
} }
}); });
onNet(events.MESSAGES_CREATE_MESSAGE_GROUP, async (phoneNumbers: string[]) => { onNet(events.MESSAGES_CREATE_MESSAGE_GROUP, async (phoneNumbers: string[], label: string = null) => {
try { try {
console.log(phoneNumbers); console.log(phoneNumbers);
const _identifier = await useIdentifier(); const _identifier = await useIdentifier();
await createMessageGroupsFromPhoneNumbers(_identifier, phoneNumbers) await createMessageGroupsFromPhoneNumbers(_identifier, phoneNumbers, label)
emitNet(events.MESSAGES_CREATE_MESSAGE_GROUP_SUCCESS, getSource()); emitNet(events.MESSAGES_CREATE_MESSAGE_GROUP_SUCCESS, getSource());
} catch (e) { } catch (e) {
emitNet(events.MESSAGES_CREATE_MESSAGE_GROUP_FAILED, getSource()); emitNet(events.MESSAGES_CREATE_MESSAGE_GROUP_FAILED, getSource());