Skip to content

Commit

Permalink
refactor: split auth context and provider into separate files
Browse files Browse the repository at this point in the history
  • Loading branch information
aliakbar-deriv committed Jan 8, 2025
1 parent 34db244 commit 956a55c
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/App.jsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { HashRouter as Router, Routes, Route } from "react-router-dom";
import { ThemeProvider, SnackbarProvider, Spinner } from "@deriv-com/quill-ui";
import { useAuth } from "./hooks/useAuth.jsx";
import { AuthProvider } from "./hooks/authContext.jsx";
import AuthProvider from "./providers/AuthProvider";
import Login from "./components/Login";
import Header from "./components/Header";
import Dashboard from "./components/Dashboard";
Expand Down
2 changes: 1 addition & 1 deletion src/components/__tests__/ProtectedRoute.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { describe, it, expect } from 'vitest'
import { render, screen } from '@testing-library/react'
import { MemoryRouter } from 'react-router-dom'
import ProtectedRoute from '../ProtectedRoute'
import { AuthContext } from '../../hooks/authContext.jsx'
import { AuthContext } from '../../contexts/auth'

const mockAuthContext = {
defaultAccount: null,
Expand Down
3 changes: 3 additions & 0 deletions src/contexts/auth.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { createContext } from 'react';

export const AuthContext = createContext(null);
2 changes: 1 addition & 1 deletion src/hooks/useAuth.jsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useState, useCallback, useEffect, useContext } from 'react';
import useWebSocket from "./useWebSocket";
import { AuthContext } from './authContext.jsx';
import { AuthContext } from '../contexts/auth';

export const useAuthState = () => {
const [isAuthorized, setIsAuthorized] = useState(false);
Expand Down
8 changes: 4 additions & 4 deletions src/hooks/authContext.jsx → src/providers/AuthProvider.jsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { createContext } from 'react';
import PropTypes from 'prop-types';
import { useAuthState } from './useAuth';

export const AuthContext = createContext(null);
import { useAuthState } from '../hooks/useAuth';
import { AuthContext } from '../contexts/auth';

export const AuthProvider = ({ children }) => {
const auth = useAuthState();
Expand All @@ -12,3 +10,5 @@ export const AuthProvider = ({ children }) => {
AuthProvider.propTypes = {
children: PropTypes.node.isRequired,
};

export default AuthProvider;
13 changes: 11 additions & 2 deletions vite.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,20 @@ export default defineConfig(({ mode }) => {
environment: 'jsdom',
setupFiles: ['./src/test/setup.ts'],
deps: {
inline: ['@deriv-com/quill-ui']
optimizer: {
web: {
include: ['@deriv-com/quill-ui']
}
}
},
mockReset: true,
moduleNameMapper: {
'\\.(css|less|sass|scss)$': './src/test/mocks/styleMock.js'
'\\.(css|less|sass|scss)$': './src/test/mocks/styleMock.js',
'^@deriv-com/quill-ui/.*\\.css$': './src/test/mocks/styleMock.js'
},
transformMode: {
web: [/\.[jt]sx?$/],
ssr: [/\.css$/]
}
},
plugins: [
Expand Down

0 comments on commit 956a55c

Please sign in to comment.