mirror of
https://github.com/Reckless-Satoshi/robosats.git
synced 2024-09-11 11:55:26 +03: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 { windowSize } = useContext<UseAppStoreType>(AppContext);
|
||||
const { setDelay } = useContext<UseFederationStoreType>(FederationContext);
|
||||
const { setDelay, setCurrentOrderId } = useContext<UseFederationStoreType>(FederationContext);
|
||||
const { garage } = useContext<UseGarageStoreType>(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);
|
||||
|
@ -14,7 +14,8 @@ import { GarageContext, type UseGarageStoreType } from '../../contexts/GarageCon
|
||||
|
||||
const MakerPage = (): JSX.Element => {
|
||||
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 { 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 => {
|
||||
>
|
||||
<MakerForm
|
||||
onOrderCreated={(shortAlias, id) => {
|
||||
setCurrentOrderId({ id, shortAlias });
|
||||
navigate(`/order/${shortAlias}/${id}`);
|
||||
}}
|
||||
disableRequest={matches.length > 0 && !showMatches}
|
||||
|
@ -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<UseAppStoreType>(AppContext);
|
||||
const { garage, orderUpdatedAt, robotUpdatedAt } = useContext<UseGarageStoreType>(GarageContext);
|
||||
const { setCurrentOrderId } = useContext<UseFederationStoreType>(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);
|
||||
|
@ -24,8 +24,9 @@ const OrderPage = (): JSX.Element => {
|
||||
hostUrl,
|
||||
origin,
|
||||
} = useContext<UseAppStoreType>(AppContext);
|
||||
const { federation } = useContext<UseFederationStoreType>(FederationContext);
|
||||
const { garage, badOrder, setBadOrder } = useContext<UseGarageStoreType>(GarageContext);
|
||||
const { federation, currentOrder, currentOrderId, setCurrentOrderId } =
|
||||
useContext<UseFederationStoreType>(FederationContext);
|
||||
const { badOrder } = useContext<UseGarageStoreType>(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<string>(hostUrl);
|
||||
const [currentOrder, setCurrentOrder] = useState<Order | null>(null);
|
||||
const [currentOrderId, setCurrentOrderId] = useState<number | null>(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 => {
|
||||
<OrderDetails
|
||||
shortAlias={String(currentOrder.shortAlias)}
|
||||
currentOrder={currentOrder}
|
||||
updateCurrentOrder={updateCurrentOrder}
|
||||
onClickCoordinator={onClickCoordinator}
|
||||
onClickGenerateRobot={() => {
|
||||
navigate('/robot');
|
||||
|
@ -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<UseAppStoreType>(AppContext);
|
||||
const { garage, robotUpdatedAt, orderUpdatedAt } = useContext<UseGarageStoreType>(GarageContext);
|
||||
const { setCurrentOrderId } = useContext<UseFederationStoreType>(FederationContext);
|
||||
|
||||
const { t } = useTranslation();
|
||||
const theme = useTheme();
|
||||
@ -152,6 +154,7 @@ const RobotProfile = ({
|
||||
<Grid item>
|
||||
<Button
|
||||
onClick={() => {
|
||||
setCurrentOrderId({ id: robot?.activeOrderId, shortAlias: slot?.activeShortAlias });
|
||||
navigate(
|
||||
`/order/${String(slot?.activeShortAlias)}/${String(robot?.activeOrderId)}`,
|
||||
);
|
||||
@ -167,6 +170,7 @@ const RobotProfile = ({
|
||||
<Grid item>
|
||||
<Button
|
||||
onClick={() => {
|
||||
setCurrentOrderId({ id: robot?.lastOrderId, shortAlias: slot?.activeShortAlias });
|
||||
navigate(`/order/${String(slot?.lastShortAlias)}/${String(robot?.lastOrderId)}`);
|
||||
}}
|
||||
>
|
||||
|
@ -32,7 +32,6 @@ import { type UseFederationStoreType, FederationContext } from '../../contexts/F
|
||||
interface TakeButtonProps {
|
||||
currentOrder: Order;
|
||||
info?: Info;
|
||||
updateCurrentOrder?: () => void;
|
||||
onClickGenerateRobot?: () => void;
|
||||
}
|
||||
|
||||
@ -45,14 +44,13 @@ const closeAll = { inactiveMaker: false, confirmation: false };
|
||||
const TakeButton = ({
|
||||
currentOrder,
|
||||
info,
|
||||
updateCurrentOrder = () => null,
|
||||
onClickGenerateRobot = () => null,
|
||||
}: TakeButtonProps): JSX.Element => {
|
||||
const { t } = useTranslation();
|
||||
const theme = useTheme();
|
||||
const { settings, origin, hostUrl } = useContext<UseAppStoreType>(AppContext);
|
||||
const { garage, orderUpdatedAt } = useContext<UseGarageStoreType>(GarageContext);
|
||||
const { federation } = useContext<UseFederationStoreType>(FederationContext);
|
||||
const { federation, setCurrentOrderId } = useContext<UseFederationStoreType>(FederationContext);
|
||||
|
||||
const [takeAmount, setTakeAmount] = useState<string>('');
|
||||
const [badRequest, setBadRequest] = useState<string>('');
|
||||
@ -324,6 +322,7 @@ const TakeButton = ({
|
||||
const { url, basePath } = federation
|
||||
.getCoordinator(currentOrder.shortAlias)
|
||||
.getEndpoint(settings.network, origin, settings.selfhostedClient, hostUrl);
|
||||
setCurrentOrderId({ id: null, shortAlias: null });
|
||||
apiClient
|
||||
.post(
|
||||
url + basePath,
|
||||
@ -338,7 +337,7 @@ const TakeButton = ({
|
||||
if (data?.bad_request !== undefined) {
|
||||
setBadRequest(data.bad_request);
|
||||
} else {
|
||||
updateCurrentOrder();
|
||||
setCurrentOrderId({ id: currentOrder?.id, shortAlias: currentOrder?.shortAlias });
|
||||
setBadRequest('');
|
||||
}
|
||||
})
|
||||
|
@ -47,7 +47,6 @@ import { type Order } from '../../models';
|
||||
interface OrderDetailsProps {
|
||||
shortAlias: string;
|
||||
currentOrder: Order;
|
||||
updateCurrentOrder?: () => void;
|
||||
onClickCoordinator?: () => void;
|
||||
onClickGenerateRobot?: () => void;
|
||||
}
|
||||
@ -55,7 +54,6 @@ interface OrderDetailsProps {
|
||||
const OrderDetails = ({
|
||||
shortAlias,
|
||||
currentOrder,
|
||||
updateCurrentOrder = () => null,
|
||||
onClickCoordinator = () => null,
|
||||
onClickGenerateRobot = () => null,
|
||||
}: OrderDetailsProps): JSX.Element => {
|
||||
@ -534,7 +532,6 @@ const OrderDetails = ({
|
||||
<TakeButton
|
||||
currentOrder={currentOrder}
|
||||
info={coordinator.info}
|
||||
updateCurrentOrder={updateCurrentOrder}
|
||||
onClickGenerateRobot={onClickGenerateRobot}
|
||||
/>
|
||||
</Grid>
|
||||
|
@ -31,6 +31,7 @@ import { UserNinjaIcon } from '../Icons';
|
||||
import { getWebln } from '../../utils';
|
||||
import { signCleartextMessage } from '../../pgp';
|
||||
import { GarageContext, type UseGarageStoreType } from '../../contexts/GarageContext';
|
||||
import { FederationContext, UseFederationStoreType } from '../../contexts/FederationContext';
|
||||
|
||||
interface Props {
|
||||
coordinator: Coordinator;
|
||||
@ -39,6 +40,7 @@ interface Props {
|
||||
|
||||
const RobotInfo: React.FC<Props> = ({ coordinator, onClose }: Props) => {
|
||||
const { garage } = useContext<UseGarageStoreType>(GarageContext);
|
||||
const { setCurrentOrderId } = useContext<UseFederationStoreType>(FederationContext);
|
||||
const navigate = useNavigate();
|
||||
const { t } = useTranslation();
|
||||
|
||||
@ -133,6 +135,10 @@ const RobotInfo: React.FC<Props> = ({ coordinator, onClose }: Props) => {
|
||||
{slot?.activeShortAlias === coordinator.shortAlias ? (
|
||||
<ListItemButton
|
||||
onClick={() => {
|
||||
setCurrentOrderId({
|
||||
id: slot?.activeShortAlias,
|
||||
shortAlias: slot?.getRobot(slot?.activeShortAlias ?? '')?.activeOrderId,
|
||||
});
|
||||
navigate(
|
||||
`/order/${String(slot?.activeShortAlias)}/${String(
|
||||
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 ? (
|
||||
<ListItemButton
|
||||
onClick={() => {
|
||||
setCurrentOrderId({
|
||||
id: slot?.activeShortAlias,
|
||||
shortAlias: slot?.getRobot(slot?.activeShortAlias ?? '')?.lastOrderId,
|
||||
});
|
||||
navigate(
|
||||
`/order/${String(slot?.lastShortAlias)}/${String(
|
||||
slot?.getRobot(slot?.lastShortAlias ?? '')?.lastOrderId,
|
||||
|
@ -23,7 +23,7 @@ const CancelButton = ({
|
||||
|
||||
const showCancelButton =
|
||||
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 noConfirmation =
|
||||
Boolean(order?.is_maker && [0, 1, 2].includes(order?.status)) ||
|
||||
|
@ -9,7 +9,7 @@ import React, {
|
||||
ReactNode,
|
||||
} from 'react';
|
||||
|
||||
import { type Coordinator, type Order, Federation } from '../models';
|
||||
import { type Order, Federation } from '../models';
|
||||
|
||||
import { federationLottery } from '../utils';
|
||||
|
||||
@ -40,6 +40,11 @@ const statusToDelay = [
|
||||
300000, // 'Taker lost dispute'
|
||||
];
|
||||
|
||||
export interface CurrentOrderIdProps {
|
||||
id: number | null;
|
||||
shortAlias: string | null;
|
||||
}
|
||||
|
||||
export interface FederationContextProviderProps {
|
||||
children: ReactNode;
|
||||
}
|
||||
@ -48,6 +53,9 @@ export interface UseFederationStoreType {
|
||||
federation: Federation;
|
||||
sortedCoordinators: string[];
|
||||
setDelay: Dispatch<SetStateAction<number>>;
|
||||
currentOrderId: CurrentOrderIdProps;
|
||||
setCurrentOrderId: Dispatch<SetStateAction<CurrentOrderIdProps>>;
|
||||
currentOrder: Order | null;
|
||||
coordinatorUpdatedAt: string;
|
||||
federationUpdatedAt: string;
|
||||
}
|
||||
@ -56,6 +64,9 @@ export const initialFederationContext: UseFederationStoreType = {
|
||||
federation: new Federation(),
|
||||
sortedCoordinators: [],
|
||||
setDelay: () => {},
|
||||
currentOrderId: { id: null, shortAlias: null },
|
||||
setCurrentOrderId: () => {},
|
||||
currentOrder: null,
|
||||
coordinatorUpdatedAt: '',
|
||||
federationUpdatedAt: '',
|
||||
};
|
||||
@ -67,8 +78,7 @@ export const FederationContextProvider = ({
|
||||
}: FederationContextProviderProps): JSX.Element => {
|
||||
const { settings, page, origin, hostUrl, open, torStatus } =
|
||||
useContext<UseAppStoreType>(AppContext);
|
||||
const { setMaker, garage, setBadOrder, robotUpdatedAt } =
|
||||
useContext<UseGarageStoreType>(GarageContext);
|
||||
const { setMaker, garage, setBadOrder } = useContext<UseGarageStoreType>(GarageContext);
|
||||
const [federation, setFederation] = useState(initialFederationContext.federation);
|
||||
const sortedCoordinators = useMemo(() => {
|
||||
const sortedCoordinators = federationLottery(federation);
|
||||
@ -81,6 +91,12 @@ export const FederationContextProvider = ({
|
||||
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 [timer, setTimer] = useState<NodeJS.Timer | undefined>(() =>
|
||||
@ -106,6 +122,7 @@ export const FederationContextProvider = ({
|
||||
newDelay = 99999999;
|
||||
setBadOrder(order.bad_request);
|
||||
garage.updateOrder(null);
|
||||
setCurrentOrder(null);
|
||||
}
|
||||
if (order?.id) {
|
||||
newDelay =
|
||||
@ -115,9 +132,11 @@ export const FederationContextProvider = ({
|
||||
: statusToDelay[order.status] * 5 // If user is not looking at "order" tab, refresh less often.
|
||||
: 99999999;
|
||||
garage.updateOrder(order);
|
||||
setCurrentOrder(order);
|
||||
setBadOrder(undefined);
|
||||
}
|
||||
clearInterval(timer);
|
||||
console.log('New Delay:', newDelay);
|
||||
setDelay(newDelay);
|
||||
setTimer(setTimeout(fetchCurrentOrder, newDelay));
|
||||
};
|
||||
@ -125,7 +144,12 @@ export const FederationContextProvider = ({
|
||||
const fetchCurrentOrder: () => void = () => {
|
||||
const slot = garage?.getSlot();
|
||||
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);
|
||||
void coordinator?.fetchOrder(robot.activeOrderId, robot, slot.token).then((order) => {
|
||||
onOrderReceived(order as Order);
|
||||
@ -137,12 +161,15 @@ export const FederationContextProvider = ({
|
||||
};
|
||||
|
||||
useEffect(() => {
|
||||
clearInterval(timer);
|
||||
fetchCurrentOrder();
|
||||
if (currentOrderId.id && currentOrderId.shortAlias) {
|
||||
setCurrentOrder(null);
|
||||
clearInterval(timer);
|
||||
fetchCurrentOrder();
|
||||
}
|
||||
return () => {
|
||||
clearInterval(timer);
|
||||
};
|
||||
}, [page]);
|
||||
}, [currentOrderId]);
|
||||
|
||||
useEffect(() => {
|
||||
if (page === 'offers') void federation.updateBook();
|
||||
@ -160,7 +187,6 @@ export const FederationContextProvider = ({
|
||||
// use effects to fetchRobots on Profile open
|
||||
useEffect(() => {
|
||||
const slot = garage.getSlot();
|
||||
const robot = slot?.getRobot();
|
||||
|
||||
if (open.profile && slot?.hashId && slot?.token) {
|
||||
void federation.fetchRobot(garage, slot?.token); // refresh/update existing robot
|
||||
@ -172,6 +198,9 @@ export const FederationContextProvider = ({
|
||||
value={{
|
||||
federation,
|
||||
sortedCoordinators,
|
||||
currentOrderId,
|
||||
setCurrentOrderId,
|
||||
currentOrder,
|
||||
setDelay,
|
||||
coordinatorUpdatedAt,
|
||||
federationUpdatedAt,
|
||||
|
Loading…
Reference in New Issue
Block a user