merging
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import {GET_USER_ROUTE} from "@/core/data/apiRoutes";
|
||||
import {GET_USER} from "@/core/data/apiRoutes";
|
||||
import axios from "axios";
|
||||
import {createContext, useCallback, useEffect, useReducer} from "react";
|
||||
import {errorRequest, errorResponse, errorSetting} from "@/core/utils/errorHandler";
|
||||
@@ -71,7 +71,7 @@ export const UserProvider = ({children}) => {
|
||||
(callback = () => {
|
||||
}) => {
|
||||
axios
|
||||
.get(GET_USER_ROUTE, {
|
||||
.get(GET_USER, {
|
||||
headers: {authorization: `Bearer ${state.token}`},
|
||||
})
|
||||
.then(({data}) => {
|
||||
|
||||
@@ -16,10 +16,9 @@ const useNotification = () => {
|
||||
};
|
||||
|
||||
const {data, mutate} = useSWR( isNotificationEnabled ? GET_SIDEBAR_NOTIFICATION : '', fetcher , {keepPreviousData:true})
|
||||
const notification_count = data
|
||||
//swr config
|
||||
|
||||
// render data
|
||||
return {notification_count, update_notification: mutate}
|
||||
return {notification_count: data, update_notification: mutate}
|
||||
}
|
||||
export default useNotification
|
||||
30
src/lib/app/hooks/useProvince.jsx
Normal file
30
src/lib/app/hooks/useProvince.jsx
Normal file
@@ -0,0 +1,30 @@
|
||||
import {GET_PROVINCE_LIST} from "@/core/data/apiRoutes";
|
||||
import useRequest from "@/lib/app/hooks/useRequest";
|
||||
import useSWR from "swr";
|
||||
|
||||
const useProvince = () => {
|
||||
const requestServer = useRequest({auth: true, notification: false})
|
||||
|
||||
//swr config
|
||||
const fetcher = (...args) => {
|
||||
return requestServer(args, 'get').then(({data}) => {
|
||||
return data.data;
|
||||
}).catch(() => {
|
||||
})
|
||||
};
|
||||
|
||||
const {data, isLoading} = useSWR(GET_PROVINCE_LIST, fetcher, {
|
||||
revalidateIfStale: false,
|
||||
revalidateOnFocus: false,
|
||||
revalidateOnReconnect: false,
|
||||
keepPreviousData: true
|
||||
});
|
||||
|
||||
return {
|
||||
provinceList: data,
|
||||
isLoadingProvinceList: isLoading,
|
||||
errorProvinceList: !data
|
||||
}
|
||||
};
|
||||
|
||||
export default useProvince;
|
||||
30
src/lib/app/hooks/useRole.jsx
Normal file
30
src/lib/app/hooks/useRole.jsx
Normal file
@@ -0,0 +1,30 @@
|
||||
import {GET_ROLE_LIST} from "@/core/data/apiRoutes";
|
||||
import useRequest from "@/lib/app/hooks/useRequest";
|
||||
import useSWR from "swr";
|
||||
|
||||
const useRole = () => {
|
||||
const requestServer = useRequest({auth: true, notification: false})
|
||||
|
||||
//swr config
|
||||
const fetcher = (...args) => {
|
||||
return requestServer(args, 'get').then(({data}) => {
|
||||
return data.data;
|
||||
}).catch(() => {
|
||||
})
|
||||
};
|
||||
|
||||
const {data, isLoading} = useSWR(GET_ROLE_LIST, fetcher, {
|
||||
revalidateIfStale: false,
|
||||
revalidateOnFocus: false,
|
||||
revalidateOnReconnect: false,
|
||||
keepPreviousData: true
|
||||
});
|
||||
|
||||
return {
|
||||
roleList: data,
|
||||
isLoadingRoleList: isLoading,
|
||||
errorRoleList: !data
|
||||
}
|
||||
};
|
||||
|
||||
export default useRole;
|
||||
@@ -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,13 +16,16 @@ 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={{
|
||||
value = {{
|
||||
openCallDialog,
|
||||
setOpenCallDialog,
|
||||
state,
|
||||
dispatch,
|
||||
activeTab,
|
||||
setActiveTab
|
||||
}}>{children}</AnswersContext.Provider>
|
||||
}
|
||||
|
||||
@@ -2,16 +2,37 @@ 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,
|
||||
date: new Date(),
|
||||
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
|
||||
37
src/lib/callWidget/hooks/useCallerHistory.jsx
Normal file
37
src/lib/callWidget/hooks/useCallerHistory.jsx
Normal file
@@ -0,0 +1,37 @@
|
||||
import useRequest from "@/lib/app/hooks/useRequest";
|
||||
import {useEffect, useState} from "react";
|
||||
import useSWR from "swr";
|
||||
import {GET_CALLER_HISTORY} from "@/core/data/apiRoutes";
|
||||
|
||||
const useCallerHistory = (call_id, phone_number, max_size) => {
|
||||
const requestServer = useRequest({auth: true, notification: false});
|
||||
const [validData, setValidData] = useState([]);
|
||||
|
||||
//swr config
|
||||
const fetcher = (...args) => {
|
||||
return requestServer(args, 'get').then(({data}) => {
|
||||
return data.data;
|
||||
}).catch(() => {
|
||||
})
|
||||
};
|
||||
|
||||
const {data, isLoading} = useSWR(`${GET_CALLER_HISTORY}?phone_number=${phone_number}&size=${max_size}`, fetcher, {
|
||||
revalidateIfStale: false,
|
||||
revalidateOnFocus: false,
|
||||
revalidateOnReconnect: false,
|
||||
keepPreviousData: true
|
||||
});
|
||||
|
||||
useEffect(() => {
|
||||
data && setValidData(data.filter((item) => item.id != call_id));
|
||||
}, [data, call_id]);
|
||||
|
||||
return {
|
||||
firstItemOfHistory: validData.length === 0 ? false : validData[0],
|
||||
historyList: validData.length < 2 ? false : validData.slice(1),
|
||||
isLoadingHistoryList: true,
|
||||
errorHistoryList: false
|
||||
}
|
||||
};
|
||||
|
||||
export default useCallerHistory;
|
||||
Reference in New Issue
Block a user