Autocomplete contacts + improve messages ui a bit

This commit is contained in:
J Francisco Rader committed 2021-02-17 01:23:08 -05:00
1 parent 17f5fb62d9
commit cf4baafb19
6 files changed
+233 -212

No files matched your search

@@ -1,86 +1,110 @@
import React, { FormEvent, useCallback, useEffect, useMemo, useState } from 'react';
import React, { FormEvent, useCallback, useEffect, useState } from 'react';
import { useTranslation } from 'react-i18next';
import { TextField, Button } from '@material-ui/core';
import { TextField, Button, Box } from '@material-ui/core';
import useStyles from './form.styles';
import Nui from '../../../../os/nui-events/utils/Nui';
import { useHistory } from 'react-router-dom';
const sendNuiCreateMessageGroup = ({ parts, isGroupChat, labelValue }) =>
Nui.send('phone:createMessageGroup', {
phoneNumbers: parts,
label: isGroupChat && labelValue ? labelValue : null,
});
import { Autocomplete } from '@material-ui/lab';
import { useContacts } from '../../../contacts/hooks/useContacts';
import { Contact } from '../../../../common/typings/contact';
const NewMessageGroupForm = ({ phoneNumber }: { phoneNumber?: string }) => {
const classes = useStyles();
const history = useHistory();
const { t } = useTranslation();
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 getGroupParts = useCallback(
(numbers) => numbers.split(',').map((part) => part.replace(/[^0-9]/g, '')),
[]
);
const parts = useMemo(() => getGroupParts(participants), [participants, getGroupParts]);
const isGroupChat = parts.length > 1;
const labelValue = label.trim();
const { contacts } = useContacts();
useEffect(() => {
if (phoneNumber) {
sendNuiCreateMessageGroup({ parts: getGroupParts(phoneNumber), isGroupChat: false, labelValue: null });
history.push('/messages');
const find = contacts.find((c) => c.number === phoneNumber);
const newPart = find || { number: phoneNumber };
setParticipants((all) => [...all, newPart]);
}
}, [phoneNumber, labelValue, history, getGroupParts]);
}, [phoneNumber, contacts]);
const handleSubmit = (event: FormEvent) => {
event.preventDefault();
if (parts.length > 0) {
sendNuiCreateMessageGroup({ parts, isGroupChat, labelValue });
setParticipants('');
history.push('/messages');
const isGroupChat = participants.length > 1;
const handleSubmit = useCallback(
(event: FormEvent) => {
event.preventDefault();
// handles phone numbers in a csv format and strips all spaces and
// external characters out of them:
// 123-4567, 987-6543, 333-4444
const phoneNumbers = participants.map(({ number }) =>
number.replace(/[^0-9]/g, '')
);
const labelValue = isGroupChat ? label.trim() : null;
if (phoneNumbers.length) {
Nui.send('phone:createMessageGroup', {
phoneNumbers,
label: labelValue,
});
setParticipants([]);
history.push('/messages');
}
},
[history, label, participants, isGroupChat]
);
const onAutocompleteChange = useCallback((_e, value: any) => {
const lastIdx = value.length - 1;
if (typeof value[lastIdx] === 'string') {
value.splice(lastIdx, 1, { number: value[lastIdx] });
setParticipants(value);
return;
}
};
setParticipants(value as any[]);
}, []);
const renderAutocompleteInput = useCallback(
(params) => (
<TextField
{...params}
fullWidth
label={t('APPS_MESSAGES_INPUT_NAME_OR_NUMBER')}
inputProps={{
...params.inputProps,
autoFocus: true,
}}
/>
),
[t]
);
const submitDisabled = !participants.length || (isGroupChat && !label);
return (
<form className={classes.newGroupForm} onSubmit={handleSubmit}>
<TextField
value={participants}
onChange={(e) => setParticipants(e.target.value || '')}
placeholder={t('APPS_MESSAGES_NEW_MESSAGE_GROUP')}
className={classes.newGroupinput}
autoFocus
inputProps={{
className: classes.messagesInput,
}}
multiline
/>
{isGroupChat && (
<TextField
value={label}
onChange={(e) => setLabel(e.target.value)}
placeholder={t('APPS_MESSAGES_GROUP_CHAT_LABEL')}
className={classes.newGroupinput}
inputProps={{
className: classes.messagesInput,
}}
<form onSubmit={handleSubmit}>
<Box px={2} py={3}>
<Autocomplete<Contact, boolean, boolean, boolean>
value={participants}
multiple
freeSolo
autoHighlight
options={contacts || []}
getOptionLabel={(c) => c.display || c.number}
onChange={onAutocompleteChange}
renderInput={renderAutocompleteInput}
/>
</Box>
{isGroupChat && (
<Box px={2} py={3}>
<TextField
fullWidth
value={label}
onChange={(e) => setLabel(e.target.value)}
placeholder={t('APPS_MESSAGES_GROUP_CHAT_LABEL')}
/>
</Box>
)}
<Button
variant='contained'
className={classes.newGroupSubmitButton}
color='primary'
type='submit'
>
{t('APPS_MESSAGES_NEW_MESSAGE_GROUP_SUBMIT')}
</Button>
<Box px={2} py={3}>
<Button disabled={submitDisabled} variant='contained' fullWidth color='primary' type='submit'>
{t('APPS_MESSAGES_NEW_MESSAGE_GROUP_SUBMIT')}
</Button>
</Box>
</form>
);
};