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

feat: edit collection permission #11217

Open
wants to merge 17 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 10 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
158 changes: 153 additions & 5 deletions components/collection/EditModal.vue
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,86 @@
</div>
</div>
</NeoField>

<NeoField
:label="$t('mint.collection.permission.label')"
>
<div class="w-full flex flex-col gap-4">
<div class="flex items-center justify-between">
<p>
{{ $t('mint.mintType') }}
</p>
<NeoSelect
v-model="selectedMintingType"
>
<option
v-for="menu in COLLECTION_MINTING_TYPES_OPTIONS"
:key="menu.value"
:value="menu.value"
>
{{ menu.text }}
</option>
</NeoSelect>
</div>

<div>
<div class="flex justify-between capitalize">
<p>{{ $t(mintingPriceUnset ? 'mint.collection.permission.noPriceSet' : 'mint.collection.permission.pricePlaceholder') }}</p>
<NeoSwitch
v-if="selectedMintingType === 'HolderOf'"
v-model="mintingPriceUnset"
position="left"
/>
</div>
<div
v-if="!mintingPriceUnset"
class="flex focus-within:!border-border-color border border-k-shade h-12 mt-3"
>
<input
v-model="mintingPrice"
type="number"
step="0.01"
min="0.0001"
pattern="[0-9]+([\.,][0-9]+)?"
class="indent-2.5 border-none outline-none w-20 bg-background-color text-text-color w-full"
:placeholder="$t('mint.collection.permission.pricePlaceholder')"
>
<div class="px-3 flex items-center">
{{ chainSymbol }}
</div>
</div>
<div
v-if="selectedMintingType === 'HolderOf'"
class="mt-4"
>
<p class="mb-2">
{{ $t('mint.collection.permission.holderOfCollection') }}
</p>
<CollectionSearchInput
:collection-id="holderOfCollectionId"
@update:collection="holderOfCollectionId = $event?.collection_id"
/>
</div>
</div>

<div class="flex flex-col w-full">
<div
v-if="permissionSettingWarningMessage"
class="flex items-center gap-2 bg-yellow-50 border border-yellow-200 rounded-md p-3 !mt-2"
>
<NeoIcon
icon="warning"
class="text-yellow-500"
size="small"
/>

<p class="text-sm text-yellow-700">
{{ permissionSettingWarningMessage }}
</p>
</div>
</div>
</div>
</NeoField>
</form>

<div class="flex flex-col !mt-6">
Expand All @@ -142,9 +222,9 @@
</template>

<script setup lang="ts">
import { NeoButton, NeoField, NeoInput, NeoModal, NeoSwitch } from '@kodadot1/brick'
import { NeoButton, NeoField, NeoInput, NeoModal, NeoSwitch, NeoSelect, NeoIcon } from '@kodadot1/brick'
import ModalBody from '@/components/shared/modals/ModalBody.vue'
import type { UpdateCollection } from '@/composables/transaction/types'
import type { UpdateCollection, CollectionMintSetting, CollectionMintSettingType } from '@/composables/transaction/types'

export type CollectionEditMetadata = {
name: string
Expand All @@ -153,16 +233,21 @@ export type CollectionEditMetadata = {
imageType: string
banner?: string
max: number | null
mintingSettings: CollectionMintSetting
}

const COLLECTION_MINTING_TYPES_OPTIONS = (['Issuer', 'Public', 'HolderOf'] as CollectionMintSettingType[]).map(type => ({ value: type, text: type }))

const emit = defineEmits(['submit'])
const props = defineProps<{
modelValue: boolean
collection: CollectionEditMetadata
min?: number
}>()

const { $i18n } = useNuxtApp()
const isModalActive = useVModel(props, 'modelValue')
const { chainSymbol, decimals, withDecimals } = useChain()

const name = ref<string>()
const description = ref<string>()
Expand All @@ -171,13 +256,20 @@ const banner = ref<File>()
const imageUrl = ref<string>()
const bannerUrl = ref<string>()
const unlimited = ref(true)

const mintingPriceUnset = ref(true)
const mintingPrice = ref<number | null>(null)
const selectedMintingType = ref<CollectionMintSettingType | null>(null)
const min = computed(() => props.min || 1)
const max = ref<number | null>(null)

const nameChanged = computed(() => props.collection.name !== name.value)
const hasImageChanged = computed(() => (!imageUrl.value && Boolean(props.collection.image)) || Boolean(image.value))
const originalLogoImageUrl = computed(() => sanitizeIpfsUrl(props.collection.image))
const mintTypeChanged = computed(() => selectedMintingType.value !== props.collection.mintingSettings.mintType)
const mintPriceChanged = computed(() => mintingPrice.value !== originalMintPrice.value)
const originalMintPrice = computed(() => props.collection.mintingSettings.price ? Number(props.collection.mintingSettings.price) / (10 ** decimals.value) : null)
const originalHolderOfCollectionId = computed(() => props.collection.mintingSettings.holderOf)
const holderOfCollectionId = ref<string | undefined>(originalHolderOfCollectionId.value)

const disabled = computed(() => {
const hasImage = imageUrl.value
Expand All @@ -186,10 +278,17 @@ const disabled = computed(() => {
const descriptionChanged = props.collection.description !== description.value
const hasBannerChanged = (!bannerUrl.value && Boolean(props.collection.banner)) || Boolean(banner.value)
const hasMaxChanged = max.value !== props.collection.max
const holderOfCollectionIdChanged = holderOfCollectionId.value !== originalHolderOfCollectionId.value
const invalidPublicCollection = selectedMintingType.value === 'Public' && !mintingPrice.value

const invalidHolderOfCollection = selectedMintingType.value === 'HolderOf' && !holderOfCollectionId.value
hassnian marked this conversation as resolved.
Show resolved Hide resolved

return !hasImage || !isNameFilled || (!nameChanged.value && !descriptionChanged && !hasImageChanged.value && !hasBannerChanged && !hasMaxChanged)
return !hasImage || !isNameFilled || invalidHolderOfCollection || invalidPublicCollection
|| (!nameChanged.value && !descriptionChanged && !hasImageChanged.value && !hasBannerChanged && !hasMaxChanged && !mintTypeChanged.value && !mintPriceChanged.value && !holderOfCollectionIdChanged)
})

const permissionSettingWarningMessage = computed(() => selectedMintingType.value && permissionSettingCheckingMap[selectedMintingType.value] && permissionSettingCheckingMap[selectedMintingType.value]())
hassnian marked this conversation as resolved.
Show resolved Hide resolved

const initLogoImage = () => {
imageUrl.value = originalLogoImageUrl.value
image.value = undefined
Expand All @@ -203,6 +302,11 @@ const editCollection = async () => {
imageType: props.collection.imageType,
banner: bannerUrl.value ? banner.value || props.collection.banner : undefined,
max: max.value,
mintingSettings: {
mintType: selectedMintingType.value,
price: mintingPriceUnset.value ? null : String(withDecimals(mintingPrice.value || 0)),
holderOf: holderOfCollectionId.value || originalHolderOfCollectionId.value,
},
} as UpdateCollection)
}

Expand All @@ -215,14 +319,58 @@ watch(isModalActive, (value) => {
initLogoImage()
unlimited.value = !props.collection.max
max.value = props.collection.max

// permission
selectedMintingType.value = props.collection.mintingSettings.mintType
mintingPriceUnset.value = !props.collection.mintingSettings.price
mintingPrice.value = originalMintPrice.value || null
holderOfCollectionId.value = originalHolderOfCollectionId.value
}
})

const priceHandlerMap = {
Issuer: () => {
mintingPrice.value = null
mintingPriceUnset.value = true
},
Public: () => {
mintingPriceUnset.value = false
},
}

const permissionSettingCheckingMap = {
hassnian marked this conversation as resolved.
Show resolved Hide resolved
Issuer: () => {
if (mintingPrice.value) {
return $i18n.t('mint.collection.permission.issuerWarning')
}
},
Public: () => {
if (!mintingPrice.value || mintingPrice.value <= 0) {
return $i18n.t('mint.collection.permission.publicWarning')
}
return $i18n.t('mint.collection.permission.publicWithPriceWarning')
},
HolderOf: () => {
if (!holderOfCollectionId.value) {
return $i18n.t('mint.collection.permission.holderOfIdWarning')
}
return $i18n.t('mint.collection.permission.holderOfWarning')
},
}

watch(selectedMintingType, (type) => {
if (type && priceHandlerMap[type]) {
priceHandlerMap[type as CollectionMintSettingType]()
}
})

watch([banner, unlimited], ([banner, unlimited]) => {
watch([banner, unlimited, mintingPriceUnset], ([banner, unlimited, priceUnset]) => {
if (banner) {
bannerUrl.value = URL.createObjectURL(banner)
}

max.value = unlimited ? null : max.value || props.collection.max

mintingPrice.value = priceUnset ? null : originalMintPrice.value
})
</script>
37 changes: 32 additions & 5 deletions components/collection/HeroButtonEditCollection.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<template>
<NeoDropdownItem
:disabled="!collectionMetadata"
@click="isModalActive = true"
>
{{ $t('moreActions.editCollection') }}
Expand All @@ -24,7 +25,7 @@
<script setup lang="ts">
import { NeoDropdownItem } from '@kodadot1/brick'
import { type CollectionEditMetadata } from '@/components/collection/EditModal.vue'
import { Collections, type UpdateCollection } from '@/composables/transaction/types'
import type { CollectionMintSettingType, Collections, UpdateCollection, CollectionMintSetting } from '@/composables/transaction/types'

const props = defineProps<{
collection: any
Expand All @@ -34,30 +35,36 @@ const { transaction, status, isLoading } = useTransaction()
const { $i18n } = useNuxtApp()
const { urlPrefix } = usePrefix()
const route = useRoute()

const collectionId = route.params.id.toString()
const collectionPermissionSettings = ref<CollectionMintSetting>()
const isModalActive = ref(false)

const collectionMetadata = computed(() =>
props.collection
props.collection && collectionPermissionSettings.value
? {
name: props.collection.meta.name,
description: props.collection.meta.description,
image: props.collection.meta.image,
imageType: props.collection.meta.type,
banner: props.collection.meta.banner || undefined,
max: props.collection.max,
mintingSettings: collectionPermissionSettings.value!,
} as CollectionEditMetadata
: null)

const updateMetadata = (a: UpdateCollection, b: UpdateCollection) => {
const getMetadataKey = (m: UpdateCollection) => {
const { max, ...rest } = m
const { max, mintingSettings, ...rest } = m
return JSON.stringify(rest)
}

return getMetadataKey(a) !== getMetadataKey(b)
}

const shouldUpdatePermission = (a: CollectionMintSetting, b: CollectionMintSetting) => {
return a.price !== b.price || a.mintType !== b.mintType
}

const editCollection = async (collection: UpdateCollection) => {
isModalActive.value = false

Expand All @@ -67,14 +74,34 @@ const editCollection = async (collection: UpdateCollection) => {

await transaction({
interaction: Collections.UPDATE_COLLECTION,
collectionId: route.params.id.toString(),
collectionId: collectionId,
collection,
update: {
metadata: updateMetadata(collection, collectionMetadata.value),
max: collection.max !== collectionMetadata.value.max,
permission: shouldUpdatePermission(collection.mintingSettings, collectionPermissionSettings.value!),
},
urlPrefix: urlPrefix.value,
successMessage: $i18n.t('edit.collection.success'),
})
}

watchEffect(async () => {
const { apiInstance } = useApi()
const api = await apiInstance.value
const config = await api.query.nfts.collectionConfigOf(collectionId)
const mintSettings = (config.toHuman() as { mintSettings: {
price: string
mintType: CollectionMintSettingType
holderOf: string
} }).mintSettings
hassnian marked this conversation as resolved.
Show resolved Hide resolved

mintSettings.price = mintSettings.price?.replaceAll(',', '')

if (typeof mintSettings.mintType !== 'string') {
mintSettings.holderOf = (mintSettings.mintType as any as { HolderOf: string }).HolderOf
mintSettings.mintType = 'HolderOf'
}
collectionPermissionSettings.value = mintSettings
})
</script>
Loading
Loading