Skip to content

Commit

Permalink
feat: add flow events
Browse files Browse the repository at this point in the history
  • Loading branch information
pociej committed Jun 19, 2024
1 parent 9ed717a commit 140f684
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 22 deletions.
33 changes: 24 additions & 9 deletions frontend/src/components/homePage/action.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,28 +12,32 @@ import { useDepositReleasedEvents } from "hooks/events/useDepositReleasedEvents"
import { useYagnaEvents } from "hooks/events/useYagnaEvents";
import { Event } from "types/events";
import { useDepositPaymentEvents } from "hooks/events/usePaymentEvents";
import { useFlowEvents } from "components/providers/flowEventsProvider";
export const Action = () => {
const currentAgreement = useCurrentAgreement();

const { user } = useUser();

//TODO : this logic should be move outside of this component
// probably we should extend user state provider to handle this

const [state, setState] = useState<
UserState | "HAS_FILE_SCANNED" | "AGREEMENT_RELEASED" | "DEPOSIT_RELEASED"
| UserState
| "HAS_FILE_SCANNED"
| "AGREEMENT_RELEASED"
| "DEPOSIT_RELEASED"
| "WAITING_FOR_PROVIDER_PAYMENT"
| "WAITING_FOR_FEE_PAYMENT"
>(user.state);

useEffect(() => {
if (
!["HAS_FILE_SCANNED", "AGREEMENT_RELEASED", "DEPOSIT_RELEASED"].includes(
state
)
) {
setState(user.state);
}
setState(user.state);
}, [user.state]);

const { events$: scanResults$ } = useScanResults();
const { events$: depositResults$ } = useDepositPaymentEvents();
const { events$: yagnaEvents$ } = useYagnaEvents();
const { events$: flowEvents$ } = useFlowEvents();
useEffect(() => {
const subscription = scanResults$.subscribe((event) => {
console.log(event);
Expand All @@ -49,7 +53,7 @@ export const Action = () => {
const subscription2 = depositResults$.subscribe((event) => {
console.log(event);
if (event.kind === Event.DEPOSIT_FEE_PAYMENT) {
// setState("DEPOSIT_RELEASED");
setState("DEPOSIT_RELEASED");
}
});

Expand All @@ -59,10 +63,21 @@ export const Action = () => {
}
});

const subscription4 = flowEvents$.subscribe((event) => {
if (event === "releaseAllocation") {
setState("WAITING_FOR_FEE_PAYMENT");
}

if (event === "releaseAgreement") {
setState("WAITING_FOR_PROVIDER_PAYMENT");
}
});

return () => {
subscription.unsubscribe();
subscription2.unsubscribe();
subscription3.unsubscribe();
subscription4.unsubscribe();
};
}, []);

Expand Down
31 changes: 22 additions & 9 deletions frontend/src/components/providers/flowEventsProvider.tsx
Original file line number Diff line number Diff line change
@@ -1,23 +1,36 @@
import { F } from "ramda";
import { PropsWithChildren, createContext, useContext } from "react";
import { PropsWithChildren, createContext, useContext, useRef } from "react";
import { Subject } from "rxjs";

const FlowEventsContext = createContext({
events$: new Subject(),
closeSession: () => {
console.log("close");
closeSession() {
this.events$.next("close");
},
releaseAgreement() {
this.events$.next("releaseAgreement");
},
releaseAllocation() {
this.events$.next("releaseAllocation");
},
});

export const FlowEventsProvider = ({ children }: PropsWithChildren) => {
const flowEvents = new Subject();
const closeSession = () => {
console.log("close");
flowEvents.complete();
};
const flowEvents = useRef({
events$: new Subject(),
closeSession() {
this.events$.next("close");
},
releaseAgreement() {
this.events$.next("releaseAgreement");
},
releaseAllocation() {
this.events$.next("releaseAllocation");
},
});

return (
<FlowEventsContext.Provider value={{ events$: flowEvents, closeSession }}>
<FlowEventsContext.Provider value={flowEvents.current}>
{children}
</FlowEventsContext.Provider>
);
Expand Down
4 changes: 3 additions & 1 deletion frontend/src/components/providers/userProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -163,11 +163,13 @@ const userActionReducer = (
.with(UserAction.HAS_AGREEMENT, () => UserState.HAS_AGREEMENT)
.otherwise(() => user.state);

console.log("state", state);
console.log("kind of action", kind);
const newUser = {
...user,
...payload,
state:
UserStateOrderValue[state] > UserStateOrderValue[user.state]
UserStateOrderValue[state] >= UserStateOrderValue[user.state]
? state
: user.state,
};
Expand Down
10 changes: 8 additions & 2 deletions frontend/src/hooks/yagna/useReleaseAgreement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ import axios from "axios";
import useSWRMutation from "swr/mutation";

import { useActionDebounce } from "hooks/useActionDbounce";
import { useFlowEvents } from "components/providers/flowEventsProvider";

export const useReleaseAgreement = () => {
const { trigger, isMutating } = useSWRMutation(
const { releaseAgreement } = useFlowEvents();
const { trigger, isMutating, data } = useSWRMutation(
`${import.meta.env.VITE_BACKEND_HTTP_URL}/release-agreement`,
function (url, { arg }: { arg: string }) {
return axios.post(url, {
Expand All @@ -15,7 +17,11 @@ export const useReleaseAgreement = () => {

const isReleasing = useActionDebounce(isMutating, 1000);
return {
releaseAgreement: trigger,
releaseAgreement: (arg?: any) => {
trigger(arg).then(() => {
releaseAgreement();
});
},
isReleasing,
};
};
8 changes: 7 additions & 1 deletion frontend/src/hooks/yagna/useReleaseAllocation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ import axios from "axios";
import useSWRMutation from "swr/mutation";

import { useActionDebounce } from "hooks/useActionDbounce";
import { useFlowEvents } from "components/providers/flowEventsProvider";

export const useReleaseAllocation = () => {
const { releaseAllocation } = useFlowEvents();
const { trigger, isMutating } = useSWRMutation(
`${import.meta.env.VITE_BACKEND_HTTP_URL}/me`,
function () {
Expand All @@ -14,7 +16,11 @@ export const useReleaseAllocation = () => {
);
const isReleasing = useActionDebounce(isMutating, 1000);
return {
releaseAllocation: trigger,
releaseAllocation: (arg?: any) => {
trigger(arg).then(() => {
releaseAllocation();
});
},
isReleasing,
};
};

0 comments on commit 140f684

Please sign in to comment.