Skip to content

Commit 242f2ff

Browse files
authored
Merge pull request #671 from dev-protocol/cart
Cart
2 parents 589ba5e + e6702e2 commit 242f2ff

31 files changed

+1952
-594
lines changed

package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@devprotocol/clubs-plugin-payments",
3-
"version": "0.4.5",
3+
"version": "0.5.0",
44
"type": "module",
55
"exports": {
66
".": {
@@ -43,8 +43,8 @@
4343
"@astrojs/svelte": "7.1.1",
4444
"@astrojs/tailwind": "6.0.2",
4545
"@astrojs/vue": "5.1.1",
46-
"@devprotocol/clubs-core": "3.27.1",
47-
"@devprotocol/clubs-plugin-passports": "0.9.1",
46+
"@devprotocol/clubs-core": "3.28.0",
47+
"@devprotocol/clubs-plugin-passports": "0.9.0-alpha.7",
4848
"@eslint/js": "^9.13.0",
4949
"@rollup/plugin-typescript": "^12.1.1",
5050
"@tailwindcss/typography": "^0.5.15",
@@ -92,7 +92,7 @@
9292
"rimraf": "^6.0.1"
9393
},
9494
"peerDependencies": {
95-
"@devprotocol/clubs-core": "^3.22.1",
95+
"@devprotocol/clubs-core": "^3.27.0",
9696
"@devprotocol/clubs-plugin-passports": "^0.1.0-beta.36 || ^0.2.0 || ^0.3.0 || ^0.4.0 || ^0.5.0 || ^0.6.0 || ^0.7.0 || ^0.8.0 || ^0.9.0",
9797
"ethers": "^6.0.0"
9898
},

src/CartButton.vue

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
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

Comments
 (0)