-
Notifications
You must be signed in to change notification settings - Fork 392
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix:Optimize NFT attributes filter. (#2568)
- Loading branch information
Showing
3 changed files
with
177 additions
and
95 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
81
packages/kit/src/views/NFTMarket/Modals/NFTAttributesModal/refs.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
// attributes_name--attributes_value | ||
type Refs = Record<string, boolean>; | ||
|
||
const selectedAttributes: Refs = {}; | ||
|
||
function setAttributesStatus({ | ||
attributeName, | ||
attributesValue, | ||
enable, | ||
}: { | ||
attributeName: string; | ||
attributesValue: string; | ||
enable: boolean; | ||
}) { | ||
const key = `${attributeName}--${attributesValue}`; | ||
selectedAttributes[key] = enable; | ||
} | ||
|
||
function getAttributesStatus({ | ||
attributeName, | ||
attributesValue, | ||
}: { | ||
attributeName: string; | ||
attributesValue: string; | ||
}) { | ||
const key = `${attributeName}--${attributesValue}`; | ||
if (selectedAttributes[key]) { | ||
return selectedAttributes[key]; | ||
} | ||
return false; | ||
} | ||
|
||
function clearAttributeStatus() { | ||
for (const [key] of Object.entries(selectedAttributes)) { | ||
delete selectedAttributes[key]; | ||
} | ||
} | ||
|
||
function generteAttributeParams() { | ||
const map: Record<string, string[]> = {}; | ||
for (const [key, enable] of Object.entries(selectedAttributes)) { | ||
if (enable) { | ||
const attributeName = key.split('--')[0]; | ||
const attributesValue = key.split('--')[1]; | ||
const items = map[attributeName] ?? []; | ||
items.push(attributesValue); | ||
map[attributeName] = items; | ||
} | ||
} | ||
return Object.entries(map).map(([key, values]) => ({ | ||
attribute_name: key, | ||
attribute_values: values, | ||
})); | ||
} | ||
|
||
function isSelectedAttribute() { | ||
for (const [, enable] of Object.entries(selectedAttributes)) { | ||
if (enable) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
function isAttributeNameSelected(attributeName: string) { | ||
for (const [key, enable] of Object.entries(selectedAttributes)) { | ||
if (key.split('--')[0] === attributeName && enable) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
export { | ||
isSelectedAttribute, | ||
setAttributesStatus, | ||
getAttributesStatus, | ||
clearAttributeStatus, | ||
generteAttributeParams, | ||
isAttributeNameSelected, | ||
}; |