feat: apply new order view api
All checks were successful
it-academy/dry-wash-pl/pipeline/pr-main This commit looks good
it-academy/dry-wash-pl/pipeline/head This commit looks good

This commit is contained in:
RustamRu 2025-01-19 17:40:29 +03:00
parent 0307f36e57
commit 431a8b27f2
5 changed files with 69 additions and 86 deletions

View File

@ -42,8 +42,7 @@
"dry-wash.order-create.create-order-query.success.title": "The order is successfully created", "dry-wash.order-create.create-order-query.success.title": "The order is successfully created",
"dry-wash.order-create.create-order-query.error.title": "Failed to create an order", "dry-wash.order-create.create-order-query.error.title": "Failed to create an order",
"dry-wash.order-view.title": "Your order", "dry-wash.order-view.title": "Your order",
"dry-wash.order-view.error.title": "Error", "dry-wash.order-view.get-order-query.error.title": "Failed to fetch the details of order #{{number}}",
"dry-wash.order-view.fetch.error": "Failed to fetch the details of order #{{number}}",
"dry-wash.order-view.details.title": "Order #{{number}}", "dry-wash.order-view.details.title": "Order #{{number}}",
"dry-wash.order-view.details.owner": "Owner", "dry-wash.order-view.details.owner": "Owner",
"dry-wash.order-view.details.car": "Car", "dry-wash.order-view.details.car": "Car",

View File

@ -81,8 +81,7 @@
"dry-wash.order-create.create-order-query.success.title": "Заказ успешно создан", "dry-wash.order-create.create-order-query.success.title": "Заказ успешно создан",
"dry-wash.order-create.create-order-query.error.title": "Не удалось создать заказ", "dry-wash.order-create.create-order-query.error.title": "Не удалось создать заказ",
"dry-wash.order-view.title": "Ваш заказ", "dry-wash.order-view.title": "Ваш заказ",
"dry-wash.order-view.error.title": "Ошибка", "dry-wash.order-view.get-order-query.error.title": "Не удалось загрузить детали заказа №{{number}}",
"dry-wash.order-view.fetch.error": "Не удалось загрузить детали заказа №{{number}}",
"dry-wash.order-view.details.title": "Заказ №{{number}}", "dry-wash.order-view.details.title": "Заказ №{{number}}",
"dry-wash.order-view.details.owner": "Владелец", "dry-wash.order-view.details.owner": "Владелец",
"dry-wash.order-view.details.car": "Автомобиль", "dry-wash.order-view.details.car": "Автомобиль",

View File

@ -17,7 +17,19 @@ import { carBodySelectOptions } from '../../order-form/form/car-body/helper';
import { OrderStatus } from './status'; import { OrderStatus } from './status';
type OrderDetailsProps = Order.View; type OrderDetailsProps = Pick<
Order.View,
| 'id'
| 'status'
| 'phone'
| 'carNumber'
| 'carBody'
| 'carColor'
| 'location'
| 'startWashTime'
| 'endWashTime'
| 'created'
>;
export const OrderDetails: FC<OrderDetailsProps> = ({ export const OrderDetails: FC<OrderDetailsProps> = ({
id, id,
@ -27,8 +39,8 @@ export const OrderDetails: FC<OrderDetailsProps> = ({
carBody, carBody,
carColor, carColor,
location, location,
datetimeBegin, startWashTime,
datetimeEnd, endWashTime,
}) => { }) => {
const { t } = useTranslation('~', { const { t } = useTranslation('~', {
keyPrefix: 'dry-wash.order-view.details', keyPrefix: 'dry-wash.order-view.details',
@ -75,8 +87,8 @@ export const OrderDetails: FC<OrderDetailsProps> = ({
{ {
label: t('datetime-range'), label: t('datetime-range'),
value: [ value: [
formatDatetime(datetimeBegin), formatDatetime(startWashTime),
formatDatetime(datetimeEnd), formatDatetime(endWashTime),
].join(' - '), ].join(' - '),
}, },
].map(({ label, value }, i) => ( ].map(({ label, value }, i) => (

View File

@ -1,40 +0,0 @@
import { useEffect, useState } from 'react';
import { LandingService } from '../../api/landing';
import { Order } from '../../models/landing';
import { FetchOrderQueryResponse } from '../../models/api';
export const useFetchOrderDetails = ({
orderId,
}: {
orderId: Order.View['id'];
}) => {
const { fetchOrder } = LandingService();
const [data, setData] = useState<FetchOrderQueryResponse>();
const [isLoading, setIsLoading] = useState(false);
const [error, setError] = useState<string | null>(null);
useEffect(() => {
const fetchData = async () => {
setIsLoading(true);
try {
const data = await fetchOrder(orderId);
setData(data.body);
} catch (error) {
setError(error.message);
} finally {
setIsLoading(false);
}
};
fetchData();
}, []);
return {
isLoading,
data,
error,
};
};

View File

@ -1,36 +1,32 @@
import React, { FC, useEffect } from 'react'; import React, { FC } from 'react';
import { HStack, Spinner, useToast } from '@chakra-ui/react'; import { Alert, AlertDescription, AlertIcon, AlertTitle, HStack, Spinner } from '@chakra-ui/react';
import { useTranslation } from 'react-i18next'; import { useTranslation } from 'react-i18next';
import { Container, Heading, VStack } from '@chakra-ui/react'; import { Container, Heading, VStack } from '@chakra-ui/react';
import { useParams } from 'react-router-dom'; import { useParams } from 'react-router-dom';
import { LandingThemeProvider } from '../../containers'; import {
LandingThemeProvider,
withLandingThemeProvider,
} from '../../containers';
import { OrderDetails } from '../../components/order-view'; import { OrderDetails } from '../../components/order-view';
import { Order } from '../../models/landing';
import { useFetchOrderDetails } from './helper'; import { useGetOrderQuery } from '../../api';
const Page: FC = () => { const Page: FC = () => {
const { t } = useTranslation('~', { const { t } = useTranslation('~', {
keyPrefix: 'dry-wash.order-view', keyPrefix: 'dry-wash.order-view',
}); });
const { orderId } = useParams(); const { orderId } = useParams<Order.Id>();
const {
const { isLoading, data, error } = useFetchOrderDetails({ orderId }); isLoading,
isSuccess,
const toast = useToast(); data: { body: order } = {},
useEffect(() => { isError,
if (error) { error,
toast({ } = useGetOrderQuery({
title: t('error.title'), orderId,
description: t('fetch.error'),
status: 'error',
duration: 5000,
isClosable: true,
position: 'bottom-right',
}); });
}
}, [error]);
return ( return (
<LandingThemeProvider> <LandingThemeProvider>
@ -51,20 +47,37 @@ const Page: FC = () => {
<Spinner size='lg' /> <Spinner size='lg' />
</HStack> </HStack>
) : ( ) : (
data && ( <>
<>
{isSuccess && (
<OrderDetails <OrderDetails
id={data.id} id={order.id}
orderDate={data.orderDate} status={order.status}
status={data.status} phone={order.phone}
phone={data.phone} carNumber={order.carNumber}
carNumber={data.carNumber} carBody={order.carBody}
carBody={data.carBody} carColor={order.carColor}
carColor={data.carColor} location={order.location}
location={data.location} startWashTime={order.startWashTime}
datetimeBegin={data.startWashTime} endWashTime={order.endWashTime}
datetimeEnd={data.endWashTime} created={order.created}
/> />
) )}
</>
<>
{isError && (
<Alert status='error'>
<AlertIcon />
<AlertTitle>
{t('get-order-query.error.title', {
number: orderId,
})}
</AlertTitle>
<AlertDescription>{error.data?.error}</AlertDescription>
</Alert>
)}
</>
</>
)} )}
</VStack> </VStack>
</Container> </Container>
@ -72,4 +85,4 @@ const Page: FC = () => {
); );
}; };
export default Page; export default withLandingThemeProvider(Page);