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(); const { addNumber, referal } = useQueryParams({ 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 (
setName(e.target.value)} label={t('APPS_CONTACT_FORM_NAME')} fullWidth inputProps={{ className: classes.inputProps, }} /> setNumber(e.target.value)} label={t('APPS_CONTACT_FORM_NUMBER')} fullWidth inputProps={{ className: classes.inputProps, }} /> setAvatar(e.target.value)} inputProps={{ className: classes.inputProps, }} /> {/* Display if fetched contact */} {contact && ( <> )} {/* Display if no fetched contact */} {!contact && ( )}
); }; export default ContactsInfoPage;