mirror of
https://github.com/RoboSats/robosats.git
synced 2025-02-07 13:59:06 +00:00
Fix currentOrder fetch
This commit is contained in:
parent
27b265220e
commit
ed9dd3599f
@ -17,7 +17,7 @@ import { GarageContext, type UseGarageStoreType } from '../../contexts/GarageCon
|
|||||||
|
|
||||||
const BookPage = (): JSX.Element => {
|
const BookPage = (): JSX.Element => {
|
||||||
const { windowSize } = useContext<UseAppStoreType>(AppContext);
|
const { windowSize } = useContext<UseAppStoreType>(AppContext);
|
||||||
const { setDelay } = useContext<UseFederationStoreType>(FederationContext);
|
const { setDelay, setCurrentOrderId } = useContext<UseFederationStoreType>(FederationContext);
|
||||||
const { garage } = useContext<UseGarageStoreType>(GarageContext);
|
const { garage } = useContext<UseGarageStoreType>(GarageContext);
|
||||||
const { t } = useTranslation();
|
const { t } = useTranslation();
|
||||||
const navigate = useNavigate();
|
const navigate = useNavigate();
|
||||||
@ -33,6 +33,7 @@ const BookPage = (): JSX.Element => {
|
|||||||
const onOrderClicked = function (id: number, shortAlias: string): void {
|
const onOrderClicked = function (id: number, shortAlias: string): void {
|
||||||
if (garage.getSlot()?.hashId) {
|
if (garage.getSlot()?.hashId) {
|
||||||
setDelay(10000);
|
setDelay(10000);
|
||||||
|
setCurrentOrderId({ id, shortAlias });
|
||||||
navigate(`/order/${shortAlias}/${id}`);
|
navigate(`/order/${shortAlias}/${id}`);
|
||||||
} else {
|
} else {
|
||||||
setOpenNoRobot(true);
|
setOpenNoRobot(true);
|
||||||
|
@ -14,7 +14,8 @@ import { GarageContext, type UseGarageStoreType } from '../../contexts/GarageCon
|
|||||||
|
|
||||||
const MakerPage = (): JSX.Element => {
|
const MakerPage = (): JSX.Element => {
|
||||||
const { fav, windowSize, navbarHeight } = useContext<UseAppStoreType>(AppContext);
|
const { fav, windowSize, navbarHeight } = useContext<UseAppStoreType>(AppContext);
|
||||||
const { federation, setDelay } = useContext<UseFederationStoreType>(FederationContext);
|
const { federation, setDelay, setCurrentOrderId } =
|
||||||
|
useContext<UseFederationStoreType>(FederationContext);
|
||||||
const { garage, maker } = useContext<UseGarageStoreType>(GarageContext);
|
const { garage, maker } = useContext<UseGarageStoreType>(GarageContext);
|
||||||
const { t } = useTranslation();
|
const { t } = useTranslation();
|
||||||
const navigate = useNavigate();
|
const navigate = useNavigate();
|
||||||
@ -53,6 +54,7 @@ const MakerPage = (): JSX.Element => {
|
|||||||
const onOrderClicked = function (id: number, shortAlias: string): void {
|
const onOrderClicked = function (id: number, shortAlias: string): void {
|
||||||
if (garage.getSlot()?.hashId) {
|
if (garage.getSlot()?.hashId) {
|
||||||
setDelay(10000);
|
setDelay(10000);
|
||||||
|
setCurrentOrderId({ id, shortAlias });
|
||||||
navigate(`/order/${shortAlias}/${id}`);
|
navigate(`/order/${shortAlias}/${id}`);
|
||||||
} else {
|
} else {
|
||||||
setOpenNoRobot(true);
|
setOpenNoRobot(true);
|
||||||
@ -103,6 +105,7 @@ const MakerPage = (): JSX.Element => {
|
|||||||
>
|
>
|
||||||
<MakerForm
|
<MakerForm
|
||||||
onOrderCreated={(shortAlias, id) => {
|
onOrderCreated={(shortAlias, id) => {
|
||||||
|
setCurrentOrderId({ id, shortAlias });
|
||||||
navigate(`/order/${shortAlias}/${id}`);
|
navigate(`/order/${shortAlias}/${id}`);
|
||||||
}}
|
}}
|
||||||
disableRequest={matches.length > 0 && !showMatches}
|
disableRequest={matches.length > 0 && !showMatches}
|
||||||
|
@ -17,6 +17,7 @@ import {
|
|||||||
import RobotAvatar from '../../components/RobotAvatar';
|
import RobotAvatar from '../../components/RobotAvatar';
|
||||||
import { AppContext, type UseAppStoreType, closeAll } from '../../contexts/AppContext';
|
import { AppContext, type UseAppStoreType, closeAll } from '../../contexts/AppContext';
|
||||||
import { GarageContext, type UseGarageStoreType } from '../../contexts/GarageContext';
|
import { GarageContext, type UseGarageStoreType } from '../../contexts/GarageContext';
|
||||||
|
import { FederationContext, UseFederationStoreType } from '../../contexts/FederationContext';
|
||||||
|
|
||||||
const NavBar = (): JSX.Element => {
|
const NavBar = (): JSX.Element => {
|
||||||
const theme = useTheme();
|
const theme = useTheme();
|
||||||
@ -24,6 +25,7 @@ const NavBar = (): JSX.Element => {
|
|||||||
const { page, setPage, settings, setSlideDirection, open, setOpen, windowSize, navbarHeight } =
|
const { page, setPage, settings, setSlideDirection, open, setOpen, windowSize, navbarHeight } =
|
||||||
useContext<UseAppStoreType>(AppContext);
|
useContext<UseAppStoreType>(AppContext);
|
||||||
const { garage, orderUpdatedAt, robotUpdatedAt } = useContext<UseGarageStoreType>(GarageContext);
|
const { garage, orderUpdatedAt, robotUpdatedAt } = useContext<UseGarageStoreType>(GarageContext);
|
||||||
|
const { setCurrentOrderId } = useContext<UseFederationStoreType>(FederationContext);
|
||||||
|
|
||||||
const navigate = useNavigate();
|
const navigate = useNavigate();
|
||||||
const location = useLocation();
|
const location = useLocation();
|
||||||
@ -75,13 +77,14 @@ const NavBar = (): JSX.Element => {
|
|||||||
const slot = garage.getSlot();
|
const slot = garage.getSlot();
|
||||||
handleSlideDirection(page, newPage);
|
handleSlideDirection(page, newPage);
|
||||||
setPage(newPage);
|
setPage(newPage);
|
||||||
|
const shortAlias = String(slot?.activeShortAlias);
|
||||||
|
const activeOrderId = slot?.getRobot(slot?.activeShortAlias ?? '')?.activeOrderId;
|
||||||
|
const lastOrderId = slot?.getRobot(slot?.lastShortAlias ?? '')?.lastOrderId;
|
||||||
const param =
|
const param =
|
||||||
newPage === 'order'
|
newPage === 'order' ? `${shortAlias}/${String(activeOrderId ?? lastOrderId)}` : '';
|
||||||
? `${String(slot?.activeShortAlias)}/${String(
|
if (shortAlias && (activeOrderId || lastOrderId)) {
|
||||||
slot?.getRobot(slot?.activeShortAlias ?? '')?.activeOrderId ??
|
setCurrentOrderId({ id: activeOrderId ?? lastOrderId, shortAlias });
|
||||||
slot?.getRobot(slot?.lastShortAlias ?? '')?.lastOrderId,
|
}
|
||||||
)}`
|
|
||||||
: '';
|
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
navigate(`/${newPage}/${param}`);
|
navigate(`/${newPage}/${param}`);
|
||||||
}, theme.transitions.duration.leavingScreen * 3);
|
}, theme.transitions.duration.leavingScreen * 3);
|
||||||
|
@ -24,8 +24,9 @@ const OrderPage = (): JSX.Element => {
|
|||||||
hostUrl,
|
hostUrl,
|
||||||
origin,
|
origin,
|
||||||
} = useContext<UseAppStoreType>(AppContext);
|
} = useContext<UseAppStoreType>(AppContext);
|
||||||
const { federation } = useContext<UseFederationStoreType>(FederationContext);
|
const { federation, currentOrder, currentOrderId, setCurrentOrderId } =
|
||||||
const { garage, badOrder, setBadOrder } = useContext<UseGarageStoreType>(GarageContext);
|
useContext<UseFederationStoreType>(FederationContext);
|
||||||
|
const { badOrder } = useContext<UseGarageStoreType>(GarageContext);
|
||||||
const { t } = useTranslation();
|
const { t } = useTranslation();
|
||||||
const navigate = useNavigate();
|
const navigate = useNavigate();
|
||||||
const params = useParams();
|
const params = useParams();
|
||||||
@ -35,11 +36,10 @@ const OrderPage = (): JSX.Element => {
|
|||||||
|
|
||||||
const [tab, setTab] = useState<'order' | 'contract'>('contract');
|
const [tab, setTab] = useState<'order' | 'contract'>('contract');
|
||||||
const [baseUrl, setBaseUrl] = useState<string>(hostUrl);
|
const [baseUrl, setBaseUrl] = useState<string>(hostUrl);
|
||||||
const [currentOrder, setCurrentOrder] = useState<Order | null>(null);
|
|
||||||
const [currentOrderId, setCurrentOrderId] = useState<number | null>(null);
|
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const coordinator = federation.getCoordinator(params.shortAlias ?? '');
|
const shortAlias = params.shortAlias;
|
||||||
|
const coordinator = federation.getCoordinator(shortAlias ?? '');
|
||||||
const { url, basePath } = coordinator.getEndpoint(
|
const { url, basePath } = coordinator.getEndpoint(
|
||||||
settings.network,
|
settings.network,
|
||||||
origin,
|
origin,
|
||||||
@ -50,40 +50,11 @@ const OrderPage = (): JSX.Element => {
|
|||||||
setBaseUrl(`${url}${basePath}`);
|
setBaseUrl(`${url}${basePath}`);
|
||||||
|
|
||||||
const orderId = Number(params.orderId);
|
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 });
|
if (!acknowledgedWarning) setOpen({ ...closeAll, warning: true });
|
||||||
}, [params]);
|
}, [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 {
|
const onClickCoordinator = function (): void {
|
||||||
if (currentOrder?.shortAlias != null) {
|
if (currentOrder?.shortAlias != null) {
|
||||||
setOpen((open) => {
|
setOpen((open) => {
|
||||||
@ -100,7 +71,6 @@ const OrderPage = (): JSX.Element => {
|
|||||||
<OrderDetails
|
<OrderDetails
|
||||||
shortAlias={String(currentOrder.shortAlias)}
|
shortAlias={String(currentOrder.shortAlias)}
|
||||||
currentOrder={currentOrder}
|
currentOrder={currentOrder}
|
||||||
updateCurrentOrder={updateCurrentOrder}
|
|
||||||
onClickCoordinator={onClickCoordinator}
|
onClickCoordinator={onClickCoordinator}
|
||||||
onClickGenerateRobot={() => {
|
onClickGenerateRobot={() => {
|
||||||
navigate('/robot');
|
navigate('/robot');
|
||||||
|
@ -22,6 +22,7 @@ import { AppContext, type UseAppStoreType } from '../../contexts/AppContext';
|
|||||||
import { genBase62Token } from '../../utils';
|
import { genBase62Token } from '../../utils';
|
||||||
import { LoadingButton } from '@mui/lab';
|
import { LoadingButton } from '@mui/lab';
|
||||||
import { GarageContext, type UseGarageStoreType } from '../../contexts/GarageContext';
|
import { GarageContext, type UseGarageStoreType } from '../../contexts/GarageContext';
|
||||||
|
import { FederationContext, UseFederationStoreType } from '../../contexts/FederationContext';
|
||||||
|
|
||||||
interface RobotProfileProps {
|
interface RobotProfileProps {
|
||||||
robot: Robot;
|
robot: Robot;
|
||||||
@ -45,6 +46,7 @@ const RobotProfile = ({
|
|||||||
}: RobotProfileProps): JSX.Element => {
|
}: RobotProfileProps): JSX.Element => {
|
||||||
const { windowSize } = useContext<UseAppStoreType>(AppContext);
|
const { windowSize } = useContext<UseAppStoreType>(AppContext);
|
||||||
const { garage, robotUpdatedAt, orderUpdatedAt } = useContext<UseGarageStoreType>(GarageContext);
|
const { garage, robotUpdatedAt, orderUpdatedAt } = useContext<UseGarageStoreType>(GarageContext);
|
||||||
|
const { setCurrentOrderId } = useContext<UseFederationStoreType>(FederationContext);
|
||||||
|
|
||||||
const { t } = useTranslation();
|
const { t } = useTranslation();
|
||||||
const theme = useTheme();
|
const theme = useTheme();
|
||||||
@ -152,6 +154,7 @@ const RobotProfile = ({
|
|||||||
<Grid item>
|
<Grid item>
|
||||||
<Button
|
<Button
|
||||||
onClick={() => {
|
onClick={() => {
|
||||||
|
setCurrentOrderId({ id: robot?.activeOrderId, shortAlias: slot?.activeShortAlias });
|
||||||
navigate(
|
navigate(
|
||||||
`/order/${String(slot?.activeShortAlias)}/${String(robot?.activeOrderId)}`,
|
`/order/${String(slot?.activeShortAlias)}/${String(robot?.activeOrderId)}`,
|
||||||
);
|
);
|
||||||
@ -167,6 +170,7 @@ const RobotProfile = ({
|
|||||||
<Grid item>
|
<Grid item>
|
||||||
<Button
|
<Button
|
||||||
onClick={() => {
|
onClick={() => {
|
||||||
|
setCurrentOrderId({ id: robot?.lastOrderId, shortAlias: slot?.activeShortAlias });
|
||||||
navigate(`/order/${String(slot?.lastShortAlias)}/${String(robot?.lastOrderId)}`);
|
navigate(`/order/${String(slot?.lastShortAlias)}/${String(robot?.lastOrderId)}`);
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
|
@ -32,7 +32,6 @@ import { type UseFederationStoreType, FederationContext } from '../../contexts/F
|
|||||||
interface TakeButtonProps {
|
interface TakeButtonProps {
|
||||||
currentOrder: Order;
|
currentOrder: Order;
|
||||||
info?: Info;
|
info?: Info;
|
||||||
updateCurrentOrder?: () => void;
|
|
||||||
onClickGenerateRobot?: () => void;
|
onClickGenerateRobot?: () => void;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -45,14 +44,13 @@ const closeAll = { inactiveMaker: false, confirmation: false };
|
|||||||
const TakeButton = ({
|
const TakeButton = ({
|
||||||
currentOrder,
|
currentOrder,
|
||||||
info,
|
info,
|
||||||
updateCurrentOrder = () => null,
|
|
||||||
onClickGenerateRobot = () => null,
|
onClickGenerateRobot = () => null,
|
||||||
}: TakeButtonProps): JSX.Element => {
|
}: TakeButtonProps): JSX.Element => {
|
||||||
const { t } = useTranslation();
|
const { t } = useTranslation();
|
||||||
const theme = useTheme();
|
const theme = useTheme();
|
||||||
const { settings, origin, hostUrl } = useContext<UseAppStoreType>(AppContext);
|
const { settings, origin, hostUrl } = useContext<UseAppStoreType>(AppContext);
|
||||||
const { garage, orderUpdatedAt } = useContext<UseGarageStoreType>(GarageContext);
|
const { garage, orderUpdatedAt } = useContext<UseGarageStoreType>(GarageContext);
|
||||||
const { federation } = useContext<UseFederationStoreType>(FederationContext);
|
const { federation, setCurrentOrderId } = useContext<UseFederationStoreType>(FederationContext);
|
||||||
|
|
||||||
const [takeAmount, setTakeAmount] = useState<string>('');
|
const [takeAmount, setTakeAmount] = useState<string>('');
|
||||||
const [badRequest, setBadRequest] = useState<string>('');
|
const [badRequest, setBadRequest] = useState<string>('');
|
||||||
@ -324,6 +322,7 @@ const TakeButton = ({
|
|||||||
const { url, basePath } = federation
|
const { url, basePath } = federation
|
||||||
.getCoordinator(currentOrder.shortAlias)
|
.getCoordinator(currentOrder.shortAlias)
|
||||||
.getEndpoint(settings.network, origin, settings.selfhostedClient, hostUrl);
|
.getEndpoint(settings.network, origin, settings.selfhostedClient, hostUrl);
|
||||||
|
setCurrentOrderId({ id: null, shortAlias: null });
|
||||||
apiClient
|
apiClient
|
||||||
.post(
|
.post(
|
||||||
url + basePath,
|
url + basePath,
|
||||||
@ -338,7 +337,7 @@ const TakeButton = ({
|
|||||||
if (data?.bad_request !== undefined) {
|
if (data?.bad_request !== undefined) {
|
||||||
setBadRequest(data.bad_request);
|
setBadRequest(data.bad_request);
|
||||||
} else {
|
} else {
|
||||||
updateCurrentOrder();
|
setCurrentOrderId({ id: currentOrder?.id, shortAlias: currentOrder?.shortAlias });
|
||||||
setBadRequest('');
|
setBadRequest('');
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
@ -47,7 +47,6 @@ import { type Order } from '../../models';
|
|||||||
interface OrderDetailsProps {
|
interface OrderDetailsProps {
|
||||||
shortAlias: string;
|
shortAlias: string;
|
||||||
currentOrder: Order;
|
currentOrder: Order;
|
||||||
updateCurrentOrder?: () => void;
|
|
||||||
onClickCoordinator?: () => void;
|
onClickCoordinator?: () => void;
|
||||||
onClickGenerateRobot?: () => void;
|
onClickGenerateRobot?: () => void;
|
||||||
}
|
}
|
||||||
@ -55,7 +54,6 @@ interface OrderDetailsProps {
|
|||||||
const OrderDetails = ({
|
const OrderDetails = ({
|
||||||
shortAlias,
|
shortAlias,
|
||||||
currentOrder,
|
currentOrder,
|
||||||
updateCurrentOrder = () => null,
|
|
||||||
onClickCoordinator = () => null,
|
onClickCoordinator = () => null,
|
||||||
onClickGenerateRobot = () => null,
|
onClickGenerateRobot = () => null,
|
||||||
}: OrderDetailsProps): JSX.Element => {
|
}: OrderDetailsProps): JSX.Element => {
|
||||||
@ -534,7 +532,6 @@ const OrderDetails = ({
|
|||||||
<TakeButton
|
<TakeButton
|
||||||
currentOrder={currentOrder}
|
currentOrder={currentOrder}
|
||||||
info={coordinator.info}
|
info={coordinator.info}
|
||||||
updateCurrentOrder={updateCurrentOrder}
|
|
||||||
onClickGenerateRobot={onClickGenerateRobot}
|
onClickGenerateRobot={onClickGenerateRobot}
|
||||||
/>
|
/>
|
||||||
</Grid>
|
</Grid>
|
||||||
|
@ -31,6 +31,7 @@ import { UserNinjaIcon } from '../Icons';
|
|||||||
import { getWebln } from '../../utils';
|
import { getWebln } from '../../utils';
|
||||||
import { signCleartextMessage } from '../../pgp';
|
import { signCleartextMessage } from '../../pgp';
|
||||||
import { GarageContext, type UseGarageStoreType } from '../../contexts/GarageContext';
|
import { GarageContext, type UseGarageStoreType } from '../../contexts/GarageContext';
|
||||||
|
import { FederationContext, UseFederationStoreType } from '../../contexts/FederationContext';
|
||||||
|
|
||||||
interface Props {
|
interface Props {
|
||||||
coordinator: Coordinator;
|
coordinator: Coordinator;
|
||||||
@ -39,6 +40,7 @@ interface Props {
|
|||||||
|
|
||||||
const RobotInfo: React.FC<Props> = ({ coordinator, onClose }: Props) => {
|
const RobotInfo: React.FC<Props> = ({ coordinator, onClose }: Props) => {
|
||||||
const { garage } = useContext<UseGarageStoreType>(GarageContext);
|
const { garage } = useContext<UseGarageStoreType>(GarageContext);
|
||||||
|
const { setCurrentOrderId } = useContext<UseFederationStoreType>(FederationContext);
|
||||||
const navigate = useNavigate();
|
const navigate = useNavigate();
|
||||||
const { t } = useTranslation();
|
const { t } = useTranslation();
|
||||||
|
|
||||||
@ -133,6 +135,10 @@ const RobotInfo: React.FC<Props> = ({ coordinator, onClose }: Props) => {
|
|||||||
{slot?.activeShortAlias === coordinator.shortAlias ? (
|
{slot?.activeShortAlias === coordinator.shortAlias ? (
|
||||||
<ListItemButton
|
<ListItemButton
|
||||||
onClick={() => {
|
onClick={() => {
|
||||||
|
setCurrentOrderId({
|
||||||
|
id: slot?.activeShortAlias,
|
||||||
|
shortAlias: slot?.getRobot(slot?.activeShortAlias ?? '')?.activeOrderId,
|
||||||
|
});
|
||||||
navigate(
|
navigate(
|
||||||
`/order/${String(slot?.activeShortAlias)}/${String(
|
`/order/${String(slot?.activeShortAlias)}/${String(
|
||||||
slot?.getRobot(slot?.activeShortAlias ?? '')?.activeOrderId,
|
slot?.getRobot(slot?.activeShortAlias ?? '')?.activeOrderId,
|
||||||
@ -156,6 +162,10 @@ const RobotInfo: React.FC<Props> = ({ coordinator, onClose }: Props) => {
|
|||||||
) : (robot?.lastOrderId ?? 0) > 0 && slot?.lastShortAlias === coordinator.shortAlias ? (
|
) : (robot?.lastOrderId ?? 0) > 0 && slot?.lastShortAlias === coordinator.shortAlias ? (
|
||||||
<ListItemButton
|
<ListItemButton
|
||||||
onClick={() => {
|
onClick={() => {
|
||||||
|
setCurrentOrderId({
|
||||||
|
id: slot?.activeShortAlias,
|
||||||
|
shortAlias: slot?.getRobot(slot?.activeShortAlias ?? '')?.lastOrderId,
|
||||||
|
});
|
||||||
navigate(
|
navigate(
|
||||||
`/order/${String(slot?.lastShortAlias)}/${String(
|
`/order/${String(slot?.lastShortAlias)}/${String(
|
||||||
slot?.getRobot(slot?.lastShortAlias ?? '')?.lastOrderId,
|
slot?.getRobot(slot?.lastShortAlias ?? '')?.lastOrderId,
|
||||||
|
@ -23,7 +23,7 @@ const CancelButton = ({
|
|||||||
|
|
||||||
const showCancelButton =
|
const showCancelButton =
|
||||||
Boolean(order?.is_maker && [0, 1, 2].includes(order?.status)) ||
|
Boolean(order?.is_maker && [0, 1, 2].includes(order?.status)) ||
|
||||||
Boolean([3, 7].includes(order?.status ?? -1));
|
Boolean([3, 6, 7].includes(order?.status ?? -1));
|
||||||
const showCollabCancelButton = order?.status === 9 && !order?.asked_for_cancel;
|
const showCollabCancelButton = order?.status === 9 && !order?.asked_for_cancel;
|
||||||
const noConfirmation =
|
const noConfirmation =
|
||||||
Boolean(order?.is_maker && [0, 1, 2].includes(order?.status)) ||
|
Boolean(order?.is_maker && [0, 1, 2].includes(order?.status)) ||
|
||||||
|
@ -9,7 +9,7 @@ import React, {
|
|||||||
ReactNode,
|
ReactNode,
|
||||||
} from 'react';
|
} from 'react';
|
||||||
|
|
||||||
import { type Coordinator, type Order, Federation } from '../models';
|
import { type Order, Federation } from '../models';
|
||||||
|
|
||||||
import { federationLottery } from '../utils';
|
import { federationLottery } from '../utils';
|
||||||
|
|
||||||
@ -40,6 +40,11 @@ const statusToDelay = [
|
|||||||
300000, // 'Taker lost dispute'
|
300000, // 'Taker lost dispute'
|
||||||
];
|
];
|
||||||
|
|
||||||
|
export interface CurrentOrderIdProps {
|
||||||
|
id: number | null;
|
||||||
|
shortAlias: string | null;
|
||||||
|
}
|
||||||
|
|
||||||
export interface FederationContextProviderProps {
|
export interface FederationContextProviderProps {
|
||||||
children: ReactNode;
|
children: ReactNode;
|
||||||
}
|
}
|
||||||
@ -48,6 +53,9 @@ export interface UseFederationStoreType {
|
|||||||
federation: Federation;
|
federation: Federation;
|
||||||
sortedCoordinators: string[];
|
sortedCoordinators: string[];
|
||||||
setDelay: Dispatch<SetStateAction<number>>;
|
setDelay: Dispatch<SetStateAction<number>>;
|
||||||
|
currentOrderId: CurrentOrderIdProps;
|
||||||
|
setCurrentOrderId: Dispatch<SetStateAction<CurrentOrderIdProps>>;
|
||||||
|
currentOrder: Order | null;
|
||||||
coordinatorUpdatedAt: string;
|
coordinatorUpdatedAt: string;
|
||||||
federationUpdatedAt: string;
|
federationUpdatedAt: string;
|
||||||
}
|
}
|
||||||
@ -56,6 +64,9 @@ export const initialFederationContext: UseFederationStoreType = {
|
|||||||
federation: new Federation(),
|
federation: new Federation(),
|
||||||
sortedCoordinators: [],
|
sortedCoordinators: [],
|
||||||
setDelay: () => {},
|
setDelay: () => {},
|
||||||
|
currentOrderId: { id: null, shortAlias: null },
|
||||||
|
setCurrentOrderId: () => {},
|
||||||
|
currentOrder: null,
|
||||||
coordinatorUpdatedAt: '',
|
coordinatorUpdatedAt: '',
|
||||||
federationUpdatedAt: '',
|
federationUpdatedAt: '',
|
||||||
};
|
};
|
||||||
@ -67,8 +78,7 @@ export const FederationContextProvider = ({
|
|||||||
}: FederationContextProviderProps): JSX.Element => {
|
}: FederationContextProviderProps): JSX.Element => {
|
||||||
const { settings, page, origin, hostUrl, open, torStatus } =
|
const { settings, page, origin, hostUrl, open, torStatus } =
|
||||||
useContext<UseAppStoreType>(AppContext);
|
useContext<UseAppStoreType>(AppContext);
|
||||||
const { setMaker, garage, setBadOrder, robotUpdatedAt } =
|
const { setMaker, garage, setBadOrder } = useContext<UseGarageStoreType>(GarageContext);
|
||||||
useContext<UseGarageStoreType>(GarageContext);
|
|
||||||
const [federation, setFederation] = useState(initialFederationContext.federation);
|
const [federation, setFederation] = useState(initialFederationContext.federation);
|
||||||
const sortedCoordinators = useMemo(() => {
|
const sortedCoordinators = useMemo(() => {
|
||||||
const sortedCoordinators = federationLottery(federation);
|
const sortedCoordinators = federationLottery(federation);
|
||||||
@ -81,6 +91,12 @@ export const FederationContextProvider = ({
|
|||||||
new Date().toISOString(),
|
new Date().toISOString(),
|
||||||
);
|
);
|
||||||
const [federationUpdatedAt, setFederationUpdatedAt] = useState<string>(new Date().toISOString());
|
const [federationUpdatedAt, setFederationUpdatedAt] = useState<string>(new Date().toISOString());
|
||||||
|
const [currentOrderId, setCurrentOrderId] = useState<CurrentOrderIdProps>(
|
||||||
|
initialFederationContext.currentOrderId,
|
||||||
|
);
|
||||||
|
const [currentOrder, setCurrentOrder] = useState<Order | null>(
|
||||||
|
initialFederationContext.currentOrder,
|
||||||
|
);
|
||||||
|
|
||||||
const [delay, setDelay] = useState<number>(defaultDelay);
|
const [delay, setDelay] = useState<number>(defaultDelay);
|
||||||
const [timer, setTimer] = useState<NodeJS.Timer | undefined>(() =>
|
const [timer, setTimer] = useState<NodeJS.Timer | undefined>(() =>
|
||||||
@ -106,6 +122,7 @@ export const FederationContextProvider = ({
|
|||||||
newDelay = 99999999;
|
newDelay = 99999999;
|
||||||
setBadOrder(order.bad_request);
|
setBadOrder(order.bad_request);
|
||||||
garage.updateOrder(null);
|
garage.updateOrder(null);
|
||||||
|
setCurrentOrder(null);
|
||||||
}
|
}
|
||||||
if (order?.id) {
|
if (order?.id) {
|
||||||
newDelay =
|
newDelay =
|
||||||
@ -115,9 +132,11 @@ export const FederationContextProvider = ({
|
|||||||
: statusToDelay[order.status] * 5 // If user is not looking at "order" tab, refresh less often.
|
: statusToDelay[order.status] * 5 // If user is not looking at "order" tab, refresh less often.
|
||||||
: 99999999;
|
: 99999999;
|
||||||
garage.updateOrder(order);
|
garage.updateOrder(order);
|
||||||
|
setCurrentOrder(order);
|
||||||
setBadOrder(undefined);
|
setBadOrder(undefined);
|
||||||
}
|
}
|
||||||
clearInterval(timer);
|
clearInterval(timer);
|
||||||
|
console.log('New Delay:', newDelay);
|
||||||
setDelay(newDelay);
|
setDelay(newDelay);
|
||||||
setTimer(setTimeout(fetchCurrentOrder, newDelay));
|
setTimer(setTimeout(fetchCurrentOrder, newDelay));
|
||||||
};
|
};
|
||||||
@ -125,7 +144,12 @@ export const FederationContextProvider = ({
|
|||||||
const fetchCurrentOrder: () => void = () => {
|
const fetchCurrentOrder: () => void = () => {
|
||||||
const slot = garage?.getSlot();
|
const slot = garage?.getSlot();
|
||||||
const robot = slot?.getRobot();
|
const robot = slot?.getRobot();
|
||||||
if (slot?.token && slot?.activeShortAlias && robot?.activeOrderId) {
|
if (currentOrderId.id && currentOrderId.shortAlias) {
|
||||||
|
const coordinator = federation.getCoordinator(currentOrderId.shortAlias);
|
||||||
|
void coordinator?.fetchOrder(currentOrderId.id, robot, slot.token).then((order) => {
|
||||||
|
onOrderReceived(order as Order);
|
||||||
|
});
|
||||||
|
} else if (slot?.token && slot?.activeShortAlias && robot?.activeOrderId) {
|
||||||
const coordinator = federation.getCoordinator(slot.activeShortAlias);
|
const coordinator = federation.getCoordinator(slot.activeShortAlias);
|
||||||
void coordinator?.fetchOrder(robot.activeOrderId, robot, slot.token).then((order) => {
|
void coordinator?.fetchOrder(robot.activeOrderId, robot, slot.token).then((order) => {
|
||||||
onOrderReceived(order as Order);
|
onOrderReceived(order as Order);
|
||||||
@ -137,12 +161,15 @@ export const FederationContextProvider = ({
|
|||||||
};
|
};
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
|
if (currentOrderId.id && currentOrderId.shortAlias) {
|
||||||
|
setCurrentOrder(null);
|
||||||
clearInterval(timer);
|
clearInterval(timer);
|
||||||
fetchCurrentOrder();
|
fetchCurrentOrder();
|
||||||
|
}
|
||||||
return () => {
|
return () => {
|
||||||
clearInterval(timer);
|
clearInterval(timer);
|
||||||
};
|
};
|
||||||
}, [page]);
|
}, [currentOrderId]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (page === 'offers') void federation.updateBook();
|
if (page === 'offers') void federation.updateBook();
|
||||||
@ -160,7 +187,6 @@ export const FederationContextProvider = ({
|
|||||||
// use effects to fetchRobots on Profile open
|
// use effects to fetchRobots on Profile open
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const slot = garage.getSlot();
|
const slot = garage.getSlot();
|
||||||
const robot = slot?.getRobot();
|
|
||||||
|
|
||||||
if (open.profile && slot?.hashId && slot?.token) {
|
if (open.profile && slot?.hashId && slot?.token) {
|
||||||
void federation.fetchRobot(garage, slot?.token); // refresh/update existing robot
|
void federation.fetchRobot(garage, slot?.token); // refresh/update existing robot
|
||||||
@ -172,6 +198,9 @@ export const FederationContextProvider = ({
|
|||||||
value={{
|
value={{
|
||||||
federation,
|
federation,
|
||||||
sortedCoordinators,
|
sortedCoordinators,
|
||||||
|
currentOrderId,
|
||||||
|
setCurrentOrderId,
|
||||||
|
currentOrder,
|
||||||
setDelay,
|
setDelay,
|
||||||
coordinatorUpdatedAt,
|
coordinatorUpdatedAt,
|
||||||
federationUpdatedAt,
|
federationUpdatedAt,
|
||||||
|
Loading…
Reference in New Issue
Block a user