-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make services for initAuthData, add ability share json settings betwe…
…en sessions and browsers
- Loading branch information
1 parent
ae5eb64
commit e340f43
Showing
8 changed files
with
76 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,46 @@ | ||
import React from 'react' | ||
import { useDispatch } from 'react-redux' | ||
import { useSelector } from 'react-redux' | ||
|
||
import { classNames } from '@/shared/lib/ClassNames/ClassNames' | ||
import { useTheme } from '@/app/providers/ThemeProvider' | ||
import { AppRouter } from '@/app/providers/router' | ||
import { Navbar } from '@/widgets/Navbar' | ||
import { Sidebar } from '@/widgets/Sidebar' | ||
import { USER_LOCALSTORAGE_KEY } from '@/shared/const/lodalStorage' | ||
import { userActions } from '@/entities/User' | ||
import { getUserInited, initAuthData } from '@/entities/User' | ||
import { useAppDispatch } from '@/shared/lib/hooks/useAppDispatch/useAppDispatch' | ||
import { PageLoader } from '@/widgets/PageLoader' | ||
|
||
function App() { | ||
const { theme } = useTheme() | ||
const dispatch = useDispatch() | ||
const dispatch = useAppDispatch() | ||
const inited = useSelector(getUserInited) | ||
|
||
//* Есть баг в этом участке, следует разобраться | ||
// const inited = useSelector(getUserInited); | ||
|
||
React.useEffect(() => { | ||
const user = localStorage.getItem(USER_LOCALSTORAGE_KEY) | ||
if (user) { | ||
dispatch(userActions.initAuthData(JSON.parse(user))) | ||
} | ||
dispatch(initAuthData()) | ||
}, [dispatch]) | ||
|
||
return ( | ||
<div className={classNames('app', {}, [theme])}> | ||
<React.Suspense fallback=""> | ||
<Navbar /> | ||
<div className="content-page"> | ||
<Sidebar /> | ||
<AppRouter /> | ||
</div> | ||
</React.Suspense> | ||
</div> | ||
let content = ( | ||
<React.Suspense fallback=""> | ||
<Navbar /> | ||
<div className="content-page"> | ||
<Sidebar /> | ||
<AppRouter /> | ||
</div> | ||
</React.Suspense> | ||
) | ||
|
||
if (!inited) { | ||
content = ( | ||
<div className={classNames('app', {}, [theme])}> | ||
<PageLoader /> | ||
</div> | ||
) | ||
} | ||
|
||
return <div className={classNames('app', {}, [theme])}>{content}</div> | ||
} | ||
|
||
export default App |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { createAsyncThunk } from '@reduxjs/toolkit' | ||
|
||
import { User } from '../types/user' | ||
import { getUserDataByIdQuery } from '../api/userApi' | ||
|
||
import { ThunkConfig } from '@/app/providers/StoreProvider' | ||
import { USER_LOCALSTORAGE_KEY } from '@/shared/const/lodalStorage' | ||
|
||
export const initAuthData = createAsyncThunk<User, void, ThunkConfig<string>>( | ||
'user/initAuthData', | ||
async (newJsonSettings, thunkApi) => { | ||
const { rejectWithValue, dispatch } = thunkApi | ||
const userId = localStorage.getItem(USER_LOCALSTORAGE_KEY) | ||
|
||
if (!userId) { | ||
return rejectWithValue('Уникальный id пользователя не был найден') | ||
} | ||
|
||
try { | ||
const response = await dispatch(getUserDataByIdQuery(userId)).unwrap() | ||
|
||
return response | ||
} catch (e) { | ||
return rejectWithValue('error') | ||
} | ||
}, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters