CFE-25 Return to previous structure call widget

This commit is contained in:
AmirHossein Mahmoodi
2023-10-14 13:06:05 +03:30
parent 3add418f4e
commit 33eeb37179
12 changed files with 146 additions and 60 deletions

View File

@@ -1,10 +0,0 @@
import useAnswers from "@/lib/callWidget/hooks/useAnswers";
const CallActions = () => {
const {answer} = useAnswers()
return (
<>CallActions - {answer.id}</>
)
}
export default CallActions

View File

@@ -1,10 +0,0 @@
import useAnswers from "@/lib/callWidget/hooks/useAnswers";
const CallHistory = () => {
const {answer} = useAnswers()
return (
<>CallHistory - {answer.id}</>
)
}
export default CallHistory

View File

@@ -1,18 +0,0 @@
import {Grid} from "@mui/material";
import CallActions from "@/components/layouts/Dashboard/CallWidget/CallWidgetDialog/CallPanel/CallActions";
import CallHistory from "@/components/layouts/Dashboard/CallWidget/CallWidgetDialog/CallPanel/CallHistory";
const CallPanel = () => {
return (
<Grid container columns={{xs: 3}}>
<Grid item xs={2}>
<CallActions/>
</Grid>
<Grid item xs={1}>
<CallHistory/>
</Grid>
</Grid>
)
}
export default CallPanel

View File

@@ -0,0 +1,7 @@
const CallActions = ({tab}) => {
return (
<>CallActions - {tab.id}</>
)
}
export default CallActions

View File

@@ -0,0 +1,7 @@
const CallHistory = ({tab}) => {
return (
<>CallHistory - {tab.id}</>
)
}
export default CallHistory

View File

@@ -0,0 +1,19 @@
import {Grid} from "@mui/material";
import CallActions from "@/components/layouts/Dashboard/CallWidget/CallWidgetDialog/CallTabs/CallTabPanel/CallActions";
import CallHistory from "@/components/layouts/Dashboard/CallWidget/CallWidgetDialog/CallTabs/CallTabPanel/CallHistory";
const CallTabPanel = ({tab}) => {
return (
<Grid container columns={{xs: 3}} sx={{display: !tab.active && 'none'}} role="tabpanel"
id={`answer-tabpanel-${tab.id}`} aria-labelledby={`answer-tab-${tab.id}`}>
<Grid item xs={2}>
<CallActions tab={tab}/>
</Grid>
<Grid item xs={1}>
<CallHistory tab={tab}/>
</Grid>
</Grid>
)
}
export default CallTabPanel

View File

@@ -0,0 +1,23 @@
import {IconButton, Stack, Typography, Zoom} from "@mui/material";
import CloseIcon from '@mui/icons-material/Close';
import useAnswers from "@/lib/callWidget/hooks/useAnswers";
const CallTabLabel = ({tab}) => {
const {closeAnswerTab} = useAnswers()
const handleCloseClick = (id) => {
closeAnswerTab(id)
};
return (
<Stack sx={{width: '100%'}} direction={'row'} alignItems={'center'} justifyContent={'space-between'}>
<Typography>{tab.phone_number} - {tab.id}</Typography>
<Zoom in={tab.active}>
<IconButton onClick={() => handleCloseClick(tab.id)}>
<CloseIcon/>
</IconButton>
</Zoom>
</Stack>
)
}
export default CallTabLabel

View File

@@ -0,0 +1,27 @@
import {Box, Tab, Tabs} from "@mui/material";
import CallTabLabel
from "@/components/layouts/Dashboard/CallWidget/CallWidgetDialog/CallTabs/CallTabsList/CallTabLabel";
import useAnswers from "@/lib/callWidget/hooks/useAnswers";
const CallTabsList = () => {
const {answersList, changeActiveTabAnswers, activeTab} = useAnswers()
const handleChange = (event, newValue) => {
changeActiveTabAnswers(newValue)
};
return (
<Box sx={{borderBottom: 1, borderColor: 'divider'}}>
<Tabs
value={activeTab}
onChange={handleChange}
variant={'fullWidth'}
>
{answersList.map((answer) => (
<Tab component={'div'} sx={{py: 0.5, pr: 0}} label={<CallTabLabel tab={answer}/>} key={answer.id}/>
))}
</Tabs>
</Box>
)
}
export default CallTabsList

View File

@@ -0,0 +1,17 @@
import CallTabsList from "src/components/layouts/Dashboard/CallWidget/CallWidgetDialog/CallTabs/CallTabsList";
import CallTabPanel from "@/components/layouts/Dashboard/CallWidget/CallWidgetDialog/CallTabs/CallTabPanel";
import useAnswers from "@/lib/callWidget/hooks/useAnswers";
const CallTabs = () => {
const {answersList} = useAnswers()
return (
<>
<CallTabsList/>
{answersList.map((answer) => (
<CallTabPanel key={answer.id} tab={answer}/>
))}
</>
)
}
export default CallTabs

View File

@@ -4,18 +4,18 @@ import {useEffect} from "react";
import useCallDialog from "@/lib/callWidget/hooks/useCallDialog";
import useAnswers from "@/lib/callWidget/hooks/useAnswers";
import useSocket from "@/lib/app/hooks/useSocket";
import CallPanel from "@/components/layouts/Dashboard/CallWidget/CallWidgetDialog/CallPanel";
import CallTabs from "@/components/layouts/Dashboard/CallWidget/CallWidgetDialog/CallTabs";
const CallWidgetDialog = () => {
const {openCallDialog, setOpenCallDialog} = useCallDialog()
const {answer, newAnswer} = useAnswers()
const {answersList, newAnswerTab} = useAnswers()
const socket = useSocket()
useEffect(() => {
const answerCall = (_data, act) => {
const data = JSON.parse(_data)
newAnswer({
id: data.id,
newAnswerTab({
id: data.call_id,
phone_number: data.phone_number
})
act()
@@ -28,12 +28,12 @@ const CallWidgetDialog = () => {
});
useEffect(() => {
if (answer) {
setOpenCallDialog(true)
if (answersList.length === 0) {
setOpenCallDialog(false)
return
}
setOpenCallDialog(false)
}, [answer]);
setOpenCallDialog(true)
}, [answersList.length]);
return (
<Dialog
@@ -41,9 +41,7 @@ const CallWidgetDialog = () => {
open={openCallDialog}
TransitionComponent={DialogTransition}
>
{answer && (
<CallPanel/>
)}
<CallTabs/>
</Dialog>
)
}

View File

@@ -2,10 +2,13 @@ import {createContext, useReducer, useState} from "react";
const reducer = (state, action) => {
switch (action.type) {
case "CREATE_ANSWER":
return {...action.data}
case "DELETE_ANSWER":
return null
case "CHANGE_ACTIVE_TAB":
return state.map((answer, index) => action.newValue === index ? {...answer, active: true} : {
...answer,
active: false
});
case "CHANGE_LIST":
return [...action.newList]
default:
return state;
}
@@ -13,7 +16,8 @@ const reducer = (state, action) => {
export const AnswersContext = createContext()
export const AnswersProvider = ({children}) => {
const [openCallDialog, setOpenCallDialog] = useState(false)
const [state, dispatch] = useReducer(reducer, null);
const [activeTab, setActiveTab] = useState(0)
const [state, dispatch] = useReducer(reducer, []);
return <AnswersContext.Provider
value={{
@@ -21,5 +25,7 @@ export const AnswersProvider = ({children}) => {
setOpenCallDialog,
state,
dispatch,
activeTab,
setActiveTab
}}>{children}</AnswersContext.Provider>
}

View File

@@ -2,16 +2,36 @@ import {useContext} from "react";
import {AnswersContext} from "@/lib/callWidget/contexts/answers";
const useAnswers = () => {
const {state, dispatch} = useContext(AnswersContext)
const {state, dispatch, activeTab, setActiveTab} = useContext(AnswersContext)
const newAnswer = (data) => {
dispatch({type: 'CREATE_ANSWER', data})
const changeActiveTabAnswers = (newValue) => {
dispatch({type: 'CHANGE_ACTIVE_TAB', newValue})
setActiveTab(newValue)
}
const deleteAnswer = () => {
dispatch({type: 'DELETE_ANSWER'})
const newAnswerTab = (data) => {
const newAnswer = {
id: data.id,
phone_number: data.phone_number,
active: true
}
const newList = [...state, newAnswer]
dispatch({type: 'CHANGE_LIST', newList})
changeActiveTabAnswers(newList.length - 1)
}
return {answer: state, newAnswer, deleteAnswer}
const closeAnswerTab = (id) => {
const index = state.findIndex(answer => answer.id === id)
const newIndex = index === 0 ? 0 : index - 1
const newList = [...state]
newList.splice(index, 1);
if (newList.length !== 0) {
newList[newIndex].active = true
setActiveTab(newIndex)
}
dispatch({type: 'CHANGE_LIST', newList})
}
return {answersList: state, activeTab, setActiveTab, changeActiveTabAnswers, closeAnswerTab, newAnswerTab}
}
export default useAnswers