Skip to content

fix: 가게 상세 페이지 조회시 항상 count 0 처리, 장바구니 비어있을시 이동 제어 #97

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Nov 27, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 41 additions & 35 deletions src/screens/MarketDetailScreen/MarketDetailPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -114,41 +114,6 @@ const MarketDetailPage = ({
{} as Record<string, ProductType[]>,
);

const handleCheckout = async (marketId: number, cartItems: CartItem[]) => {
try {
const bucketProducts = cartItems.map(cartItem => {
const productDetails = products.find(
(product): product is ProductType =>
product.id === cartItem.productId,
);

if (!productDetails) {
throw new Error(`타입 방지 위한 error`);
}

return {
count: cartItem.count,
id: productDetails.id,
name: productDetails.name,
image: productDetails.image,
originPrice: productDetails.originPrice,
discountPrice: productDetails.discountPrice,
};
});

const bucketPostValidate = await addToBucket(marketId, bucketProducts);
if (bucketPostValidate) {
navigation.navigate('CartRoot', {
screen: 'Cart',
});
} else {
console.log('add to bucket failed');
}
} catch (error) {
console.error('Error in handleCheckout:', error);
}
};

const scrollToSection = useCallback(
(tag: string) => {
if (scrollViewRef.current && sectionOffsets[tag] !== undefined) {
Expand Down Expand Up @@ -251,10 +216,49 @@ const MarketDetailPage = ({
const handleSubscribe = () => {
setMarketIsLiked(prevState => !prevState);
};

const handleCheckout = async (marketId: number, cartItems: CartItem[]) => {
try {
const bucketProducts = cartItems.map(cartItem => {
const productDetails = products.find(
(product): product is ProductType =>
product.id === cartItem.productId,
);

if (!productDetails) {
throw new Error(`타입 방지 위한 error`);
}

return {
count: cartItem.count,
id: productDetails.id,
name: productDetails.name,
image: productDetails.image,
originPrice: productDetails.originPrice,
discountPrice: productDetails.discountPrice,
};
});

const bucketPostValidate = await addToBucket(marketId, bucketProducts);
if (bucketPostValidate) {
navigation.navigate('CartRoot', {
screen: 'Cart',
});
} else {
console.log('add to bucket failed');
}
} catch (error) {
console.error('Error in handleCheckout:', error);
}
};
const addProductToBucket = async (
marketId: number,
addProducts: CartItem[],
) => {
if (addProducts.length === 0) {
Alert.alert('장바구니가 비어있습니다.');
return;
}
if (!(await validateBucket(marketId))) {
Alert.alert(
'기존에 담아두었던 장바구니가 존재합니다.',
Expand All @@ -264,6 +268,7 @@ const MarketDetailPage = ({
text: '예',
onPress: () => {
handleCheckout(marketId, addProducts);
setCart([]);
},
},
{
Expand All @@ -278,6 +283,7 @@ const MarketDetailPage = ({
return;
}
handleCheckout(marketId, addProducts);
setCart([]);
};

const caculateRemainingPickupTime = () => {
Expand Down
Loading