Skip to content

Commit 1fcf8b5

Browse files
fagundesjgmarcodmc
authored andcommitted
Hotfix/merge bugs (SOS-RS#183)
* fix: shelter list item duplicate code after merge * fix: order in filter select menu, filter supplies by category and useSupplies interface * fix: update shelter validation schema and created useAuthRoles hook to reduce complexity of code
1 parent cd5a3c7 commit 1fcf8b5

File tree

15 files changed

+289
-274
lines changed

15 files changed

+289
-274
lines changed

src/components/Authenticated/Authenticated.tsx

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,12 @@
1-
import { Fragment, useContext } from 'react';
1+
import { Fragment } from 'react';
22

3-
import { SessionContext } from '@/contexts';
43
import { IAuthenticatedProps } from './types';
5-
import { AccessLevel } from '@/service/sessions/types';
6-
7-
const MappedRoles: Record<AccessLevel, AccessLevel[]> = {
8-
Admin: ['Admin'],
9-
DistributionCenter: ['Admin', 'DistributionCenter'],
10-
Staff: ['Admin', 'Staff'],
11-
User: ['Admin', 'Staff', 'DistributionCenter', 'User'],
12-
};
4+
import { useAuthRoles } from '@/hooks';
135

146
const Authenticated = ({ children, role = 'User' }: IAuthenticatedProps) => {
15-
const { session } = useContext(SessionContext);
7+
const isAuthenticated = useAuthRoles(role);
168

17-
if (!session || !MappedRoles[role].includes(session.accessLevel))
18-
return <Fragment />;
9+
if (!isAuthenticated) return <Fragment />;
1910

2011
return <div className="contents">{children}</div>;
2112
};

src/components/DialogSelector/DialogSelector.tsx

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -52,18 +52,6 @@ const DialogSelector = (props: IDialogSelectorProps) => {
5252
Quantidade
5353
</label>
5454
<div className="flex gap-2 items-center justify-center py-2">
55-
<Button
56-
onClick={(event) => {
57-
event.preventDefault();
58-
setQuantity((prev) => {
59-
const newQuantity = Math.max(prev - 1, 0);
60-
return newQuantity;
61-
});
62-
}}
63-
className="bg-blue-700 text-white hover:bg-blue-600 active:bg-blue-500 px-8"
64-
>
65-
-
66-
</Button>
6755
<Input
6856
type="number"
6957
name="quantity"
@@ -72,18 +60,9 @@ const DialogSelector = (props: IDialogSelectorProps) => {
7260
placeholder="Quantidade"
7361
min={0}
7462
/>
75-
<Button
76-
onClick={(event) => {
77-
event.preventDefault();
78-
setQuantity((prev) => prev + 1);
79-
}}
80-
className="bg-blue-700 text-white hover:bg-blue-600 active:bg-blue-500 px-8"
81-
>
82-
+
83-
</Button>
8463
</div>
8564
</div>
86-
<div className="px-2 py-4 max-h-[50vh] overflow-y-auto">
65+
<div className="px-2 max-h-[50vh] overflow-y-auto">
8766
<RadioGroup
8867
value={selectedItem}
8968
onValueChange={(v) => setSelectedItem(v)}

src/hooks/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { useSupplies } from './useSupplies';
1010
import { useViaCep } from './useViaCep';
1111
import { usePartners } from './usePartners';
1212
import { useGithubContributors } from './useGithubContributors';
13+
import { useAuthRoles } from './useAuthRoles';
1314

1415
export {
1516
useShelters,
@@ -24,4 +25,5 @@ export {
2425
useViaCep,
2526
usePartners,
2627
useGithubContributors,
28+
useAuthRoles,
2729
};

src/hooks/useAuthRoles/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { useAuthRoles } from './useAuthRoles';
2+
3+
export { useAuthRoles };
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { useContext } from 'react';
2+
3+
import { SessionContext } from '@/contexts';
4+
import { AccessLevel } from '@/service/sessions/types';
5+
6+
const MappedRoles: Record<AccessLevel, AccessLevel[]> = {
7+
Admin: ['Admin'],
8+
DistributionCenter: ['Admin', 'DistributionCenter'],
9+
Staff: ['Admin', 'Staff'],
10+
User: ['Admin', 'Staff', 'DistributionCenter', 'User'],
11+
};
12+
13+
const useAuthRoles = (...roles: AccessLevel[]) => {
14+
const { session } = useContext(SessionContext);
15+
16+
if (
17+
!session ||
18+
!roles.some((role) => MappedRoles[role].includes(session.accessLevel))
19+
)
20+
return false;
21+
22+
return true;
23+
};
24+
25+
export { useAuthRoles };

src/hooks/useSupplies/types.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
export interface IUseSuppliesData {
2+
id: string;
3+
name: string;
4+
supplyCategory: IUseSuppliesDataCategory;
5+
createdAt: Date;
6+
updatedAt: null;
7+
}
8+
9+
export interface IUseSuppliesDataCategory {
10+
id: string;
11+
name: string;
12+
}

src/hooks/useSupplies/useSupplies.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import { ISupply } from '@/service/supply/types';
21
import { useFetch } from '../useFetch';
32
import { PaginatedQueryPath } from '../usePaginatedQuery/paths';
3+
import { IUseSuppliesData } from './types';
44

55
const useSupplies = () => {
6-
return useFetch<ISupply[]>(PaginatedQueryPath.Supplies, {
6+
return useFetch<IUseSuppliesData[]>(PaginatedQueryPath.Supplies, {
77
initialValue: [],
88
cache: true,
99
});

src/pages/EditShelterSupply/EditShelterSupply.tsx

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,20 @@ import { IDialogSelectorProps } from '@/components/DialogSelector/types';
1111
import { ISupplyRowItemProps } from './components/SupplyRow/types';
1212
import { ShelterSupplyServices } from '@/service';
1313
import { useToast } from '@/components/ui/use-toast';
14-
import { ISupply, SupplyPriority } from '@/service/supply/types';
14+
import { SupplyPriority } from '@/service/supply/types';
1515
import { IUseShelterDataSupply } from '@/hooks/useShelter/types';
1616
import { clearCache } from '@/api/cache';
17+
import { IUseSuppliesData } from '@/hooks/useSupplies/types';
1718

1819
const EditShelterSupply = () => {
1920
const navigate = useNavigate();
2021
const { shelterId = '-1' } = useParams();
2122
const { toast } = useToast();
2223
const { data: shelter, loading, refresh } = useShelter(shelterId);
2324
const { data: supplies } = useSupplies();
24-
const [filteredSupplies, setFilteredSupplies] = useState<ISupply[]>([]);
25+
const [filteredSupplies, setFilteredSupplies] = useState<IUseSuppliesData[]>(
26+
[]
27+
);
2528
const [searchValue, setSearchValue] = useState<string>('');
2629
const [, setSearch] = useThrottle<string>(
2730
{
@@ -60,7 +63,8 @@ const EditShelterSupply = () => {
6063
);
6164
}, [shelter?.shelterSupplies]);
6265
const supplyGroups = useMemo(
63-
() => group<ISupply>(filteredSupplies ?? [], 'supplyCategory.name'),
66+
() =>
67+
group<IUseSuppliesData>(filteredSupplies ?? [], 'supplyCategory.name'),
6468
[filteredSupplies]
6569
);
6670

@@ -193,15 +197,17 @@ const EditShelterSupply = () => {
193197
</div>
194198
<div className="flex flex-col gap-2 w-full my-4">
195199
{Object.entries(supplyGroups).map(([key, values], idx) => {
196-
const items: ISupplyRowItemProps[] = values.map((v) => {
197-
const supply = shelterSupplyData[v.id];
198-
return {
199-
id: v.id,
200-
name: v.name,
201-
quantity: supply?.quantity,
202-
priority: supply?.priority,
203-
};
204-
}).sort((a, b) => a.name.localeCompare(b.name));
200+
const items: ISupplyRowItemProps[] = values
201+
.map((v) => {
202+
const supply = shelterSupplyData[v.id];
203+
return {
204+
id: v.id,
205+
name: v.name,
206+
quantity: supply?.quantity,
207+
priority: supply?.priority,
208+
};
209+
})
210+
.sort((a, b) => a.name.localeCompare(b.name));
205211
return (
206212
<SupplyRow
207213
key={idx}

src/pages/EditShelterSupply/components/SupplyRowInfo/SupplyRowInfo.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ const SupplyRowInfo = (props: ISupplyRowInfoProps) => {
2222
<div className="flex justify-end items-center gap-2">
2323
<CircleStatus className={className} />
2424
<p className="text-muted-foreground text-nowrap pl-1">{label}</p>
25-
{Boolean(quantity) && <Badge className="bg-gray-700">{quantity}</Badge>}
25+
{Boolean(quantity) && <Badge variant="secondary">{quantity}</Badge>}
2626
</div>
2727
</div>
2828
);

0 commit comments

Comments
 (0)