Files
new-phone-who-dis/phone/src/apps/contacts/components/views/ContactInfo.tsx
T
Taso 0f61088999 refactor(events): Events are now tied to a common enum typings (#167)
* chore(scripts/twitter): Refactored twitter to use event enums

* chore(scripts/marketplace): Refactored marketplace to use event enums

* chore(scripts/messages): Refactored messages to use event enums

* chore(scripts/contacts): Refactored contacts to use event enums

* chore(scripts/calls): Refactored calls to use event enums

* chore(scripts/notes): Refactored notes to use event enums

* chore(scripts/photos): Refactored photos to use event enums

* chore(scripts/bank): Refactored bank to use event enums

* chore(scripts/phone): Refactored phone to use event enums

* chore(events): Removed old events object

* refactor(phone/nui-events): Attached NUI events to respective enum

* tweak(event-enums): Added npwd prefix to all events

* tweak(phone/serve): Added craco to override CRA without eject

Also bumped react-scripts to 4.0.1, with seemingly no issues

* fix(typings/twitter): Fixed method enums for twitter
2021-03-22 13:37:07 -04:00

185 lines
4.7 KiB
TypeScript

import React, { useEffect, useState } from 'react';
import { Avatar as MuiAvatar, Box, Button, Paper, TextField } from '@material-ui/core';
import { makeStyles } 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';
import { useQueryParams } from '../../../../common/hooks/useQueryParams';
import { ContactEvents } from '../../../../../../typings/contact';
interface ContactInfoRouteParams {
mode: string;
id: string;
}
interface ContactInfoRouteQuery {
addNumber?: string;
referal?: string;
}
const useStyles = makeStyles({
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,
},
});
const ContactsInfoPage = () => {
const classes = useStyles();
const history = useHistory();
const { getContact } = useContacts();
const { id } = useParams<ContactInfoRouteParams>();
const { addNumber, referal } = useQueryParams<ContactInfoRouteQuery>({
referal: '/contacts',
});
const contact = getContact(parseInt(id));
// Set state after checking if null
const [name, setName] = useState(contact ? contact.display : '');
const [number, setNumber] = useState(contact ? contact.number : '');
const [avatar, setAvatar] = useState(contact ? contact.avatar : '');
const { t } = useTranslation();
useEffect(() => {
if (addNumber) {
setNumber(addNumber);
}
}, [addNumber]);
const handleContactAdd = () => {
LogDebugEvent({
action: 'Handling Contact Add',
data: contact,
level: 2,
});
Nui.send(ContactEvents.ADD_CONTACT, {
display: name,
number,
avatar,
});
history.replace(referal);
};
const handleContactSave = () => {
LogDebugEvent({
action: 'Handling Contact Update',
data: contact,
level: 2,
});
Nui.send(ContactEvents.UPDATE_CONTACT, {
id: contact.id,
display: name,
number,
avatar,
});
history.goBack();
};
const handleContactDelete = () => {
LogDebugEvent({
action: 'Handling Contact Delete',
data: contact,
level: 2,
});
Nui.send(ContactEvents.DELETE_CONTACT, {
id: contact.id,
});
history.goBack();
};
return (
<Paper className={classes.root} square>
<Button style={{ margin: 10 }} onClick={() => history.goBack()}>
<ArrowBackIcon fontSize="large" />
</Button>
<div className={classes.listContainer}>
<MuiAvatar className={classes.avatar} src={avatar} />
<TextField
autoFocus
className={classes.input}
value={name}
onChange={(e) => setName(e.target.value)}
label={t('APPS_CONTACT_FORM_NAME')}
fullWidth
inputProps={{
className: classes.inputProps,
}}
/>
<TextField
className={classes.input}
value={number}
onChange={(e) => setNumber(e.target.value)}
label={t('APPS_CONTACT_FORM_NUMBER')}
fullWidth
inputProps={{
className: classes.inputProps,
}}
/>
<TextField
className={classes.input}
label={t('APPS_CONTACT_FORM_AVATAR')}
fullWidth
value={avatar}
onChange={(e) => setAvatar(e.target.value)}
inputProps={{
className: classes.inputProps,
}}
/>
{/* Display if fetched contact */}
{contact && (
<>
<Box py={1} display="block">
<Button color="primary" variant="contained" onClick={handleContactSave}>
{t('GENERIC_UPDATE')}
</Button>
</Box>
<Box py={1} display="block">
<Button variant="contained" color="secondary" onClick={handleContactDelete}>
{t('GENERIC_DELETE')}
</Button>
</Box>
</>
)}
{/* Display if no fetched contact */}
{!contact && (
<Box display="block">
<Button color="primary" variant="contained" onClick={handleContactAdd}>
{t('APPS_CONTACT_MODAL_BUTTON_ADD')}
</Button>
</Box>
)}
</div>
</Paper>
);
};
export default ContactsInfoPage;