|
| 1 | +import { useCallback, useState } from 'react'; |
| 2 | +import { |
| 3 | + useEditKeyValuesOfQuayMutation, |
| 4 | + useGetStopPointsByQuayIdLazyQuery, |
| 5 | + useRemoveStopMutation, |
| 6 | +} from '../../../../../generated/graphql'; |
| 7 | +import { StopWithDetails } from '../../../../../types'; |
| 8 | +import { |
| 9 | + removeMirroredBy, |
| 10 | + stripKeyValueTypenames, |
| 11 | +} from '../../../../../utils/stop-registry/mirrorRelation'; |
| 12 | +import { useDeleteQuay } from '../../queries/useDeleteQuay'; |
| 13 | + |
| 14 | +type RemoveMirrorRelationParams = { |
| 15 | + readonly parentStop: StopWithDetails; |
| 16 | + readonly childQuayId: string; |
| 17 | + readonly childStopPlaceId: string; |
| 18 | +}; |
| 19 | + |
| 20 | +export function useRemoveMirrorRelation() { |
| 21 | + const [loading, setLoading] = useState(false); |
| 22 | + |
| 23 | + const [editKeyValuesOfQuay] = useEditKeyValuesOfQuayMutation({ |
| 24 | + refetchQueries: ['GetStopDetails'], |
| 25 | + awaitRefetchQueries: true, |
| 26 | + }); |
| 27 | + |
| 28 | + const [getStopPointsByQuayId] = useGetStopPointsByQuayIdLazyQuery(); |
| 29 | + const [removeStopMutation] = useRemoveStopMutation(); |
| 30 | + const deleteQuay = useDeleteQuay(); |
| 31 | + |
| 32 | + const removeMirrorRelation = useCallback( |
| 33 | + async ({ |
| 34 | + parentStop, |
| 35 | + childQuayId, |
| 36 | + childStopPlaceId, |
| 37 | + }: RemoveMirrorRelationParams) => { |
| 38 | + const parentQuayId = parentStop.quay?.id; |
| 39 | + const parentStopPlaceId = parentStop.stop_place?.id; |
| 40 | + if (!parentQuayId || !parentStopPlaceId) { |
| 41 | + return false; |
| 42 | + } |
| 43 | + |
| 44 | + setLoading(true); |
| 45 | + try { |
| 46 | + // 1. Remove the child's SSP (scheduled stop point) |
| 47 | + const sspResult = await getStopPointsByQuayId({ |
| 48 | + variables: { quayIds: [childQuayId] }, |
| 49 | + }); |
| 50 | + const childStopPoints = |
| 51 | + sspResult.data?.service_pattern_scheduled_stop_point ?? []; |
| 52 | + await Promise.all( |
| 53 | + childStopPoints.map((ssp) => |
| 54 | + removeStopMutation({ |
| 55 | + variables: { stop_id: ssp.scheduled_stop_point_id }, |
| 56 | + }), |
| 57 | + ), |
| 58 | + ); |
| 59 | + |
| 60 | + // 2. Delete the child quay from Tiamat |
| 61 | + await deleteQuay(childStopPlaceId, childQuayId); |
| 62 | + |
| 63 | + // 3. Remove child from parent's mirroredBy list |
| 64 | + const updatedParentKeyValues = removeMirroredBy( |
| 65 | + parentStop.quay?.keyValues ?? undefined, |
| 66 | + childQuayId, |
| 67 | + ); |
| 68 | + |
| 69 | + await editKeyValuesOfQuay({ |
| 70 | + variables: { |
| 71 | + stopId: parentStopPlaceId, |
| 72 | + quayId: parentQuayId, |
| 73 | + keyValues: stripKeyValueTypenames(updatedParentKeyValues), |
| 74 | + versionComment: 'Yhteiskäyttöpysäkin poisto', |
| 75 | + }, |
| 76 | + }); |
| 77 | + |
| 78 | + return true; |
| 79 | + } finally { |
| 80 | + setLoading(false); |
| 81 | + } |
| 82 | + }, |
| 83 | + [ |
| 84 | + editKeyValuesOfQuay, |
| 85 | + getStopPointsByQuayId, |
| 86 | + removeStopMutation, |
| 87 | + deleteQuay, |
| 88 | + ], |
| 89 | + ); |
| 90 | + |
| 91 | + return { removeMirrorRelation, loading }; |
| 92 | +} |
0 commit comments