Skip to content

Commit 4644423

Browse files
committed
Multiple machineries on single shop UI
1 parent 5cab654 commit 4644423

4 files changed

Lines changed: 175 additions & 136 deletions

File tree

src/frontend/src/pages/AdminToolsPage/ScheduleConfig/AdminToolsScheduleConfig.tsx

Lines changed: 5 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ const AdminToolsScheduleConfig: React.FC = () => {
115115

116116
const [openCreate, setOpenCreate] = useState(false);
117117
const [openCreateMachinery, setOpenCreateMachinery] = useState(false);
118-
const [editMachinery, setEditMachinery] = useState<{ machineryId: string; shopId: string } | null>(null);
118+
const [editMachineryId, setEditMachineryId] = useState<string | null>(null);
119119
const [openEdit, setOpenEdit] = useState(false);
120120
const [editingShop, setEditingShop] = useState<Shop | null>(null);
121121
const [shopToDelete, setShopToDelete] = useState<Shop | undefined>(undefined);
@@ -416,15 +416,7 @@ const AdminToolsScheduleConfig: React.FC = () => {
416416
<Box display="flex" gap={1} justifyContent="center">
417417
<Tooltip title="Edit" arrow>
418418
<span>
419-
<IconButton
420-
size="small"
421-
onClick={() =>
422-
setEditMachinery({
423-
machineryId: machine.machineryId,
424-
shopId: shopMachinery.shop.shopId
425-
})
426-
}
427-
>
419+
<IconButton size="small" onClick={() => setEditMachineryId(machine.machineryId)}>
428420
<EditIcon fontSize="small" />
429421
</IconButton>
430422
</span>
@@ -500,24 +492,13 @@ const AdminToolsScheduleConfig: React.FC = () => {
500492
<CreateMachineryModal open={openCreateMachinery} onClose={() => setOpenCreateMachinery(false)} />
501493

502494
{/* Edit Machine Modal */}
503-
{editMachinery &&
495+
{editMachineryId &&
504496
machines &&
505497
(() => {
506-
const selectedMachine = machines.find((m) => m.machineryId === editMachinery.machineryId);
498+
const selectedMachine = machines.find((m) => m.machineryId === editMachineryId);
507499
if (!selectedMachine) return null;
508500

509-
const selectedShopMachinery = selectedMachine.shops?.find(
510-
(sm: { shop: { shopId: string } }) => sm.shop.shopId === editMachinery.shopId
511-
);
512-
513-
if (!selectedShopMachinery) return null;
514-
515-
const machineryForEdit: typeof selectedMachine = {
516-
...selectedMachine,
517-
shops: [selectedShopMachinery]
518-
};
519-
520-
return <EditMachineryModal open={true} onClose={() => setEditMachinery(null)} machinery={machineryForEdit} />;
501+
return <EditMachineryModal open={true} onClose={() => setEditMachineryId(null)} machinery={selectedMachine} />;
521502
})()}
522503

523504
{/* Edit Shop Modal */}

src/frontend/src/pages/AdminToolsPage/ScheduleConfig/Machinery/CreateMachineryModal.tsx

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,30 @@
1-
import ErrorPage from '../../../ErrorPage';
2-
import LoadingIndicator from '../../../../components/LoadingIndicator';
31
import { useCreateMachinery, MACHINERY_KEY } from '../../../../hooks/calendar.hooks';
42
import { postAddMachineryToShop } from '../../../../apis/calendar.api';
53
import { useQueryClient } from 'react-query';
6-
import MachineryFormModal from './MachineryFormModal';
4+
import MachineryFormModal, { MachineryFormValues } from './MachineryFormModal';
75

86
interface CreateMachineryModalProps {
97
open: boolean;
108
onClose: () => void;
119
}
1210

1311
const CreateMachineryModal = ({ open, onClose }: CreateMachineryModalProps) => {
14-
const { isLoading, isError, error, mutateAsync: createMachinery } = useCreateMachinery();
12+
const { mutateAsync: createMachinery } = useCreateMachinery();
1513
const queryClient = useQueryClient();
1614

17-
if (isError) return <ErrorPage message={error?.message} />;
18-
if (isLoading) return <LoadingIndicator />;
19-
20-
const onSubmit = async (data: { shopId: string; machineName: string; quantity: number }) => {
21-
const { machineName, shopId, quantity } = data;
15+
const onSubmit = async (data: MachineryFormValues) => {
16+
const { machineName, shopEntries } = data;
2217
// First create the machinery
2318
const createdMachinery = await createMachinery({ machineName });
24-
// Then add it to the shop
25-
const result = await postAddMachineryToShop({
26-
machineryId: createdMachinery.machineryId,
27-
shopId,
28-
quantity
29-
});
19+
// Then add it to each shop
20+
let result = createdMachinery;
21+
for (const entry of shopEntries) {
22+
result = await postAddMachineryToShop({
23+
machineryId: createdMachinery.machineryId,
24+
shopId: entry.shopId,
25+
quantity: entry.quantity
26+
});
27+
}
3028
// Invalidate and refetch to ensure UI updates immediately
3129
await queryClient.invalidateQueries(MACHINERY_KEY);
3230
await queryClient.refetchQueries(MACHINERY_KEY);

src/frontend/src/pages/AdminToolsPage/ScheduleConfig/Machinery/EditMachineryModal.tsx

Lines changed: 49 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
1-
import ErrorPage from '../../../ErrorPage';
2-
import LoadingIndicator from '../../../../components/LoadingIndicator';
3-
import { useEditMachinery, useAddMachineryToShop, MACHINERY_KEY } from '../../../../hooks/calendar.hooks';
1+
import { useEditMachinery, MACHINERY_KEY } from '../../../../hooks/calendar.hooks';
42
import { postAddMachineryToShop } from '../../../../apis/calendar.api';
53
import { useQueryClient } from 'react-query';
64
import { Machinery } from 'shared';
7-
import MachineryFormModal from './MachineryFormModal';
8-
import { MachineryFormValues } from './MachineryFormModal';
5+
import MachineryFormModal, { MachineryFormValues } from './MachineryFormModal';
96

107
interface EditMachineryModalProps {
118
open: boolean;
@@ -14,38 +11,21 @@ interface EditMachineryModalProps {
1411
}
1512

1613
const EditMachineryModal = ({ open, onClose, machinery }: EditMachineryModalProps) => {
17-
const shopMachinery = machinery.shops?.[0];
18-
const originalShopId = shopMachinery?.shop?.shopId || '';
1914
const queryClient = useQueryClient();
15+
const { mutateAsync: editMachinery } = useEditMachinery(machinery.machineryId);
2016

21-
const {
22-
isLoading: isEditing,
23-
isError: isEditError,
24-
error: editError,
25-
mutateAsync: editMachinery
26-
} = useEditMachinery(machinery.machineryId);
27-
const {
28-
isLoading: isAdding,
29-
isError: isAddError,
30-
error: addError,
31-
mutateAsync: addMachineryToShop
32-
} = useAddMachineryToShop(machinery.machineryId);
33-
34-
const isLoading = isEditing || isAdding;
35-
const isError = isEditError || isAddError;
36-
const error = editError || addError;
17+
const originalShops = machinery.shops || [];
3718

3819
const machineryData: MachineryFormValues = {
39-
shopId: originalShopId,
4020
machineName: machinery.name,
41-
quantity: shopMachinery?.quantity || 1
21+
shopEntries:
22+
originalShops.length > 0
23+
? originalShops.map((sm) => ({ shopId: sm.shop.shopId, quantity: sm.quantity }))
24+
: [{ shopId: '', quantity: 1 }]
4225
};
4326

44-
if (isError) return <ErrorPage message={error?.message} />;
45-
if (isLoading) return <LoadingIndicator />;
46-
47-
const onSubmit = async (data: { shopId: string; machineName: string; quantity: number }) => {
48-
const { machineName, shopId, quantity } = data;
27+
const onSubmit = async (data: MachineryFormValues) => {
28+
const { machineName, shopEntries } = data;
4929
let currentMachineryId = machinery.machineryId;
5030

5131
// Check if name changed - this may merge with another machinery
@@ -54,25 +34,47 @@ const EditMachineryModal = ({ open, onClose, machinery }: EditMachineryModalProp
5434
currentMachineryId = updatedMachinery.machineryId;
5535
}
5636

57-
// Update shop/quantity relationship using the current machinery ID
58-
if (currentMachineryId !== machinery.machineryId) {
59-
// Use the API directly since the machinery ID changed after merge
60-
const result = await postAddMachineryToShop({
61-
machineryId: currentMachineryId,
62-
shopId,
63-
quantity,
64-
originalShopId: originalShopId || undefined
65-
});
66-
queryClient.invalidateQueries(MACHINERY_KEY);
67-
return result;
37+
const originalShopIds = new Set(originalShops.map((sm) => sm.shop.shopId));
38+
const newShopIds = new Set(shopEntries.map((e) => e.shopId));
39+
40+
// Remove shops that were removed from the form
41+
for (const sm of originalShops) {
42+
if (!newShopIds.has(sm.shop.shopId)) {
43+
await postAddMachineryToShop({
44+
machineryId: currentMachineryId,
45+
shopId: sm.shop.shopId,
46+
quantity: 0
47+
});
48+
}
49+
}
50+
51+
// Add or update shop entries
52+
for (const entry of shopEntries) {
53+
if (!entry.shopId) continue;
54+
const original = originalShops.find((sm) => sm.shop.shopId === entry.shopId);
55+
if (original) {
56+
// Existing shop - update if quantity changed
57+
if (original.quantity !== entry.quantity) {
58+
await postAddMachineryToShop({
59+
machineryId: currentMachineryId,
60+
shopId: entry.shopId,
61+
quantity: entry.quantity,
62+
originalShopId: entry.shopId
63+
});
64+
}
65+
} else {
66+
// New shop association
67+
await postAddMachineryToShop({
68+
machineryId: currentMachineryId,
69+
shopId: entry.shopId,
70+
quantity: entry.quantity
71+
});
72+
}
6873
}
6974

70-
// Same machinery ID, use the hook as normal
71-
return await addMachineryToShop({
72-
shopId,
73-
quantity,
74-
originalShopId: originalShopId || undefined
75-
});
75+
await queryClient.invalidateQueries(MACHINERY_KEY);
76+
await queryClient.refetchQueries(MACHINERY_KEY);
77+
return machinery;
7678
};
7779

7880
return <MachineryFormModal open={open} onClose={onClose} onSubmit={onSubmit} initialValues={machineryData} />;

0 commit comments

Comments
 (0)