-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
api: Интеграция с бэком для отправки поискового запроса (#202)
* api: Интеграция с бэком для отправки поискового запроса #173 * hotfix: исправлена опечатка в SroreProvider, fix ESLint issues
- Loading branch information
Showing
25 changed files
with
158 additions
and
20 deletions.
There are no files selected for viewing
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
File renamed without changes.
File renamed without changes.
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,4 +1,4 @@ | ||
import { RootState } from '@/app/providers/SroreProvider/config/store' | ||
import { RootState } from '@/app/providers/StoreProvider/config/store' | ||
|
||
export const selectCategories = (state: RootState) => state.category.categories | ||
export const selectDisplayedCategories = (state: RootState) => state.category.displayedCategories |
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/features/SearchProduct/selectors/searchProductSelectors.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 { RootState } from '@/app/providers/StoreProvider/config/store' | ||
|
||
export const selectSearchResult = (state: RootState) => state.searchResult |
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,55 @@ | ||
import { createSlice, createAsyncThunk } from '@reduxjs/toolkit' | ||
import { ApiError, ApiErrorTypes, ApiRoutes } from '@/shared/api/types' | ||
import { ThunkConfig } from '@/app/providers/StoreProvider/config/StateSchema' | ||
import { SearchResultSchema } from '../types/types' | ||
import { apiErrorIdentify } from '@/shared/api/apiErrorIdentify' | ||
import { rejectedPayloadHandle } from '@/shared/api/rejectedPayloadHandle' | ||
import { Category } from '@/entities/Category/types/types' | ||
import { TProduct } from '@/shared/model/types/common' | ||
|
||
type SearchPayload = { | ||
category: Category[] | ||
product: { results: TProduct[] } | ||
} | ||
|
||
const initialState: SearchResultSchema = { | ||
categories: [], | ||
products: [], | ||
error: undefined | ||
} | ||
|
||
export const search = createAsyncThunk<SearchPayload, string, ThunkConfig<ApiError>>( | ||
'catalogue/search', | ||
async (query: string, thunkAPI) => { | ||
const { rejectWithValue, extra } = thunkAPI | ||
|
||
try { | ||
const { data } = await extra.api.get(`api/${ApiRoutes.SEARCH}/?search=${query}`) | ||
return data | ||
} catch (error) { | ||
return rejectWithValue(apiErrorIdentify(error, ApiErrorTypes.AUTH_ERROR)) as ApiError | ||
} | ||
} | ||
) | ||
|
||
const searchProductSlice = createSlice({ | ||
name: 'searchResult', | ||
initialState, | ||
reducers: {}, | ||
extraReducers: builder => { | ||
builder | ||
.addCase(search.pending, state => { | ||
state.error = undefined | ||
}) | ||
.addCase(search.fulfilled, (state, action) => { | ||
const { category, product } = action.payload | ||
state.categories = category || [] | ||
state.products = product.results || [] | ||
}) | ||
.addCase(search.rejected, (state, { payload }) => { | ||
state.error = rejectedPayloadHandle(payload) | ||
}) | ||
} | ||
}) | ||
|
||
export default searchProductSlice.reducer |
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,8 @@ | ||
import { Category } from '@/entities/Category/types/types' | ||
import { TProduct } from '@/shared/model/types/common' | ||
|
||
export interface SearchResultSchema { | ||
categories: Category[] | ||
products: TProduct[] | ||
error?: string | 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
2 changes: 1 addition & 1 deletion
2
src/features/login/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
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 |
---|---|---|
@@ -0,0 +1,3 @@ | ||
.heading { | ||
align-self: flex-start; | ||
} |
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,48 @@ | ||
import { useEffect } from 'react' | ||
import { useSelector, useDispatch } from 'react-redux' | ||
import { useParams } from 'react-router-dom' | ||
import WrapperForMainContent from '@/components/WrapperForMainContent/WrapperForMainContent' | ||
import Heading from '@/shared/ui/Heading/Heading' | ||
import Subheading from '@/shared/ui/Subheading/Subheading' | ||
import styles from './SearchResultsPage.module.scss' | ||
import { search } from '@/features/SearchProduct/slice/searchProductSlice' | ||
import { selectSearchResult } from '@/features/SearchProduct/selectors/searchProductSelectors' | ||
import { AnyAction, ThunkDispatch } from '@reduxjs/toolkit' | ||
import { RootState } from '@/app/providers/StoreProvider/config/store' | ||
import { ThunkExtraArg } from '@/app/providers/StoreProvider/config/StateSchema' | ||
|
||
/** | ||
* Страница с результатами поискового запроса | ||
* @TODO Сверстать страницу SearchResults, разбить на компоненты | ||
* https://github.com/Studio-Yandex-Practicum/maxboom_frontend/issues/201 | ||
*/ | ||
const SearchResultsPage = () => { | ||
const { query } = useParams() | ||
const searchResult = useSelector(selectSearchResult) | ||
const dispatch = useDispatch<ThunkDispatch<RootState, ThunkExtraArg, AnyAction>>() | ||
|
||
useEffect(() => { | ||
if (query && query.length > 0) { | ||
dispatch(search(query)) | ||
} | ||
}, [query, dispatch]) | ||
|
||
return ( | ||
<WrapperForMainContent> | ||
<Heading className={styles.heading}>Результаты поиска</Heading> | ||
<Subheading>В разработке</Subheading> | ||
<ul> | ||
{searchResult && | ||
searchResult.categories.map(category => { | ||
return <li key={category.slug}>{category.name}</li> | ||
})} | ||
{searchResult && | ||
searchResult.products.map(product => { | ||
return <li key={product.slug}>{product.name}</li> | ||
})} | ||
</ul> | ||
</WrapperForMainContent> | ||
) | ||
} | ||
|
||
export default SearchResultsPage |
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
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