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

Small refactor #6983

Merged
merged 4 commits into from
Aug 30, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
65 changes: 35 additions & 30 deletions composables/massmint/parsers/parseTxt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,26 +56,46 @@ function parsePriceAndCurrency(fieldValue: string): {
return { price: undefined, currency: undefined }
}

const updateEntry = (entry, line) => {
const parsedField = parseField(line)

if (!parsedField) {
return
}
const { fieldName, fieldValue } = parsedField
if (fieldName === 'price') {
const { price, currency } = parsePriceAndCurrency(fieldValue)
entry.price = price
entry.currency = currency
} else {
entry[fieldName] = fieldValue
}
}

function processBlock(block: string): Partial<Entry> {
const lines = block.split(/\r?\n/)
const entry: Partial<Entry> = {}

for (const line of lines) {
const parsedField = parseField(line)

if (parsedField) {
const { fieldName, fieldValue } = parsedField
if (fieldName === 'price') {
const { price, currency } = parsePriceAndCurrency(fieldValue)
entry.price = price
entry.currency = currency
} else {
entry[fieldName] = fieldValue
}
lines.forEach((line) => updateEntry(entry, line))
return entry
}

const updateEntries = (entries, block) => {
const entry = processBlock(block)
const { $consola } = useNuxtApp()

if (entry.file) {
entries[entry.file] = {
file: entry.file,
name: entry.name || undefined,
description: entry.description || undefined,
price: entry.price || undefined,
currency: entry.currency || undefined,
pbkompasz marked this conversation as resolved.
Show resolved Hide resolved
valid: isValidEntry(entry),
}
} else {
$consola.error('Unable to extract file name from invalid block')
}

return entry
}

export function parseTxt(
Expand All @@ -87,22 +107,7 @@ export function parseTxt(

const entries: Record<string, Entry> = {}

for (const block of blocks) {
const entry = processBlock(block)

if (entry.file) {
entries[entry.file] = {
file: entry.file,
name: entry.name || undefined,
description: entry.description || undefined,
price: entry.price || undefined,
currency: entry.currency || undefined,
valid: isValidEntry(entry),
}
} else {
$consola.error('Unable to extract file name from invalid block')
}
}
blocks.forEach((block) => updateEntries(entries, block))

if (Object.keys(entries).length === 0) {
$consola.error('Invalid TXT file structure')
Expand Down
30 changes: 12 additions & 18 deletions stores/history.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,27 +95,21 @@ export const useHistoryStore = defineStore('history', {
actions: {
addHistoryItem(payload) {
this.currentlyViewedItem = payload
const { id, title, image, collection, date, prefix } = payload
const historyItem = { id, title, image, collection, date, prefix }
const { id } = payload
const existingIndex = this.visitedNFTs.findIndex((item) => item.id === id)

if (typeof historyItem === 'string') {
this.visitedNFTs = this.visitedNFTs.filter(
(item) => item.id !== historyItem
)
} else {
// check if nft was visited before -> in that case delete it from history first
if (this.visitedNFTs.find((item) => item.id === historyItem.id)) {
this.visitedNFTs = this.visitedNFTs.filter(
(item) => item.id !== historyItem.id
)
}
// save a maximum of 30 items
if (this.visitedNFTs.length > 30) {
this.visitedNFTs.pop()
}
this.visitedNFTs.unshift(historyItem)
// check if nft was visited before -> in that case delete it from history first
if (existingIndex !== -1) {
this.visitedNFTs.splice(existingIndex, 1)
}

// save a maximum of 30 items
if (this.visitedNFTs.length > 30) {
this.visitedNFTs.pop()
}

this.visitedNFTs.unshift(payload)

storage.value = { history: { visitedNFTs: this.visitedNFTs } }
},
setCurrentlyViewedCollection(payload) {
Expand Down
6 changes: 5 additions & 1 deletion utils/transactionExecutor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ const exec = async (
: extractFromKeyring(address, password)

const tx = await transfer.signAsync(signer, options)
const hash = hasCallback ? await tx.send(statusCb) : await transfer.send()
const hash = await getHash(hasCallback, tx, transfer, statusCb)
return typeof hash === 'function'
? constructCallback(hash, tx.hash.toHex())
: hash.toHex()
Expand All @@ -48,6 +48,10 @@ const exec = async (
}
}

const getHash = async (hasCallback, tx, transfer, statusCb) => {
return hasCallback ? await tx.send(statusCb) : await transfer.send()
}

const extractFromKeyring = (address: string, password: string | null) => {
const alicePair = keyring.getPair(address)
if (password) {
Expand Down