CFE-6 Merge branch 'develop' into 'bugfix/CFE-6_fix_toas_containers'

This commit is contained in:
AmirHossein Mahmoodi
2023-10-31 11:52:42 +00:00
44 changed files with 1076 additions and 137 deletions

View File

@@ -17,6 +17,7 @@ export const SocketProvider = ({children}) => {
const {directionApp} = useDirection();
const socket = useMemo(() => io(process.env.NEXT_PUBLIC_SERVER_SOCKET_URL, {
autoConnect: false,
transports: ['websocket'],
auth: {
token: ""
}

View File

@@ -1,8 +1,12 @@
import useSWR from 'swr'
import {GET_SIDEBAR_NOTIFICATION} from "@/core/data/apiRoutes";
import useRequest from "@/lib/app/hooks/useRequest";
const GLOBAL_HAS_VALUE = process.env.NEXT_PUBLIC_HAS_VALUE;
const hasPermissionsValue = GLOBAL_HAS_VALUE ? JSON.parse(GLOBAL_HAS_VALUE) : [];
const isNotificationEnabled = hasPermissionsValue.includes("NEXT_PUBLIC_HAS_NOTIFICATION");
const useNotification = () => {
const requestServer = useRequest({auth: true, notification: false})
//swr config
@@ -13,7 +17,7 @@ const useNotification = () => {
})
};
const {data, mutate} = useSWR(GET_SIDEBAR_NOTIFICATION, fetcher)
const {data, mutate} = useSWR( isNotificationEnabled ? GET_SIDEBAR_NOTIFICATION : '', fetcher , {keepPreviousData:true})
//swr config
// render data

View File

@@ -1,31 +1,86 @@
import {createContext, useReducer, useState} from "react";
import { createContext, useEffect, useReducer, useState } from "react";
import { GET_CATEGORY } from "@/core/data/apiRoutes";
import useRequest from "@/lib/app/hooks/useRequest";
const reducer = (state, action) => {
switch (action.type) {
case "CHANGE_ACTIVE_TAB":
return state.map((answer, index) => action.newValue === index ? {...answer, active: true} : {
...answer,
active: false
});
return state.map((answer, index) =>
action.newValue === index
? { ...answer, active: true }
: {
...answer,
active: false,
}
);
case "CHANGE_LIST":
return [...action.newList]
return [...action.newList];
case "CHANGE_ACTIVE_ID":
return state.map((answer, index) =>
action.tab_id === answer.id
? {
...answer,
active_category_id: action.category_id,
}
: { ...answer }
);
default:
return state;
}
}
export const AnswersContext = createContext()
export const AnswersProvider = ({children}) => {
const [openCallDialog, setOpenCallDialog] = useState(false)
const [activeTab, setActiveTab] = useState(0)
};
const submission = (handleCategories, operation) => {
switch (operation.type) {
case "SET_CATEGORIES":
return { ...handleCategories, categoryLists: operation.categoriesByAPI };
case "SET_SUBCATEGORIES":
return { ...handleCategories, subCategoryLists: operation.subCategoriesByAPI };
default:
return handleCategories;
}
};
export const AnswersContext = createContext();
export const AnswersProvider = ({ children }) => {
const requestServer = useRequest({ auth: true, notification: false });
const [openCallDialog, setOpenCallDialog] = useState(false);
const [activeTab, setActiveTab] = useState(0);
const [handleCategories, operation] = useReducer(submission, {
categoryLists: [],
subCategoryLists: [],
});
const [state, dispatch] = useReducer(reducer, []);
return <AnswersContext.Provider
value = {{
openCallDialog,
setOpenCallDialog,
state,
dispatch,
activeTab,
setActiveTab
}}>{children}</AnswersContext.Provider>
}
useEffect(() => {
requestServer(GET_CATEGORY, "get")
.then(({ data }) => {
const subCategoriesByAPI = data.data;
const categoriesByAPI = subCategoriesByAPI.reduce((accumulator, currentValue) => {
if (!accumulator.find((item) => item.category_id === currentValue.category_id)) {
accumulator.push(currentValue);
}
return accumulator;
}, []);
operation({ type: "SET_CATEGORIES", categoriesByAPI });
operation({ type: "SET_SUBCATEGORIES", subCategoriesByAPI });
})
.catch(() => {});
}, []);
return (
<AnswersContext.Provider
value={{
openCallDialog,
setOpenCallDialog,
state,
dispatch,
activeTab,
setActiveTab,
categoryLists: handleCategories.categoryLists,
subCategoryLists: handleCategories.subCategoryLists,
}}
>
{children}
</AnswersContext.Provider>
);
};

View File

@@ -0,0 +1,59 @@
import { createContext, useCallback, useEffect, useReducer } from "react";
import useRequest from "@/lib/app/hooks/useRequest";
import { GET_CATEGORY } from "@/core/data/apiRoutes";
const reducer = (state, action) => {
switch (action.type) {
case "CHANGE_ACTIVE_ID":
return { ...state, active_id: action.id };
case "SET_CATEGORIES":
return { ...state, categoryLists: action.cat };
case "SET_SUBCATEGORIES":
return { ...state, subCategoryLists: action.subCat };
default:
return state;
}
};
export const CategoriesContext = createContext();
export const CategoriesProvider = ({ children }) => {
const requestServer = useRequest({ auth: true, notification: false });
const [state, dispatch] = useReducer(reducer, {
categoryLists: [],
subCategoryLists: [],
active_id: null,
});
useEffect(() => {
requestServer(GET_CATEGORY, "get")
.then(({ data }) => {
const subCat = data.data;
const cat = subCat.reduce((accumulator, currentValue) => {
if (!accumulator.find((item) => item.category_id === currentValue.category_id)) {
accumulator.push(currentValue);
}
return accumulator;
}, []);
dispatch({ type: "SET_CATEGORIES", cat });
dispatch({ type: "SET_SUBCATEGORIES", subCat });
})
.catch(() => {});
}, []);
const setCategoryID = useCallback((id) => {
dispatch({ type: "CHANGE_ACTIVE_ID", id });
});
return (
<CategoriesContext.Provider
value={{
categoryLists: state.categoryLists,
subCategoryLists: state.subCategoryLists,
active_id: state.active_id,
setCategoryID,
}}
>
{children}
</CategoriesContext.Provider>
);
};

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;

View File

@@ -29,8 +29,8 @@ const useCallerHistory = (call_id, phone_number, max_size) => {
return {
firstItemOfHistory: validData.length === 0 ? false : validData[0],
historyList: validData.length < 2 ? false : validData.slice(1),
isLoadingHistoryList: true,
errorHistoryList: false
isLoadingHistoryList: isLoading,
errorHistoryList: !data
}
};

View File

@@ -0,0 +1,17 @@
import { useContext } from "react";
import { AnswersContext } from "@/lib/callWidget/contexts/answers";
const useCategories = () => {
const { categoryLists, subCategoryLists, active_category_id, dispatch } = useContext(AnswersContext);
const setActiveCategoryID = (tab_id, category_id) => {
dispatch({ type: "CHANGE_ACTIVE_ID", tab_id, category_id });
};
return {
categoryLists,
subCategoryLists,
active_category_id,
setActiveCategoryID,
};
};
export default useCategories;