/ src / modules / TransactionStatus / TransactionStatus.utilities.js
TransactionStatus.utilities.js
 1  const COOKIE_NAME = 'TRANSACTION_STATUS_COOKIE'
 2  
 3  export const TYPE_NONE = 0
 4  export const TYPE_SUBMIT = 1
 5  export const TYPE_UPVOTE = 2
 6  export const TYPE_DOWNVOTE = 3
 7  
 8  class TransactionStatus {
 9    constructor() {
10      this.dappId = ''
11      this.dappTx = ''
12      this.txDesc = ''
13      this.dappName = ''
14      this.dappImg = ''
15      this.type = TYPE_NONE
16      this.progress = false
17      this.published = false
18      this.failed = false
19    }
20  
21    persistTransactionData() {
22      localStorage.setItem(COOKIE_NAME, JSON.stringify(this))
23    }
24  
25    setDappName(name) {
26      this.dappName = name
27      this.persistTransactionData()
28    }
29  
30    setTransactionInfo(id, tx) {
31      this.dappId = id
32      this.dappTx = tx
33      this.persistTransactionData()
34    }
35  
36    setProgress(progress) {
37      this.progress = progress
38      this.published = false
39      this.failed = false
40      this.persistTransactionData()
41    }
42  
43    setPublished(published) {
44      this.progress = false
45      this.published = published
46      this.failed = false
47      this.persistTransactionData()
48    }
49  
50    setFailed(failed) {
51      this.progress = false
52      this.published = false
53      this.failed = failed
54      this.persistTransactionData()
55    }
56  
57    setType(type) {
58      this.type = type
59      this.persistTransactionData()
60    }
61  }
62  
63  const getTransactionData = () => {
64    return localStorage.getItem(COOKIE_NAME)
65  }
66  
67  export const transactionStatusInitInstance = (name, img, desc, type) => {
68    const model = new TransactionStatus()
69    model.dappName = name
70    model.dappImg = img
71    model.progress = true
72    model.txDesc = desc
73    model.type = type
74    return model
75  }
76  
77  export const transactionStatusFetchedInstance = () => {
78    const data = getTransactionData()
79    let transactionStatus = new TransactionStatus()
80    if (data !== null)
81      transactionStatus = Object.assign(transactionStatus, JSON.parse(data))
82    return transactionStatus
83  }