Skip to content

Commit a5254be

Browse files
fix: some issues with dispute (#884)
1 parent 629d350 commit a5254be

File tree

6 files changed

+81
-30
lines changed

6 files changed

+81
-30
lines changed

src/components/modal/components/Chat/components/EscalateModal/steps/EscalateStepTwo.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -511,9 +511,9 @@ function EscalateStepTwo({
511511
values?.disputeId || "",
512512
[FormModel.formFields.buyerAddress.name]:
513513
values?.buyerAddress ? values?.buyerAddress : "",
514-
[`Unsigned message: ${FormModel.formFields.message.name}`]:
514+
[`Signed message: ${FormModel.formFields.message.name}`]:
515515
values?.message
516-
? `Unsigned message: ${values?.message}`
516+
? `Signed message: ${values?.message}`
517517
: "",
518518
[FormModel.formFields.signature.name]:
519519
values?.signature || ""
@@ -524,7 +524,7 @@ function EscalateStepTwo({
524524
<Input {...FormModel.formFields.buyerAddress} disabled />
525525
<UnsignedMessageWrapper>
526526
<FieldInput
527-
value="Unsigned message:"
527+
value="Signed message:"
528528
disabled
529529
id="unsigned_prefix"
530530
/>

src/lib/utils/dispute.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { subgraph } from "@bosonprotocol/react-kit";
2+
3+
import { getDateTimestamp } from "./getDateTimestamp";
4+
5+
export const getDisputeDates = (
6+
dispute: subgraph.DisputeFieldsFragment | undefined
7+
) => {
8+
const endOfResolutionPeriod = dispute?.timeout
9+
? getDateTimestamp(dispute?.timeout)
10+
: undefined;
11+
const finishedResolutionPeriod = endOfResolutionPeriod
12+
? Date.now() > endOfResolutionPeriod
13+
: false;
14+
return {
15+
endOfResolutionPeriod,
16+
finishedResolutionPeriod
17+
};
18+
};

src/lib/utils/exchange.ts

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,28 @@ export const getHasExchangeDisputeResolutionElapsed = (
3939
return false;
4040
}
4141
return (
42-
Number(exchange.redeemedDate) * 1000 +
43-
Number(offer.disputePeriodDuration) * 1000 <
42+
getDateTimestamp(exchange.redeemedDate) +
43+
getDateTimestamp(offer.disputePeriodDuration) <
4444
Date.now()
4545
);
4646
};
4747

48+
export const getHasExchangeEscalationPeriodElapsed = (
49+
escalationResponsePeriod: string | undefined | null,
50+
escalatedDate: string | undefined | null
51+
): boolean => {
52+
if (!escalationResponsePeriod || !escalatedDate) {
53+
return false;
54+
}
55+
const fixedEscalatedDate = getDateTimestamp(escalatedDate);
56+
const escalationPeriodSecondsNumber = Number(escalationResponsePeriod);
57+
if (!fixedEscalatedDate || isNaN(escalationPeriodSecondsNumber)) {
58+
return false;
59+
}
60+
const escalationPeriodInMs = escalationPeriodSecondsNumber * 1000;
61+
return fixedEscalatedDate + escalationPeriodInMs < Date.now();
62+
};
63+
4864
const PROTOCOL_DEPLOYMENT_TIMES = {
4965
v221: {
5066
local: 0,
@@ -69,10 +85,10 @@ export const getExchangeTokenId = (
6985
};
7086

7187
export const getExchangeDisputeDates = (exchange: Exchange) => {
72-
const raisedDisputeAt = new Date(Number(exchange.disputedDate) * 1000);
88+
const raisedDisputeAt = new Date(getDateTimestamp(exchange.disputedDate));
7389
const lastDayToResolveDispute = new Date(
7490
raisedDisputeAt.getTime() +
75-
Number(exchange.offer.resolutionPeriodDuration) * 1000
91+
getDateTimestamp(exchange.offer.resolutionPeriodDuration)
7692
);
7793
const totalDaysToResolveDispute = dayjs(lastDayToResolveDispute).diff(
7894
raisedDisputeAt,

src/lib/utils/getDateTimestamp.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export const getDateTimestamp = (date: string) => {
1+
export const getDateTimestamp = (date: string | undefined | null): number => {
22
const number = Number(date);
33

44
return !isNaN(number) ? number * 1000 : 0;

src/lib/utils/hooks/useDisputeResolver.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,15 @@ interface Props {
2828
};
2929
}
3030

31-
export function useDisputeResolver(id: string) {
31+
export function useDisputeResolver(id: string | undefined | null) {
3232
const { config } = useConfigContext();
3333
const { subgraphUrl } = config.envConfig;
3434
const props = { id };
3535

3636
const result = useQuery(["disputeResolver", props, subgraphUrl], async () => {
37+
if (!id) {
38+
return;
39+
}
3740
const result = await fetchSubgraph<Props>(
3841
subgraphUrl,
3942
gql`

src/pages/chat/components/ExchangeSidePreview.tsx

Lines changed: 35 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import {
77
buyerAndSellerAgreementIncluding,
88
customisedExchangePolicy
99
} from "lib/constants/policies";
10+
import { getDisputeDates } from "lib/utils/dispute";
11+
import { useDisputeResolver } from "lib/utils/hooks/useDisputeResolver";
1012
import { getExchangePolicyName } from "lib/utils/policy/getExchangePolicyName";
1113
import { ArrowSquareOut } from "phosphor-react";
1214
import {
@@ -44,6 +46,7 @@ import { calcPercentage } from "../../../lib/utils/calcPrice";
4446
import {
4547
getExchangeDisputeDates,
4648
getHasExchangeDisputeResolutionElapsed,
49+
getHasExchangeEscalationPeriodElapsed,
4750
isExchangeCompletableByBuyer,
4851
isExchangeCompletableBySeller
4952
} from "../../../lib/utils/exchange";
@@ -377,7 +380,15 @@ export default memo(function ExchangeSidePreview({
377380
},
378381
{ enabled: !!exchange }
379382
);
383+
const { disputeResolver } = useDisputeResolver(
384+
exchange?.offer.disputeResolverId
385+
);
386+
380387
const dispute = disputes?.[0];
388+
const isElapsedEscalation = getHasExchangeEscalationPeriodElapsed(
389+
disputeResolver?.escalationResponsePeriod,
390+
dispute?.escalatedDate
391+
);
381392
const offer = exchange?.offer;
382393
const { showModal, modalTypes } = useModal();
383394
const OFFER_DETAIL_DATA = useMemo(
@@ -438,6 +449,7 @@ export default memo(function ExchangeSidePreview({
438449
const isEscalated = !!dispute?.escalatedDate;
439450
const isRetracted = !!dispute?.retractedDate;
440451
const isFinalized = !!dispute?.finalizedDate;
452+
const { finishedResolutionPeriod } = getDisputeDates(dispute);
441453

442454
const { totalDaysToResolveDispute, daysLeftToResolveDispute } =
443455
getExchangeDisputeDates(exchange);
@@ -521,7 +533,7 @@ export default memo(function ExchangeSidePreview({
521533
<Section>
522534
<DetailTable align noBorder data={OFFER_DETAIL_DATA ?? ({} as never)} />
523535
</Section>
524-
{isInDispute && iAmTheBuyer && !isEscalated && !isRetracted ? (
536+
{isInDispute && iAmTheBuyer && !isRetracted && !isElapsedEscalation ? (
525537
<CTASection>
526538
<Button
527539
theme="secondary"
@@ -549,26 +561,28 @@ export default memo(function ExchangeSidePreview({
549561
>
550562
Retract
551563
</Button>
552-
<Button
553-
theme="secondary"
554-
onClick={() =>
555-
showModal(
556-
"ESCALATE_MODAL",
557-
{
558-
title: "Escalate",
559-
exchange: exchange,
560-
refetch: refetchItAll,
561-
addMessage,
562-
setHasError,
563-
onSentMessage,
564-
destinationAddress
565-
},
566-
"l"
567-
)
568-
}
569-
>
570-
Escalate
571-
</Button>
564+
{!finishedResolutionPeriod && !isEscalated && (
565+
<Button
566+
theme="secondary"
567+
onClick={() =>
568+
showModal(
569+
"ESCALATE_MODAL",
570+
{
571+
title: "Escalate",
572+
exchange: exchange,
573+
refetch: refetchItAll,
574+
addMessage,
575+
setHasError,
576+
onSentMessage,
577+
destinationAddress
578+
},
579+
"l"
580+
)
581+
}
582+
>
583+
Escalate
584+
</Button>
585+
)}
572586
{CompleteExchangeButton}
573587
</CTASection>
574588
) : isInRedeemed && iAmTheBuyer ? (

0 commit comments

Comments
 (0)