147 lines
3.3 KiB
TypeScript
147 lines
3.3 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} : {adapter: IAdapter}) {
|
|
// @ts-expect-error sdfsf
|
|
const [callState] = useReducer<any>(reducer, initialState,()=> initialState);
|
|
|
|
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 registerOnAdapterReady(callback : ()=> void) {
|
|
adapter.registerOnAdapterReady(callback);
|
|
}
|
|
|
|
function acceptCall() {
|
|
adapter.acceptCall();
|
|
}
|
|
|
|
function rejectCall() {
|
|
adapter.rejectCall();
|
|
}
|
|
|
|
function muteCall() {
|
|
adapter.muteCall();
|
|
}
|
|
|
|
function unmuteCall() {
|
|
adapter.unmuteCall();
|
|
}
|
|
|
|
function initialize() {
|
|
adapter.init();
|
|
}
|
|
|
|
function setOnBreak() {
|
|
adapter.setOnBreak();
|
|
}
|
|
|
|
function setAvailable() {
|
|
adapter.setAvailable();
|
|
}
|
|
|
|
function getAgentAvailability(): boolean {
|
|
return adapter.getAgentAvailability();
|
|
}
|
|
|
|
return {
|
|
callState,
|
|
registerOnCallIncoming,
|
|
registerOnCallConnected,
|
|
registerOnCallDisconnected,
|
|
acceptCall,
|
|
rejectCall,
|
|
muteCall,
|
|
unmuteCall,
|
|
initialize,
|
|
setAvailable,
|
|
setOnBreak,
|
|
registerOnAdapterReady,
|
|
getAgentAvailability
|
|
|
|
}
|
|
}
|
|
|
|
export default UseCallSdk; |