/ src / common / data / dapp.js
dapp.js
  1  import * as Categories from './categories'
  2  
  3  export default class DappModel {
  4    constructor() {
  5      // blockchain
  6      this.id = ''
  7      this.sntValue = 0
  8  
  9      // metadata
 10      this.email = ''
 11      this.name = ''
 12      this.image = ''
 13      this.desc = ''
 14      this.category = ''
 15      this.dateAdded = 0
 16      this.status = 'NEW'
 17    }
 18  
 19    clone() {
 20      return Object.assign(new DappModel(), this)
 21    }
 22  
 23    isApproved() {
 24      return this.status === 'APPROVED'
 25    }
 26  
 27    static instanceFromBlockchainWithMetadata(source) {
 28      return Object.assign(new DappModel(), source.metadata, {
 29        id: source.id,
 30        sntValue: parseInt(source.effectiveBalance, 10),
 31      })
 32    }
 33  }
 34  
 35  const RECENTLY_ADDED_SIZE = 50
 36  // const HIGHEST_RANKED_SIZE = 50
 37  
 38  export class DappState {
 39    constructor() {
 40      this.loaded = true
 41      this.dapps = []
 42      this.dappsHightestRanked = null
 43      this.dappsRecentlyAdded = null
 44      this.categoryMap = new Map()
 45      this.categoryMap.set(Categories.EXCHANGES, null)
 46      this.categoryMap.set(Categories.DEFI, null)
 47      this.categoryMap.set(Categories.MARKETPLACES, null)
 48      this.categoryMap.set(Categories.COLLECTIBLES, null)
 49      this.categoryMap.set(Categories.GAMES, null)
 50      this.categoryMap.set(Categories.SOCIAL_NETWORKS, null)
 51      this.categoryMap.set(Categories.UTILITIES, null)
 52      this.categoryMap.set(Categories.CRYPTO_ONRAMPS, null)
 53      this.categoryMap.set(Categories.OTHER, null)
 54    }
 55  
 56    clone() {
 57      const result = new DappState()
 58      result.dapps = [...this.dapps]
 59      return result
 60    }
 61  
 62    creditDapp(dapp) {
 63      for (let i = 0; i < this.dapps.length; i += 1)
 64        if (this.dapps[i].id === dapp.id) return this.updateDapp(dapp)
 65      return this.addDapp(dapp)
 66    }
 67  
 68    addDapp(dapp) {
 69      const result = new DappState()
 70      let pushed = false
 71      for (let i = 0; i < this.dapps.length; i += 1) {
 72        if (!pushed && dapp.sntValue > this.dapps[i].sntValue) {
 73          result.dapps.push(dapp)
 74          pushed = true
 75        }
 76        result.dapps.push(this.dapps[i].clone())
 77      }
 78      if (!pushed) result.dapps.push(dapp)
 79      return result
 80    }
 81  
 82    addDapps(dapps) {
 83      const result = new DappState()
 84      result.dapps = this.dapps.concat(dapps)
 85      result.dapps.sort((a, b) => {
 86        return b.sntValue - a.sntValue
 87      })
 88      return result
 89    }
 90  
 91    updateDapp(dapp) {
 92      const result = new DappState()
 93      for (let i = 0; i < this.dapps.length; i += 1) {
 94        if (dapp.id === this.dapps[i].id) result.dapps.push(dapp)
 95        else result.dapps.push(this.dapps[i].clone())
 96      }
 97      result.dapps.sort((a, b) => {
 98        return b.sntValue - a.sntValue
 99      })
100      return result
101    }
102  
103    getDappsByCategory(category) {
104      let filtered = this.categoryMap.get(category)
105      if (filtered === null) {
106        filtered = this.dapps.filter(
107          dapp => dapp.category === category && dapp.isApproved() === true,
108        )
109        this.categoryMap.set(category, filtered)
110      }
111      return filtered
112    }
113  
114    getHighestRanked() {
115      if (this.dappsHightestRanked === null) {
116        this.dappsHightestRanked = this.dapps.filter(dapp => dapp.sntValue > 0)
117      }
118      return this.dappsHightestRanked
119    }
120  
121    getRecentlyAdded() {
122      if (this.dappsRecentlyAdded === null) {
123        this.dappsRecentlyAdded = this.dapps
124          .filter(dapp => dapp.isApproved() === true)
125          .sort((a, b) => {
126            return (
127              new Date(b.dateAdded).getTime() - new Date(a.dateAdded).getTime()
128            )
129          })
130          .slice(0, RECENTLY_ADDED_SIZE)
131      }
132      return this.dappsRecentlyAdded
133    }
134  }
135  
136  const dappsInitialState = new DappState()
137  dappsInitialState.loaded = false
138  export { dappsInitialState }