UNPKG

@mojito-inc/secondary-market

Version:

Mojito secondary market is the platform to purchase NFT.

2 lines (1 loc) 5.18 kB
"use strict";var e,l=require("react"),t=require("@mojito-inc/core-service"),i=require("../constant/index.js"),o=require("../utils/getPrice.utils.js"),n=require("../utils/getValidURL.utils.js");exports.RegistryTokenFilterInput=void 0,(e=exports.RegistryTokenFilterInput||(exports.RegistryTokenFilterInput={})).RECENTLY_LISTED="recently_listed",e.RECENTLY_MINTED="recently_minted",e.INITIALLY_MINTED="initially_minted",e.PRICE_LOW_TO_HIGH="price_low_to_high",e.PRICE_HIGH_TO_LOW="price_high_to_low";exports.useCollections=e=>{const{getAllRegistryTokensV2:a,getCollectionTraits:r}=t.useNFT(),[u,d]=l.useState(!1),[s,c]=l.useState(null),[v,T]=l.useState(0),[p,y]=l.useState(!1),[I,E]=l.useState([]),[_,D]=l.useState(!1),[g,C]=l.useState(!1),L=l.useMemo((()=>e.walletAddress),[e.walletAddress]),f=l.useCallback((()=>{y(!0)}),[]),k=l.useCallback(((e,l,t,o)=>{const n=(null==e?void 0:e.toLowerCase())===(null==L?void 0:L.toLowerCase());if(L){if(l&&n)return"OPEN_FOR_SALE"===l?i.ButtonType.LIST_ITEM:"SALE_PENDING"===l?i.ButtonType.EDIT_LIST_ITEM:i.ButtonType.ENQUIRE;if(("OPEN_FOR_SALE"===l||"SALE_PENDING"===l)&&!n)return i.ButtonType.MAKE_OFFER}return"SALE_PENDING"===l?i.ButtonType.BUY_NOW:i.ButtonType.MAKE_OFFER}),[L]),m=l.useCallback(((e,l,t)=>{const o=(null==e?void 0:e.toLowerCase())===(null==L?void 0:L.toLowerCase());return"SALE_PENDING"===l&&!o&&t?i.ButtonType.BUY_NOW:i.ButtonType.ENQUIRE}),[L]),N=l.useCallback(((e,l,t)=>{if(e){return o.getPrice(t,e,l)}return{price:[],label:""}}),[]),R=l.useCallback(((e,l,t,o,n)=>{const a=m(e,l,t),r=k(e,l,o,n);return a===i.ButtonType.BUY_NOW?"Buy Now":r===i.ButtonType.EDIT_LIST_ITEM?"View Listing":r===i.ButtonType.MAKE_OFFER?"Make Offer":"List Item"}),[m,k]),A=l.useCallback((async()=>{var l,t,i,o,r;try{d(!0),y(!1),c(null);const u=await a({orgID:e.orgID,marketplaceID:e.marketplaceID,limit:null==e?void 0:e.limit,offset:null==e?void 0:e.offset,searchKey:null==e?void 0:e.search,filters:null==e?void 0:e.filters,buyerAddress:null==e?void 0:e.walletAddress,filterByMetadata:null==e?void 0:e.metaDataFilters,registryID:null==e?void 0:e.selectedCollection,currency:null==e?void 0:e.currencyType},{fetchPolicy:"no-cache"}),s=null===(t=null===(l=u.data)||void 0===l?void 0:l.getAllRegistryTokensV2)||void 0===t?void 0:t.data,v=null==s?void 0:s.map((l=>{var t,i,o,a,r,u,d,s,c,v,T,p,y,I,E,_,D,g,C,L,f,k;const m=R(null!==(t=l.TokenOwnerAddress)&&void 0!==t?t:"",null!==(i=l.Status)&&void 0!==i?i:"",l.isBuyNowEnabled,l.isMakeOfferEnabled,l.isOfferExist),A=N(l.Price,null!==(a=null===(o=l.listedOrderInfo)||void 0===o?void 0:o.price)&&void 0!==a?a:[],m);return{cardDetail:{tokenImage:n.getValidURL(l.mediaSourceURL||l.MetaData.image||""),thumbnailImage:n.getValidURL(l.MetaData.image||""),tokenName:null!==(r=l.MetaData.name)&&void 0!==r?r:"",tokenSubtitle:null!==(u=l.MetaData.description)&&void 0!==u?u:"",collectionName:null!==(d=l.Artist.artistName)&&void 0!==d?d:"",collectionType:"solana"===(null==e?void 0:e.walletType)?"SOLANA":"ERC721"===l.TokenType?"ERC-721":"ERC-1155",cryptoPrice:null!==(v=null===(c=null===(s=null==A?void 0:A.price)||void 0===s?void 0:s[1])||void 0===c?void 0:c.value)&&void 0!==v?v:0,usdPrice:null!==(y=null===(p=null===(T=null==A?void 0:A.price)||void 0===T?void 0:T[0])||void 0===p?void 0:p.value)&&void 0!==y?y:0,priceLabel:null!==(E=null===(I=null==A?void 0:A.label)||void 0===I?void 0:I.toUpperCase())&&void 0!==E?E:"",cryptoLabel:"solana"===(null==e?void 0:e.walletType)?"VIN":null!==(g=null===(D=null===(_=null==A?void 0:A.price)||void 0===_?void 0:_[1])||void 0===D?void 0:D.unit)&&void 0!==g?g:"ETH",buttonName:m},id:null!==(C=l.NFTTokenID)&&void 0!==C?C:"",tokenId:null!==(f=null===(L=l.TokenID)||void 0===L?void 0:L.toString())&&void 0!==f?f:"",contractAddress:null!==(k=l.Registry.ContractAddress)&&void 0!==k?k:""}}));return c(null!=v?v:[]),T(null!==(r=null===(o=null===(i=null==u?void 0:u.data)||void 0===i?void 0:i.getAllRegistryTokensV2)||void 0===o?void 0:o.totalCount)&&void 0!==r?r:0),d(!1),v}catch(e){return y(!1),d(!1),c(null),null}}),[null==e?void 0:e.filters,null==e?void 0:e.limit,e.marketplaceID,null==e?void 0:e.offset,e.orgID,null==e?void 0:e.search,null==e?void 0:e.walletAddress,null==e?void 0:e.metaDataFilters,null==e?void 0:e.selectedCollection,a,null==e?void 0:e.walletType,R,null==e?void 0:e.currencyType,N]),S=l.useCallback((()=>{C(!0)}),[]),O=l.useCallback((async()=>{var l,t;try{C(!1),D(!0),E([]);const i=null===(t=(await r({orgID:e.orgID,marketplaceID:e.marketplaceID,registryID:null!==(l=e.selectedCollection)&&void 0!==l?l:void 0},{fetchPolicy:"no-cache"})).data)||void 0===t?void 0:t.GetCollectionTraitforSMP,o=null==i?void 0:i.map((e=>({title:e.traitType,type:e.dataType,values:e.traitValues.map((e=>{var l;return{title:e.traitValue,value:e.traitValue,count:null!==(l=e.count)&&void 0!==l?l:0}}))})));return E(null!=o?o:[]),D(!1),o}catch(e){return C(!1),D(!1),E([]),null}}),[e.marketplaceID,e.orgID,r,e.selectedCollection]);return l.useEffect((()=>{p&&(A(),y(!1))}),[p,A]),l.useEffect((()=>{g&&(C(!1),O())}),[g,O,e.selectedCollection]),{collectionsLoading:u,traitsLoading:_,collectionsData:s,totalCollections:v,traitsData:I,fetchCollection:f,fetchMetaDataDetails:S}};