Skip to content

Commit

Permalink
feat: loading state and display
Browse files Browse the repository at this point in the history
Relates #256
  • Loading branch information
nichgalzin committed Jun 27, 2024
1 parent 1e1b8bb commit 9af8704
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 29 deletions.
7 changes: 4 additions & 3 deletions app/(dashboard)/search/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@ export default function SearchItemPage() {
const [searchParams, setSearchParams] =
useState<SearchParamsType>(initialSearchParams);
const [searchResults, setSearchResults] = useState<PartialItem[]>([]);
const [isLoading, setIsLoading] = useState(false);

const fetchSearchResults = async () => {
setIsLoading(true);
let data: PartialItem[] = [];
switch (true) {
// case !!searchParams.query &&
Expand Down Expand Up @@ -58,6 +60,7 @@ export default function SearchItemPage() {
break;
}
setSearchResults(data);
setIsLoading(false);
};

useEffect(() => {
Expand All @@ -72,10 +75,8 @@ export default function SearchItemPage() {
/>
<QuickBrowse />
<ItemDisplayContainer
query={searchParams.query}
category={searchParams.category}
subcategory={searchParams.subcategory}
searchResults={searchResults}
isLoading={isLoading}
/>
</div>
);
Expand Down
51 changes: 25 additions & 26 deletions components/search/ItemDisplayContainer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,41 +3,40 @@ import { PartialItem } from '@/types/supabaseTypes';
import ItemCard from '../ItemCard';

type ItemDisplayContainerProps = {
query: string;
category: string;
subcategory: string;
searchResults: PartialItem[] | [];
isLoading: boolean;
};

const ItemDisplayContainer: React.FC<ItemDisplayContainerProps> = ({
query,
category,
subcategory,
searchResults,
isLoading,
}) => {
if (isLoading) {
return (
<div className='mt-8 flex justify-center'>
<h2>Loading....</h2>
</div>
);
}
return (
<div className='lg-px-20 m-auto mt-5 lg:w-5/6'>
<h2 className='mx-5 font-semibold italic'>
Search for:{' '}
<span className='font-normal'>
{query.length > 0 ? query : ''}
{category.length > 0 ? category : ''}
{subcategory.length > 0 ? subcategory : ''}
</span>
</h2>
<div className='mt-10 flex flex-col items-center gap-5'>
{searchResults.map((result) => (
<ItemCard
key={result.id}
imageSrc={result.imageSrc}
item_name={result.item_name}
condition={result.condition}
item_type={result.item_type}
postcode={result.postcode}
postable={result.postable}
itemId={result.id}
/>
))}
{searchResults.length > 0 ? (
searchResults.map((result) => (
<ItemCard
key={result.id}
imageSrc={result.imageSrc}
item_name={result.item_name}
condition={result.condition}
item_type={result.item_type}
postcode={result.postcode}
postable={result.postable}
itemId={result.id}
/>
))
) : (
<p className='text-center'>No results found.</p>
)}
</div>
</div>
);
Expand Down

0 comments on commit 9af8704

Please sign in to comment.