-
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.
Merge pull request #28 from atlp-rwanda/bg-fix-use-redux-store
Bg fix use redux store
- Loading branch information
Showing
11 changed files
with
456 additions
and
238 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
Large diffs are not rendered by default.
Oops, something went wrong.
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,49 @@ | ||
import { useState } from 'react'; | ||
import { LuChevronDown } from 'react-icons/lu'; | ||
import { cn } from '../../utils'; | ||
|
||
interface SearchParamProps { | ||
searchQuery: string; | ||
} | ||
const SearchHeader = ({ searchQuery }: SearchParamProps) => { | ||
const [sortInfoHidden, setSortInfoHidden] = useState(true); | ||
|
||
return ( | ||
<> | ||
<div className='p-3 md:p-4 xl:px-10 '> | ||
<p className='font-medium text-lg border-b-2 pb-1 md:text-xl md:border-b-[3px] md:pb-3 flex flex-col md:flex-row md:justify-between'> | ||
<span> | ||
Search Results for | ||
<span className='text-xl font-bold md:text-2xl self-start justify-self-start ms-2'>"{searchQuery}"</span> | ||
</span> | ||
<button | ||
className='w-fit p-2 font-medium text-sm rounded-lg border border-overlay | ||
flex flex-row items-center gap-2 text-greenColor transition-all relative' | ||
onClick={() => setSortInfoHidden(!sortInfoHidden)} | ||
> | ||
Sort By | ||
<LuChevronDown className='w-5' /> | ||
<div | ||
className={cn( | ||
' rounded-lg bg-whiteColor absolute top-9 right-0 md:right-1 shadow-customShadow flex flex-col overflow-hidden transition-all z-10', | ||
sortInfoHidden ? 'h-0 hidden' : 'h-fit flex' | ||
)} | ||
> | ||
<span className='text-sm p-2 font-medium w-full text-wrap md:text-nowrap text-darkGreen hover:text-whiteColor hover:bg-overlay transition-all '> | ||
Name Ascending | ||
</span> | ||
<span className='text-sm p-2 font-medium w-full text-wrap md:text-nowrap text-darkGreen hover:text-whiteColor hover:bg-overlay transition-all '> | ||
Name Descending | ||
</span> | ||
<span className='text-sm p-2 font-medium w-full text-wrap md:text-nowrap text-darkGreen hover:text-whiteColor hover:bg-overlay transition-all '> | ||
Date Added | ||
</span> | ||
</div> | ||
</button> | ||
</p> | ||
</div> | ||
</> | ||
); | ||
}; | ||
|
||
export default SearchHeader; |
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,54 @@ | ||
// import { useSelector } from 'react-redux'; | ||
import { useSelector } from 'react-redux'; | ||
import ProductCard from '../Products/ProductCard'; | ||
import Button from '../common/Button'; | ||
import { Product } from '../../types/Types'; | ||
import { useNavigate } from 'react-router-dom'; | ||
import { useEffect } from 'react'; | ||
|
||
interface SearchQueryProps { | ||
searchQuery: string; | ||
} | ||
const SearchResults = ({ searchQuery }: SearchQueryProps) => { | ||
const { isLoading, productsDataList: productsList } = useSelector((state: any) => state.products); | ||
const filteredProducts = [ | ||
...productsList.filter((product: Product) => product.name.toLowerCase().includes(searchQuery.toLowerCase())), | ||
...productsList.filter((product: Product) => product.description.toLowerCase().includes(searchQuery.toLowerCase())), | ||
]; | ||
useEffect(() => { | ||
if (searchQuery.length !== 0 && filteredProducts.length !== 0) { | ||
const productNames = filteredProducts.map(product => product.name); | ||
let frequentlySearched = JSON.parse(localStorage.getItem('frequentlySearched') as string) || []; | ||
frequentlySearched = [...new Set(frequentlySearched)]; | ||
frequentlySearched = [...new Set([...frequentlySearched, ...productNames])]; | ||
localStorage.setItem('frequentlySearched', JSON.stringify(frequentlySearched)); | ||
} | ||
}, [searchQuery]); | ||
|
||
|
||
const navigate = useNavigate(); | ||
const handleNavigate = () => { | ||
navigate('/'); | ||
}; | ||
return ( | ||
<div> | ||
|
||
<div className='p-3 md:p-4 xl:px-10 flex flex-row flex-wrap gap-4'> | ||
{isLoading ? ( | ||
'Loading....' | ||
) : filteredProducts.length === 0 ? ( | ||
<div className='w-full '> | ||
<p className='font-medium text-2xl italic'> | ||
No matching products were found for <span className='font-bold'>{searchQuery}</span> | ||
</p> | ||
<Button text='Return to HomePage' className='mt-10 mx-auto block' onClick={handleNavigate} /> | ||
</div> | ||
) : ( | ||
filteredProducts.map((product: Product) => <ProductCard key={product.id} product={product} />) | ||
)} | ||
</div> | ||
</div> | ||
); | ||
}; | ||
|
||
export default SearchResults; |
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,22 @@ | ||
import Footer from '../../components/footer/Footer'; | ||
import Navbar from '../../components/navbar/Navbar'; | ||
import SearchHeader from '../../components/search/SearchHeader'; | ||
import SearchResults from '../../components/search/SearchResults'; | ||
import { useSearchParams } from 'react-router-dom'; | ||
|
||
const Searchpage = () => { | ||
const [searchParams] = useSearchParams(); | ||
const searchParam = searchParams.get('searchQuery') as string; | ||
return ( | ||
<div className='w-full'> | ||
<span className='w-full h-screen block'> | ||
<Navbar /> | ||
<SearchHeader searchQuery={searchParam} /> | ||
<SearchResults searchQuery={searchParam} /> | ||
</span> | ||
<Footer /> | ||
</div> | ||
); | ||
}; | ||
|
||
export default Searchpage; |
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,36 @@ | ||
import { createSlice, PayloadAction } from '@reduxjs/toolkit'; | ||
import { Product } from '../../types/Types'; | ||
|
||
interface ProductsStateProps { | ||
error: any; | ||
isLoading: boolean; | ||
productsFetched: boolean; | ||
productsDataList: Product[]; | ||
} | ||
const initialState: ProductsStateProps = { | ||
isLoading: true, | ||
error: null, | ||
productsFetched: false, | ||
productsDataList: [], | ||
}; | ||
const productSlice = createSlice({ | ||
name: 'producs', | ||
initialState, | ||
reducers: { | ||
setProductFetched: (state, action: PayloadAction<any>) => { | ||
state.productsFetched = action.payload; | ||
}, | ||
setProductsDataList: (state, action: PayloadAction<any>) => { | ||
state.productsDataList = action.payload; | ||
}, | ||
setIsLoading: (state, action: PayloadAction<any>) => { | ||
state.isLoading = action.payload; | ||
}, | ||
setError: (state, action: PayloadAction<any>) => { | ||
state.error = action.payload; | ||
}, | ||
}, | ||
}); | ||
export const { setProductFetched, setProductsDataList, setIsLoading,setError } = productSlice.actions; | ||
|
||
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