-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: changed QRCodeModal to be normal component instead of modal
- Loading branch information
1 parent
25e3383
commit f35a218
Showing
6 changed files
with
132 additions
and
86 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
<script lang="ts"> | ||
import { QRCodeReader } from '@dfinity/gix-components'; | ||
import { toastsError } from '$lib/stores/toasts.store'; | ||
import { decodeQrCode } from '$lib/utils/qr-code.utils'; | ||
import { nonNullish } from '@dfinity/utils'; | ||
import type { Token } from '@dfinity/utils'; | ||
import type { QrStatus } from '$lib/types/qr-code'; | ||
import { i18n } from '$lib/stores/i18n.store'; | ||
import { createEventDispatcher, onDestroy, onMount } from 'svelte'; | ||
import ButtonGroup from '$lib/components/ui/ButtonGroup.svelte'; | ||
export let expectedToken: Token; | ||
export let destination: string | undefined; | ||
export let amount: number | undefined; | ||
const dispatch = createEventDispatcher(); | ||
let timeoutId: NodeJS.Timeout | undefined; | ||
let resolveQrCodePromise: | ||
| (({ status, code }: { status: QrStatus; code?: string }) => void) | ||
| undefined = undefined; | ||
onMount(async () => { | ||
await scanQrCode(); | ||
}); | ||
onDestroy(() => { | ||
if (nonNullish(timeoutId)) { | ||
clearTimeout(timeoutId); | ||
} | ||
}); | ||
const scanQrCode = async () => { | ||
const result = await new Promise<{ status: QrStatus; code?: string | undefined }>((resolve) => { | ||
resolveQrCodePromise = resolve; | ||
timeoutId = setTimeout(() => { | ||
resolve({ status: 'timeout' }); | ||
}, 30000); | ||
resolveQrCodePromise = ({ status, code }) => { | ||
clearTimeout(timeoutId); | ||
resolve({ status, code }); | ||
}; | ||
}); | ||
const { status, code } = result; | ||
if (status === 'timeout') { | ||
toastsError({ msg: { text: $i18n.send.error.qr_scan_timeout } }); | ||
back(); | ||
return; | ||
} | ||
const qrResponse = decodeQrCode({ status, code, expectedToken }); | ||
if (qrResponse.status === 'token_incompatible') { | ||
toastsError({ msg: { text: $i18n.send.error.incompatible_token } }); | ||
back(); | ||
return; | ||
} | ||
if (nonNullish(qrResponse.destination)) { | ||
destination = qrResponse.destination; | ||
} | ||
if (nonNullish(qrResponse.amount)) { | ||
amount = qrResponse.amount; | ||
} | ||
back(); | ||
}; | ||
const onQRCode = ({ detail: code }: CustomEvent<string>) => { | ||
resolveQrCodePromise?.({ status: 'success', code }); | ||
resolveQrCodePromise = undefined; | ||
}; | ||
const onCancel = () => { | ||
resolveQrCodePromise?.({ status: 'cancelled' }); | ||
resolveQrCodePromise = undefined; | ||
}; | ||
const back = () => { | ||
dispatch('icBack'); | ||
}; | ||
</script> | ||
|
||
<div class="stretch"> | ||
<QRCodeReader on:nnsCancel={onCancel} on:nnsQRCode={onQRCode} /> | ||
</div> | ||
|
||
<ButtonGroup> | ||
<button class="secondary block flex-1" on:click={back}> | ||
{$i18n.core.text.back} | ||
</button> | ||
</ButtonGroup> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
src/frontend/src/tests/lib/components/send/QRCodeScan.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { ICP_TOKEN } from '$env/tokens.env'; | ||
import QRCodeScan from '$lib/components/send/QRCodeScan.svelte'; | ||
import { toastsError } from '$lib/stores/toasts.store'; | ||
import { render, waitFor } from '@testing-library/svelte'; | ||
import { afterEach, describe, expect, it, vi } from 'vitest'; | ||
import en from '../../../mocks/i18n.mock'; | ||
|
||
const expectedToken = ICP_TOKEN; | ||
|
||
vi.mock('$lib/stores/toasts.store'); | ||
|
||
describe('QRCodeReaderComponent', () => { | ||
afterEach(() => { | ||
vi.clearAllMocks(); | ||
}); | ||
|
||
it('should show timeout error on QR scan timeout', async () => { | ||
vi.useFakeTimers(); | ||
|
||
render(QRCodeScan, { expectedToken }); | ||
|
||
vi.advanceTimersByTime(30001); | ||
|
||
await waitFor(() => | ||
expect(toastsError).toHaveBeenCalledWith({ | ||
msg: { text: en.send.error.qr_scan_timeout } | ||
}) | ||
); | ||
|
||
vi.useRealTimers(); | ||
}); | ||
}); |