/ 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  export const TYPE_WITHDRAW = 4
 8  export const TYPE_UPDATE = 5
 9  
10  class TransactionStatus {
11    constructor() {
12      this.dappId = '' // responsible for background transaction check
13      this.dappTx = '' // responsible for background transaction check
14      this.txDesc = ''
15      this.dappName = '' // responsible for UI visibility
16      this.dappImg = ''
17      this.type = TYPE_NONE
18      this.progress = false
19      this.message = ''
20      this.published = false
21      this.publishedEmpty = false
22      this.failed = false
23    }
24  
25    persistTransactionData() {
26      localStorage.setItem(COOKIE_NAME, JSON.stringify(this))
27    }
28  
29    setDappName(name) {
30      this.dappName = name
31      this.persistTransactionData()
32    }
33  
34    setTransactionInfo(id, tx) {
35      this.dappId = id
36      this.dappTx = tx
37      this.persistTransactionData()
38    }
39  
40    setProgress(progress) {
41      this.progress = progress
42      this.published = false
43      this.publishedEmpty = false
44      this.failed = false
45      this.persistTransactionData()
46    }
47  
48    setPublished(published, message) {
49      this.progress = false
50      this.published = published
51      this.message = message
52      this.publishedEmpty = false
53      this.failed = false
54      this.persistTransactionData()
55    }
56  
57    setFailed(failed) {
58      this.progress = false
59      this.published = false
60      this.publishedEmpty = false
61      this.failed = failed
62      this.persistTransactionData()
63    }
64  
65    setPublishedEmpty(published) {
66      this.progress = false
67      this.published = false
68      this.publishedEmpty = published
69      this.failed = false
70      this.persistTransactionData()
71    }
72  
73    setType(type) {
74      this.type = type
75      this.persistTransactionData()
76    }
77  }
78  
79  const getTransactionData = () => {
80    return localStorage.getItem(COOKIE_NAME)
81  }
82  
83  export const transactionStatusInitInstance = (name, img, desc, type) => {
84    const model = new TransactionStatus()
85    model.dappName = name
86    model.dappImg = img
87    model.progress = true
88    model.txDesc = desc
89    model.type = type
90    return model
91  }
92  
93  export const transactionStatusFetchedInstance = () => {
94    const data = getTransactionData()
95    let transactionStatus = new TransactionStatus()
96    if (data !== null)
97      transactionStatus = Object.assign(transactionStatus, JSON.parse(data))
98    return transactionStatus
99  }