write some behavioral test and rendered test for create form component
This commit is contained in:
15
src/lib/app/contexts/socket.jsx
Normal file
15
src/lib/app/contexts/socket.jsx
Normal file
@@ -0,0 +1,15 @@
|
||||
import {createContext, useMemo} from "react";
|
||||
import {io} from "socket.io-client";
|
||||
|
||||
export const SocketContext = createContext()
|
||||
|
||||
export const SocketProvider = ({children}) => {
|
||||
const socket = useMemo(() => io(process.env.NEXT_PUBLIC_SERVER_SOCKET_URL, {
|
||||
autoConnect: false,
|
||||
auth: {
|
||||
token: ""
|
||||
}
|
||||
}), [])
|
||||
|
||||
return <SocketContext.Provider value={{socket}}>{children}</SocketContext.Provider>
|
||||
}
|
||||
@@ -2,6 +2,7 @@ import {GET_USER_ROUTE} from "@/core/data/apiRoutes";
|
||||
import axios from "axios";
|
||||
import {createContext, useCallback, useEffect, useReducer} from "react";
|
||||
import {errorRequest, errorResponse, errorSetting} from "@/core/utils/errorHandler";
|
||||
import useSocket from "@/lib/app/hooks/useSocket";
|
||||
|
||||
const initialUser = {
|
||||
isAuth: false,
|
||||
@@ -39,6 +40,7 @@ const reducer = (state, action) => {
|
||||
export const UserContext = createContext();
|
||||
|
||||
export const UserProvider = ({children}) => {
|
||||
const socket = useSocket()
|
||||
const [state, dispatch] = useReducer(reducer, initialUser);
|
||||
|
||||
const clearUser = useCallback(() => {
|
||||
@@ -75,7 +77,7 @@ export const UserProvider = ({children}) => {
|
||||
headers: {authorization: `Bearer ${state.token}`},
|
||||
})
|
||||
.then(({data}) => {
|
||||
if (typeof callback === "function") callback(data);
|
||||
if (typeof callback === "function") callback(data.data);
|
||||
})
|
||||
.catch(error => {
|
||||
if (error.response) {
|
||||
@@ -100,12 +102,16 @@ export const UserProvider = ({children}) => {
|
||||
clearUser();
|
||||
changeAuthState(false);
|
||||
changeLanguageState(false);
|
||||
|
||||
socket.auth.token = ""
|
||||
return;
|
||||
}
|
||||
getUser((data) => {
|
||||
changeUser(data);
|
||||
changeAuthState(true);
|
||||
changeLanguageState(true);
|
||||
|
||||
socket.auth.token = data.telephone_id
|
||||
});
|
||||
}, [state.token]);
|
||||
|
||||
|
||||
10
src/lib/app/hooks/useSocket.jsx
Normal file
10
src/lib/app/hooks/useSocket.jsx
Normal file
@@ -0,0 +1,10 @@
|
||||
import {useContext} from "react";
|
||||
import {SocketContext} from "@/lib/app/contexts/socket";
|
||||
|
||||
const useSocket = () => {
|
||||
const {socket} = useContext(SocketContext)
|
||||
|
||||
return socket
|
||||
}
|
||||
|
||||
export default useSocket
|
||||
Reference in New Issue
Block a user