error update
This commit is contained in:
@@ -64,6 +64,15 @@ interface CoordsData {
|
|||||||
polygon: [number, number][][];
|
polygon: [number, number][][];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type DRFError = {
|
||||||
|
[key: string]: Array<
|
||||||
|
| string
|
||||||
|
| {
|
||||||
|
[key: string]: string[];
|
||||||
|
}
|
||||||
|
>;
|
||||||
|
};
|
||||||
|
|
||||||
// Yetkazib berish vaqt oraliqlar
|
// Yetkazib berish vaqt oraliqlar
|
||||||
const deliveryTimeSlots = [
|
const deliveryTimeSlots = [
|
||||||
{ id: 1, label: '10:00 - 12:00', start: '10:00', end: '12:00' },
|
{ id: 1, label: '10:00 - 12:00', start: '10:00', end: '12:00' },
|
||||||
@@ -119,14 +128,38 @@ const OrderPage = () => {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
onError: (error: AxiosError) => {
|
onError: (error: AxiosError<DRFError>) => {
|
||||||
const errors = (error.response?.data as { data: { message: string } })
|
const data = error.response?.data;
|
||||||
.data.message;
|
|
||||||
const errorsDetail = (
|
|
||||||
error.response?.data as { data: { detail: string } }
|
|
||||||
).data.detail;
|
|
||||||
|
|
||||||
toast.error(errors || errorsDetail || t('Xatolik yuz berdi'), {
|
let message = t('Xatolik yuz berdi');
|
||||||
|
|
||||||
|
if (data) {
|
||||||
|
// 🔥 DRF validation error parsing
|
||||||
|
if (typeof data === 'object') {
|
||||||
|
const firstKey = Object.keys(data)[0]; // "order"
|
||||||
|
const firstValue = data[firstKey];
|
||||||
|
|
||||||
|
if (Array.isArray(firstValue)) {
|
||||||
|
const firstErrorObj = firstValue[0]; // { note: [...] }
|
||||||
|
|
||||||
|
if (typeof firstErrorObj === 'object') {
|
||||||
|
const innerKey = Object.keys(firstErrorObj)[0]; // "note"
|
||||||
|
const innerValue = firstErrorObj[innerKey];
|
||||||
|
|
||||||
|
if (Array.isArray(innerValue)) {
|
||||||
|
message = innerValue[0]; // "This field may not be blank."
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// fallback
|
||||||
|
if (!error.response) {
|
||||||
|
message = error.message;
|
||||||
|
}
|
||||||
|
|
||||||
|
toast.error(message, {
|
||||||
richColors: true,
|
richColors: true,
|
||||||
position: 'top-center',
|
position: 'top-center',
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -36,6 +36,7 @@ import {
|
|||||||
ZoomControl,
|
ZoomControl,
|
||||||
} from '@pbe/react-yandex-maps';
|
} from '@pbe/react-yandex-maps';
|
||||||
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query';
|
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query';
|
||||||
|
import { AxiosError } from 'axios';
|
||||||
import {
|
import {
|
||||||
Calendar as CalIcon,
|
Calendar as CalIcon,
|
||||||
CheckCircle2,
|
CheckCircle2,
|
||||||
@@ -72,6 +73,15 @@ interface CoordsData {
|
|||||||
polygon: [number, number][][];
|
polygon: [number, number][][];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type DRFError = {
|
||||||
|
[key: string]: Array<
|
||||||
|
| string
|
||||||
|
| {
|
||||||
|
[key: string]: string[];
|
||||||
|
}
|
||||||
|
>;
|
||||||
|
};
|
||||||
|
|
||||||
const RefreshOrder = () => {
|
const RefreshOrder = () => {
|
||||||
const [deliveryDate, setDeliveryDate] = useState<Date>();
|
const [deliveryDate, setDeliveryDate] = useState<Date>();
|
||||||
const [selectedTimeSlot, setSelectedTimeSlot] = useState<string>('');
|
const [selectedTimeSlot, setSelectedTimeSlot] = useState<string>('');
|
||||||
@@ -126,13 +136,57 @@ const RefreshOrder = () => {
|
|||||||
|
|
||||||
const { mutate, isPending } = useMutation({
|
const { mutate, isPending } = useMutation({
|
||||||
mutationFn: (body: OrderCreateBody) => cart_api.createOrder(body),
|
mutationFn: (body: OrderCreateBody) => cart_api.createOrder(body),
|
||||||
onSuccess: () => {
|
onSuccess: (res) => {
|
||||||
setOrderSuccess(true);
|
const message = JSON.parse(res.data.response);
|
||||||
queryClient.refetchQueries({ queryKey: ['cart_items'] });
|
|
||||||
queryClient.refetchQueries({ queryKey: ['order_list'] });
|
if (message.successes && message.successes.length > 0) {
|
||||||
|
setOrderSuccess(true);
|
||||||
|
queryClient.refetchQueries({ queryKey: ['cart_items'] });
|
||||||
|
queryClient.refetchQueries({ queryKey: ['order_list'] });
|
||||||
|
} else if (message.errors && message.errors.length > 0) {
|
||||||
|
toast.error(t('Xatolik yuz berdi') + message.errors[0].message, {
|
||||||
|
richColors: true,
|
||||||
|
position: 'top-center',
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
toast.error(t('Xatolik yuz berdi'), {
|
||||||
|
richColors: true,
|
||||||
|
position: 'top-center',
|
||||||
|
});
|
||||||
|
}
|
||||||
},
|
},
|
||||||
onError: () => {
|
onError: (error: AxiosError<DRFError>) => {
|
||||||
toast.error(t('Xatolik yuz berdi'), {
|
const data = error.response?.data;
|
||||||
|
|
||||||
|
let message = t('Xatolik yuz berdi');
|
||||||
|
|
||||||
|
if (data) {
|
||||||
|
// 🔥 DRF validation error parsing
|
||||||
|
if (typeof data === 'object') {
|
||||||
|
const firstKey = Object.keys(data)[0]; // "order"
|
||||||
|
const firstValue = data[firstKey];
|
||||||
|
|
||||||
|
if (Array.isArray(firstValue)) {
|
||||||
|
const firstErrorObj = firstValue[0]; // { note: [...] }
|
||||||
|
|
||||||
|
if (typeof firstErrorObj === 'object') {
|
||||||
|
const innerKey = Object.keys(firstErrorObj)[0]; // "note"
|
||||||
|
const innerValue = firstErrorObj[innerKey];
|
||||||
|
|
||||||
|
if (Array.isArray(innerValue)) {
|
||||||
|
message = innerValue[0]; // "This field may not be blank."
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// fallback
|
||||||
|
if (!error.response) {
|
||||||
|
message = error.message;
|
||||||
|
}
|
||||||
|
|
||||||
|
toast.error(message, {
|
||||||
richColors: true,
|
richColors: true,
|
||||||
position: 'top-center',
|
position: 'top-center',
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -45,10 +45,6 @@ httpClient.interceptors.response.use(
|
|||||||
if ((error as AxiosError)?.status === 403) {
|
if ((error as AxiosError)?.status === 403) {
|
||||||
try {
|
try {
|
||||||
const refToken = getRefToken();
|
const refToken = getRefToken();
|
||||||
if (!refToken) {
|
|
||||||
removeToken();
|
|
||||||
removeRefToken();
|
|
||||||
}
|
|
||||||
const { data } = await axios.post(
|
const { data } = await axios.post(
|
||||||
`${BASE_URL}api/v1/accounts/refresh/token/`,
|
`${BASE_URL}api/v1/accounts/refresh/token/`,
|
||||||
{ refresh: refToken },
|
{ refresh: refToken },
|
||||||
@@ -62,10 +58,6 @@ httpClient.interceptors.response.use(
|
|||||||
} else if ((error as AxiosError)?.status === 401) {
|
} else if ((error as AxiosError)?.status === 401) {
|
||||||
try {
|
try {
|
||||||
const refToken = getRefToken();
|
const refToken = getRefToken();
|
||||||
if (!refToken) {
|
|
||||||
removeToken();
|
|
||||||
removeRefToken();
|
|
||||||
}
|
|
||||||
const { data } = await axios.post(
|
const { data } = await axios.post(
|
||||||
`${BASE_URL}api/v1/accounts/refresh/token/`,
|
`${BASE_URL}api/v1/accounts/refresh/token/`,
|
||||||
{ refresh: refToken },
|
{ refresh: refToken },
|
||||||
|
|||||||
Reference in New Issue
Block a user