-
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.
- Loading branch information
1 parent
653f958
commit 9836eaa
Showing
58 changed files
with
359 additions
and
23 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,19 +1,29 @@ | ||
import React, { Suspense } from 'react'; | ||
import React, { Suspense, useEffect } from 'react'; | ||
import { classNames } from 'shared/lib/classNames/classNames'; | ||
import { AppRouter } from 'app/providers/router'; | ||
import { Navbar } from 'widgets/Navbar'; | ||
import { Sidebar } from 'widgets/Sidebar'; | ||
import { useDispatch } from 'react-redux'; | ||
import { userActions } from 'entities/User'; | ||
|
||
const App = () => ( | ||
<div className={classNames('app', {}, [])}> | ||
<Suspense fallback=""> | ||
<Navbar /> | ||
<div className="content-page"> | ||
<Sidebar /> | ||
<AppRouter /> | ||
</div> | ||
</Suspense> | ||
</div> | ||
); | ||
const App = () => { | ||
const dispatch = useDispatch(); | ||
|
||
useEffect(() => { | ||
dispatch(userActions.initAuthData()); | ||
}, [dispatch]); | ||
|
||
return ( | ||
<div className={classNames('app', {}, [])}> | ||
<Suspense fallback=""> | ||
<Navbar /> | ||
<div className="content-page"> | ||
<Sidebar /> | ||
<AppRouter /> | ||
</div> | ||
</Suspense> | ||
</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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,9 @@ | ||
import { CounterSchema } from 'entities/Counter'; | ||
import { UserSchema } from 'entities/User'; | ||
import { LoginSchema } from 'features/AuthByUsername'; | ||
|
||
export interface StateSchema { | ||
counter: CounterSchema; | ||
user: UserSchema; | ||
loginForm: LoginSchema; | ||
} |
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 |
---|---|---|
|
@@ -20,4 +20,6 @@ | |
|
||
// colors | ||
--overlay-color: rgba(0 0 0 / 60%); | ||
--red-light: #ff0000; | ||
--red-dark: #ce0505; | ||
} |
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
3 changes: 3 additions & 0 deletions
3
src/entities/User/model/selectors/getUserAuthData/getUserAuthData.ts
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,3 @@ | ||
import { StateSchema } from 'app/providers/StoreProvider'; | ||
|
||
export const getUserAuthData = (state: StateSchema) => state.user.authData; |
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 +1,3 @@ | ||
export { LoginModal } from './ui/LoginModal/LoginModal'; | ||
export { LoginSchema } from './model/types/loginSchema'; | ||
export { loginReducer } from './model/slice/loginSlice'; |
3 changes: 3 additions & 0 deletions
3
src/features/AuthByUsername/model/selectors/getLoginState/getLoginState.ts
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,3 @@ | ||
import { StateSchema } from 'app/providers/StoreProvider'; | ||
|
||
export const getLoginState = (state: StateSchema) => state?.loginForm; |
Empty file.
30 changes: 30 additions & 0 deletions
30
src/features/AuthByUsername/model/services/loginByUsername/loginByUsername.ts
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,30 @@ | ||
import { createAsyncThunk } from '@reduxjs/toolkit'; | ||
import axios from 'axios'; | ||
import { User, userActions } from 'entities/User'; | ||
import { USER_LOCALSTORAGE_KEY } from 'shared/const/localstorage'; | ||
|
||
interface LoginByUsernameProps { | ||
username: string; | ||
password: string; | ||
} | ||
|
||
export const loginByUsername = createAsyncThunk<User, LoginByUsernameProps, { rejectValue: string }>( | ||
'login/loginByUsername', | ||
async (authData, thunkAPI) => { | ||
try { | ||
const response = await axios.post<User>('http://localhost:8000/login', authData); | ||
|
||
if (!response.data) { | ||
throw new Error(); | ||
} | ||
|
||
localStorage.setItem(USER_LOCALSTORAGE_KEY, JSON.stringify(response.data)); | ||
thunkAPI.dispatch(userActions.setAuthData(response.data)); | ||
|
||
return response.data; | ||
} catch (e) { | ||
console.log(e); | ||
return thunkAPI.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { createSlice, PayloadAction } from '@reduxjs/toolkit'; | ||
import { loginByUsername } from '../services/loginByUsername/loginByUsername'; | ||
import { LoginSchema } from '../types/loginSchema'; | ||
|
||
const initialState: LoginSchema = { | ||
isLoading: false, | ||
username: '', | ||
password: '', | ||
}; | ||
|
||
export const loginSlice = createSlice({ | ||
name: 'login', | ||
initialState, | ||
reducers: { | ||
setUsername: (state, action: PayloadAction<string>) => { | ||
state.username = action.payload; | ||
}, | ||
setPassword: (state, action: PayloadAction<string>) => { | ||
state.password = action.payload; | ||
}, | ||
}, | ||
extraReducers: (builder) => { | ||
builder | ||
.addCase(loginByUsername.pending, (state, action) => { | ||
state.error = undefined; | ||
state.isLoading = true; | ||
}) | ||
.addCase(loginByUsername.fulfilled, (state, action) => { | ||
state.isLoading = false; | ||
}) | ||
.addCase(loginByUsername.rejected, (state, action) => { | ||
state.isLoading = false; | ||
state.error = action.payload; | ||
}); | ||
}, | ||
}); | ||
|
||
export const { actions: loginActions } = loginSlice; | ||
export const { reducer: loginReducer } = loginSlice; |
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,6 @@ | ||
export interface LoginSchema { | ||
username: string; | ||
password: string; | ||
isLoading: boolean; | ||
error?: string; | ||
} |
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,30 +1,63 @@ | ||
import { classNames } from 'shared/lib/classNames/classNames'; | ||
import { useTranslation } from 'react-i18next'; | ||
import { Button } from 'shared/ui/Button/Button'; | ||
import { Button, ButtonTheme } from 'shared/ui/Button/Button'; | ||
import { Input } from 'shared/ui/Input/Input'; | ||
import { useDispatch, useSelector } from 'react-redux'; | ||
import { memo, useCallback } from 'react'; | ||
import { Text, TextTheme } from 'shared/ui/Text/Text'; | ||
import { loginByUsername } from '../../model/services/loginByUsername/loginByUsername'; | ||
import { loginActions } from '../../model/slice/loginSlice'; | ||
import cls from './LoginForm.module.scss'; | ||
import { getLoginState } from '../../model/selectors/getLoginState/getLoginState'; | ||
|
||
interface LoginFormProps { | ||
className?: string; | ||
} | ||
|
||
export const LoginForm = ({ className }: LoginFormProps) => { | ||
export const LoginForm = memo(({ className }: LoginFormProps) => { | ||
const { t } = useTranslation(); | ||
const dispatch = useDispatch(); | ||
const { | ||
username, password, isLoading, error, | ||
} = useSelector(getLoginState); | ||
|
||
const onChangeUsername = useCallback((value: string) => { | ||
dispatch(loginActions.setUsername(value)); | ||
}, [dispatch]); | ||
|
||
const onChangePassword = useCallback((value: string) => { | ||
dispatch(loginActions.setPassword(value)); | ||
}, [dispatch]); | ||
|
||
const onLoginClick = useCallback(() => { | ||
dispatch(loginByUsername({ username, password })); | ||
}, [dispatch, username, password]); | ||
|
||
return ( | ||
<div className={classNames(cls.LoginForm, {}, [className])}> | ||
<Text title={t('sign_in')} /> | ||
{error && <Text text={t('incorrect_username_or_password')} theme={TextTheme.ERROR} />} | ||
<Input | ||
autofocus | ||
className={cls.input} | ||
placeholder={t('enter_username')} | ||
onChange={onChangeUsername} | ||
value={username} | ||
/> | ||
<Input | ||
className={cls.input} | ||
placeholder={t('enter_password')} | ||
onChange={onChangePassword} | ||
value={password} | ||
/> | ||
<Button className={cls.loginBtn}> | ||
<Button | ||
theme={ButtonTheme.OUTLINE} | ||
className={cls.loginBtn} | ||
onClick={onLoginClick} | ||
disabled={isLoading} | ||
> | ||
{t('sign_in')} | ||
</Button> | ||
</div> | ||
); | ||
}; | ||
}); |
9 changes: 9 additions & 0 deletions
9
src/shared/config/storybook/StoreDecorator/StoreDecorator.tsx
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,9 @@ | ||
import { Story } from '@storybook/react'; | ||
import { StateSchema, StoreProvider } from 'app/providers/StoreProvider'; | ||
import { DeepPartial } from '@reduxjs/toolkit'; | ||
|
||
export const StoreDecorator = (state: DeepPartial<StateSchema>) => (StoryComponent: Story) => ( | ||
<StoreProvider initialState={state}> | ||
<StoryComponent /> | ||
</StoreProvider> | ||
); |
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 @@ | ||
export const USER_LOCALSTORAGE_KEY = 'user'; |
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 |
---|---|---|
|
@@ -64,3 +64,7 @@ | |
.size_xl { | ||
font: var(--font-xl); | ||
} | ||
|
||
.disabled { | ||
opacity: 0.5; | ||
} |
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
Oops, something went wrong.