Skip to content

Commit

Permalink
✨ Support restock and migrate to auto delivery (#95)
Browse files Browse the repository at this point in the history
* ✨ Support restock and migrate to auto delivery

* 🎨 Set payload default field to `undefined`

* 💄 Update warning text

* 💬 Improve terminology

* 💬 Make memo more layman friendly

* 💬 Update default memo

* 🐛 Fix cannot edit title bug

* 💬 Add default memo when switching

* 🚸 Guard auto delivery prices

* 💬 Update memo for transferring NFT to API wallet

---------

Co-authored-by: Ng Wing Tat, David <[email protected]>
  • Loading branch information
2 people authored and williamchong committed Jan 22, 2024
1 parent b2c438d commit b2a4945
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 35 deletions.
25 changes: 15 additions & 10 deletions pages/nft-book-store/new.vue
Original file line number Diff line number Diff line change
Expand Up @@ -82,16 +82,16 @@
</UFormGroup>

<URadioGroup
v-model="p.deliverMethod"
:legend="`Deliver method of this ${priceItemLabel}`"
v-model="p.deliveryMethod"
:legend="`Delivery method of this ${priceItemLabel}`"
:options="deliverMethodOptions"
/>

<UFormGroup
v-if="p.deliverMethod === 'auto'"
v-if="p.deliveryMethod === 'auto'"
:label="`Memo of this ${priceItemLabel}`"
>
<UInput placeholder="Thank you! 謝謝你的支持!" :value="p.autoMemo" @input="e => updatePrice(e, 'autoMemo', index)" />
<UInput :value="p.autoMemo" @input="e => updatePrice(e, 'autoMemo', index)" />
</UFormGroup>

<UFormGroup
Expand Down Expand Up @@ -429,8 +429,8 @@ const mustClaimToView = ref(false)
const hideDownload = ref(false)
const prices = ref<any[]>([{
price: MINIMAL_PRICE,
deliverMethod: 'auto',
autoMemo: '',
deliveryMethod: 'auto',
autoMemo: 'Thanks for purchasing this NFT ebook.',
stock: Number(route.query.count as string || 1),
nameEn: 'Standard Edition',
nameZh: '標準版',
Expand Down Expand Up @@ -606,7 +606,7 @@ function addMorePrice () {
prices.value.push({
index: uuidv4(),
price: MINIMAL_PRICE,
deliverMethod: 'auto',
deliveryMethod: 'auto',
autoMemo: '',
stock: 1,
nameEn: `Tier ${nextPriceIndex.value}`,
Expand Down Expand Up @@ -673,8 +673,8 @@ function mapPrices (prices:any) {
priceInDecimal: Math.round(Number(p.price) * 100),
price: Number(p.price),
stock: Number(p.stock),
isAutoDeliver: p.deliverMethod === 'auto',
autoMemo: p.deliverMethod === 'auto' ? (p.autoMemo || '') : '',
isAutoDeliver: p.deliveryMethod === 'auto',
autoMemo: p.deliveryMethod === 'auto' ? (p.autoMemo || '') : '',
hasShipping: p.hasShipping || false
}))
}
Expand Down Expand Up @@ -732,11 +732,16 @@ async function submitNewClass () {
}))
: undefined
if (p.some(price => price.isAutoDeliver)) {
const ok = confirm('NFT Book Press - Reminder\nOnce you choose automatic delivery, you can\'t switch it back to manual delivery. Are you sure?')
if (!ok) { return }
}
const autoDeliverCount = p
.filter(price => price.isAutoDeliver)
.reduce((acc, price) => acc + price.stock, 0)
let autoDeliverNFTsTxHash = ''
let autoDeliverNFTsTxHash
if (autoDeliverCount > 0) {
if (!wallet.value || !signer.value) {
await connect()
Expand Down
80 changes: 65 additions & 15 deletions pages/nft-book-store/status/[classId]/edit/[editionIndex].vue
Original file line number Diff line number Diff line change
Expand Up @@ -45,29 +45,29 @@
</UFormGroup>

<UFormGroup :label="`Total number of NFT for sale of this ${priceItemLabel}`">
<UInput v-model="stock" type="number" step="1" :min="0" :disabled="isAutoDeliver" />
<UInput v-model="stock" type="number" step="1" :min="minStock" />
</UFormGroup>

<URadioGroup
v-model="deliverMethod"
disabled
:legend="`Deliver method of this ${priceItemLabel}`"
v-model="deliveryMethod"
:disabled="oldIsAutoDeliver"
:legend="`Delivery method of this ${priceItemLabel}`"
:options="deliverMethodOptions"
/>

<UFormGroup
v-if="isAutoDeliver"
:label="`Memo of this ${priceItemLabel}`"
>
<UInput v-model="autoMemo" placeholder="Thank you! 謝謝你的支持!" />
<UInput v-model="autoMemo" />
</UFormGroup>

<UFormGroup
:label="`Product name of this ${priceItemLabel}`"
:ui="{ container: 'space-y-2' }"
>
<UInput placeholder="Product name in English" :value="nameEn" />
<UInput placeholder="產品中文名字" :value="nameZh" />
<UInput v-model="nameEn" placeholder="Product name in English" />
<UInput v-model="nameZh" placeholder="產品中文名字" />
</UFormGroup>

<h5 class="!mt-8 font-bold font-mono">
Expand Down Expand Up @@ -206,10 +206,12 @@ import { LIKE_CO_API } from '~/constant'
import { useBookStoreApiStore } from '~/stores/book-store-api'
import { useWalletStore } from '~/stores/wallet'
import { deliverMethodOptions } from '~/utils'
import { sendNFTsToAPIWallet } from '~/utils/cosmos'
const walletStore = useWalletStore()
const bookStoreApiStore = useBookStoreApiStore()
const { wallet } = storeToRefs(walletStore)
const { connect } = walletStore
const { wallet, signer } = storeToRefs(walletStore)
const { token } = storeToRefs(bookStoreApiStore)
const router = useRouter()
Expand All @@ -228,8 +230,8 @@ const hasMultiplePrices = computed(() => classData?.value?.prices?.length > 1)
const price = ref(MINIMAL_PRICE)
const stock = ref(1)
const deliverMethod = ref('auto')
const autoMemo = ref('')
const deliveryMethod = ref('auto')
const autoMemo = ref('Thanks for purchasing this NFT ebook.')
const nameEn = ref('Standard Edition')
const nameZh = ref('標準版')
const descriptionEn = ref('')
Expand All @@ -242,8 +244,12 @@ const shippingRates = ref<any[]>([{
}])
const hasMultipleShippingRates = computed(() => shippingRates.value.length > 1)
const oldStock = ref(0)
const oldIsAutoDeliver = ref(false)
const priceItemLabel = computed(() => hasMultiplePrices.value ? 'edition' : 'book')
const isAutoDeliver = computed(() => deliverMethod.value === 'auto')
const isAutoDeliver = computed(() => deliveryMethod.value === 'auto')
const minStock = computed(() => oldIsAutoDeliver.value ? oldStock.value : 0)
const toolbarOptions = ref<string[]>([
'bold',
Expand Down Expand Up @@ -273,6 +279,19 @@ config({
}
})
watch(isAutoDeliver, (newValue) => {
if (newValue) {
const ok = confirm('NFT Book Press - Reminder\nOnce you choose automatic delivery, you can\'t switch it back to manual delivery. Are you sure?')
if (!ok) {
nextTick(() => {
deliveryMethod.value = 'manual'
})
} else if (!autoMemo.value) {
autoMemo.value = 'Thanks for purchasing this NFT ebook.'
}
}
})
onMounted(async () => {
try {
isLoading.value = true
Expand All @@ -294,14 +313,17 @@ onMounted(async () => {
if (currentEdition) {
price.value = currentEdition.price || 0
stock.value = currentEdition.stock || 0
deliverMethod.value = currentEdition.isAutoDeliver ? 'auto' : 'manual'
deliveryMethod.value = currentEdition.isAutoDeliver ? 'auto' : 'manual'
autoMemo.value = currentEdition.autoMemo || ''
nameEn.value = currentEdition.name?.en || currentEdition.name || ''
nameZh.value = currentEdition.name?.zh || currentEdition.name || ''
const legacyDescription = typeof currentEdition.description === 'string' ? currentEdition.description : undefined
descriptionEn.value = currentEdition.description?.en || legacyDescription || ' '
descriptionZh.value = currentEdition.description?.zh || legacyDescription || ' '
hasShipping.value = currentEdition.hasShipping || false
oldStock.value = currentEdition.stock
oldIsAutoDeliver.value = currentEdition.isAutoDeliver
}
}
} catch (error) {
Expand Down Expand Up @@ -387,8 +409,12 @@ async function handleSubmit () {
throw new Error('Please input stock of edition')
}
if (editedPrice.stock < 0) {
throw new Error('Stock cannot be negative')
if (editedPrice.stock < minStock.value) {
throw new Error(`Stock cannot be less than ${minStock.value}`)
}
if (oldIsAutoDeliver.value && !editedPrice.isAutoDeliver) {
throw new Error('Cannot change automatic delivery to manual delivery')
}
if (!editedPrice.name.en || !editedPrice.name.zh) {
Expand All @@ -397,8 +423,32 @@ async function handleSubmit () {
isLoading.value = true
let newAutoDeliverNFTsCount = 0
if (editedPrice.isAutoDeliver) {
newAutoDeliverNFTsCount = oldIsAutoDeliver.value
? editedPrice.stock - oldStock.value
: editedPrice.stock
}
let autoDeliverNFTsTxHash
if (newAutoDeliverNFTsCount > 0) {
if (!wallet.value || !signer.value) {
await connect()
}
if (!wallet.value || !signer.value) {
throw new Error('Unable to connect to wallet')
}
autoDeliverNFTsTxHash = await sendNFTsToAPIWallet(
classId.value as string,
newAutoDeliverNFTsCount,
signer.value,
wallet.value
)
}
await bookStoreApiStore.updateEditionPrice(classId.value as string, editionIndex.value, {
price: editedPrice
price: editedPrice,
autoDeliverNFTsTxHash
})
router.push({
Expand Down
23 changes: 14 additions & 9 deletions pages/nft-book-store/status/[classId]/edit/new.vue
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,16 @@
</UFormGroup>

<URadioGroup
v-model="deliverMethod"
:legend="`Deliver method of this ${priceItemLabel}`"
v-model="deliveryMethod"
:legend="`Delivery method of this ${priceItemLabel}`"
:options="deliverMethodOptions"
/>

<UFormGroup
v-if="deliverMethod === 'auto'"
v-if="deliveryMethod === 'auto'"
:label="`Memo of this ${priceItemLabel}`"
>
<UInput v-model="autoMemo" placeholder="Thank you! 謝謝你的支持!" />
<UInput v-model="autoMemo" />
</UFormGroup>

<UFormGroup
Expand Down Expand Up @@ -229,8 +229,8 @@ const hasMultiplePrices = computed(() => classData?.value?.prices?.length > 1)
const price = ref(MINIMAL_PRICE)
const stock = ref(1)
const deliverMethod = ref('auto')
const autoMemo = ref('')
const deliveryMethod = ref('auto')
const autoMemo = ref('Thanks for purchasing this NFT ebook.')
const nameEn = ref('Standard Edition')
const nameZh = ref('標準版')
const descriptionEn = ref('')
Expand Down Expand Up @@ -356,8 +356,8 @@ async function handleSubmit () {
priceInDecimal: Math.round(Number(price.value) * 100),
price: Number(price.value),
stock: Number(stock.value),
isAutoDeliver: deliverMethod.value === 'auto',
autoMemo: deliverMethod.value === 'auto' ? (autoMemo.value || '') : '',
isAutoDeliver: deliveryMethod.value === 'auto',
autoMemo: deliveryMethod.value === 'auto' ? (autoMemo.value || '') : '',
hasShipping: hasShipping.value || false
}
Expand All @@ -380,9 +380,14 @@ async function handleSubmit () {
throw new Error('Please input product name')
}
if (editedPrice.isAutoDeliver) {
const ok = confirm('NFT Book Press - Reminder\nOnce you choose automatic delivery, you can\'t switch it back to manual delivery. Are you sure?')
if (!ok) { return }
}
isLoading.value = true
let autoDeliverNFTsTxHash = ''
let autoDeliverNFTsTxHash
if (editedPrice.isAutoDeliver && editedPrice.stock > 0) {
if (!wallet.value || !signer.value) {
await connect()
Expand Down
2 changes: 1 addition & 1 deletion utils/cosmos.ts
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ export async function sendNFTsToAPIWallet (
nftIds,
signer,
ownerAddress,
'Send auto delivering NFT Book to API wallet'
'Commission Liker Land to help issue this NFT ebook'
)

if (!transactionHash || code !== 0) {
Expand Down

0 comments on commit b2a4945

Please sign in to comment.