Skip to content

Commit 71484d7

Browse files
committed
feat: added all supplies in edit shelter supply and renamed editer shelter supply page
1 parent 506d85a commit 71484d7

File tree

17 files changed

+100
-60
lines changed

17 files changed

+100
-60
lines changed

src/App.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
import { BrowserRouter } from "react-router-dom";
1+
import { BrowserRouter } from 'react-router-dom';
22

3-
import { Routes } from "./routes/Routes";
4-
import { SessionProvider } from "./contexts";
3+
import { Routes } from './routes/Routes';
4+
import { SessionProvider } from './contexts';
55

66
const App = () => {
77
return (
88
<BrowserRouter>
99
<SessionProvider>
10-
<Routes />;
10+
<Routes />
1111
</SessionProvider>
1212
</BrowserRouter>
1313
);

src/components/Chip/Chip.tsx

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,23 @@ import { cva } from 'class-variance-authority';
55
const Chip = React.forwardRef<HTMLDivElement, IChipProps>((props, ref) => {
66
const { label, className, variant = 'info', ...rest } = props;
77

8-
const variants = cva('px-4 py-1 text-nowrap font-semibold rounded-3xl', {
9-
variants: {
10-
variant: {
11-
warn: 'bg-light-yellow',
12-
success: 'bg-light-green',
13-
danger: 'bg-light-red',
14-
alert: 'bg-light-orange',
15-
info: 'bg-light-blue',
8+
const variants = cva(
9+
'px-4 py-1 text-nowrap font-normal text-sm md:text-md rounded-3xl',
10+
{
11+
variants: {
12+
variant: {
13+
warn: 'bg-light-yellow',
14+
success: 'bg-light-green',
15+
danger: 'bg-light-red',
16+
alert: 'bg-light-orange',
17+
info: 'bg-light-blue',
18+
},
1619
},
17-
},
18-
defaultVariants: {
19-
variant: 'info',
20-
},
21-
});
20+
defaultVariants: {
21+
variant: 'info',
22+
},
23+
}
24+
);
2225

2326
return (
2427
<div ref={ref} {...rest} className={variants({ className, variant })}>

src/components/DialogSelector/DialogSelector.tsx

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,7 @@ const DialogSelector = (props: IDialogSelectorProps) => {
3030
<Dialog open={open} onOpenChange={onClose}>
3131
<DialogContent className="rounded-md">
3232
<DialogHeader>
33-
<DialogTitle>
34-
<h1 className="text-base font-medium">{title}</h1>
35-
</DialogTitle>
33+
<DialogTitle className="text-base font-medium">{title}</DialogTitle>
3634
{description && <DialogDescription>{description}</DialogDescription>}
3735
</DialogHeader>
3836
<div className="px-2 py-4 max-h-[50vh] overflow-y-auto">

src/lib/utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ function getSupplyPriorityProps(priority: SupplyPriority) {
6868
case SupplyPriority.Remaining:
6969
return {
7070
label: 'Disponível para doação',
71-
className: 'bg-light-green',
71+
className: 'bg-light-green text-gray-100',
7272
};
7373
case SupplyPriority.Needing:
7474
return {

src/pages/ShelterItem/ShelterItem.tsx renamed to src/pages/EditShelterSupply/EditShelterSupply.tsx

Lines changed: 61 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,12 @@ import { ISupplyRowItemProps } from './components/SupplyRow/types';
1212
import { ShelterSupplyServices } from '@/service';
1313
import { useToast } from '@/components/ui/use-toast';
1414
import { ISupply, SupplyPriority } from '@/service/supply/types';
15+
import { IUseShelterDataSupply } from '@/hooks/useShelter/types';
1516

16-
const ShelterItem = () => {
17+
const EditShelterSupply = () => {
1718
const navigate = useNavigate();
1819
const { shelterId = '-1' } = useParams();
20+
const { toast } = useToast();
1921
const { data: shelter, loading, refresh } = useShelter(shelterId);
2022
const { data: supplies } = useSupplies();
2123
const [filteredSupplies, setFilteredSupplies] = useState<ISupply[]>([]);
@@ -41,36 +43,60 @@ const ShelterItem = () => {
4143
IDialogSelectorProps,
4244
'value' | 'onSave'
4345
> | null>();
44-
const { toast } = useToast();
45-
46+
const shelterSupplyData = useMemo(() => {
47+
return (shelter?.shelterSupplies ?? []).reduce(
48+
(prev, current) => ({ ...prev, [current.supply.id]: current }),
49+
{} as Record<string, IUseShelterDataSupply>
50+
);
51+
}, [shelter?.shelterSupplies]);
4652
const supplyGroups = useMemo(
4753
() => group<ISupply>(filteredSupplies ?? [], 'supplyCategory.name'),
4854
[filteredSupplies]
4955
);
5056

5157
const handleClickSupplyRow = useCallback(
5258
(item: ISupplyRowItemProps) => {
59+
console.log('Item: ', item);
5360
setModalOpened(true);
5461
setModalData({
55-
value: `${item.priority}`,
62+
value: `${item.priority ?? SupplyPriority.UnderControl}`,
5663
onSave: (v) => {
64+
const isNewSupply = item.priority === undefined;
5765
setLoadingSave(true);
58-
ShelterSupplyServices.update(shelterId, item.id, { priority: +v })
59-
.then(() => {
60-
setModalOpened(false);
61-
setModalData(null);
62-
refresh();
66+
67+
const successCallback = () => {
68+
setModalOpened(false);
69+
setModalData(null);
70+
refresh();
71+
};
72+
73+
const errorCallback = (err: any) => {
74+
toast({
75+
variant: 'destructive',
76+
title: 'Ocorreu um erro ao atualizar a categoria do suprimento',
77+
description: `${err}`,
78+
});
79+
};
80+
81+
if (isNewSupply) {
82+
ShelterSupplyServices.create({
83+
shelterId,
84+
supplyId: item.id,
85+
priority: +v,
6386
})
64-
.catch((err) => {
65-
toast({
66-
variant: 'destructive',
67-
title: 'Ocorreu um erro ao atualizar a categoria do suprimento',
68-
description: `${err}`,
87+
.then(successCallback)
88+
.catch(errorCallback)
89+
.finally(() => {
90+
setLoadingSave(false);
6991
});
70-
})
71-
.finally(() => {
72-
setLoadingSave(false);
73-
});
92+
} else {
93+
ShelterSupplyServices.update(shelterId, item.id, { priority: +v })
94+
.then(successCallback)
95+
.catch(errorCallback)
96+
.finally(() => {
97+
setLoadingSave(false);
98+
});
99+
}
74100
},
75101
});
76102
},
@@ -151,23 +177,29 @@ const ShelterItem = () => {
151177
/>
152178
</div>
153179
<div className="flex flex-col gap-2 w-full my-4">
154-
{Object.entries(supplyGroups).map(([key, values], idx) => (
155-
<SupplyRow
156-
key={idx}
157-
name={key}
158-
items={values.map((v) => ({
180+
{Object.entries(supplyGroups).map(([key, values], idx) => {
181+
const items: ISupplyRowItemProps[] = values.map((v) => {
182+
const supply = shelterSupplyData[v.id];
183+
return {
159184
id: v.id,
160185
name: v.name,
161-
priority: 0, //v.priority,
162-
}))}
163-
onClick={handleClickSupplyRow}
164-
/>
165-
))}
186+
priority: supply?.priority,
187+
};
188+
});
189+
return (
190+
<SupplyRow
191+
key={idx}
192+
name={key}
193+
items={items}
194+
onClick={handleClickSupplyRow}
195+
/>
196+
);
197+
})}
166198
</div>
167199
</div>
168200
</div>
169201
</Fragment>
170202
);
171203
};
172204

173-
export { ShelterItem };
205+
export { EditShelterSupply };

src/pages/ShelterItem/components/SupplyRow/SupplyRow.tsx renamed to src/pages/EditShelterSupply/components/SupplyRow/SupplyRow.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { SupplyPriority } from '@/service/supply/types';
12
import { SupplyRowInfo } from '../SupplyRowInfo';
23
import { ISupplyRowProps } from './types';
34

@@ -12,7 +13,7 @@ const SupplyRow = (props: ISupplyRowProps) => {
1213
<SupplyRowInfo
1314
key={idy}
1415
name={item.name}
15-
priority={item.priority}
16+
priority={item.priority ?? SupplyPriority.UnderControl}
1617
onClick={() => (onClick ? onClick(item) : undefined)}
1718
/>
1819
))}
File renamed without changes.

src/pages/ShelterItem/components/SupplyRow/types.ts renamed to src/pages/EditShelterSupply/components/SupplyRow/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { SupplyPriority } from '@/service/supply/types';
33
export interface ISupplyRowItemProps {
44
id: string;
55
name: string;
6-
priority: SupplyPriority;
6+
priority?: SupplyPriority;
77
}
88

99
export interface ISupplyRowProps {
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)