2022-10-24 18:16:15 +00:00
|
|
|
import { useRef } from 'react';
|
|
|
|
import { AsyncRenderer, Icon, Intent } from '@vegaprotocol/ui-toolkit';
|
2022-11-17 21:35:29 +00:00
|
|
|
import { useClosePosition, usePositionsData, PositionsTable } from '../';
|
2022-10-19 23:59:36 +00:00
|
|
|
import type { AgGridReact } from 'ag-grid-react';
|
2022-10-24 18:16:15 +00:00
|
|
|
import { Requested } from './close-position-dialog/requested';
|
|
|
|
import { Complete } from './close-position-dialog/complete';
|
|
|
|
import type { TransactionResult } from '@vegaprotocol/wallet';
|
|
|
|
import { t } from '@vegaprotocol/react-helpers';
|
2022-03-30 00:01:34 +00:00
|
|
|
|
|
|
|
interface PositionsManagerProps {
|
|
|
|
partyId: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
export const PositionsManager = ({ partyId }: PositionsManagerProps) => {
|
2022-10-19 23:59:36 +00:00
|
|
|
const gridRef = useRef<AgGridReact | null>(null);
|
2022-11-02 07:01:40 +00:00
|
|
|
const { data, error, loading } = usePositionsData(partyId, gridRef);
|
2022-10-24 18:16:15 +00:00
|
|
|
const {
|
|
|
|
submit,
|
|
|
|
closingOrder,
|
|
|
|
closingOrderResult,
|
|
|
|
transaction,
|
|
|
|
transactionResult,
|
|
|
|
Dialog,
|
|
|
|
} = useClosePosition();
|
|
|
|
|
2022-03-30 00:01:34 +00:00
|
|
|
return (
|
2022-09-02 20:53:00 +00:00
|
|
|
<>
|
2022-09-22 11:09:12 +00:00
|
|
|
<AsyncRenderer loading={loading} error={error} data={data}>
|
2022-10-19 23:59:36 +00:00
|
|
|
<PositionsTable
|
|
|
|
ref={gridRef}
|
2022-11-02 07:01:40 +00:00
|
|
|
rowData={data}
|
2022-10-24 18:16:15 +00:00
|
|
|
onClose={(position) => submit(position)}
|
2022-10-19 23:59:36 +00:00
|
|
|
/>
|
2022-09-02 20:53:00 +00:00
|
|
|
</AsyncRenderer>
|
2022-10-24 18:16:15 +00:00
|
|
|
<Dialog
|
|
|
|
intent={getDialogIntent(transactionResult)}
|
|
|
|
icon={getDialogIcon(transactionResult)}
|
|
|
|
title={getDialogTitle(transactionResult)}
|
|
|
|
content={{
|
|
|
|
Requested: <Requested partyId={partyId} order={closingOrder} />,
|
|
|
|
Complete: (
|
|
|
|
<Complete
|
|
|
|
partyId={partyId}
|
|
|
|
closingOrder={closingOrder}
|
|
|
|
closingOrderResult={closingOrderResult}
|
|
|
|
transaction={transaction}
|
|
|
|
transactionResult={transactionResult}
|
|
|
|
/>
|
|
|
|
),
|
|
|
|
}}
|
|
|
|
/>
|
2022-09-02 20:53:00 +00:00
|
|
|
</>
|
2022-03-30 00:01:34 +00:00
|
|
|
);
|
|
|
|
};
|
2022-10-24 18:16:15 +00:00
|
|
|
|
|
|
|
const getDialogIntent = (transactionResult?: TransactionResult) => {
|
|
|
|
if (!transactionResult) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (
|
|
|
|
transactionResult &&
|
|
|
|
'error' in transactionResult &&
|
|
|
|
transactionResult.error
|
|
|
|
) {
|
|
|
|
return Intent.Danger;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Intent.Success;
|
|
|
|
};
|
|
|
|
|
|
|
|
const getDialogIcon = (transactionResult?: TransactionResult) => {
|
|
|
|
if (!transactionResult) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (transactionResult.status) {
|
|
|
|
return <Icon name="tick" />;
|
|
|
|
}
|
|
|
|
|
|
|
|
return <Icon name="error" />;
|
|
|
|
};
|
|
|
|
|
|
|
|
const getDialogTitle = (transactionResult?: TransactionResult) => {
|
|
|
|
if (!transactionResult) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (transactionResult.status) {
|
|
|
|
return t('Position closed');
|
|
|
|
}
|
|
|
|
|
|
|
|
return t('Position not closed');
|
|
|
|
};
|