|
| 1 | +<template> |
| 2 | + <span class="grid"> |
| 3 | + <button |
| 4 | + @click="onClick" |
| 5 | + :disabled="loading || Boolean(error)" |
| 6 | + class="hs-button relative group is-large is-filled" |
| 7 | + :class="{ 'animate-pulse': loading, 'bg-red-600': error }" |
| 8 | + > |
| 9 | + <IconBouncingArrowRight |
| 10 | + v-if="completed || (!completed && !loading)" |
| 11 | + :justifyLeft="true" |
| 12 | + class="group-disabled:hidden" |
| 13 | + /> |
| 14 | + <IconSpinner v-if="loading" class="absolute left-5 size-5" /> |
| 15 | + {{ i18n('AddCart') }} |
| 16 | + </button> |
| 17 | + |
| 18 | + <p v-if="error" class="text-bold mt-2 rounded-md bg-red-600 p-2 text-white"> |
| 19 | + {{ error }} |
| 20 | + </p> |
| 21 | + </span> |
| 22 | +</template> |
| 23 | + |
| 24 | +<script lang="ts" setup> |
| 25 | +import { ref, onMounted } from 'vue' |
| 26 | +import { UndefinedOr, whenDefined, whenDefinedAll } from '@devprotocol/util-ts' |
| 27 | +import { |
| 28 | + bytes32Hex, |
| 29 | + ClubsOffering, |
| 30 | + i18nFactory, |
| 31 | + Signal, |
| 32 | +} from '@devprotocol/clubs-core' |
| 33 | +import { Strings } from './i18n' |
| 34 | +import { Signer } from 'ethers' |
| 35 | +import { PluginId } from './constants' |
| 36 | +import { IconBouncingArrowRight } from '@devprotocol/clubs-core/ui/vue' |
| 37 | +import type { connection as ConnectionType } from '@devprotocol/clubs-core/connection' |
| 38 | +
|
| 39 | +const props = defineProps<{ |
| 40 | + payload: ClubsOffering['payload'] |
| 41 | + quantity?: number |
| 42 | + onComplete: () => void | Promise<() => void> |
| 43 | + base: string |
| 44 | +}>() |
| 45 | +
|
| 46 | +const account = ref<string | undefined>(undefined) |
| 47 | +const loading = ref(false) |
| 48 | +const completed = ref(false) |
| 49 | +const error = ref<string | undefined>(undefined) |
| 50 | +const i18nBase = i18nFactory(Strings) |
| 51 | +const i18n = ref(i18nBase(['en'])) |
| 52 | +const clubsConnection = ref<ReturnType<typeof ConnectionType>>() |
| 53 | +const message = `Add Cart: ${bytes32Hex(props.payload)}` |
| 54 | +
|
| 55 | +let signer: Signer | undefined |
| 56 | +
|
| 57 | +const onClick = async () => { |
| 58 | + loading.value = true |
| 59 | + if (!signer) { |
| 60 | + return clubsConnection.value?.signal.next(Signal.SignInRequest) |
| 61 | + } |
| 62 | + const signature = await signer?.signMessage(message) |
| 63 | + const url = new URL(`${props.base}/api/${PluginId}/cart`) |
| 64 | +
|
| 65 | + const params = whenDefinedAll( |
| 66 | + [props.payload, signature], |
| 67 | + ([payload, sig]) => { |
| 68 | + return { |
| 69 | + quantity: props.quantity ?? 1, |
| 70 | + payload: bytes32Hex(payload), |
| 71 | + message, |
| 72 | + signature: sig, |
| 73 | + } |
| 74 | + }, |
| 75 | + ) |
| 76 | + const apiCall = await whenDefined(params, (params) => |
| 77 | + fetch(url, { method: 'POST', body: JSON.stringify(params) }), |
| 78 | + ) |
| 79 | + const err = await whenDefined(apiCall, (res) => |
| 80 | + res.json().then((x) => x.error as UndefinedOr<string>), |
| 81 | + ) |
| 82 | + error.value = err |
| 83 | + loading.value = false |
| 84 | + if (!err) { |
| 85 | + completed.value = true |
| 86 | + props.onComplete() |
| 87 | + } |
| 88 | +} |
| 89 | +
|
| 90 | +onMounted(async () => { |
| 91 | + i18n.value = i18nBase(navigator.languages) |
| 92 | + const { connection } = await import('@devprotocol/clubs-core/connection') |
| 93 | +
|
| 94 | + clubsConnection.value = connection() |
| 95 | +
|
| 96 | + connection().account.subscribe(async (acc) => { |
| 97 | + account.value = acc |
| 98 | + }) |
| 99 | +
|
| 100 | + connection().signer.subscribe((_signer) => { |
| 101 | + signer = _signer |
| 102 | + }) |
| 103 | +}) |
| 104 | +</script> |
0 commit comments