Refactored Contacts to use Routes and not a Modal (#80)

The new route allows for other components to directly route to contacts/:id, where id is the contact id for a users given contacts. It also adds a getContact function accessible by the useConfig hook in case a contact needs to be retrieved using a contact id.
This commit is contained in:
Taso authored and GitHub committed 2021-02-09 09:59:54 -05:00
1 parent eaf27f37ef
commit c0509be2d1
14 files changed
+324 -453

No files matched your search

@@ -0,0 +1,197 @@
import React, { useState } from 'react';
import {
Avatar as MuiAvatar,
Button,
Paper,
TextField,
} from '@material-ui/core';
import { makeStyles, Theme } from '@material-ui/core/styles';
import { useTranslation } from 'react-i18next';
import { useHistory, useParams } from 'react-router-dom';
import { useContacts } from '../../hooks/useContacts';
import Nui from '../../../../os/nui-events/utils/Nui';
import ArrowBackIcon from '@material-ui/icons/ArrowBack';
import LogDebugEvent from '../../../../os/debug/LogDebugEvents';
interface ContactInfoRouteParams {
mode: string;
id: string;
}
const useStyles = makeStyles((theme: Theme) => ({
root: {
height: '100%',
width: '100%',
},
listContainer: {
marginTop: 30,
width: '75%',
margin: '0 auto',
textAlign: 'center',
},
avatar: {
margin: 'auto',
height: '125px',
width: '124px',
marginBottom: 29,
},
input: {
marginBottom: 20,
margin: 'auto',
textAlign: 'center',
},
inputProps: {
fontSize: 22,
},
updateButton: {
background: '#2196f3',
width: 150,
height: 45,
fontSize: 16,
marginTop: 29,
marginBottom: 29,
},
deleteButton: {
background: '#d32f2f',
width: 150,
height: 40,
fontSize: 16,
},
saveButton: {
background: theme.palette.success.dark,
color: '#fff',
width: 150,
height: 45,
fontsize: 22,
marginTop: theme.spacing(2),
},
}));
const ContactsInfoPage = () => {
const classes = useStyles();
const history = useHistory();
const { getContact } = useContacts();
const { id } = useParams<ContactInfoRouteParams>();
const contact = getContact(parseInt(id));
// Set state after checking if null
const [name, setName] = useState(contact ? contact.display : contact);
const [number, setNumber] = useState(contact ? contact.number : contact);
const [avatar, setAvatar] = useState(contact ? contact.avatar : contact);
const { t } = useTranslation();
const handleContactAdd = () => {
LogDebugEvent({
action: 'Handling Contact Add',
data: contact,
level: 2,
});
Nui.send('phone:addContact', {
name,
number,
avatar,
});
};
const handleContactSave = () => {
LogDebugEvent({
action: 'Handling Contact Update',
data: contact,
level: 2,
});
Nui.send('phone:updateContact', {
id: contact.id,
display: name,
number,
avatar,
});
};
const handleContactDelete = () => {
LogDebugEvent({
action: 'Handling Contact Delete',
data: contact,
level: 2,
});
Nui.send('phone:deleteContact', {
id: contact.id,
});
};
return (
<Paper className={classes.root} square>
<Button style={{ margin: 10 }} onClick={() => history.goBack()}>
<ArrowBackIcon fontSize='large' />
</Button>
<div className={classes.listContainer}>
{avatar ? (
<MuiAvatar className={classes.avatar} src={contact.avatar} />
) : (
<MuiAvatar className={classes.avatar} />
)}
<TextField
className={classes.input}
value={name}
onChange={(e) => setName(e.target.value)}
placeholder={t('APPS_CONTACT_FORM_NAME')}
fullWidth
inputProps={{
className: classes.inputProps,
}}
/>
<TextField
className={classes.input}
value={number}
onChange={(e) => setNumber(e.target.value)}
placeholder={t('APPS_CONTACT_FORM_NUMBER')}
fullWidth
inputProps={{
className: classes.inputProps,
}}
/>
<TextField
className={classes.input}
placeholder={t('APPS_CONTACT_FORM_AVATAR')}
fullWidth
value={avatar}
onChange={(e) => setAvatar(e.target.value)}
inputProps={{
className: classes.inputProps,
}}
/>
{/* Display if fetched contact */}
{contact && (
<div style={{ display: 'block' }}>
<Button
onClick={handleContactSave}
className={classes.updateButton}
>
{t('GENERIC_UPDATE')}
</Button>
<Button
onClick={handleContactDelete}
className={classes.deleteButton}
>
{t('GENERIC_DELETE')}
</Button>
</div>
)}
{/* Display if no fetched contact */}
{!contact && (
<div style={{ display: 'block' }}>
<Button onClick={handleContactAdd} className={classes.saveButton}>
{t('APPS_CONTACT_MODAL_BUTTON_ADD')}
</Button>
</div>
)}
</div>
</Paper>
);
};
export default ContactsInfoPage;
@@ -0,0 +1,39 @@
import React from 'react';
import { ContactList } from '../List/ContactList';
import Fab from '@material-ui/core/Fab';
import PersonAddIcon from '@material-ui/icons/PersonAdd';
import { ContactAlert } from '../alert/ContactAlert';
import { useHistory } from 'react-router-dom';
import { makeStyles, Theme } from '@material-ui/core/styles';
const useStyles = makeStyles((theme: Theme) => ({
absolute: {
position: 'absolute',
bottom: theme.spacing(2),
right: theme.spacing(2),
},
icon: {
color: '#fff',
fontSize: 30,
},
}));
export const ContactPage = () => {
const history = useHistory();
const classes = useStyles();
return (
<>
<ContactList />
<Fab
onClick={() => history.push('/contacts/add')}
style={{ backgroundColor: '#2196f3', color: '#fff' }}
className={classes.absolute}
>
<PersonAddIcon />
</Fab>
<ContactAlert />
</>
);
};