-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5f25e96
commit af8db0a
Showing
7 changed files
with
125 additions
and
6 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/* eslint-disable */ | ||
import React, { useEffect, useState } from 'react'; | ||
import { useAppDispatch, useAppSelector } from '../store/store'; | ||
import { searchProduct, selectSearchResults, selectSearchError } from '../store/features/ProductSlice'; | ||
import { useLocation } from 'react-router-dom'; | ||
|
||
const SearchBar: React.FC = () => { | ||
const location = useLocation() | ||
const params = new URLSearchParams(location.search); | ||
const name = params.get("name") | ||
const category = params.get("category") | ||
const maxPrice = params.get("maxPrice") | ||
const minPrice = params.get("minPrice") | ||
|
||
const dispatch = useAppDispatch(); | ||
const searchResults = useAppSelector(selectSearchResults); | ||
const searchError = useAppSelector(selectSearchError); | ||
useEffect(()=>{ | ||
dispatch(searchProduct({name:name?name:undefined,category:category?category:undefined,maxPrice:maxPrice?parseInt(maxPrice):undefined,minPrice:minPrice?parseInt(minPrice):undefined})) | ||
},[]) | ||
return ( | ||
<div> | ||
<h1> | ||
search | ||
</h1> | ||
{searchError && <p>Error: {searchError}</p>} | ||
<div> | ||
{searchResults.length > 0 ? ( | ||
<ul> | ||
{searchResults.map((product:any) => ( | ||
<li key={product.id}>{product.name} : ${product.price}</li> | ||
))} | ||
</ul> | ||
) : ( | ||
<p>No products found.</p> | ||
)} | ||
</div> | ||
</div> | ||
); | ||
}; | ||
|
||
export default SearchBar; |
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,55 @@ | ||
/* eslint-disable */ | ||
import { createSlice, createAsyncThunk, PayloadAction } from '@reduxjs/toolkit'; | ||
import axiosInstance from '../../utils/axios/axiosInstance'; | ||
import { IProducts } from '../../utils/types/Product'; | ||
|
||
const initialState: IProducts = { | ||
nextPage: 0, | ||
currentPage: 0, | ||
previousPage: 0, | ||
limit: 0, | ||
data: [], | ||
}; | ||
|
||
interface SearchCriteria { | ||
name?: string; | ||
category?: string; | ||
minPrice?: number; | ||
maxPrice?: number; | ||
} | ||
|
||
export const searchProduct = createAsyncThunk<IProducts, SearchCriteria>( | ||
'searchProduct', | ||
async (criteria) => { | ||
const response = await axiosInstance.get<IProducts>('/api/shop/user-search-products', { | ||
params: criteria, | ||
}); | ||
return response.data; | ||
} | ||
); | ||
|
||
const productSlice = createSlice({ | ||
name: 'product', | ||
initialState, | ||
reducers: {}, | ||
extraReducers: (builder) => { | ||
builder | ||
.addCase(searchProduct.fulfilled, (state, action: PayloadAction<IProducts>) => { | ||
state.nextPage = action.payload.nextPage; | ||
state.currentPage = action.payload.currentPage; | ||
state.previousPage = action.payload.previousPage; | ||
state.limit = action.payload.limit; | ||
state.data = action.payload.data; | ||
state.error = undefined; | ||
}) | ||
.addCase(searchProduct.rejected, (state, action) => { | ||
state.currentPage = -1; | ||
state.error = action.error.message; | ||
}); | ||
}, | ||
}); | ||
|
||
export const selectSearchResults = (state:any) => state.searchProduct.data; | ||
export const selectSearchError = (state:any) => state.searchProduct.error; | ||
|
||
export default productSlice.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
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,11 @@ | ||
export interface IProducts { | ||
nextPage:number; | ||
currentPage: number; | ||
previousPage:number; | ||
limit:number; | ||
data:[]; | ||
error?:string | ||
} | ||
export interface IProductsState { | ||
searchProduct: searchProduct; | ||
} |