From 667af8f85823416ae186a8e84954395311a395b9 Mon Sep 17 00:00:00 2001 From: pociej Date: Thu, 20 Jun 2024 08:46:46 +0200 Subject: [PATCH] fix: agreement flow close #81 close #82 close #85 --- backend/src/services/yagna/service.ts | 51 +++++++++++-------- frontend/src/components/homePage/action.tsx | 14 +++-- .../src/components/homePage/events/events.tsx | 20 ++++---- .../homePage/statusSections/agreement.tsx | 40 ++++++++++++++- .../homePage/statusSections/allocation.tsx | 22 +++++++- .../homePage/statusSections/approve.tsx | 2 - .../homePage/statusSections/register.tsx | 9 ---- .../providers/flowEventsProvider.tsx | 6 +++ .../src/hooks/events/usePaymentEvents.tsx | 1 - .../src/hooks/yagna/useCurrentAgreement.ts | 2 +- 10 files changed, 111 insertions(+), 56 deletions(-) diff --git a/backend/src/services/yagna/service.ts b/backend/src/services/yagna/service.ts index d239209..b7e803b 100644 --- a/backend/src/services/yagna/service.ts +++ b/backend/src/services/yagna/service.ts @@ -121,12 +121,13 @@ export class Yagna { }); } container.cradle.userService.setCurrentAgreementId(userId, agreement.id); - agreement.events.on("terminated", (e: any) => { - this.agreementEvents.next({ - agreement, - event: "terminated", - }); - }); + // agreement.events.on("terminated", (e: any) => { + // console.log("agreement terminated"); + // this.agreementEvents.next({ + // agreement, + // event: "terminated", + // }); + // }); } async getUserAgreement(userId: string) { @@ -337,20 +338,24 @@ export class Yagna { "creating worker, executor found getting agreement", userId ); - executor - .run(async (ctx: WorkContext) => { - debugLog("yagna", "created worker", userId); - newWorker.context = ctx; - - newWorker.setState("free"); - - resolve(newWorker); - }) - .catch((e: any) => { - console.log("Error", e); - debugLog("yagna", "error creating worker", e); - reject(e); - }); + try { + executor + .run(async (ctx: WorkContext) => { + debugLog("yagna", "created worker", userId); + newWorker.context = ctx; + + newWorker.setState("free"); + + resolve(newWorker); + }) + .catch((e: any) => { + console.log("Error", e); + debugLog("yagna", "error creating worker", e); + reject(e); + }); + } catch (e) { + console.log("Error", e); + } } }); } @@ -377,9 +382,10 @@ export class Yagna { }); debitNoteEvents.forEach(async (event: any) => { - console.log("debit note event", event); + console.log("debit note event"); const debitNoteId = event.debitNoteId; const debitNote = await this.paymentService.getDebitNote(debitNoteId); + console.log("debit note"); this.debitNoteEvents.next({ debitNote, event, @@ -410,6 +416,7 @@ export class Yagna { console.log("invoice event", event); const invoiceId = event.invoiceId; const invoice = await this.paymentService.getInvoice(invoiceId); + console.log("invoice"); this.invoiceEvents.next({ invoice, event, id: uuidv4() }); }); } @@ -439,7 +446,7 @@ export class Yagna { if (event.eventType === "AgreementTerminatedEvent") { container.cradle.userService.onAgreementTerminated(agreementId); } - + console.log("agreement event"); this.agreementEvents.next({ id: uuidv4(), agreement, diff --git a/frontend/src/components/homePage/action.tsx b/frontend/src/components/homePage/action.tsx index 684ce7d..fd9df06 100644 --- a/frontend/src/components/homePage/action.tsx +++ b/frontend/src/components/homePage/action.tsx @@ -11,7 +11,6 @@ import { useDepositPaymentEvents } from "hooks/events/usePaymentEvents"; import { useFlowEvents } from "components/providers/flowEventsProvider"; import { init, set } from "ramda"; import { AnimatePresence, Variants, motion } from "framer-motion"; -console.log("Action", copy); export const Action = () => { const { user } = useUser(); //TODO : this logic should be move outside of this component @@ -48,13 +47,9 @@ export const Action = () => { } }, [user.state]); - useEffect(() => { - console.log("user.state", state); - }, [state]); - const { events$: scanResults$ } = useScanResults(); const { events$: yagnaEvents$ } = useYagnaEvents(); - const { events$: flowEvents$ } = useFlowEvents(); + const { events$: flowEvents$, restartSession } = useFlowEvents(); const { events$: paymentEvents$ } = useDepositPaymentEvents(); useEffect(() => { const scanResultSub = scanResults$.subscribe((event) => { @@ -80,6 +75,9 @@ export const Action = () => { if (event === "releaseAgreement") { setState("WAITING_FOR_PROVIDER_PAYMENT"); } + if (event === "restartSession") { + setState(user.state); + } }); const paymentsSub = paymentEvents$.subscribe((event) => { @@ -92,7 +90,6 @@ export const Action = () => { }); } if (event.kind === Event.DEPOSIT_FEE_PAYMENT) { - console.log("DEPOSIT_FEE_PAYMENT"); setState("DEPOSIT_RELEASED"); } }); @@ -143,7 +140,8 @@ export const Action = () => { )} diff --git a/frontend/src/components/homePage/statusSections/allocation.tsx b/frontend/src/components/homePage/statusSections/allocation.tsx index 51c3821..0e7a743 100644 --- a/frontend/src/components/homePage/statusSections/allocation.tsx +++ b/frontend/src/components/homePage/statusSections/allocation.tsx @@ -9,6 +9,7 @@ import { formatBalance } from "utils/formatBalance"; import { Tooltip } from "react-daisyui"; import { useEffect, useState } from "react"; import { Bip } from "components/atoms/bip"; +import { useFlowEvents } from "components/providers/flowEventsProvider"; export const Allocation = () => { const { isCreating, createAllocation } = useCreateAllocation(); const { currentAllocation } = useCurrentAllocation(); @@ -16,12 +17,29 @@ export const Allocation = () => { const { user } = useUser(); const [isCreateAllocationButtonActive, setIsCreateAllocationButtonActive] = useState(false); + const [canButtonBeActive, setCanButtonBeActive] = useState(true); + + const { events$: flowEvents$ } = useFlowEvents(); useEffect(() => { setIsCreateAllocationButtonActive( - user.hasDeposit() && !user.hasAllocation() + user.hasDeposit() && !user.hasAllocation() && canButtonBeActive ); - }, [user.hasAllocation(), user.hasDeposit()]); + }, [user.hasAllocation(), user.hasDeposit(), canButtonBeActive]); + + useEffect(() => { + const sub = flowEvents$.subscribe((event) => { + if (event === "restartSession") { + setCanButtonBeActive(true); + } + if (event === "releaseAllocation") { + setCanButtonBeActive(false); + } + }); + return () => { + sub.unsubscribe(); + }; + }, []); return (
diff --git a/frontend/src/components/homePage/statusSections/approve.tsx b/frontend/src/components/homePage/statusSections/approve.tsx index 1510749..3fe31ce 100644 --- a/frontend/src/components/homePage/statusSections/approve.tsx +++ b/frontend/src/components/homePage/statusSections/approve.tsx @@ -21,8 +21,6 @@ export const Approve = () => { const [isApproveButtonActive, setIsApproveButtonActive] = useState(false); useEffect(() => { - console.log(user.hasEnoughAllowance()); - console.log(user.isRegistered()); setIsApproveButtonActive(user.isRegistered() && !user.hasEnoughAllowance()); }, [user.hasEnoughAllowance(), user.isRegistered()]); return ( diff --git a/frontend/src/components/homePage/statusSections/register.tsx b/frontend/src/components/homePage/statusSections/register.tsx index 998ea94..807a62e 100644 --- a/frontend/src/components/homePage/statusSections/register.tsx +++ b/frontend/src/components/homePage/statusSections/register.tsx @@ -6,15 +6,6 @@ export const Register = () => { const { user } = useUser(); const { events$: flowEvents$, releaseAgreement } = useFlowEvents(); - useEffect(() => { - const subscription = flowEvents$.subscribe((event) => { - console.log("flow event", event); - }); - - return () => { - subscription.unsubscribe(); - }; - }, []); return (
diff --git a/frontend/src/components/providers/flowEventsProvider.tsx b/frontend/src/components/providers/flowEventsProvider.tsx index 0bf5bb6..81ef54f 100644 --- a/frontend/src/components/providers/flowEventsProvider.tsx +++ b/frontend/src/components/providers/flowEventsProvider.tsx @@ -12,6 +12,9 @@ const FlowEventsContext = createContext({ releaseAllocation() { this.events$.next("releaseAllocation"); }, + restartSession() { + this.events$.next("restartSession"); + }, }); export const FlowEventsProvider = ({ children }: PropsWithChildren) => { @@ -29,6 +32,9 @@ export const FlowEventsProvider = ({ children }: PropsWithChildren) => { releaseAllocation() { events.current.next("releaseAllocation"); }, + restartSession() { + events.current.next("restartSession"); + }, }} > {children} diff --git a/frontend/src/hooks/events/usePaymentEvents.tsx b/frontend/src/hooks/events/usePaymentEvents.tsx index 6741415..c1b6bee 100644 --- a/frontend/src/hooks/events/usePaymentEvents.tsx +++ b/frontend/src/hooks/events/usePaymentEvents.tsx @@ -29,7 +29,6 @@ const useDepositEvent = ({ const onLogs = useCallback((logs: any) => { logs.forEach((log: any) => { - console.log("got e", log); const e = { txHash: log.transactionHash, amount: Number(log.args.amount), diff --git a/frontend/src/hooks/yagna/useCurrentAgreement.ts b/frontend/src/hooks/yagna/useCurrentAgreement.ts index 663ec1e..5c93476 100644 --- a/frontend/src/hooks/yagna/useCurrentAgreement.ts +++ b/frontend/src/hooks/yagna/useCurrentAgreement.ts @@ -32,8 +32,8 @@ export const useCurrentAgreement = () => { setIsPaused(true); } if (event.kind === Event.AGREEMENT_SIGNED) { - setIsPaused(false); mutate(); + setIsPaused(false); } }); return () => {