diff --git a/src/app/modules/components/NickSearch.tsx b/src/app/modules/components/NickSearch.tsx index 044d89c..6f32029 100644 --- a/src/app/modules/components/NickSearch.tsx +++ b/src/app/modules/components/NickSearch.tsx @@ -1,6 +1,6 @@ import Select, { GroupBase } from 'react-select'; import * as Interfaces from "@/app/interfaces"; -import { useCallback, useState } from 'react'; +import { useCallback, useRef, useState } from 'react'; import Style from "@/app/styles/nick_search.module.css"; import ApiManager from '../utils/apiManager'; import { debounce } from 'lodash'; @@ -32,10 +32,14 @@ const Searcher = ({ onChange }: SearchProps) => { isDisabled: true }]); const [nickValue, setNickValue] = useState({ value: "no_data", label: <>Введите никнейм }); + const abortController = useRef(null); const fetchNicknames = (nickname: string) => { setLoading(true); - ApiManager.searchNicks(nickname) + + abortController.current?.abort?.(); + abortController.current = new AbortController(); + ApiManager.searchNicks(nickname, abortController.current.signal) .then(response_data => { if (!response_data.data) return; @@ -89,8 +93,6 @@ const Searcher = ({ onChange }: SearchProps) => { } setNicknames([{ value: nickname, label: {nickname} }]); - if (nickname.length === 17) return; - if (nickname.length > 2) debouncedFetch(nickname); } diff --git a/src/app/modules/utils/apiManager.ts b/src/app/modules/utils/apiManager.ts index 6d4809e..3191425 100644 --- a/src/app/modules/utils/apiManager.ts +++ b/src/app/modules/utils/apiManager.ts @@ -1,6 +1,6 @@ import * as Interfaces from "@/app/interfaces"; import { authApi } from "./api"; -import axios, { AxiosResponse, Method } from "axios"; +import axios, { AxiosResponse, GenericAbortSignal, Method } from "axios"; import { Query } from "../components/Header"; import { SettingsResponse } from "@/app/me/settings/page"; import { SearchResponse } from "../components/NickSearch"; @@ -10,7 +10,8 @@ type RequestProps = { url: string, method: Method, data?: any, - params?: any + params?: any, + signal?: GenericAbortSignal }; type UpdateUsersProps = { @@ -43,7 +44,8 @@ class ApiManager { url, method, data, - params + params, + signal }: RequestProps): Promise> { const response = await axios.request({ url: process.env.NEXT_PUBLIC_API_URL.slice(0, -1) + url, @@ -51,7 +53,8 @@ class ApiManager { data, params, headers: { 'accept-language': 'ru' }, - withCredentials: true + withCredentials: true, + signal }); if (response.status >= 400) throw response; @@ -228,10 +231,11 @@ class ApiManager { } /* Search Minecraft nicks */ - static async searchNicks(nickname: string): Promise { + static async searchNicks(nickname: string, signal?: GenericAbortSignal): Promise { return (await this.doRequestSimple({ url: `/minecraft/search/${nickname}`, - method: 'GET' + method: 'GET', + signal })).data; }