From ed9dd3599f4fc8a9a8b70c1e21678d531091b6aa Mon Sep 17 00:00:00 2001 From: Reckless_Satoshi Date: Mon, 15 Jan 2024 00:48:40 +0000 Subject: [PATCH] Fix currentOrder fetch --- frontend/src/basic/BookPage/index.tsx | 3 +- frontend/src/basic/MakerPage/index.tsx | 5 ++- frontend/src/basic/NavBar/NavBar.tsx | 15 ++++--- frontend/src/basic/OrderPage/index.tsx | 44 +++--------------- frontend/src/basic/RobotPage/RobotProfile.tsx | 4 ++ .../components/OrderDetails/TakeButton.tsx | 7 ++- .../src/components/OrderDetails/index.tsx | 3 -- frontend/src/components/RobotInfo/index.tsx | 10 +++++ .../src/components/TradeBox/CancelButton.tsx | 2 +- frontend/src/contexts/FederationContext.tsx | 45 +++++++++++++++---- 10 files changed, 77 insertions(+), 61 deletions(-) diff --git a/frontend/src/basic/BookPage/index.tsx b/frontend/src/basic/BookPage/index.tsx index 0c9b008b..ed74cc24 100644 --- a/frontend/src/basic/BookPage/index.tsx +++ b/frontend/src/basic/BookPage/index.tsx @@ -17,7 +17,7 @@ import { GarageContext, type UseGarageStoreType } from '../../contexts/GarageCon const BookPage = (): JSX.Element => { const { windowSize } = useContext(AppContext); - const { setDelay } = useContext(FederationContext); + const { setDelay, setCurrentOrderId } = useContext(FederationContext); const { garage } = useContext(GarageContext); const { t } = useTranslation(); const navigate = useNavigate(); @@ -33,6 +33,7 @@ const BookPage = (): JSX.Element => { const onOrderClicked = function (id: number, shortAlias: string): void { if (garage.getSlot()?.hashId) { setDelay(10000); + setCurrentOrderId({ id, shortAlias }); navigate(`/order/${shortAlias}/${id}`); } else { setOpenNoRobot(true); diff --git a/frontend/src/basic/MakerPage/index.tsx b/frontend/src/basic/MakerPage/index.tsx index 5066645b..4fd2ba31 100644 --- a/frontend/src/basic/MakerPage/index.tsx +++ b/frontend/src/basic/MakerPage/index.tsx @@ -14,7 +14,8 @@ import { GarageContext, type UseGarageStoreType } from '../../contexts/GarageCon const MakerPage = (): JSX.Element => { const { fav, windowSize, navbarHeight } = useContext(AppContext); - const { federation, setDelay } = useContext(FederationContext); + const { federation, setDelay, setCurrentOrderId } = + useContext(FederationContext); const { garage, maker } = useContext(GarageContext); const { t } = useTranslation(); const navigate = useNavigate(); @@ -53,6 +54,7 @@ const MakerPage = (): JSX.Element => { const onOrderClicked = function (id: number, shortAlias: string): void { if (garage.getSlot()?.hashId) { setDelay(10000); + setCurrentOrderId({ id, shortAlias }); navigate(`/order/${shortAlias}/${id}`); } else { setOpenNoRobot(true); @@ -103,6 +105,7 @@ const MakerPage = (): JSX.Element => { > { + setCurrentOrderId({ id, shortAlias }); navigate(`/order/${shortAlias}/${id}`); }} disableRequest={matches.length > 0 && !showMatches} diff --git a/frontend/src/basic/NavBar/NavBar.tsx b/frontend/src/basic/NavBar/NavBar.tsx index 8c797955..7d25b634 100644 --- a/frontend/src/basic/NavBar/NavBar.tsx +++ b/frontend/src/basic/NavBar/NavBar.tsx @@ -17,6 +17,7 @@ import { import RobotAvatar from '../../components/RobotAvatar'; import { AppContext, type UseAppStoreType, closeAll } from '../../contexts/AppContext'; import { GarageContext, type UseGarageStoreType } from '../../contexts/GarageContext'; +import { FederationContext, UseFederationStoreType } from '../../contexts/FederationContext'; const NavBar = (): JSX.Element => { const theme = useTheme(); @@ -24,6 +25,7 @@ const NavBar = (): JSX.Element => { const { page, setPage, settings, setSlideDirection, open, setOpen, windowSize, navbarHeight } = useContext(AppContext); const { garage, orderUpdatedAt, robotUpdatedAt } = useContext(GarageContext); + const { setCurrentOrderId } = useContext(FederationContext); const navigate = useNavigate(); const location = useLocation(); @@ -75,13 +77,14 @@ const NavBar = (): JSX.Element => { const slot = garage.getSlot(); handleSlideDirection(page, newPage); setPage(newPage); + const shortAlias = String(slot?.activeShortAlias); + const activeOrderId = slot?.getRobot(slot?.activeShortAlias ?? '')?.activeOrderId; + const lastOrderId = slot?.getRobot(slot?.lastShortAlias ?? '')?.lastOrderId; const param = - newPage === 'order' - ? `${String(slot?.activeShortAlias)}/${String( - slot?.getRobot(slot?.activeShortAlias ?? '')?.activeOrderId ?? - slot?.getRobot(slot?.lastShortAlias ?? '')?.lastOrderId, - )}` - : ''; + newPage === 'order' ? `${shortAlias}/${String(activeOrderId ?? lastOrderId)}` : ''; + if (shortAlias && (activeOrderId || lastOrderId)) { + setCurrentOrderId({ id: activeOrderId ?? lastOrderId, shortAlias }); + } setTimeout(() => { navigate(`/${newPage}/${param}`); }, theme.transitions.duration.leavingScreen * 3); diff --git a/frontend/src/basic/OrderPage/index.tsx b/frontend/src/basic/OrderPage/index.tsx index 5ac9bac8..2e42e6cd 100644 --- a/frontend/src/basic/OrderPage/index.tsx +++ b/frontend/src/basic/OrderPage/index.tsx @@ -24,8 +24,9 @@ const OrderPage = (): JSX.Element => { hostUrl, origin, } = useContext(AppContext); - const { federation } = useContext(FederationContext); - const { garage, badOrder, setBadOrder } = useContext(GarageContext); + const { federation, currentOrder, currentOrderId, setCurrentOrderId } = + useContext(FederationContext); + const { badOrder } = useContext(GarageContext); const { t } = useTranslation(); const navigate = useNavigate(); const params = useParams(); @@ -35,11 +36,10 @@ const OrderPage = (): JSX.Element => { const [tab, setTab] = useState<'order' | 'contract'>('contract'); const [baseUrl, setBaseUrl] = useState(hostUrl); - const [currentOrder, setCurrentOrder] = useState(null); - const [currentOrderId, setCurrentOrderId] = useState(null); useEffect(() => { - const coordinator = federation.getCoordinator(params.shortAlias ?? ''); + const shortAlias = params.shortAlias; + const coordinator = federation.getCoordinator(shortAlias ?? ''); const { url, basePath } = coordinator.getEndpoint( settings.network, origin, @@ -50,40 +50,11 @@ const OrderPage = (): JSX.Element => { setBaseUrl(`${url}${basePath}`); const orderId = Number(params.orderId); - if (Boolean(orderId) && currentOrderId !== orderId) setCurrentOrderId(orderId); + if (orderId && currentOrderId.id !== orderId && currentOrderId.shortAlias !== shortAlias) + setCurrentOrderId({ id: orderId, shortAlias }); if (!acknowledgedWarning) setOpen({ ...closeAll, warning: true }); }, [params]); - useEffect(() => { - setCurrentOrder(null); - updateCurrentOrder(); - }, [currentOrderId]); - - const updateCurrentOrder = (): void => { - if (currentOrderId !== null) { - const coordinator = federation.getCoordinator(params.shortAlias ?? ''); - const slot = garage.getSlot(); - const robot = slot?.getRobot(); - if (robot != null && slot?.token != null) { - coordinator - .fetchOrder(currentOrderId, robot, slot.token) - .then((order) => { - if (order?.bad_request !== undefined) { - setBadOrder(order.bad_request); - } else if (order?.id) { - setCurrentOrder(order); - if (order?.is_participant) { - garage.updateOrder(order); - } - } - }) - .catch((e) => { - console.log(e); - }); - } - } - }; - const onClickCoordinator = function (): void { if (currentOrder?.shortAlias != null) { setOpen((open) => { @@ -100,7 +71,6 @@ const OrderPage = (): JSX.Element => { { navigate('/robot'); diff --git a/frontend/src/basic/RobotPage/RobotProfile.tsx b/frontend/src/basic/RobotPage/RobotProfile.tsx index 187682a8..36370fe4 100644 --- a/frontend/src/basic/RobotPage/RobotProfile.tsx +++ b/frontend/src/basic/RobotPage/RobotProfile.tsx @@ -22,6 +22,7 @@ import { AppContext, type UseAppStoreType } from '../../contexts/AppContext'; import { genBase62Token } from '../../utils'; import { LoadingButton } from '@mui/lab'; import { GarageContext, type UseGarageStoreType } from '../../contexts/GarageContext'; +import { FederationContext, UseFederationStoreType } from '../../contexts/FederationContext'; interface RobotProfileProps { robot: Robot; @@ -45,6 +46,7 @@ const RobotProfile = ({ }: RobotProfileProps): JSX.Element => { const { windowSize } = useContext(AppContext); const { garage, robotUpdatedAt, orderUpdatedAt } = useContext(GarageContext); + const { setCurrentOrderId } = useContext(FederationContext); const { t } = useTranslation(); const theme = useTheme(); @@ -152,6 +154,7 @@ const RobotProfile = ({