This commit is contained in:
Samandar Turgunboyev
2026-01-24 16:46:02 +05:00
parent 1f77cc360d
commit ecc0029322
24 changed files with 632 additions and 563 deletions

View File

@@ -1,16 +1,16 @@
// store/orderStore.ts
import { CartItem } from '@/features/cart/lib/api';
import { create } from 'zustand';
import { OrderListRes } from './api';
type State = {
order: OrderListRes | null;
order: CartItem | null;
};
type Actions = {
setOrder: (order: OrderListRes) => void;
setOrder: (order: CartItem) => void;
};
const getInitialOrder = (): OrderListRes | null => {
const getInitialOrder = (): CartItem | null => {
if (typeof window === 'undefined') return null; // SSR check
const stored = localStorage.getItem('order');
if (!stored) return null;
@@ -24,7 +24,7 @@ const getInitialOrder = (): OrderListRes | null => {
const useOrderStore = create<State & Actions>((set) => ({
order: getInitialOrder(),
setOrder: (order: OrderListRes) => {
setOrder: (order: CartItem) => {
if (typeof window !== 'undefined') {
localStorage.setItem('order', JSON.stringify(order));
}

View File

@@ -18,12 +18,10 @@ import { useTranslations } from 'next-intl';
import { useSearchParams } from 'next/navigation';
import { useEffect, useState } from 'react';
import { order_api, OrderListRes } from '../lib/api';
import useOrderStore from '../lib/order';
const HistoryTabs = () => {
const t = useTranslations();
const searchParams = useSearchParams();
const { setOrder } = useOrderStore();
const [page, setPage] = useState(1);
const PAGE_SIZE = 36;
const router = useRouter();
@@ -218,7 +216,7 @@ const HistoryTabs = () => {
size="sm"
onClick={() => {
router.push('/profile/refresh-order');
setOrder(order);
// setOrder(order);
}}
className="bg-transparent gap-1 md:gap-2 text-xs md:text-sm h-8 md:h-9 px-2 md:px-3"
>

View File

@@ -73,15 +73,12 @@ const RefreshOrder = () => {
const t = useTranslations();
const queryClient = useQueryClient();
const initialCartItems = initialValues?.items.map((item) => ({
const initialCartItems = initialValues?.cart_item.map((item) => ({
id: item.id,
product_id: item.product.id,
product_name: item.product.name,
product_price: item.price,
product_image:
item.product.image ||
item.product.images?.[0]?.image ||
'/placeholder.svg',
product_price: item.product.prices[0].price,
product_image: item.product.images[0].image || '/placeholder.svg',
quantity: item.quantity,
}));
@@ -90,7 +87,7 @@ const RefreshOrder = () => {
const form = useForm<z.infer<typeof orderForm>>({
resolver: zodResolver(orderForm),
defaultValues: {
comment: initialValues?.comment,
comment: '',
lat: '41.311081',
long: '69.240562',
},
@@ -100,7 +97,7 @@ const RefreshOrder = () => {
const subtotal = cartItems
? cartItems.reduce(
(sum, item) => sum + item.product_price * item.quantity,
(sum, item) => sum + Number(item.product_price) * item.quantity,
0,
)
: 0;
@@ -114,7 +111,7 @@ const RefreshOrder = () => {
queryClient.refetchQueries({ queryKey: ['cart_items'] });
},
onError: () => {
toast.error('Xatolik yuz berdi', {
toast.error(t('Xatolik yuz berdi'), {
richColors: true,
position: 'top-center',
});
@@ -205,7 +202,7 @@ const RefreshOrder = () => {
const onSubmit = (value: z.infer<typeof orderForm>) => {
if (!deliveryDate) {
toast.error('Yetkazib berish sanasini tanlang', {
toast.error(t('Yetkazib berish sanasini tanlang'), {
richColors: true,
position: 'top-center',
});
@@ -213,7 +210,7 @@ const RefreshOrder = () => {
}
if (!selectedTimeSlot) {
toast.error('Yetkazib berish vaqtini tanlang', {
toast.error(t('Yetkazib berish vaqtini tanlang'), {
richColors: true,
position: 'top-center',
});
@@ -221,25 +218,48 @@ const RefreshOrder = () => {
}
if (initialValues === null) {
toast.error('Savatcha bosh', {
toast.error(t('Savatcha bosh'), {
richColors: true,
position: 'top-center',
});
return;
}
const items = initialValues.items.map((item) => ({
product_id: Number(item.product.id),
quantity: item.quantity,
}));
const order_products = initialValues.cart_item
.filter(
(item) =>
item.product.prices &&
item.product.prices.length > 0 &&
item.product.prices[0].price_type?.code &&
item.product.prices[0].price,
)
.map((item) => ({
inventory_kind: 'G',
product_code: item.product.code,
on_balance: 'Y',
order_quant: item.quantity,
price_type_code: item.product.prices![0].price_type.code,
product_price: item.product.prices![0].price,
warehouse_code: 'wh1',
}));
mutate({
comment: value.comment,
items: items,
long: Number(value.long),
lat: Number(value.lat),
date: formatDate.format(deliveryDate, 'YYYY-MM-DD'),
time: selectedTimeSlot,
order: [
{
filial_code: 'dodge',
delivery_date: formatDate.format(deliveryDate, 'DD.MM.YYYY'),
room_code: '100',
deal_time: formatDate.format(deliveryDate, 'DD.MM.YYYY'),
robot_code: 'r2',
status: 'B#N',
sales_manager_code: '1',
person_code: '12345678',
currency_code: '860',
owner_person_code: '1234567',
note: value.comment,
order_products: order_products,
},
],
});
};
@@ -513,7 +533,7 @@ const RefreshOrder = () => {
</p>
<p className="font-semibold text-sm">
{formatPrice(
item.product_price * item.quantity,
Number(item.product_price) * item.quantity,
true,
)}
</p>