Skip to content

Commit 08d9b7f

Browse files
refactor: migrate authentication reducer to ts (#514)
1 parent ac62b73 commit 08d9b7f

File tree

9 files changed

+39
-28
lines changed

9 files changed

+39
-28
lines changed

src/containers/App/App.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import PropTypes from 'prop-types';
55
import ContentWrapper, {Content} from './Content';
66
import AsideNavigation from '../AsideNavigation/AsideNavigation';
77

8-
import {getUser} from '../../store/reducers/authentication';
8+
import {getUser} from '../../store/reducers/authentication/authentication';
99
import {registerLanguages} from '../../utils/monaco';
1010

1111
import './App.scss';

src/containers/AsideNavigation/AsideNavigation.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import userChecked from '../../assets/icons/user-check.svg';
1818
import settingsIcon from '../../assets/icons/settings.svg';
1919
import supportIcon from '../../assets/icons/support.svg';
2020

21-
import {logout} from '../../store/reducers/authentication';
21+
import {logout} from '../../store/reducers/authentication/authentication';
2222
import {getParsedSettingValue, setSettingValue} from '../../store/reducers/settings/settings';
2323
import {TENANT_PAGE, TENANT_PAGES_IDS} from '../../store/reducers/tenant/constants';
2424
import routes, {TENANT, createHref, parseQuery} from '../../routes';

src/containers/Authentication/Authentication.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import cn from 'bem-cn-lite';
55

66
import {Button, TextInput, Icon, Link as ExternalLink} from '@gravity-ui/uikit';
77

8-
import {authenticate} from '../../store/reducers/authentication';
8+
import {authenticate} from '../../store/reducers/authentication/authentication';
99
import {useTypedSelector} from '../../utils/hooks';
1010

1111
import ydbLogoIcon from '../../assets/icons/ydb.svg';

src/store/reducers/authentication.js renamed to src/store/reducers/authentication/authentication.ts

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1-
import {createRequestActionTypes, createApiRequest} from '../utils';
2-
import '../../services/api';
1+
import type {Reducer} from 'redux';
2+
3+
import '../../../services/api';
4+
import {createRequestActionTypes, createApiRequest} from '../../utils';
5+
6+
import type {AuthenticationAction, AuthenticationState} from './types';
37

48
export const SET_UNAUTHENTICATED = createRequestActionTypes(
59
'authentication',
@@ -11,16 +15,19 @@ export const FETCH_USER = createRequestActionTypes('authentication', 'FETCH_USER
1115
const initialState = {
1216
isAuthenticated: true,
1317
user: '',
14-
error: '',
18+
error: undefined,
1519
};
1620

17-
const authentication = function (state = initialState, action) {
21+
const authentication: Reducer<AuthenticationState, AuthenticationAction> = (
22+
state = initialState,
23+
action,
24+
) => {
1825
switch (action.type) {
1926
case SET_UNAUTHENTICATED.SUCCESS: {
20-
return {...state, isAuthenticated: false, user: '', error: ''};
27+
return {...state, isAuthenticated: false, user: '', error: undefined};
2128
}
2229
case SET_AUTHENTICATED.SUCCESS: {
23-
return {...state, isAuthenticated: true, error: ''};
30+
return {...state, isAuthenticated: true, error: undefined};
2431
}
2532
case SET_AUTHENTICATED.FAILURE: {
2633
return {...state, error: action.error};
@@ -34,7 +41,7 @@ const authentication = function (state = initialState, action) {
3441
}
3542
};
3643

37-
export const authenticate = (user, password) => {
44+
export const authenticate = (user: string, password: string) => {
3845
return createApiRequest({
3946
request: window.api.authenticate(user, password),
4047
actions: SET_AUTHENTICATED,
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import type {AuthErrorResponse} from '../../../types/api/error';
2+
import type {ApiRequestAction} from '../../utils';
3+
4+
import {FETCH_USER, SET_AUTHENTICATED, SET_UNAUTHENTICATED} from './authentication';
5+
6+
export interface AuthenticationState {
7+
isAuthenticated: boolean;
8+
user: string | undefined;
9+
error: AuthErrorResponse | undefined;
10+
}
11+
12+
export type AuthenticationAction =
13+
| ApiRequestAction<typeof SET_UNAUTHENTICATED, unknown, unknown>
14+
| ApiRequestAction<typeof SET_AUTHENTICATED, unknown, AuthErrorResponse>
15+
| ApiRequestAction<typeof FETCH_USER, string | undefined, unknown>;

src/store/reducers/describe.ts

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
import {createSelector, Selector} from 'reselect';
21
import {Reducer} from 'redux';
32

43
import '../../services/api';
5-
import {
6-
IDescribeRootStateSlice,
4+
import type {
75
IDescribeState,
86
IDescribeAction,
97
IDescribeHandledResponse,
@@ -93,19 +91,6 @@ export const setDataWasNotLoaded = () => {
9391
} as const;
9492
};
9593

96-
// Consumers selectors
97-
const selectConsumersNames = (state: IDescribeRootStateSlice, path?: string) =>
98-
path
99-
? state.describe.data[path]?.PathDescription?.PersQueueGroup?.PQTabletConfig?.ReadRules
100-
: undefined;
101-
102-
interface IConsumer {
103-
name: string;
104-
}
105-
106-
export const selectConsumers: Selector<IDescribeRootStateSlice, IConsumer[], [string | undefined]> =
107-
createSelector(selectConsumersNames, (names = []) => names.map((name) => ({name})));
108-
10994
export function getDescribe({path}: {path: string}) {
11095
const request = window.api.getDescribe({path});
11196
return createApiRequest({

src/store/reducers/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ import healthcheckInfo from './healthcheckInfo';
3030
import shardsWorkload from './shardsWorkload';
3131
import hotKeys from './hotKeys';
3232
import olapStats from './olapStats';
33-
import authentication from './authentication';
33+
import authentication from './authentication/authentication';
3434
import header from './header/header';
3535
import saveQuery from './saveQuery';
3636
import fullscreen from './fullscreen';

src/store/utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import {AxiosResponse} from 'axios';
33

44
import createToast from '../utils/createToast';
55

6-
import {SET_UNAUTHENTICATED} from './reducers/authentication';
6+
import {SET_UNAUTHENTICATED} from './reducers/authentication/authentication';
77
import type {GetState} from './reducers';
88

99
export const nop = (result: any) => result;

src/types/api/error.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,7 @@ export interface NetworkError {
1818
number?: unknown;
1919
stack?: string;
2020
}
21+
22+
export type AuthErrorResponse = IResponseError<{
23+
error?: string;
24+
}>;

0 commit comments

Comments
 (0)