Skip to content

Commit 9f7a08b

Browse files
committed
refactor: filter page
1 parent 5e6d430 commit 9f7a08b

File tree

14 files changed

+344
-403
lines changed

14 files changed

+344
-403
lines changed

package-lock.json

Lines changed: 47 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
"date-fns": "^3.6.0",
2828
"formik": "^2.4.6",
2929
"lucide-react": "^0.378.0",
30+
"query-string": "^9.0.0",
3031
"react": "^18.2.0",
3132
"react-dom": "^18.2.0",
3233
"react-hook-form": "^7.51.4",
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import React from 'react';
2+
import { Search } from 'lucide-react';
3+
4+
import { Input } from '../ui/input';
5+
import { ISearchInputProps } from './types';
6+
import { cn } from '@/lib/utils';
7+
8+
const SearchInput = React.forwardRef<HTMLDivElement, ISearchInputProps>(
9+
(props, ref) => {
10+
const { value, onChange, className, ...rest } = props;
11+
12+
return (
13+
<div ref={ref} className={cn(className, 'relative')} {...rest}>
14+
<Input
15+
value={value}
16+
placeholder="Buscar por abrigo ou endereço"
17+
className="h-12 text-md font-medium text-zinc-600 pl-10 pr-4"
18+
onChange={onChange}
19+
/>
20+
<div className="absolute inset-y-0 left-0 flex items-center pl-3">
21+
<Search name="search" size="20" className="stroke-zinc-300" />
22+
</div>
23+
</div>
24+
);
25+
}
26+
);
27+
28+
export { SearchInput };
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { SearchInput } from './SearchInput';
2+
3+
export { SearchInput };
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export interface ISearchInputProps
2+
extends React.ComponentPropsWithoutRef<'input'> {
3+
value: string;
4+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { VerifiedBadge } from './VerifiedBadge';
2+
3+
export { VerifiedBadge };

src/components/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import { DialogSelector } from './DialogSelector';
99
import { TextField } from './TextField';
1010
import { Chip } from './Chip';
1111
import { Footer } from './Footer';
12+
import { SearchInput } from './SearchInput';
13+
import { VerifiedBadge } from './VerifiedBadge';
1214

1315
export {
1416
LoadingScreen,
@@ -22,4 +24,6 @@ export {
2224
TextField,
2325
Chip,
2426
Footer,
27+
VerifiedBadge,
28+
SearchInput,
2529
};

src/hooks/useShelters/types.ts

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,3 @@ export interface IUseSheltersDataSupplyData {
2020
supply: { name: string };
2121
priority: number;
2222
}
23-
24-
export interface IUseShelterSearchParams {
25-
priority: number | undefined;
26-
supplies: string[] | undefined;
27-
supplyCategories: number[] | undefined;
28-
search: string | undefined;
29-
filterAvailableShelter: boolean;
30-
filterUnavailableShelter: boolean;
31-
waitingShelterAvailability: boolean;
32-
}

src/hooks/useShelters/useShelters.tsx

Lines changed: 14 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -13,94 +13,47 @@ const useShelters = () => {
1313
page: 1,
1414
perPage: 20,
1515
results: [],
16-
filters: {
17-
search: '',
18-
priority: undefined,
19-
supplies: [],
20-
supplyCategories: [],
21-
filterAvailableShelter: false,
22-
filterUnavailableShelter: false,
23-
waitingShelterAvailability: false
24-
}
2516
});
2617

27-
const resetSearch = () => {
28-
const params = {
29-
search: '',
30-
priority: undefined,
31-
supplies: [],
32-
supplyCategories: [],
33-
filterAvailableShelter: false,
34-
filterUnavailableShelter: false,
35-
waitingShelterAvailability: false,
36-
page: 1,
37-
perPage: 20,
38-
}
39-
search({
40-
params: params,
41-
});
42-
}
43-
44-
const search = useCallback(
18+
const refresh = useCallback(
4519
(config: AxiosRequestConfig<any> = {}, append: boolean = false) => {
4620
if (!append) setLoading(true);
4721
api
48-
.get<IServerResponse<any>>(
49-
'/shelters/search',
50-
{
51-
...config,
52-
params: {
53-
orderBy: 'prioritySum',
54-
order: 'desc',
55-
...(config.params ?? {}),
56-
},
57-
}
58-
)
22+
.get<IServerResponse<any>>('/shelters', {
23+
...config,
24+
params: {
25+
orderBy: 'prioritySum',
26+
order: 'desc',
27+
...(config.params ?? {}),
28+
},
29+
})
5930
.then(({ data }) => {
6031
if (append) {
6132
setData((prev) => ({
6233
...prev,
6334
...data.data,
6435
results: [...prev.results, ...data.data.results],
65-
filters: {
66-
search: '',
67-
priority: config.params.priority,
68-
supplies: config.params.supplies ?? [],
69-
supplyCategories: config.params.supplyCategories ?? [],
70-
filterAvailableShelter: config.params.filterAvailableShelter ?? false,
71-
filterUnavailableShelter: config.params.filterUnavailableShelter ?? false,
72-
waitingShelterAvailability: config.params.waitingShelterAvailability ?? false,
73-
}
7436
}));
7537
} else {
7638
setData((prev) => ({
7739
...prev,
7840
...data.data,
7941
results: [...data.data.results],
80-
filters: {
81-
search: config.params.search,
82-
priority: config.params.priority,
83-
supplies: config.params.supplies ?? [],
84-
supplyCategories: config.params.supplyCategories ?? [],
85-
filterAvailableShelter: config.params.filterAvailableShelter ?? false,
86-
filterUnavailableShelter: config.params.filterUnavailableShelter ?? false,
87-
waitingShelterAvailability: config.params.waitingShelterAvailability ?? false,
88-
}
89-
}));
42+
}));
9043
}
9144
})
9245
.finally(() => {
93-
if(!append) setLoading(false);
46+
if (!append) setLoading(false);
9447
});
9548
},
9649
[]
9750
);
9851

9952
useEffect(() => {
100-
resetSearch();
101-
}, []);
53+
refresh();
54+
}, [refresh]);
10255

103-
return { data, loading, search, resetSearch };
56+
return { data, loading, refresh };
10457
};
10558

10659
export { useShelters };

src/lib/utils.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,26 +58,34 @@ function getAvailabilityProps(
5858
};
5959
}
6060

61+
const priorityOptions: Record<SupplyPriority, string> = {
62+
[SupplyPriority.Urgent]: 'Necessita urgente',
63+
[SupplyPriority.Needing]: 'Precisa',
64+
[SupplyPriority.Remaining]: 'Disponível para doação',
65+
[SupplyPriority.NotNeeded]: 'Não preciso',
66+
};
67+
6168
function getSupplyPriorityProps(priority: SupplyPriority) {
69+
const label = priorityOptions[priority];
6270
switch (priority) {
6371
case SupplyPriority.NotNeeded:
6472
return {
65-
label: 'Não preciso',
73+
label,
6674
className: 'bg-gray-200',
6775
};
6876
case SupplyPriority.Remaining:
6977
return {
70-
label: 'Disponível para doação',
78+
label,
7179
className: 'bg-light-green',
7280
};
7381
case SupplyPriority.Needing:
7482
return {
75-
label: 'Precisa',
83+
label,
7684
className: 'bg-light-orange',
7785
};
7886
case SupplyPriority.Urgent:
7987
return {
80-
label: 'Precisa Urgentemente',
88+
label,
8189
className: 'bg-light-red',
8290
};
8391
}
@@ -114,4 +122,5 @@ export {
114122
variantStatusPriority,
115123
colorStatusPriority,
116124
nameStatusPriority,
125+
priorityOptions,
117126
};

0 commit comments

Comments
 (0)