removed some unnecessary ui elements
This commit is contained in:
@@ -19,14 +19,13 @@ import EmptyCart from "@/features/cart/components/EmptyCart";
|
||||
import ErrorPage from "@/components/ErrorPage";
|
||||
|
||||
export default function CartPage() {
|
||||
const [isClient, setIsClient] = useState(false);
|
||||
const [paymentType, setPaymentType] = useState<PaymentType | null>(null);
|
||||
const [deliveryType, setDeliveryType] =
|
||||
useState<DeliveryType>("SELECTED_DELIVERY");
|
||||
const [selectedRegion, setSelectedRegion] = useState<string>("");
|
||||
const [selectedProvince, setSelectedProvince] = useState<number | null>(null);
|
||||
const [note, setNote] = useState<string>("");
|
||||
const [phone, setPhone] = useState<string>("+993 ");
|
||||
const [phone, setPhone] = useState<string>("+993 ");
|
||||
const [name, setName] = useState<string>("");
|
||||
const [lastName, setLastName] = useState<string>("");
|
||||
const router = useRouter();
|
||||
@@ -42,38 +41,52 @@ export default function CartPage() {
|
||||
const isLoading = cartLoading || provincesLoading || paymentTypesLoading;
|
||||
|
||||
useEffect(() => {
|
||||
setIsClient(true);
|
||||
}, []);
|
||||
if (paymentTypes.length > 0) {
|
||||
const defaultType = paymentTypes.find((t) => t.id === 1);
|
||||
if (defaultType) {
|
||||
setPaymentType(defaultType);
|
||||
}
|
||||
}
|
||||
}, [paymentTypes]);
|
||||
|
||||
const regionGroups = useMemo(() => {
|
||||
return provinces.reduce((acc, province) => {
|
||||
if (!acc[province.region]) {
|
||||
acc[province.region] = [];
|
||||
}
|
||||
acc[province.region].push(province);
|
||||
return acc;
|
||||
}, {} as Record<string, typeof provinces>);
|
||||
return provinces.reduce(
|
||||
(acc, province) => {
|
||||
if (!acc[province.region]) {
|
||||
acc[province.region] = [];
|
||||
}
|
||||
acc[province.region].push(province);
|
||||
return acc;
|
||||
},
|
||||
{} as Record<string, typeof provinces>,
|
||||
);
|
||||
}, [provinces]);
|
||||
|
||||
const availableRegions = useMemo(
|
||||
() => Object.keys(regionGroups),
|
||||
[regionGroups]
|
||||
[regionGroups],
|
||||
);
|
||||
|
||||
const itemsBySeller = useMemo(() => {
|
||||
return cartItems.reduce((acc, item) => {
|
||||
const sellerId = item.product.channel?.[0]?.id || 0;
|
||||
const sellerName = item.product.channel?.[0]?.name || "Unknown Seller";
|
||||
return cartItems.reduce(
|
||||
(acc, item) => {
|
||||
const sellerId = item.product.channel?.[0]?.id || 0;
|
||||
const sellerName = item.product.channel?.[0]?.name || "Unknown Seller";
|
||||
|
||||
if (!acc[sellerId]) {
|
||||
acc[sellerId] = {
|
||||
seller: { id: sellerId, name: sellerName },
|
||||
items: [],
|
||||
};
|
||||
}
|
||||
acc[sellerId].items.push(item);
|
||||
return acc;
|
||||
}, {} as Record<number, { seller: { id: number; name: string }; items: typeof cartItems }>);
|
||||
if (!acc[sellerId]) {
|
||||
acc[sellerId] = {
|
||||
seller: { id: sellerId, name: sellerName },
|
||||
items: [],
|
||||
};
|
||||
}
|
||||
acc[sellerId].items.push(item);
|
||||
return acc;
|
||||
},
|
||||
{} as Record<
|
||||
number,
|
||||
{ seller: { id: number; name: string }; items: typeof cartItems }
|
||||
>,
|
||||
);
|
||||
}, [cartItems]);
|
||||
|
||||
const totalAmount = useMemo(() => {
|
||||
@@ -88,46 +101,50 @@ export default function CartPage() {
|
||||
setSelectedProvince(null);
|
||||
};
|
||||
|
||||
|
||||
const formatPhoneForBackend = (phoneNumber: string): string => {
|
||||
|
||||
return phoneNumber.replace(/^\+993\s*/, "").replace(/\s+/g, "");
|
||||
};
|
||||
|
||||
const handleCompleteOrder = () => {
|
||||
if (!selectedRegion || !selectedProvince || !paymentType || !phone || !name) {
|
||||
console.warn("Missing required fields for order");
|
||||
return;
|
||||
}
|
||||
|
||||
const phoneDigits = formatPhoneForBackend(phone);
|
||||
if (phoneDigits.length !== 8) {
|
||||
console.warn("Phone number must be exactly 8 digits");
|
||||
return;
|
||||
}
|
||||
|
||||
const selectedProvinceData = provinces.find((p) => p.id === selectedProvince);
|
||||
if (!selectedProvinceData) return;
|
||||
|
||||
createOrder(
|
||||
{
|
||||
customer_name: `${name} ${lastName}`.trim(),
|
||||
customer_phone: parseInt(phoneDigits, 10),
|
||||
customer_address: selectedProvinceData.name,
|
||||
shipping_method: "standart",
|
||||
payment_type_id: paymentType.id,
|
||||
region: selectedRegion,
|
||||
note: note || undefined,
|
||||
},
|
||||
{
|
||||
onSuccess: () => {
|
||||
router.push(`/orders`);
|
||||
},
|
||||
if (
|
||||
!selectedRegion ||
|
||||
!selectedProvince ||
|
||||
!paymentType ||
|
||||
!phone ||
|
||||
!name
|
||||
) {
|
||||
console.warn("Missing required fields for order");
|
||||
return;
|
||||
}
|
||||
);
|
||||
};
|
||||
|
||||
if (!isClient) return null;
|
||||
const phoneDigits = formatPhoneForBackend(phone);
|
||||
if (phoneDigits.length !== 8) {
|
||||
console.warn("Phone number must be exactly 8 digits");
|
||||
return;
|
||||
}
|
||||
|
||||
const selectedProvinceData = provinces.find(
|
||||
(p) => p.id === selectedProvince,
|
||||
);
|
||||
if (!selectedProvinceData) return;
|
||||
|
||||
createOrder(
|
||||
{
|
||||
customer_name: `${name} ${lastName}`.trim(),
|
||||
customer_phone: parseInt(phoneDigits, 10),
|
||||
customer_address: selectedProvinceData.name,
|
||||
shipping_method: "standart",
|
||||
payment_type_id: paymentType.id,
|
||||
region: selectedRegion,
|
||||
note: note || undefined,
|
||||
},
|
||||
{
|
||||
onSuccess: () => {
|
||||
router.push(`/orders`);
|
||||
},
|
||||
},
|
||||
);
|
||||
};
|
||||
|
||||
if (isLoading) {
|
||||
return (
|
||||
@@ -151,8 +168,8 @@ export default function CartPage() {
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
if (isError ) {
|
||||
|
||||
if (isError) {
|
||||
return <ErrorPage />;
|
||||
}
|
||||
if (cartItems.length === 0) {
|
||||
@@ -171,12 +188,10 @@ export default function CartPage() {
|
||||
{Object.entries(itemsBySeller).map(
|
||||
([sellerId, { seller, items }]) => (
|
||||
<div key={sellerId} className="mb-6">
|
||||
<p className="text-base font-semibold mb-3">{seller.name}</p>
|
||||
{/* <p className="text-base font-semibold mb-3">{seller.name}</p> */}
|
||||
<div className="space-y-4">
|
||||
{items.map((item) => {
|
||||
const price = parseFloat(
|
||||
item.product.price_amount || "0"
|
||||
);
|
||||
const price = parseInt(item.product.price_amount || "0");
|
||||
const quantity = item.product_quantity;
|
||||
const total = price * quantity;
|
||||
|
||||
@@ -200,7 +215,7 @@ export default function CartPage() {
|
||||
item.product.media?.[0]?.thumbnail,
|
||||
images:
|
||||
item.product.media?.map(
|
||||
(m) => m.images_800x800 || m.thumbnail
|
||||
(m) => m.images_800x800 || m.thumbnail,
|
||||
) || [],
|
||||
},
|
||||
}}
|
||||
@@ -212,7 +227,7 @@ export default function CartPage() {
|
||||
<Separator className="mt-4" />
|
||||
)}
|
||||
</div>
|
||||
)
|
||||
),
|
||||
)}
|
||||
</Card>
|
||||
</div>
|
||||
@@ -258,4 +273,4 @@ export default function CartPage() {
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -80,8 +80,9 @@ export default function FavoritesPage() {
|
||||
price_color="#0059ff"
|
||||
height={360}
|
||||
width={250}
|
||||
button={false}
|
||||
button={true}
|
||||
stock={product.stock}
|
||||
|
||||
/>
|
||||
);
|
||||
})}
|
||||
|
||||
@@ -11,12 +11,12 @@ export async function generateMetadata({ params }: Props): Promise<Metadata> {
|
||||
const { locale, slug } = await params;
|
||||
|
||||
return {
|
||||
title: `Product ${slug} | E-Commerce`,
|
||||
title: `Product ${slug} | Smart-Electronics`,
|
||||
description: `View details for product ${slug}`,
|
||||
openGraph: {
|
||||
locale,
|
||||
type: "website",
|
||||
title: `Product ${slug} | E-Commerce`,
|
||||
title: `Product ${slug} | Smart-Electronics`,
|
||||
description: `View details for product ${slug}`,
|
||||
},
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user