From 38fdce747e222d59ba931c8b82fe2e88cef9b024 Mon Sep 17 00:00:00 2001 From: Sergey Yarkov Date: Wed, 15 Sep 2021 21:38:53 +0300 Subject: [PATCH] refactor: types --- src/components/PrivateRoute/PrivateRoute.tsx | 13 +++++- src/components/PublicRoute/PublicRoute.tsx | 8 +++- src/interfaces/index.ts | 49 -------------------- src/services/AuthService.ts | 19 ++++---- src/services/UserService.ts | 22 ++++----- src/stores/AuthStore.ts | 5 +- src/stores/UserStore.ts | 18 ++----- 7 files changed, 45 insertions(+), 89 deletions(-) diff --git a/src/components/PrivateRoute/PrivateRoute.tsx b/src/components/PrivateRoute/PrivateRoute.tsx index 644f604..e693d6c 100644 --- a/src/components/PrivateRoute/PrivateRoute.tsx +++ b/src/components/PrivateRoute/PrivateRoute.tsx @@ -3,11 +3,22 @@ import React from 'react'; import Helmet from 'react-helmet'; import { ErrorBoundary } from 'react-error-boundary'; import { Route, Redirect } from 'react-router-dom'; -import { IPrivateRouteProps } from 'interfaces'; +import { RouteProps } from 'react-router'; +import { IPageProps } from 'interfaces'; +import { UserRoleEnum } from 'enums'; import { useRootStore } from 'hooks/useRootStore'; import Layout from 'components/Layout'; import ErrorFallback from 'components/ErrorFallback'; +export interface IPrivateRouteProps extends RouteProps { + component: React.FC; + title?: string; + /** + * This field means who can access the page + */ + roles?: UserRoleEnum[]; +} + /** * Checks if the user is loggedIn by reactive variable and if not, * then a redirect to "/auth" route. diff --git a/src/components/PublicRoute/PublicRoute.tsx b/src/components/PublicRoute/PublicRoute.tsx index 2668e58..912e4e2 100644 --- a/src/components/PublicRoute/PublicRoute.tsx +++ b/src/components/PublicRoute/PublicRoute.tsx @@ -1,9 +1,15 @@ import React from 'react'; import { ErrorBoundary } from 'react-error-boundary'; import { Route } from 'react-router-dom'; -import { IPublicRouteProps } from 'interfaces'; +import { RouteProps } from 'react-router'; +import { IPageProps } from 'interfaces'; import ErrorFallback from 'components/ErrorFallback'; +export interface IPublicRouteProps extends RouteProps { + component: React.FC; + title?: string; +} + /** * Render public page */ diff --git a/src/interfaces/index.ts b/src/interfaces/index.ts index 281edab..f48beba 100644 --- a/src/interfaces/index.ts +++ b/src/interfaces/index.ts @@ -1,4 +1,3 @@ -import type { RouteProps } from 'react-router-dom'; import type { RouteComponentProps } from 'react-router-dom'; import { LinkType } from 'types'; import { UserRoleEnum } from '../enums'; @@ -89,64 +88,16 @@ export interface IUserRole { slug: UserRoleEnum; } -export interface IDataResult { - data: any; - message: string; - status: number; -} - export interface IApiRespose { data: T; message: string; status: number; } -export interface ILoginResult extends IDataResult { - data: { - token: string; - type: string; - expires_at: string; - }; -} - -export interface IUpdatedContactsResult extends IDataResult { - data: IUserContacts; -} - -export interface IUpdatedUserEmail extends IDataResult { - data: { - email: IUser['email']; - }; -} - -export interface ISentCodeResult extends IDataResult { - data: { - expired_seconds: number; - }; -} - -export interface IUserResult extends IDataResult { - data: Omit; -} - export interface IPageProps extends RouteComponentProps { title?: string; } -export interface IPrivateRouteProps extends RouteProps { - component: React.FC; - title?: string; - /** - * This field means who can access the page - */ - roles?: UserRoleEnum[]; -} - -export interface IPublicRouteProps extends RouteProps { - component: React.FC; - title?: string; -} - export interface IWindowDimensions { width: number; height: number; diff --git a/src/services/AuthService.ts b/src/services/AuthService.ts index 66e0793..8feea97 100644 --- a/src/services/AuthService.ts +++ b/src/services/AuthService.ts @@ -1,5 +1,5 @@ -import { IDataResult, ILoginResult } from 'interfaces'; -import { AxiosInstance, AxiosResponse } from 'axios'; +import { IApiRespose } from 'interfaces'; +import { AxiosInstance } from 'axios'; export default class AuthService { public api: AxiosInstance; @@ -13,10 +13,13 @@ export default class AuthService { * * @param login User login * @param password User password - * @returns Login result + * @returns Login result data */ - public async requestLogin(login: string, password: string): Promise { - const result: AxiosResponse = await this.api.post('v1/auth/login', { + public async requestLogin( + login: string, + password: string + ): Promise> { + const result = await this.api.post('v1/auth/login', { login, password, }); @@ -26,10 +29,10 @@ export default class AuthService { /** * Revoke token * - * @returns Data result + * @returns Any */ - public async requestLogout(): Promise { - const result: AxiosResponse = await this.api.post('v1/auth/logout'); + public async requestLogout(): Promise> { + const result = await this.api.post('v1/auth/logout'); return result.data; } } diff --git a/src/services/UserService.ts b/src/services/UserService.ts index d56f8f8..4b09fc7 100644 --- a/src/services/UserService.ts +++ b/src/services/UserService.ts @@ -1,14 +1,5 @@ import { AxiosInstance } from 'axios'; -import { - IApiRespose, - IDataResult, - IMe, - ISentCodeResult, - IUpdatedContactsResult, - IUpdatedUserEmail, - IUser, - IUserContacts, -} from 'interfaces'; +import { IApiRespose, IMe, IUser, IUserContacts } from 'interfaces'; import * as helpers from 'helpers'; export default class UserService { @@ -28,12 +19,12 @@ export default class UserService { return result.data; } - public async updateContacts(data: IUserContacts): Promise { + public async updateContacts(data: IUserContacts): Promise> { const result = await this.api.put('v1/me/contacts', helpers.removeEmptyValues(data)); return result.data; } - public async updatePassword(oldPassword: string, newPassword: string): Promise { + public async updatePassword(oldPassword: string, newPassword: string): Promise> { const result = await this.api.patch('v1/me/password', { oldPassword, newPassword, @@ -41,12 +32,15 @@ export default class UserService { return result.data; } - public async updateEmail(email: string): Promise { + public async updateEmail(email: string): Promise> { const result = await this.api.patch('v1/me/email', { email }); return result.data; } - public async updateEmailConfirm(email: string, confirmationCode: string): Promise { + public async updateEmailConfirm( + email: string, + confirmationCode: string + ): Promise> { const result = await this.api.post('v1/me/email/change/confirm', { email, confirmationCode, diff --git a/src/stores/AuthStore.ts b/src/stores/AuthStore.ts index 82798e7..db31273 100644 --- a/src/stores/AuthStore.ts +++ b/src/stores/AuthStore.ts @@ -1,7 +1,6 @@ import { makeAutoObservable } from 'mobx'; import { AxiosInstance } from 'axios'; import Cookies from 'js-cookie'; -import { IDataResult, ILoginResult } from 'interfaces'; /** * Services @@ -30,7 +29,7 @@ export default class AuthStore { this.isLoggedIn = value; } - public async login(login: string, password: string): Promise { + public async login(login: string, password: string) { try { const result = await this.authService.requestLogin(login, password); @@ -43,7 +42,7 @@ export default class AuthStore { } } - public async logout(): Promise { + public async logout() { try { const result = await this.authService.requestLogout(); diff --git a/src/stores/UserStore.ts b/src/stores/UserStore.ts index cb3ecb1..1565b39 100644 --- a/src/stores/UserStore.ts +++ b/src/stores/UserStore.ts @@ -1,14 +1,6 @@ import { AxiosInstance } from 'axios'; import { makeAutoObservable, runInAction } from 'mobx'; -import { - IUser, - IUserContacts, - IUpdatedContactsResult, - IDataResult, - ISentCodeResult, - IUpdatedUserEmail, - IMe, -} from 'interfaces'; +import { IUser, IUserContacts, IMe } from 'interfaces'; /** * Services @@ -84,7 +76,7 @@ export default class UserStore { * @param data New contacts * @returns Updated user contacts */ - public async updateCurrentUserContacts(data: IUserContacts): Promise { + public async updateCurrentUserContacts(data: IUserContacts) { try { const result = await this.userService.updateContacts(data); @@ -107,7 +99,7 @@ export default class UserStore { * @param newPassword New password * @returns Data result status */ - public async updateCurrentUserPassword(oldPassword: string, newPassword: string): Promise { + public async updateCurrentUserPassword(oldPassword: string, newPassword: string) { try { const result = await this.userService.updatePassword(oldPassword, newPassword); return result; @@ -122,7 +114,7 @@ export default class UserStore { * @param email New email * @returns Expires at code in seconds */ - public async updateCurrentUserEmail(email: string): Promise { + public async updateCurrentUserEmail(email: string) { try { const result = await this.userService.updateEmail(email); return result; @@ -138,7 +130,7 @@ export default class UserStore { * @param confirmationCode Confirmation code * @returns Updated user email */ - public async updateCurrentUserEmailConfirm(email: string, confirmationCode: string): Promise { + public async updateCurrentUserEmailConfirm(email: string, confirmationCode: string) { try { const result = await this.userService.updateEmailConfirm(email, confirmationCode); runInAction(() => {