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' ;
42import { postAddMachineryToShop } from '../../../../apis/calendar.api' ;
53import { useQueryClient } from 'react-query' ;
64import { Machinery } from 'shared' ;
7- import MachineryFormModal from './MachineryFormModal' ;
8- import { MachineryFormValues } from './MachineryFormModal' ;
5+ import MachineryFormModal , { MachineryFormValues } from './MachineryFormModal' ;
96
107interface EditMachineryModalProps {
118 open : boolean ;
@@ -14,38 +11,21 @@ interface EditMachineryModalProps {
1411}
1512
1613const 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