|
| 1 | +<template> |
| 2 | + <div class="nft-appreciation__main"> |
| 3 | + <b-dropdown |
| 4 | + v-model="currentMenu" |
| 5 | + @input="handleInput" |
| 6 | + multiple |
| 7 | + aria-role="list" |
| 8 | + expanded |
| 9 | + scrollable |
| 10 | + :max-height="200" |
| 11 | + > |
| 12 | + <template #trigger> |
| 13 | + <b-button type="is-primary" icon-left="bookmark"> |
| 14 | + Save to pack ({{ currentMenu.length }}) |
| 15 | + </b-button> |
| 16 | + </template> |
| 17 | + |
| 18 | + <b-dropdown-item |
| 19 | + v-for="(menu, index) in menus" |
| 20 | + :key="index" |
| 21 | + :value="menu._id" |
| 22 | + aria-role="listitem" |
| 23 | + > |
| 24 | + <div class="media"> |
| 25 | + <div class="menu-down"> |
| 26 | + <h3>{{ menu.name }}</h3> |
| 27 | + </div> |
| 28 | + </div> |
| 29 | + </b-dropdown-item> |
| 30 | + |
| 31 | + <b-dropdown-item class="pack-saver-input__wrapper" custom aria-role="listitem"> |
| 32 | + <b-input class="pack-saver-input__input" v-model="newPackName" placeholder="New pack name" expanded /> |
| 33 | + <b-button type="is-info" outlined :disabled="!newPackName" @click="addPack" icon-left="plus" :loading="isLoading" /> |
| 34 | + </b-dropdown-item> |
| 35 | + </b-dropdown> |
| 36 | + </div> |
| 37 | +</template> |
| 38 | + |
| 39 | +<script lang="ts" > |
| 40 | +import { Component, Prop, Vue, Watch } from 'vue-property-decorator'; |
| 41 | +import Connector from '@vue-polkadot/vue-api'; |
| 42 | +import exec, { execResultValue } from '@/utils/transactionExecutor'; |
| 43 | +import { notificationTypes, showNotification } from '@/utils/notification'; |
| 44 | +import { getInstance, RmrkType } from '../service/RmrkService'; |
| 45 | +import shouldUpdate from '@/utils/shouldUpdate'; |
| 46 | +import groupBy from '@/utils/groupBy'; |
| 47 | +import { Debounce } from 'vue-debounce-decorator' |
| 48 | +
|
| 49 | +import { Pack } from '@/components/rmrk/service/scheme'; |
| 50 | +import { emptyObject } from '@/utils/empty'; |
| 51 | +
|
| 52 | +@Component |
| 53 | +export default class PackSaver extends Vue { |
| 54 | + @Prop() public currentOwnerId!: string; |
| 55 | + @Prop() public accountId!: string; |
| 56 | + @Prop() public nftId!: string; |
| 57 | + private currentMenu: string[] = []; |
| 58 | + private savedMenu: Record<string,boolean> = {}; |
| 59 | + private menus: Pack[] = []; |
| 60 | + private newPackName = ''; |
| 61 | + protected isLoading = false; |
| 62 | +
|
| 63 | + protected async addPack() { |
| 64 | + const rmrkService = getInstance(); |
| 65 | + const pack = emptyObject<Pack>(); |
| 66 | + pack.nfts = { [this.nftId]: true } |
| 67 | + pack.name = this.newPackName |
| 68 | +
|
| 69 | + this.isLoading = true; |
| 70 | +
|
| 71 | + try { |
| 72 | + const persisted = await rmrkService?.createPack(pack, this.accountId); |
| 73 | + this.$set(this.menus, this.menus.length, persisted); |
| 74 | + this.$set(this.currentMenu, this.currentMenu.length, persisted?._id); |
| 75 | + showNotification(`[Textile] Saved pack ${persisted?.name}`); |
| 76 | + this.newPackName = '' |
| 77 | + |
| 78 | + } catch (e) { |
| 79 | + showNotification(`[ERR] ${e}`, notificationTypes.danger); |
| 80 | + console.error(e); |
| 81 | + } |
| 82 | +
|
| 83 | + this.isLoading = false |
| 84 | +
|
| 85 | + } |
| 86 | +
|
| 87 | + @Debounce(1500) |
| 88 | + protected handleInput(value: string[]) { |
| 89 | + console.log('new value is,', value, 'database has', this.savedMenu) |
| 90 | + const changeLog: Record<string, boolean> = {}; |
| 91 | + Object.keys(this.savedMenu).forEach(m => { |
| 92 | + changeLog[m] = value.some(v => v === m) |
| 93 | + }) |
| 94 | + // value.forEach(id => changeLog[id] = !this.savedMenu[id]) |
| 95 | + console.log('[SHOULD SAVE]', changeLog, 'from', this.savedMenu) |
| 96 | + this.submit(changeLog) |
| 97 | + } |
| 98 | +
|
| 99 | + protected async submit(changeLog: Record<string, boolean>) { |
| 100 | + const rmrkService = getInstance(); |
| 101 | + try { |
| 102 | + const persisted = await rmrkService?.addNFTToPacks(this.nftId, changeLog, this.accountId); |
| 103 | + if (Object.keys(persisted || {}).length) { |
| 104 | + this.savedMenu = {...persisted} |
| 105 | + showNotification(`[Textile] packs updated`); |
| 106 | + } |
| 107 | + } catch (e) { |
| 108 | + showNotification(`[ERR] ${e}`, notificationTypes.danger); |
| 109 | + console.error(e); |
| 110 | + } |
| 111 | + } |
| 112 | +
|
| 113 | + // private async submit(rmrk: string) { |
| 114 | + // const { api } = Connector.getInstance(); |
| 115 | + // const rmrkService = getInstance(); |
| 116 | +
|
| 117 | + // try { |
| 118 | + // showNotification(rmrk); |
| 119 | + // console.log('submit', rmrk); |
| 120 | + // const tx = await exec(this.accountId, '', api.tx.system.remark, [rmrk]); |
| 121 | + // showNotification(execResultValue(tx), notificationTypes.success); |
| 122 | + // console.warn('TX IN', tx); |
| 123 | + // const persisted = await rmrkService?.resolve(rmrk, this.accountId); |
| 124 | + // console.log(persisted); |
| 125 | + // console.log('SAVED', persisted?._id); |
| 126 | + // showNotification( |
| 127 | + // `[TEXTILE] ${persisted?._id}`, |
| 128 | + // notificationTypes.success |
| 129 | + // ); |
| 130 | + |
| 131 | + // } catch (e) { |
| 132 | + // showNotification(`[ERR] ${e}`, notificationTypes.danger); |
| 133 | + // console.error(e); |
| 134 | + // } |
| 135 | + // } |
| 136 | +
|
| 137 | + async fetchPacksForUser(id: string) { |
| 138 | + const rmrkService = getInstance(); |
| 139 | + try { |
| 140 | + const packs = await rmrkService?.getPackListForAccount(id); |
| 141 | + console.log(packs); |
| 142 | + this.menus = packs || []; |
| 143 | + this.currentMenu = this.menus.filter(m => m.nfts[this.nftId]).map(m => m.id) |
| 144 | + this.savedMenu = this.currentMenu.reduce((acc, val) => ({...acc, [val]: true }) ,{}); |
| 145 | + } catch (e) { |
| 146 | + console.warn(`[Pack] unable to fetch appreciations ${e}`); |
| 147 | + } |
| 148 | + } |
| 149 | +
|
| 150 | + @Watch('accountId') |
| 151 | + private watchAccountId(val: string, oldVal: string) { |
| 152 | + if (shouldUpdate(val, oldVal)) { |
| 153 | + this.fetchPacksForUser(val); |
| 154 | + } |
| 155 | + } |
| 156 | +} |
| 157 | +</script> |
| 158 | + |
| 159 | +<style scoped> |
| 160 | +.pack-saver-input__wrapper { |
| 161 | + display: flex; |
| 162 | +} |
| 163 | +
|
| 164 | +.pack-saver-input__input { |
| 165 | + flex-grow: 1; |
| 166 | +} |
| 167 | +</style> |
0 commit comments