tweak(calls): Abstract initialize call action & own number handling

This commit is contained in:
Taso committed 2021-07-28 14:41:19 -04:00
1 parent af6e291368
commit be4323f337
6 files changed
+40 -53

No files matched your search

@@ -7,18 +7,13 @@ import MoreVertIcon from '@material-ui/icons/MoreVert';
import { SearchContacts } from './SearchContacts';
import { useHistory } from 'react-router-dom';
import LogDebugEvent from '../../../../os/debug/LogDebugEvents';
import { CallEvents } from '../../../../../../typings/call';
import { useFilteredContacts } from '../../hooks/state';
import { fetchNui } from '../../../../utils/fetchNui';
import { ServerPromiseResp } from '../../../../../../typings/common';
import { useSnackbar } from '../../../../ui/hooks/useSnackbar';
import { useTranslation } from 'react-i18next';
import { useCall } from '../../../../os/call/hooks/useCall';
export const ContactList = () => {
const filteredContacts = useFilteredContacts();
const history = useHistory();
const { addAlert } = useSnackbar();
const { t } = useTranslation();
const { initializeCall } = useCall();
const openContactInfo = (contactId: number) => {
history.push(`/contacts/${contactId}`);
@@ -30,14 +25,7 @@ export const ContactList = () => {
level: 2,
data: true,
});
fetchNui<ServerPromiseResp>(CallEvents.INITIALIZE_CALL, {
receiverNumber: number,
}).then((resp) => {
if (resp.status === 'error') {
addAlert({ message: t('CALLS.FEEDBACK.ERROR'), type: 'error' });
console.error(resp.errorMsg);
}
});
initializeCall(number);
};
const handleMessage = (phoneNumber: string) => {
@@ -5,12 +5,9 @@ import PhoneIcon from '@material-ui/icons/Phone';
import PersonAddIcon from '@material-ui/icons/PersonAdd';
import { DialInputCtx, IDialInputCtx } from '../context/InputContext';
import { useHistory } from 'react-router-dom';
import { CallEvents } from '../../../../../typings/call';
import { useTranslation } from 'react-i18next';
import { InputBase } from '../../../ui/components/Input';
import { fetchNui } from '../../../utils/fetchNui';
import { ServerPromiseResp } from '../../../../../typings/common';
import { useSnackbar } from '../../../ui/hooks/useSnackbar';
import { useCall } from '../../../os/call/hooks/useCall';
const useStyles = makeStyles((theme: Theme) => ({
root: {
@@ -35,19 +32,12 @@ export const DialerInput = () => {
const classes = useStyles();
const history = useHistory();
const { t } = useTranslation();
const { addAlert } = useSnackbar();
const { initializeCall } = useCall();
const { inputVal, set } = useContext<IDialInputCtx>(DialInputCtx);
const handleCall = (number: string) => {
fetchNui<ServerPromiseResp>(CallEvents.INITIALIZE_CALL, {
receiverNumber: number,
}).then((resp) => {
if (resp.status === 'error') {
addAlert({ message: t('CALLS.FEEDBACK.ERROR'), type: 'error' });
console.error(resp.errorMsg);
}
});
initializeCall(number);
};
const handleNewContact = (number: string) => {
@@ -12,11 +12,9 @@ import { Box, IconButton, ListItemIcon, ListItemText } from '@material-ui/core';
import { useHistory } from 'react-router-dom';
import { makeStyles, Theme } from '@material-ui/core/styles';
import dayjs from 'dayjs';
import { fetchNui } from '../../../../utils/fetchNui';
import { ServerPromiseResp } from '../../../../../../typings/common';
import { useSnackbar } from '../../../../ui/hooks/useSnackbar';
import { useMyPhoneNumber } from '../../../../os/simcard/hooks/useMyPhoneNumber';
import { useDialHistory } from '../../hooks/useDialHistory';
import { useCall } from '../../../../os/call/hooks/useCall';
const useStyles = makeStyles((theme: Theme) => ({
callForward: {
@@ -28,25 +26,16 @@ const useStyles = makeStyles((theme: Theme) => ({
}));
export const DialerHistory: React.FC = () => {
const { addAlert } = useSnackbar();
const myNumber = useMyPhoneNumber();
const { getDisplayByNumber } = useContactActions();
const { initializeCall } = useCall();
const calls = useDialHistory();
const classes = useStyles();
const history = useHistory();
const { t } = useTranslation();
const handleCall = (phoneNumber) => {
fetchNui<ServerPromiseResp>(CallEvents.INITIALIZE_CALL, {
receiverNumber: phoneNumber,
}).then((resp) => {
if (resp.status === 'error') {
addAlert({ message: t('CALLS.FEEDBACK.ERROR'), type: 'error' });
console.error(resp.errorMsg);
}
});
initializeCall(phoneNumber);
};
if (!calls?.length) {
@@ -8,11 +8,11 @@ import ChatIcon from '@material-ui/icons/Chat';
import PhoneIcon from '@material-ui/icons/Phone';
import { useTranslation } from 'react-i18next';
import { useHistory } from 'react-router';
import { CallEvents } from '../../../../../../typings/call';
import { fetchNui } from '../../../../utils/fetchNui';
import { useSnackbar } from '../../../../ui/hooks/useSnackbar';
import { ServerPromiseResp } from '../../../../../../typings/common';
import { useMyPhoneNumber } from '../../../../os/simcard/hooks/useMyPhoneNumber';
import { useCall } from '../../../../os/call/hooks/useCall';
const useStyles = makeStyles((theme: Theme) => ({
icon: {
@@ -25,6 +25,7 @@ export const ListingActions: React.FC<MarketplaceListing> = ({ children, ...list
const myNumber = useMyPhoneNumber();
const { t } = useTranslation();
const history = useHistory();
const { initializeCall } = useCall();
const { addAlert } = useSnackbar();
const handleDeleteListing = () => {
@@ -64,14 +65,7 @@ export const ListingActions: React.FC<MarketplaceListing> = ({ children, ...list
};
const handleCall = () => {
fetchNui<ServerPromiseResp>(CallEvents.INITIALIZE_CALL, {
receiverNumber: listing.number,
}).then((resp) => {
if (resp.status === 'error') {
addAlert({ message: t('CALLS.FEEDBACK.ERROR'), type: 'error' });
console.error(resp.errorMsg);
}
});
initializeCall(listing.number);
};
const handleMessage = () => {
+1
View File
@@ -14,6 +14,7 @@
"INCOMING": "Incoming call"
},
"FEEDBACK": {
"ERROR_MYSELF": "You cannot call your own phone number!",
"ERROR": "Error occurred while completing this call"
}
},
+26 -1
View File
@@ -6,6 +6,9 @@ import { fetchNui } from '../../../utils/fetchNui';
import { useCallback } from 'react';
import { useMyPhoneNumber } from '../../simcard/hooks/useMyPhoneNumber';
import { useDialingSound } from './useDialingSound';
import { useSnackbar } from '../../../ui/hooks/useSnackbar';
import { useTranslation } from 'react-i18next';
import { ServerPromiseResp } from '../../../../../typings/common';
interface CallHook {
call: ActiveCall;
@@ -13,13 +16,35 @@ interface CallHook {
acceptCall(): void;
rejectCall(): void;
endCall(): void;
initializeCall(number: string): void;
}
export const useCall = (): CallHook => {
const [call, setCall] = useRecoilState(callerState.currentCall);
const myPhoneNumber = useMyPhoneNumber();
const { t } = useTranslation();
const { addAlert } = useSnackbar();
const { endDialTone, startDialTone } = useDialingSound();
const initializeCall = useCallback(
(number) => {
// We allow calling of ourselves in development
if (process.env.NODE_ENV !== 'development' && myPhoneNumber === number) {
return addAlert({ message: t('CALLS.FEEDBACK.ERROR_MYSELF'), type: 'error' });
}
fetchNui<ServerPromiseResp>(CallEvents.INITIALIZE_CALL, {
receiverNumber: number,
}).then((resp) => {
if (resp.status === 'error') {
addAlert({ message: t('CALLS.FEEDBACK.ERROR'), type: 'error' });
console.error(resp.errorMsg);
}
});
},
[addAlert, myPhoneNumber, t],
);
const acceptCall = useCallback(() => {
fetchNui(CallEvents.ACCEPT_CALL, {
transmitterNumber: call.transmitter,
@@ -39,5 +64,5 @@ export const useCall = (): CallHook => {
});
}, [call, myPhoneNumber]);
return { call, setCall, acceptCall, rejectCall, endCall };
return { call, setCall, acceptCall, rejectCall, endCall, initializeCall };
};