125 lines
2.8 KiB
TypeScript
125 lines
2.8 KiB
TypeScript
import { useReducer} from 'react';
|
|
import {GenericObject, StateType} from "./types.ts";
|
|
import IAdapter from "@universal-call-sdk/common/lib/Interfaces/IAdapter.ts";
|
|
|
|
|
|
|
|
|
|
enum actionTypes {
|
|
CALL_INCOMING= 'CALL_INCOMING',
|
|
CALL_CONNECTED= 'CALL_CONNECTED',
|
|
CALL_DISCONNECTED= 'CALL_DISCONNECTED',
|
|
CALL_REJECTED= 'CALL_REJECTED',
|
|
CALL_MUTED= 'CALL_MUTED',
|
|
CALL_UNMUTED= 'CALL_UNMUTED',
|
|
}
|
|
type Actions = {
|
|
type: actionTypes,
|
|
payload: GenericObject
|
|
}
|
|
function reducer(state : StateType, action: Actions) : GenericObject {
|
|
if(action.type === actionTypes.CALL_INCOMING) {
|
|
return {
|
|
...state,
|
|
connectedCustomerdata: action.payload,
|
|
isRinging: true
|
|
}
|
|
}
|
|
if(action.type === actionTypes.CALL_CONNECTED) {
|
|
return {
|
|
...state,
|
|
isCallConnected: true
|
|
}
|
|
}
|
|
|
|
if(action.type === actionTypes.CALL_DISCONNECTED) {
|
|
return {
|
|
...state,
|
|
connectedCustomerData: {},
|
|
isCallDisconnected: true
|
|
}
|
|
}
|
|
if(action.type === actionTypes.CALL_REJECTED) {
|
|
return {
|
|
connectedCustomerData: {},
|
|
isRinging: false
|
|
}
|
|
|
|
}
|
|
if(action.type === actionTypes.CALL_MUTED) {
|
|
return {
|
|
...state,
|
|
isMuted: true
|
|
}
|
|
}
|
|
if(action.type === actionTypes.CALL_UNMUTED) {
|
|
return {
|
|
...state,
|
|
isMuted: false
|
|
}
|
|
}
|
|
return state;
|
|
}
|
|
|
|
|
|
|
|
|
|
const initialState : StateType = {
|
|
connectedCustomerdata: {},
|
|
isRinging: false,
|
|
isCallConnected: false,
|
|
isCallDisconnected: false,
|
|
callStartTime: Date.now(),
|
|
}
|
|
|
|
|
|
function UseCallSdk(adapter : IAdapter) {
|
|
// @ts-expect-error sdfsf
|
|
const [callState] = useReducer<any>(reducer, initialState,()=> initialState);
|
|
|
|
|
|
console.log('adapter in universal call sdk', adapter)
|
|
|
|
function registerOnCallIncoming(callback : (callState: GenericObject)=>void) {
|
|
//dispatch({type: actionTypes.CALL_INCOMING, payload: {}})
|
|
adapter.registerOnCallIncoming(callback);
|
|
}
|
|
|
|
function registerOnCallConnected(callback : (callState: GenericObject)=>void) {
|
|
adapter.registerOnCallConnected(callback);
|
|
}
|
|
|
|
function registerOnCallDisconnected(callback : (callState: GenericObject)=>void) {
|
|
adapter.registerOnCallDisconnected(callback);
|
|
}
|
|
|
|
function acceptCall() {
|
|
adapter.acceptCall();
|
|
}
|
|
|
|
function rejectCall() {
|
|
adapter.rejectCall();
|
|
}
|
|
|
|
function muteCall() {
|
|
adapter.muteCall();
|
|
}
|
|
|
|
function unmuteCall() {
|
|
adapter.unmuteCall();
|
|
}
|
|
|
|
return {
|
|
callState,
|
|
registerOnCallIncoming,
|
|
registerOnCallConnected,
|
|
registerOnCallDisconnected,
|
|
acceptCall,
|
|
rejectCall,
|
|
muteCall,
|
|
unmuteCall
|
|
|
|
}
|
|
}
|
|
|
|
export default UseCallSdk; |