Skip to content

Commit

Permalink
[projcet-base] Fix preselecting packetery with empty pickup point (#3…
Browse files Browse the repository at this point in the history
…663)
  • Loading branch information
JanMolcik authored Dec 18, 2024
2 parents ec5dd2d + 732eacf commit 9b6ebdf
Showing 1 changed file with 30 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ import { isPacketeryTransport, mapPacketeryExtendedPoint, packeteryPick } from '
import { StoreOrPacketeryPoint } from 'utils/packetery/types';
import { getInternationalizedStaticUrls } from 'utils/staticUrls/getInternationalizedStaticUrls';

const PICKUP_POINT_NOT_SET_ERROR_MESSAGE = 'Packetery pickup point is not set';

const PickupPlacePopup = dynamic(
() => import('components/Blocks/Popup/PickupPlacePopup').then((component) => component.PickupPlacePopup),
{
Expand Down Expand Up @@ -182,6 +184,10 @@ const getLastOrderPickupPlace = (
return getGtmPickupPlaceFromStore(lastOrderPickupPlaceFromApi);
}

if (!packeteryPickupPoint) {
throw new Error(PICKUP_POINT_NOT_SET_ERROR_MESSAGE);
}

return getGtmPickupPlaceFromLastOrder(lastOrderPickupPlaceIdentifier, lastOrder);
};

Expand Down Expand Up @@ -293,26 +299,33 @@ export const useLoadTransportAndPaymentFromLastOrder = (
>(LastOrderQueryDocument, {}, { requestPolicy: 'network-only' })
.toPromise();

const lastOrderPickupPlace = await loadLastOrderPickupPlace(lastOrderData);
try {
const lastOrderPickupPlace = await loadLastOrderPickupPlace(lastOrderData);

const newCart = await changeTransportInCart(
lastOrderData?.lastOrder?.transport.uuid ?? null,
lastOrderPickupPlace,
);
const successfullyChangedTransport = newCart?.transport?.uuid === lastOrderData?.lastOrder?.transport.uuid;
const successfullyChangedPickupPlace =
!!newCart?.selectedPickupPlaceIdentifier &&
newCart.selectedPickupPlaceIdentifier === lastOrderPickupPlace?.identifier;

if (successfullyChangedTransport) {
if (successfullyChangedPickupPlace) {
setLastOrderPickupPlace(lastOrderPickupPlace);
}
const newCart = await changeTransportInCart(
lastOrderData?.lastOrder?.transport.uuid ?? null,
lastOrderPickupPlace,
);
const successfullyChangedTransport = newCart?.transport?.uuid === lastOrderData?.lastOrder?.transport.uuid;
const successfullyChangedPickupPlace =
!!newCart?.selectedPickupPlaceIdentifier &&
newCart.selectedPickupPlaceIdentifier === lastOrderPickupPlace?.identifier;

await changePaymentInCart(lastOrderData?.lastOrder?.payment.uuid ?? null, null);
}
if (successfullyChangedTransport) {
if (successfullyChangedPickupPlace) {
setLastOrderPickupPlace(lastOrderPickupPlace);
}

setIsLoadingTransportAndPaymentFromLastOrder(false);
await changePaymentInCart(lastOrderData?.lastOrder?.payment.uuid ?? null, null);
}
} catch (e: unknown) {
const error = e as Error;
if (error.message && error.message !== PICKUP_POINT_NOT_SET_ERROR_MESSAGE) {
throw Error;
}
} finally {
setIsLoadingTransportAndPaymentFromLastOrder(false);
}
};

useEffect(() => {
Expand Down

0 comments on commit 9b6ebdf

Please sign in to comment.