CFE-2 reinitializing call action tab

This commit is contained in:
2023-10-26 09:27:36 +03:30
parent 80d51c6c72
commit 3e3b04a769
14 changed files with 338 additions and 67 deletions

View File

@@ -1,38 +1,46 @@
import {useContext} from "react";
import {AnswersContext} from "@/lib/callWidget/contexts/answers";
import { useContext } from "react";
import { AnswersContext } from "@/lib/callWidget/contexts/answers";
const useAnswers = () => {
const {state, dispatch, activeTab, setActiveTab} = useContext(AnswersContext)
const { state, dispatch, activeTab, setActiveTab } = useContext(AnswersContext);
const changeActiveTabAnswers = (newValue) => {
dispatch({type: 'CHANGE_ACTIVE_TAB', newValue})
setActiveTab(newValue)
}
dispatch({ type: "CHANGE_ACTIVE_TAB", newValue });
setActiveTab(newValue);
};
const newAnswerTab = (data) => {
const newAnswer = {
id: data.id,
phone_number: data.phone_number,
date: new Date(),
active: true
}
const newList = [...state, newAnswer]
dispatch({type: 'CHANGE_LIST', newList})
changeActiveTabAnswers(newList.length - 1)
}
active: true,
active_category_id: null,
};
const newList = [...state, newAnswer];
dispatch({ type: "CHANGE_LIST", newList });
changeActiveTabAnswers(newList.length - 1);
};
const closeAnswerTab = (id) => {
const index = state.findIndex(answer => answer.id === id)
const newIndex = index === 0 ? 0 : index - 1
const newList = [...state]
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)
newList[newIndex].active = true;
setActiveTab(newIndex);
}
dispatch({type: 'CHANGE_LIST', newList})
}
dispatch({ type: "CHANGE_LIST", newList });
};
return {answersList: state, activeTab, setActiveTab, changeActiveTabAnswers, closeAnswerTab, newAnswerTab}
}
return {
answersList: state,
activeTab,
setActiveTab,
changeActiveTabAnswers,
closeAnswerTab,
newAnswerTab,
};
};
export default useAnswers
export default useAnswers;