mirror of
https://github.com/sergeyyarkov/educt-client.git
synced 2026-10-11 10:19:22 +03:00
refactor: types
This commit is contained in:
parent
4ab5488f91
commit
38fdce747e
@ -3,11 +3,22 @@ import React from 'react';
|
|||||||
import Helmet from 'react-helmet';
|
import Helmet from 'react-helmet';
|
||||||
import { ErrorBoundary } from 'react-error-boundary';
|
import { ErrorBoundary } from 'react-error-boundary';
|
||||||
import { Route, Redirect } from 'react-router-dom';
|
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 { useRootStore } from 'hooks/useRootStore';
|
||||||
import Layout from 'components/Layout';
|
import Layout from 'components/Layout';
|
||||||
import ErrorFallback from 'components/ErrorFallback';
|
import ErrorFallback from 'components/ErrorFallback';
|
||||||
|
|
||||||
|
export interface IPrivateRouteProps extends RouteProps {
|
||||||
|
component: React.FC<IPageProps>;
|
||||||
|
title?: string;
|
||||||
|
/**
|
||||||
|
* This field means who can access the page
|
||||||
|
*/
|
||||||
|
roles?: UserRoleEnum[];
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Checks if the user is loggedIn by reactive variable and if not,
|
* Checks if the user is loggedIn by reactive variable and if not,
|
||||||
* then a redirect to "/auth" route.
|
* then a redirect to "/auth" route.
|
||||||
|
|||||||
@ -1,9 +1,15 @@
|
|||||||
import React from 'react';
|
import React from 'react';
|
||||||
import { ErrorBoundary } from 'react-error-boundary';
|
import { ErrorBoundary } from 'react-error-boundary';
|
||||||
import { Route } from 'react-router-dom';
|
import { Route } from 'react-router-dom';
|
||||||
import { IPublicRouteProps } from 'interfaces';
|
import { RouteProps } from 'react-router';
|
||||||
|
import { IPageProps } from 'interfaces';
|
||||||
import ErrorFallback from 'components/ErrorFallback';
|
import ErrorFallback from 'components/ErrorFallback';
|
||||||
|
|
||||||
|
export interface IPublicRouteProps extends RouteProps {
|
||||||
|
component: React.FC<IPageProps>;
|
||||||
|
title?: string;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Render public page
|
* Render public page
|
||||||
*/
|
*/
|
||||||
|
|||||||
@ -1,4 +1,3 @@
|
|||||||
import type { RouteProps } from 'react-router-dom';
|
|
||||||
import type { RouteComponentProps } from 'react-router-dom';
|
import type { RouteComponentProps } from 'react-router-dom';
|
||||||
import { LinkType } from 'types';
|
import { LinkType } from 'types';
|
||||||
import { UserRoleEnum } from '../enums';
|
import { UserRoleEnum } from '../enums';
|
||||||
@ -89,64 +88,16 @@ export interface IUserRole {
|
|||||||
slug: UserRoleEnum;
|
slug: UserRoleEnum;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface IDataResult {
|
|
||||||
data: any;
|
|
||||||
message: string;
|
|
||||||
status: number;
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface IApiRespose<T> {
|
export interface IApiRespose<T> {
|
||||||
data: T;
|
data: T;
|
||||||
message: string;
|
message: string;
|
||||||
status: number;
|
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<IUser, 'fullname'>;
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface IPageProps extends RouteComponentProps {
|
export interface IPageProps extends RouteComponentProps {
|
||||||
title?: string;
|
title?: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface IPrivateRouteProps extends RouteProps {
|
|
||||||
component: React.FC<IPageProps>;
|
|
||||||
title?: string;
|
|
||||||
/**
|
|
||||||
* This field means who can access the page
|
|
||||||
*/
|
|
||||||
roles?: UserRoleEnum[];
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface IPublicRouteProps extends RouteProps {
|
|
||||||
component: React.FC<IPageProps>;
|
|
||||||
title?: string;
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface IWindowDimensions {
|
export interface IWindowDimensions {
|
||||||
width: number;
|
width: number;
|
||||||
height: number;
|
height: number;
|
||||||
|
|||||||
@ -1,5 +1,5 @@
|
|||||||
import { IDataResult, ILoginResult } from 'interfaces';
|
import { IApiRespose } from 'interfaces';
|
||||||
import { AxiosInstance, AxiosResponse } from 'axios';
|
import { AxiosInstance } from 'axios';
|
||||||
|
|
||||||
export default class AuthService {
|
export default class AuthService {
|
||||||
public api: AxiosInstance;
|
public api: AxiosInstance;
|
||||||
@ -13,10 +13,13 @@ export default class AuthService {
|
|||||||
*
|
*
|
||||||
* @param login User login
|
* @param login User login
|
||||||
* @param password User password
|
* @param password User password
|
||||||
* @returns Login result
|
* @returns Login result data
|
||||||
*/
|
*/
|
||||||
public async requestLogin(login: string, password: string): Promise<ILoginResult> {
|
public async requestLogin(
|
||||||
const result: AxiosResponse<any> = await this.api.post('v1/auth/login', {
|
login: string,
|
||||||
|
password: string
|
||||||
|
): Promise<IApiRespose<{ token: string; type: string; expires_at: string }>> {
|
||||||
|
const result = await this.api.post('v1/auth/login', {
|
||||||
login,
|
login,
|
||||||
password,
|
password,
|
||||||
});
|
});
|
||||||
@ -26,10 +29,10 @@ export default class AuthService {
|
|||||||
/**
|
/**
|
||||||
* Revoke token
|
* Revoke token
|
||||||
*
|
*
|
||||||
* @returns Data result
|
* @returns Any
|
||||||
*/
|
*/
|
||||||
public async requestLogout(): Promise<IDataResult> {
|
public async requestLogout(): Promise<IApiRespose<any>> {
|
||||||
const result: AxiosResponse<any> = await this.api.post('v1/auth/logout');
|
const result = await this.api.post('v1/auth/logout');
|
||||||
return result.data;
|
return result.data;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,14 +1,5 @@
|
|||||||
import { AxiosInstance } from 'axios';
|
import { AxiosInstance } from 'axios';
|
||||||
import {
|
import { IApiRespose, IMe, IUser, IUserContacts } from 'interfaces';
|
||||||
IApiRespose,
|
|
||||||
IDataResult,
|
|
||||||
IMe,
|
|
||||||
ISentCodeResult,
|
|
||||||
IUpdatedContactsResult,
|
|
||||||
IUpdatedUserEmail,
|
|
||||||
IUser,
|
|
||||||
IUserContacts,
|
|
||||||
} from 'interfaces';
|
|
||||||
import * as helpers from 'helpers';
|
import * as helpers from 'helpers';
|
||||||
|
|
||||||
export default class UserService {
|
export default class UserService {
|
||||||
@ -28,12 +19,12 @@ export default class UserService {
|
|||||||
return result.data;
|
return result.data;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async updateContacts(data: IUserContacts): Promise<IUpdatedContactsResult> {
|
public async updateContacts(data: IUserContacts): Promise<IApiRespose<IUserContacts>> {
|
||||||
const result = await this.api.put('v1/me/contacts', helpers.removeEmptyValues(data));
|
const result = await this.api.put('v1/me/contacts', helpers.removeEmptyValues(data));
|
||||||
return result.data;
|
return result.data;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async updatePassword(oldPassword: string, newPassword: string): Promise<IDataResult> {
|
public async updatePassword(oldPassword: string, newPassword: string): Promise<IApiRespose<any>> {
|
||||||
const result = await this.api.patch('v1/me/password', {
|
const result = await this.api.patch('v1/me/password', {
|
||||||
oldPassword,
|
oldPassword,
|
||||||
newPassword,
|
newPassword,
|
||||||
@ -41,12 +32,15 @@ export default class UserService {
|
|||||||
return result.data;
|
return result.data;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async updateEmail(email: string): Promise<ISentCodeResult> {
|
public async updateEmail(email: string): Promise<IApiRespose<{ expired_seconds: number }>> {
|
||||||
const result = await this.api.patch('v1/me/email', { email });
|
const result = await this.api.patch('v1/me/email', { email });
|
||||||
return result.data;
|
return result.data;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async updateEmailConfirm(email: string, confirmationCode: string): Promise<IUpdatedUserEmail> {
|
public async updateEmailConfirm(
|
||||||
|
email: string,
|
||||||
|
confirmationCode: string
|
||||||
|
): Promise<IApiRespose<{ email: IUser['email'] }>> {
|
||||||
const result = await this.api.post('v1/me/email/change/confirm', {
|
const result = await this.api.post('v1/me/email/change/confirm', {
|
||||||
email,
|
email,
|
||||||
confirmationCode,
|
confirmationCode,
|
||||||
|
|||||||
@ -1,7 +1,6 @@
|
|||||||
import { makeAutoObservable } from 'mobx';
|
import { makeAutoObservable } from 'mobx';
|
||||||
import { AxiosInstance } from 'axios';
|
import { AxiosInstance } from 'axios';
|
||||||
import Cookies from 'js-cookie';
|
import Cookies from 'js-cookie';
|
||||||
import { IDataResult, ILoginResult } from 'interfaces';
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Services
|
* Services
|
||||||
@ -30,7 +29,7 @@ export default class AuthStore {
|
|||||||
this.isLoggedIn = value;
|
this.isLoggedIn = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async login(login: string, password: string): Promise<ILoginResult> {
|
public async login(login: string, password: string) {
|
||||||
try {
|
try {
|
||||||
const result = await this.authService.requestLogin(login, password);
|
const result = await this.authService.requestLogin(login, password);
|
||||||
|
|
||||||
@ -43,7 +42,7 @@ export default class AuthStore {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public async logout(): Promise<IDataResult> {
|
public async logout() {
|
||||||
try {
|
try {
|
||||||
const result = await this.authService.requestLogout();
|
const result = await this.authService.requestLogout();
|
||||||
|
|
||||||
|
|||||||
@ -1,14 +1,6 @@
|
|||||||
import { AxiosInstance } from 'axios';
|
import { AxiosInstance } from 'axios';
|
||||||
import { makeAutoObservable, runInAction } from 'mobx';
|
import { makeAutoObservable, runInAction } from 'mobx';
|
||||||
import {
|
import { IUser, IUserContacts, IMe } from 'interfaces';
|
||||||
IUser,
|
|
||||||
IUserContacts,
|
|
||||||
IUpdatedContactsResult,
|
|
||||||
IDataResult,
|
|
||||||
ISentCodeResult,
|
|
||||||
IUpdatedUserEmail,
|
|
||||||
IMe,
|
|
||||||
} from 'interfaces';
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Services
|
* Services
|
||||||
@ -84,7 +76,7 @@ export default class UserStore {
|
|||||||
* @param data New contacts
|
* @param data New contacts
|
||||||
* @returns Updated user contacts
|
* @returns Updated user contacts
|
||||||
*/
|
*/
|
||||||
public async updateCurrentUserContacts(data: IUserContacts): Promise<IUpdatedContactsResult> {
|
public async updateCurrentUserContacts(data: IUserContacts) {
|
||||||
try {
|
try {
|
||||||
const result = await this.userService.updateContacts(data);
|
const result = await this.userService.updateContacts(data);
|
||||||
|
|
||||||
@ -107,7 +99,7 @@ export default class UserStore {
|
|||||||
* @param newPassword New password
|
* @param newPassword New password
|
||||||
* @returns Data result status
|
* @returns Data result status
|
||||||
*/
|
*/
|
||||||
public async updateCurrentUserPassword(oldPassword: string, newPassword: string): Promise<IDataResult> {
|
public async updateCurrentUserPassword(oldPassword: string, newPassword: string) {
|
||||||
try {
|
try {
|
||||||
const result = await this.userService.updatePassword(oldPassword, newPassword);
|
const result = await this.userService.updatePassword(oldPassword, newPassword);
|
||||||
return result;
|
return result;
|
||||||
@ -122,7 +114,7 @@ export default class UserStore {
|
|||||||
* @param email New email
|
* @param email New email
|
||||||
* @returns Expires at code in seconds
|
* @returns Expires at code in seconds
|
||||||
*/
|
*/
|
||||||
public async updateCurrentUserEmail(email: string): Promise<ISentCodeResult> {
|
public async updateCurrentUserEmail(email: string) {
|
||||||
try {
|
try {
|
||||||
const result = await this.userService.updateEmail(email);
|
const result = await this.userService.updateEmail(email);
|
||||||
return result;
|
return result;
|
||||||
@ -138,7 +130,7 @@ export default class UserStore {
|
|||||||
* @param confirmationCode Confirmation code
|
* @param confirmationCode Confirmation code
|
||||||
* @returns Updated user email
|
* @returns Updated user email
|
||||||
*/
|
*/
|
||||||
public async updateCurrentUserEmailConfirm(email: string, confirmationCode: string): Promise<IUpdatedUserEmail> {
|
public async updateCurrentUserEmailConfirm(email: string, confirmationCode: string) {
|
||||||
try {
|
try {
|
||||||
const result = await this.userService.updateEmailConfirm(email, confirmationCode);
|
const result = await this.userService.updateEmailConfirm(email, confirmationCode);
|
||||||
runInAction(() => {
|
runInAction(() => {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user