Skip to content

Commit ca6a785

Browse files
committed
feat: add loading to the search dialog
1 parent 92ea60f commit ca6a785

File tree

4 files changed

+41
-9
lines changed

4 files changed

+41
-9
lines changed

src/app/[locale]/(main)/(homepage)/components/Header.tsx

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@ import { SearchOutlined } from '@mui/icons-material';
55
import { Box, Stack, Typography } from '@mui/material';
66
import { grey } from '@mui/material/colors';
77
import { useTranslations } from 'next-intl';
8-
import { useState } from 'react';
8+
import { useState, useTransition } from 'react';
99
import SearchDialog from './SearchDialog';
1010
import SearchSection from './SearchSection';
1111

1212
const Header = () => {
13+
const [isPending, startTransition] = useTransition();
14+
1315
const { navigate, q } = useCustomSearchParams();
1416

1517
const [open, setOpen] = useState(false);
@@ -19,8 +21,10 @@ const Header = () => {
1921
};
2022

2123
const onClickOnSearch = (q: string) => {
22-
navigate('Q', q);
23-
setOpen(false);
24+
startTransition(() => {
25+
navigate('Q', q);
26+
setOpen(false);
27+
});
2428
};
2529

2630
const t = useTranslations();
@@ -31,6 +35,7 @@ const Header = () => {
3135
<SearchSection
3236
onClickOnBack={handleToggleDialog}
3337
onClickOnSearch={onClickOnSearch}
38+
isPending={isPending}
3439
/>
3540
</SearchDialog>
3641
<Stack direction="row" spacing={1} alignItems="center" height={56}>

src/app/[locale]/(main)/(homepage)/components/MainCategories.tsx

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
'use client';
22

3+
import Image from '@/components/common/Image';
4+
import { useAppContext } from '@/hooks/useAppContext';
35
import { ChevronLeft } from '@mui/icons-material';
46
import { Box, Grid, Link, Stack } from '@mui/material';
57
import { grey } from '@mui/material/colors';
6-
import React, { FC } from 'react';
7-
import Image from '@/components/common/Image';
8+
import { FC } from 'react';
89

910
export interface IMainCategory {
1011
id: number | string;
@@ -15,19 +16,20 @@ export interface MainCategoriesProps {
1516
items: IMainCategory[];
1617
}
1718
const MainCategories: FC<MainCategoriesProps> = ({ items }) => {
19+
const { isMobile } = useAppContext();
1820
return (
1921
<Grid container spacing={2}>
2022
{items.map((item) => {
2123
const params = new URLSearchParams();
2224
params.set('categoryId', item.id.toString());
2325

2426
return (
25-
<Grid key={item.id} item xs={12} md={3}>
27+
<Grid key={item.id} item xs={6} md={3}>
2628
<Link href={`/search?${params.toString()}`}>
2729
<Stack spacing={1} alignItems="end">
2830
<Box
2931
width="100%"
30-
height={210}
32+
height={isMobile ? 150 : 210}
3133
sx={{
3234
bgcolor: grey[100],
3335
borderRadius: 2,

src/app/[locale]/(main)/(homepage)/components/SearchSection.tsx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import IconButtonWithLoading from '@/components/common/IconButtonWithLoading';
12
import useInputFiller from '@/hooks/useInputFiller';
23
import { Locale, languages } from '@/navigation';
34
import {
@@ -12,11 +13,13 @@ import { DOMAttributes, FC } from 'react';
1213
export interface SearchSectionProps {
1314
onClickOnBack?: IconButtonProps['onClick'];
1415
onClickOnSearch?: (q: string) => void;
16+
isPending?: boolean;
1517
}
1618

1719
const SearchSection: FC<SearchSectionProps> = ({
1820
onClickOnBack,
1921
onClickOnSearch,
22+
isPending,
2023
}) => {
2124
const { inputRef } = useInputFiller();
2225

@@ -47,9 +50,9 @@ const SearchSection: FC<SearchSectionProps> = ({
4750
</IconButton>
4851
),
4952
endAdornment: (
50-
<IconButton type="submit">
53+
<IconButtonWithLoading isLoading={isPending} type="submit">
5154
<SearchOutlined />
52-
</IconButton>
55+
</IconButtonWithLoading>
5356
),
5457
}}
5558
fullWidth
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { CircularProgress, IconButton, IconButtonProps } from '@mui/material';
2+
import { FC } from 'react';
3+
4+
export interface IconButtonWithLoadingProps extends IconButtonProps {
5+
isLoading?: boolean;
6+
}
7+
const IconButtonWithLoading: FC<IconButtonWithLoadingProps> = ({
8+
isLoading,
9+
...props
10+
}) => {
11+
return (
12+
<IconButton {...props} disabled={isLoading || props.disabled}>
13+
{isLoading ? (
14+
<CircularProgress color="inherit" size={20} />
15+
) : (
16+
props.children
17+
)}
18+
</IconButton>
19+
);
20+
};
21+
22+
export default IconButtonWithLoading;

0 commit comments

Comments
 (0)