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

@@ -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