Fix Tim Coding at Order Admin DnD
This commit is contained in:
@@ -1,27 +1,25 @@
|
|||||||
import {
|
import {
|
||||||
Box,
|
|
||||||
Button,
|
Button,
|
||||||
|
Card, CardContent,
|
||||||
Dialog,
|
Dialog,
|
||||||
|
DialogActions,
|
||||||
DialogContent,
|
DialogContent,
|
||||||
DialogTitle,
|
DialogTitle,
|
||||||
DialogActions,
|
|
||||||
List, ListItemText,
|
|
||||||
Typography,
|
|
||||||
useTheme,
|
|
||||||
Card, CardContent,
|
|
||||||
Stack,
|
|
||||||
Divider,
|
Divider,
|
||||||
Snackbar
|
List, ListItemText,
|
||||||
|
Snackbar,
|
||||||
|
Stack,
|
||||||
|
Typography,
|
||||||
|
useTheme
|
||||||
} from "@mui/material";
|
} from "@mui/material";
|
||||||
import React, { useState, useEffect, PropsWithChildren } from "react";
|
import { useMutation, useQuery } from "@tanstack/react-query";
|
||||||
import { useTranslation } from "react-i18next";
|
import React, { PropsWithChildren, useEffect, useState } from "react";
|
||||||
import OrderType, { OrderStatusEnum } from "../../components/Order";
|
import { DndProvider, useDrag, useDrop } from 'react-dnd';
|
||||||
import { useDrag, useDrop, DndProvider } from 'react-dnd';
|
|
||||||
import { HTML5Backend } from 'react-dnd-html5-backend';
|
import { HTML5Backend } from 'react-dnd-html5-backend';
|
||||||
import { useQuery } from "@tanstack/react-query";
|
import { useTranslation } from "react-i18next";
|
||||||
import { fetchOrdersAdmin, orderPatch } from "../query/Queries";
|
import OrderType, { OrderPatch, OrderStatusEnum } from "../../components/Order";
|
||||||
import { useAccount } from "../AccountProvider";
|
import { useAccount } from "../AccountProvider";
|
||||||
import { useQueryClient } from "@tanstack/react-query";
|
import { fetchOrdersAdmin, orderPatch } from "../query/Queries";
|
||||||
|
|
||||||
// The order in which the statuses are displayed
|
// The order in which the statuses are displayed
|
||||||
const statusOrder: OrderStatusEnum[] = [
|
const statusOrder: OrderStatusEnum[] = [
|
||||||
@@ -36,7 +34,6 @@ const statusOrder: OrderStatusEnum[] = [
|
|||||||
const OrderCard: React.FC<{ order: OrderType; onClick: () => void }> = ({ order, onClick }) => {
|
const OrderCard: React.FC<{ order: OrderType; onClick: () => void }> = ({ order, onClick }) => {
|
||||||
|
|
||||||
const { t } = useTranslation();
|
const { t } = useTranslation();
|
||||||
const theme = useTheme();
|
|
||||||
|
|
||||||
const [{ isDragging }, drag] = useDrag(() => ({
|
const [{ isDragging }, drag] = useDrag(() => ({
|
||||||
type: 'order',
|
type: 'order',
|
||||||
@@ -93,7 +90,6 @@ const Column: React.FC<PropsWithChildren<{ status: OrderStatusEnum; onDrop: (id:
|
|||||||
const EditOrder: React.FC<{ open: boolean; order: OrderType | null; onClose: () => void }> = ({ open, order, onClose }) => {
|
const EditOrder: React.FC<{ open: boolean; order: OrderType | null; onClose: () => void }> = ({ open, order, onClose }) => {
|
||||||
|
|
||||||
const { t } = useTranslation();
|
const { t } = useTranslation();
|
||||||
const theme = useTheme();
|
|
||||||
if (order === null)
|
if (order === null)
|
||||||
return "";
|
return "";
|
||||||
return (
|
return (
|
||||||
@@ -134,7 +130,6 @@ export default function OrdersInfo() {
|
|||||||
const [openSnackbar, setOpenSnackbar] = useState(false);
|
const [openSnackbar, setOpenSnackbar] = useState(false);
|
||||||
|
|
||||||
const { user: loginData } = useAccount();
|
const { user: loginData } = useAccount();
|
||||||
const queryClient = useQueryClient()
|
|
||||||
|
|
||||||
const { data, refetch } = useQuery({
|
const { data, refetch } = useQuery({
|
||||||
queryKey: ["fetchOrdersAdmin", loginData],
|
queryKey: ["fetchOrdersAdmin", loginData],
|
||||||
@@ -143,6 +138,12 @@ export default function OrdersInfo() {
|
|||||||
retryDelay: 1000,
|
retryDelay: 1000,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const patchOrderMutation = useMutation({
|
||||||
|
mutationFn: (order: OrderPatch) =>
|
||||||
|
orderPatch({ id: order.id, status: order.status }),
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
console.log("data", data);
|
console.log("data", data);
|
||||||
if (data) {
|
if (data) {
|
||||||
@@ -150,30 +151,21 @@ export default function OrdersInfo() {
|
|||||||
}
|
}
|
||||||
}, [data]);
|
}, [data]);
|
||||||
|
|
||||||
|
|
||||||
const handleDrop = async (id: number, status: OrderStatusEnum) => {
|
const handleDrop = async (id: number, status: OrderStatusEnum) => {
|
||||||
if(orders.length < 1) {
|
const obj = orders.find((o) => o.id === id);
|
||||||
const resp = await refetch();
|
|
||||||
setOrders(resp.data); // i dont know
|
|
||||||
}
|
|
||||||
const obj = orders.find((o) => {
|
|
||||||
return o.id === id
|
|
||||||
})
|
|
||||||
if (!obj) {
|
if (!obj) {
|
||||||
setOpenSnackbar(true);
|
setOpenSnackbar(true);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
obj.status = status
|
try {
|
||||||
const resp = await queryClient.fetchQuery({
|
await patchOrderMutation.mutateAsync({ id: obj.id, status: obj.status });
|
||||||
queryKey: ["orderPatch", {id: obj.id, status: obj.status}],
|
|
||||||
queryFn: () => orderPatch({id: obj.id, status: obj.status}),
|
|
||||||
retry: 0,
|
|
||||||
retryDelay: 1000,
|
|
||||||
})
|
|
||||||
refetch();
|
refetch();
|
||||||
setOrders(orders.map((o) => (o.id === id ? obj : o )));
|
setOrders(orders.map((o) => (o.id === id ? { ...o, status } : o)));
|
||||||
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||||
|
} catch (error) {
|
||||||
|
setOpenSnackbar(true);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const handleEdit = (order: OrderType) => setEditOrder(order);
|
const handleEdit = (order: OrderType) => setEditOrder(order);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
|||||||
Reference in New Issue
Block a user