From 7c06c229b472acebb59c93c89eefe4969d31748d Mon Sep 17 00:00:00 2001 From: KoalaSat Date: Wed, 15 Nov 2023 13:59:54 +0100 Subject: [PATCH] Fix order page --- frontend/src/basic/BookPage/index.tsx | 3 +- frontend/src/basic/MakerPage/index.tsx | 8 +- frontend/src/basic/NavBar/NavBar.tsx | 15 +- frontend/src/basic/OrderPage/index.tsx | 73 ++++++--- frontend/src/basic/RobotPage/RobotProfile.tsx | 20 ++- .../components/OrderDetails/TakeButton.tsx | 67 ++++----- .../src/components/OrderDetails/index.tsx | 139 ++++++++---------- frontend/src/components/TradeBox/index.tsx | 42 +++--- frontend/src/contexts/FederationContext.ts | 26 ++-- frontend/src/contexts/GarageContext.ts | 8 - frontend/src/models/Coordinator.model.ts | 35 ++--- frontend/src/models/Federation.model.ts | 23 ++- frontend/src/models/Garage.model.ts | 62 +++++--- frontend/src/models/Order.model.ts | 114 ++++++++++++++ frontend/src/models/Robot.model.ts | 1 + 15 files changed, 387 insertions(+), 249 deletions(-) diff --git a/frontend/src/basic/BookPage/index.tsx b/frontend/src/basic/BookPage/index.tsx index a4664bfb..bc9c0965 100644 --- a/frontend/src/basic/BookPage/index.tsx +++ b/frontend/src/basic/BookPage/index.tsx @@ -18,7 +18,7 @@ import { GarageContext, type UseGarageStoreType } from '../../contexts/GarageCon const BookPage = (): JSX.Element => { const { windowSize } = useContext(AppContext); const { setDelay } = useContext(FederationContext); - const { garage, clearOrder } = useContext(GarageContext); + const { garage } = useContext(GarageContext); const { t } = useTranslation(); const navigate = useNavigate(); const [view, setView] = useState<'list' | 'depth' | 'map'>('list'); @@ -32,7 +32,6 @@ const BookPage = (): JSX.Element => { const onOrderClicked = function (id: number, shortAlias: string): void { if (garage.getRobot().avatarLoaded) { - clearOrder(); setDelay(10000); navigate(`/order/${shortAlias}/${id}`); } else { diff --git a/frontend/src/basic/MakerPage/index.tsx b/frontend/src/basic/MakerPage/index.tsx index 9afc97e3..8db7878f 100644 --- a/frontend/src/basic/MakerPage/index.tsx +++ b/frontend/src/basic/MakerPage/index.tsx @@ -14,7 +14,7 @@ import { GarageContext, type UseGarageStoreType } from '../../contexts/GarageCon const MakerPage = (): JSX.Element => { const { fav, windowSize, navbarHeight } = useContext(AppContext); - const { setDelay, federation } = useContext(FederationContext); + const { federation } = useContext(FederationContext); const { garage, maker } = useContext(GarageContext); const { t } = useTranslation(); const navigate = useNavigate(); @@ -50,15 +50,9 @@ const MakerPage = (): JSX.Element => { maker.paymentMethods, ]); - const onViewOrder = function (): void { - garage.updateOrder(null); - setDelay(10000); - }; - const onOrderClicked = function (id: number): void { if (garage.getRobot().avatarLoaded) { navigate(`/order/${id}`); - onViewOrder(); } else { setOpenNoRobot(true); } diff --git a/frontend/src/basic/NavBar/NavBar.tsx b/frontend/src/basic/NavBar/NavBar.tsx index 3a01e0d2..d597505a 100644 --- a/frontend/src/basic/NavBar/NavBar.tsx +++ b/frontend/src/basic/NavBar/NavBar.tsx @@ -16,7 +16,6 @@ import { } from '@mui/icons-material'; import RobotAvatar from '../../components/RobotAvatar'; import { AppContext, type UseAppStoreType, closeAll } from '../../contexts/AppContext'; -import { FederationContext, type UseFederationStoreType } from '../../contexts/FederationContext'; import { GarageContext, type UseGarageStoreType } from '../../contexts/GarageContext'; const NavBar = (): JSX.Element => { @@ -33,8 +32,7 @@ const NavBar = (): JSX.Element => { navbarHeight, hostUrl, } = useContext(AppContext); - const { currentOrder } = useContext(FederationContext); - const { garage } = useContext(GarageContext); + const { garage, orderUpdatedAt } = useContext(GarageContext); const navigate = useNavigate(); const location = useLocation(); @@ -67,7 +65,7 @@ const NavBar = (): JSX.Element => { if (isPage(pathPage)) { setPage(pathPage); } - }, [location, navigate, setPage]); + }, [location, navigate, setPage, orderUpdatedAt]); const handleSlideDirection = function (oldPage: Page, newPage: Page): void { const oldPos: number = pagesPosition[oldPage]; @@ -79,10 +77,15 @@ const NavBar = (): JSX.Element => { const changePage = function (mouseEvent: any, newPage: Page): void { if (newPage !== 'none') { + const slot = garage.getSlot(); handleSlideDirection(page, newPage); setPage(newPage); const param = - newPage === 'order' ? `${String(currentOrder.shortAlias)}/${String(currentOrder.id)}` : ''; + newPage === 'order' + ? `${String(slot.activeOrderShortAlias)}/${String( + slot.activeOrderId ?? slot.lastOrderId, + )}` + : ''; setTimeout(() => { navigate(`/${newPage}/${param}`); }, theme.transitions.duration.leavingScreen * 3); @@ -159,7 +162,7 @@ const NavBar = (): JSX.Element => { sx={tabSx} label={smallBar ? undefined : t('Order')} value='order' - disabled={!garage.getRobot().avatarLoaded || currentOrder.id == null} + disabled={!garage.getRobot().avatarLoaded || !garage.getSlot().activeOrderId} icon={} iconPosition='start' /> diff --git a/frontend/src/basic/OrderPage/index.tsx b/frontend/src/basic/OrderPage/index.tsx index 9028fa70..d622958b 100644 --- a/frontend/src/basic/OrderPage/index.tsx +++ b/frontend/src/basic/OrderPage/index.tsx @@ -10,11 +10,12 @@ import { apiClient } from '../../services/api'; import { AppContext, type UseAppStoreType } from '../../contexts/AppContext'; import { FederationContext, type UseFederationStoreType } from '../../contexts/FederationContext'; import { GarageContext, type UseGarageStoreType } from '../../contexts/GarageContext'; +import { type Order } from '../../models'; const OrderPage = (): JSX.Element => { const { windowSize, setOpen, settings, navbarHeight, hostUrl, origin } = useContext(AppContext); - const { setFocusedCoordinator, federation, currentOrder, setCurrentOrder, focusedCoordinator } = + const { setFocusedCoordinator, federation, focusedCoordinator } = useContext(FederationContext); const { garage, badOrder, setBadOrder } = useContext(GarageContext); const { t } = useTranslation(); @@ -26,26 +27,47 @@ const OrderPage = (): JSX.Element => { const [tab, setTab] = useState<'order' | 'contract'>('contract'); const [baseUrl, setBaseUrl] = useState(hostUrl); + const [currentOrder, setCurrentOrder] = useState(null); useEffect(() => { - const newOrder = { - shortAlias: params.shortAlias ?? '', - id: Number(params.orderId) ?? null, - order: null, - }; + const coordinator = federation.getCoordinator(params.shortAlias ?? ''); + const { url, basePath } = coordinator.getEndpoint( + settings.network, + origin, + settings.selfhostedClient, + hostUrl, + ); - const { url, basePath } = federation - .getCoordinator(newOrder.shortAlias) - .getEndpoint(settings.network, origin, settings.selfhostedClient, hostUrl); setBaseUrl(`${url}${basePath}`); - if (currentOrder.id !== newOrder.id || currentOrder.shortAlias !== newOrder.shortAlias) { - setCurrentOrder(newOrder); + if (garage.getSlot().activeOrderId === Number(params.orderId)) { + if (garage.getSlot().order != null) { + setCurrentOrder(garage.getSlot().order); + } else { + coordinator + .fetchOrder(Number(params.orderId) ?? null, garage.getRobot()) + .then((response) => { + setCurrentOrder(response); + garage.updateOrder(response as Order); + }) + .catch((e) => { + console.log(e); + }); + } + } else { + coordinator + .fetchOrder(Number(params.orderId) ?? null, garage.getRobot()) + .then((response) => { + setCurrentOrder(response); + }) + .catch((e) => { + console.log(e); + }); } }, [params]); const onClickCoordinator = function (): void { - if (currentOrder.shortAlias != null) { + if (currentOrder?.shortAlias != null) { setFocusedCoordinator(currentOrder.shortAlias); } setOpen((open) => { @@ -54,7 +76,7 @@ const OrderPage = (): JSX.Element => { }; const renewOrder = function (): void { - const order = currentOrder.order; + const order = currentOrder; if (order !== null && focusedCoordinator != null) { const body = { type: order.type, @@ -73,16 +95,16 @@ const OrderPage = (): JSX.Element => { latitude: order.latitude, longitude: order.longitude, }; - const { url } = federation - .getCoordinator(focusedCoordinator) + const { url, basePath } = federation + .getCoordinator(order.shortAlias) .getEndpoint(settings.network, origin, settings.selfhostedClient, hostUrl); apiClient - .post(url, '/api/make/', body, { tokenSHA256: garage.getRobot().tokenSHA256 }) + .post(url + basePath, '/api/make/', body, { tokenSHA256: garage.getRobot().tokenSHA256 }) .then((data: any) => { if (data.bad_request !== undefined) { setBadOrder(data.bad_request); } else if (data.id !== undefined) { - navigate(`/order/${String(currentOrder.shortAlias)}/${String(data.id)}`); + navigate(`/order/${String(currentOrder?.shortAlias)}/${String(data.id)}`); } }) .catch(() => { @@ -97,14 +119,14 @@ const OrderPage = (): JSX.Element => { return ( - {currentOrder.order === null && badOrder === undefined && } + {currentOrder === null && badOrder === undefined && } {badOrder !== undefined ? ( {t(badOrder)} ) : null} - {currentOrder.order !== null && badOrder === undefined ? ( - currentOrder.order.is_participant ? ( + {currentOrder !== null && badOrder === undefined ? ( + currentOrder.is_participant ? ( windowSize.width > doublePageWidth ? ( // DOUBLE PAPER VIEW { }} > { @@ -145,6 +168,7 @@ const OrderPage = (): JSX.Element => { > { >
{ @@ -190,6 +215,7 @@ const OrderPage = (): JSX.Element => {
{ }} > { diff --git a/frontend/src/basic/RobotPage/RobotProfile.tsx b/frontend/src/basic/RobotPage/RobotProfile.tsx index e14344b9..4d70dbe8 100644 --- a/frontend/src/basic/RobotPage/RobotProfile.tsx +++ b/frontend/src/basic/RobotPage/RobotProfile.tsx @@ -22,7 +22,6 @@ 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, type UseFederationStoreType } from '../../contexts/FederationContext'; interface RobotProfileProps { robot: Robot; @@ -45,7 +44,6 @@ const RobotProfile = ({ width, }: RobotProfileProps): JSX.Element => { const { windowSize, hostUrl } = useContext(AppContext); - const { currentOrder } = useContext(FederationContext); const { garage, robotUpdatedAt } = useContext(GarageContext); const { t } = useTranslation(); @@ -146,28 +144,36 @@ const RobotProfile = ({ )} - {Boolean(garage.getRobot().activeOrderId) && + {Boolean(garage.getSlot().activeOrderId) && garage.getRobot().avatarLoaded && Boolean(garage.getRobot().nickname) ? ( ) : null} - {Boolean(garage.getRobot().lastOrderId) && + {Boolean(garage.getSlot().lastOrderId) && garage.getRobot().avatarLoaded && Boolean(garage.getRobot().nickname) ? (