Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Kloet/big pr #2145

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions frontend/jest-setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ jest.mock("./src/lib/constants/environment.constants.ts", () => ({
ENABLE_METRICS: false,
FORCE_CALL_STRATEGY: undefined,
QR_CODE_RENDERED: true,
IS_TEST_ENV: true,
}));

global.localStorage = localStorageMock;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<script lang="ts">
import { SnsSwapLifecycle } from "@dfinity/sns";
import type { SnsSummary } from "$lib/types/sns";
import { getContext, onDestroy } from "svelte";
import { getContext } from "svelte";
import { BottomSheet } from "@dfinity/gix-components";
import {
PROJECT_DETAIL_CONTEXT_KEY,
Expand All @@ -16,32 +16,19 @@
import Tooltip from "$lib/components/ui/Tooltip.svelte";
import SignInGuard from "$lib/components/common/SignInGuard.svelte";
import type { Principal } from "@dfinity/principal";
import { isNullish, nonNullish } from "@dfinity/utils";
import { nonNullish } from "@dfinity/utils";
import { snsTicketsStore } from "$lib/stores/sns-tickets.store";
import {
cancelPollGetOpenTicket,
hidePollingToast,
restoreSnsSaleParticipation,
} from "$lib/services/sns-sale.services";
import { isSignedIn } from "$lib/utils/auth.utils";
import { authStore } from "$lib/stores/auth.store";
import {
getCommitmentE8s,
hasOpenTicketInProcess,
} from "$lib/utils/sns.utils";
import { hasOpenTicketInProcess } from "$lib/utils/sns.utils";
import type { TicketStatus } from "$lib/types/sale";
import type { SaleStep } from "$lib/types/sale";
import SaleInProgressModal from "$lib/modals/sns/sale/SaleInProgressModal.svelte";
import SpinnerText from "$lib/components/ui/SpinnerText.svelte";

const { store: projectDetailStore, reload } =
getContext<ProjectDetailContext>(PROJECT_DETAIL_CONTEXT_KEY);
const { store: projectDetailStore } = getContext<ProjectDetailContext>(
PROJECT_DETAIL_CONTEXT_KEY
);

let lifecycle: number;
let swapCanisterId: Principal;
$: ({
swap: { lifecycle },
swapCanisterId,
} =
$projectDetailStore.summary ??
({
Expand All @@ -58,19 +45,12 @@
swapCommitment: $projectDetailStore.swapCommitment,
});

let userCommitment: undefined | bigint;
$: userCommitment =
// swapCommitment=null - not initialized yet
$projectDetailStore.swapCommitment === null
? undefined
: getCommitmentE8s($projectDetailStore.swapCommitment) ?? BigInt(0);

let rootCanisterId: Principal | undefined;
$: rootCanisterId = nonNullish($projectDetailStore?.summary?.rootCanisterId)
? $projectDetailStore?.summary?.rootCanisterId
: undefined;

// busy if open ticket is available or not requested
// TODO: Receive this as props
let status: TicketStatus = "unknown";
$: ({ status } = hasOpenTicketInProcess({
rootCanisterId,
Expand All @@ -80,72 +60,10 @@
let busy = true;
$: busy = status !== "none";

// Flag to avoid second getOpenTicket call on same page navigation
let loadingTicketRootCanisterId: string | undefined;

let progressStep: SaleStep | undefined = undefined;

const updateTicket = async (swapCanisterId: Principal) => {
// Avoid second call for the same rootCanisterId
if (
rootCanisterId === undefined ||
loadingTicketRootCanisterId === rootCanisterId.toText()
) {
return;
}
if (isNullish(userCommitment)) {
// Typescript guard, user commitment cannot be undefined here
return;
}
loadingTicketRootCanisterId = rootCanisterId.toText();

const updateProgress = (step: SaleStep) => (progressStep = step);

await restoreSnsSaleParticipation({
rootCanisterId,
userCommitment,
swapCanisterId,
postprocess: reload,
updateProgress,
});
};

// skip ticket update if
// - the sns is not open
// - the user is not sign in
// - user commitment information is not loaded
// - project swap canister id is not loaded, needed for the ticket call
$: if (
lifecycle === SnsSwapLifecycle.Open &&
isSignedIn($authStore.identity) &&
nonNullish(userCommitment) &&
nonNullish(swapCanisterId)
) {
updateTicket(swapCanisterId);
}

let userHasParticipatedToSwap = false;
$: userHasParticipatedToSwap = hasUserParticipatedToSwap({
swapCommitment: $projectDetailStore.swapCommitment,
});

onDestroy(() => {
if (rootCanisterId === undefined) {
return;
}

// remove the ticket to stop sale-participation-retry from another pages because of the non-obvious UX
snsTicketsStore.setTicket({
rootCanisterId,
ticket: undefined,
});
// TODO: Improve cancellatoin of actions onDestroy
// The polling was triggered by `restoreSnsSaleParticipation` call and needs to be canceled explicitly.
cancelPollGetOpenTicket();

// Hide toasts when moving away from the page
hidePollingToast();
});
</script>

{#if lifecycle === SnsSwapLifecycle.Open}
Expand Down Expand Up @@ -189,10 +107,6 @@
</BottomSheet>
{/if}

{#if status === "open" && nonNullish(progressStep)}
<SaleInProgressModal {progressStep} />
{/if}

{#if showModal}
<ParticipateSwapModal on:nnsClose={closeModal} />
{/if}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
<div class="actions content-cell-details">
{#if myCommitmentIcp !== undefined}
<div>
<KeyValuePair>
<KeyValuePair testId="sns-user-commitment">
<ProjectUserCommitmentLabel
slot="key"
summary={$projectDetailStore.summary}
Expand Down
2 changes: 2 additions & 0 deletions frontend/src/lib/constants/environment.constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,5 @@ export const FORCE_CALL_STRATEGY: "query" | undefined = undefined;
// We use a constant / environment variable here because we set it to `true` for test purpose.
// Indeed, we are running the jest suite as if the QR code would be rendered because Jest as trouble loading the QR-code dependency and because the QR-code content is anyway covered by e2e snapshot testing in gix-cmp.
export const QR_CODE_RENDERED = false;

export const IS_TEST_ENV = process.env.NODE_ENV === "test";
Loading