Skip to content

Commit 6d2ccb1

Browse files
committed
Task EPAM-JS-Competency-center#8 RDS integration
- update api endpoints - update ui to conform to data returned by api endpoints - remove request mocks
1 parent 8e9221c commit 6d2ccb1

File tree

5 files changed

+30
-84
lines changed

5 files changed

+30
-84
lines changed

src/components/pages/PageOrder/PageOrder.tsx

Lines changed: 15 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,8 @@ import MenuItem from "@mui/material/MenuItem";
1414
import { Field, Form, Formik, FormikProps } from "formik";
1515
import Grid from "@mui/material/Grid";
1616
import TextField from "~/components/Form/TextField";
17-
import Table from "@mui/material/Table";
18-
import TableHead from "@mui/material/TableHead";
19-
import TableRow from "@mui/material/TableRow";
20-
import TableCell from "@mui/material/TableCell";
21-
import TableBody from "@mui/material/TableBody";
22-
import TableContainer from "@mui/material/TableContainer";
2317
import Box from "@mui/material/Box";
24-
import { useQueries } from "react-query";
18+
import { useQuery } from "react-query";
2519
import { useInvalidateOrder, useUpdateOrderStatus } from "~/queries/orders";
2620

2721
type FormValues = {
@@ -31,63 +25,37 @@ type FormValues = {
3125

3226
export default function PageOrder() {
3327
const { id } = useParams<{ id: string }>();
34-
const results = useQueries([
35-
{
36-
queryKey: ["order", { id }],
37-
queryFn: async () => {
38-
const res = await axios.get<Order>(`${API_PATHS.order}/order/${id}`);
39-
return res.data;
40-
},
28+
const {
29+
data: order,
30+
isLoading,
31+
} = useQuery({
32+
queryKey: ["order", { id }],
33+
queryFn: async () => {
34+
const res = await axios.get<Order>(`${API_PATHS.order}/order/${id}`);
35+
return res.data;
4136
},
42-
{
43-
queryKey: "products",
44-
queryFn: async () => {
45-
const res = await axios.get<AvailableProduct[]>(
46-
`${API_PATHS.bff}/product/available`
47-
);
48-
return res.data;
49-
},
50-
},
51-
]);
52-
const [
53-
{ data: order, isLoading: isOrderLoading },
54-
{ data: products, isLoading: isProductsLoading },
55-
] = results;
37+
});
5638
const { mutateAsync: updateOrderStatus } = useUpdateOrderStatus();
5739
const invalidateOrder = useInvalidateOrder();
58-
const cartItems: CartItem[] = React.useMemo(() => {
59-
if (order && products) {
60-
return order.items.map((item: OrderItem) => {
61-
const product = products.find((p) => p.id === item.productId);
62-
if (!product) {
63-
throw new Error("Product not found");
64-
}
65-
return { product, count: item.count };
66-
});
67-
}
68-
return [];
69-
}, [order, products]);
70-
71-
if (isOrderLoading || isProductsLoading) return <p>loading...</p>;
7240

73-
const statusHistory = order?.statusHistory || [];
41+
if (isLoading) return <p>loading...</p>;
7442

75-
const lastStatusItem = statusHistory[statusHistory.length - 1];
43+
const orderStatus = order.status;
7644

7745
return order ? (
7846
<PaperLayout>
7947
<Typography component="h1" variant="h4" align="center">
8048
Manage order
8149
</Typography>
82-
<ReviewOrder address={order.address} items={cartItems} />
50+
<ReviewOrder address={order.delivery.address} items={order.cart.items} />
8351
<Typography variant="h6">Status:</Typography>
8452
<Typography variant="h6" color="primary">
85-
{lastStatusItem?.status.toUpperCase()}
53+
{orderStatus.toUpperCase()}
8654
</Typography>
8755
<Typography variant="h6">Change status:</Typography>
8856
<Box py={2}>
8957
<Formik
90-
initialValues={{ status: lastStatusItem.status, comment: "" }}
58+
initialValues={{ status: orderStatus, comment: "" }}
9159
enableReinitialize
9260
onSubmit={(values) =>
9361
updateOrderStatus(
@@ -145,30 +113,6 @@ export default function PageOrder() {
145113
</Formik>
146114
</Box>
147115
<Typography variant="h6">Status history:</Typography>
148-
<TableContainer>
149-
<Table aria-label="simple table">
150-
<TableHead>
151-
<TableRow>
152-
<TableCell>Status</TableCell>
153-
<TableCell align="right">Date and Time</TableCell>
154-
<TableCell align="right">Comment</TableCell>
155-
</TableRow>
156-
</TableHead>
157-
<TableBody>
158-
{statusHistory.map((statusHistoryItem) => (
159-
<TableRow key={order.id}>
160-
<TableCell component="th" scope="row">
161-
{statusHistoryItem.status.toUpperCase()}
162-
</TableCell>
163-
<TableCell align="right">
164-
{new Date(statusHistoryItem.timestamp).toString()}
165-
</TableCell>
166-
<TableCell align="right">{statusHistoryItem.comment}</TableCell>
167-
</TableRow>
168-
))}
169-
</TableBody>
170-
</Table>
171-
</TableContainer>
172116
</PaperLayout>
173117
) : null;
174118
}

src/components/pages/PageOrders/components/Orders.tsx

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,11 @@ export default function Orders() {
3434
{data?.map((order) => (
3535
<TableRow key={order.id}>
3636
<TableCell component="th" scope="row">
37-
{order.address?.firstName} {order.address?.lastName}
38-
</TableCell>
39-
<TableCell align="right">{order.items.length}</TableCell>
40-
<TableCell align="right">{order.address?.address}</TableCell>
41-
<TableCell align="right">
42-
{order.statusHistory[order.statusHistory.length - 1].status}
37+
{order.user?.firstName} {order.user?.lastName}
4338
</TableCell>
39+
<TableCell align="right">{order.cart.items.length}</TableCell>
40+
<TableCell align="right">{order.delivery.address}</TableCell>
41+
<TableCell align="right">{order.status}</TableCell>
4442
<TableCell align="right">
4543
<Button
4644
size="small"

src/constants/apiPaths.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
const API_PATHS = {
22
product: "https://bsar9vq69i.execute-api.us-east-1.amazonaws.com",
3-
order: "https://.execute-api.eu-west-1.amazonaws.com/dev",
43
import: "https://ol9cna0j4c.execute-api.us-east-1.amazonaws.com/dev",
54
bff: "https://bsar9vq69i.execute-api.us-east-1.amazonaws.com",
6-
cart: "https://.execute-api.eu-west-1.amazonaws.com/dev",
5+
order: "https://hknrgnihte.execute-api.us-east-1.amazonaws.com/dev/api",
6+
cart: "https://hknrgnihte.execute-api.us-east-1.amazonaws.com/dev/api",
77
};
88

99
export default API_PATHS;

src/index.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@ const queryClient = new QueryClient({
1515
},
1616
});
1717

18-
if (import.meta.env.DEV) {
19-
const { worker } = await import("./mocks/browser");
20-
worker.start({ onUnhandledRequest: "bypass" });
21-
}
18+
// if (import.meta.env.DEV) {
19+
// const { worker } = await import("./mocks/browser");
20+
// worker.start({ onUnhandledRequest: "warn" });
21+
// }
2222
axios.interceptors.response.use(
2323
(response) => response,
2424
(error) => {

src/queries/orders.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,11 @@ import { Order } from "~/models/Order";
77

88
export function useOrders() {
99
return useQuery<Order[], AxiosError>("orders", async () => {
10-
const res = await axios.get<Order[]>(`${API_PATHS.order}/order`);
10+
const res = await axios.get<Order[]>(`${API_PATHS.order}/order`, {
11+
headers: {
12+
Authorization: `Basic ${localStorage.getItem("authorization_token")}`,
13+
},
14+
});
1115
return res.data;
1216
});
1317
}

0 commit comments

Comments
 (0)