Refactoring store and association stores into one

This commit is contained in:
DwCay
2022-07-15 18:54:03 +03:00
parent 1e9830c8d4
commit 429f36b6a6
11 changed files with 133 additions and 221 deletions

View File

@@ -1,7 +1,14 @@
import { reaction, makeAutoObservable } from "mobx";
import { reaction, makeAutoObservable, onBecomeObserved, onBecomeUnobserved} from "mobx";
import {cryptography} from "@liskhq/lisk-client";
import {getNodeInfo} from '../api/node';
import {fetchWrapper} from '../shared/fetchWrapper';
import {labelMap} from "../shared/labelMap";
import User08 from "../images/user-28-08.jpg";
import User06 from "../images/user-28-06.jpg";
import User05 from "../images/user-28-05.jpg";
import User09 from "../images/user-28-09.jpg";
const usersImages = [User08,User06,User05,User09];
class Store {
_accountData = []
@@ -10,27 +17,26 @@ class Store {
_nodeInfo = {}
_userData = {}
_userData = []
_keysArray = []
_transactionsInfo = {}
_sharedData = []
_transactionsInfo = []
constructor() {
makeAutoObservable(this);
this.fetchNodeInfo()
reaction(() => this.tokenKey, () => this.getData())
reaction(() => this.tokenKey, () => this.fetchNewAccountData())
reaction(() => this.tokenKey, () => this.fetchTransactionsInfo())
reaction(()=>this.keysArray, ()=>this.fetchSharedData())
onBecomeObserved(this, "userData", () => this.fetchNewAccountData())
onBecomeObserved(this, "transactionsInfo", ()=>this.fetchTransactionsInfo())
onBecomeUnobserved(this, "transactionsInfo", ()=>this.unobservedTransactionsInfo())
onBecomeObserved(this, "sharedData", ()=>this.fetchKeysArray())
};
getData() {
this.fetchNewAccountData();
this.fetchKeysArray();
}
fetchNewAccountData() {
fetchWrapper.getAuth('http://3.125.47.101/api/data/private', {
networkIdentifier: this.nodeInfo.networkIdentifier,
@@ -63,18 +69,24 @@ class Store {
.then((res)=>this.saveInfoTransactions(res))
}
fetchSharedData() {
this._keysArray.forEach((elem) => fetchWrapper.get(`http://3.125.47.101/api/data/shared/${elem}`).then((res) => this.changeSharedData(res)))
};
unobservedTransactionsInfo() {
this._transactionsInfo=[]
}
changeSharedData(incomingData) {
this._sharedData.push({
data: incomingData.data,
})
}
get accountMadeTransaction() {
return "b444b7ff3118cf2a30cbd54cfcdb8fd5d805017a"
}
userDataFetchChange(res) {
this._userData = res;
}
keysArrayFetchChange(res) {
this._keysArray = res.data;
}
get keysArray() {
return this._keysArray;
}
@@ -83,10 +95,6 @@ class Store {
return this._userData;
}
get transactionsInfo() {
return this._transactionsInfo
}
savePassPhrase(phrase) {
this._passPhrase = phrase
}
@@ -96,7 +104,15 @@ class Store {
};
saveInfoTransactions(transaction) {
this._transactionsInfo = transaction
this._transactionsInfo = transaction.data
}
userDataFetchChange(res) {
this._userData = res.data;
}
keysArrayFetchChange(res) {
this._keysArray = res.data;
}
fetchNodeInfo() {
@@ -121,6 +137,46 @@ class Store {
return this._accountData
}
get sharedData() {
return this._sharedData.map(elem => ({
data: elem.data.map(item => ({
label: labelMap[item.label],
value: item.value,
}))
}));
};
get transactionsInfo() {
return this._transactionsInfo.map(item=>{
return {
id: item.id,
users_images: usersImages.map(image=>{
return {
image: image,
size: 24
}
}),
transaction: item.asset.features.map(asset=>{
return {
transaction_id: item.id,
address: this.accountMadeTransaction,
value: asset.value,
label: labelMap[asset.label],
}
})
}
})
}
get decryptedUserData(){
return this._userData.map((elem) => ({
...elem,
key: elem.label,
label: labelMap[elem.label],
value: cryptography.decryptMessageWithPassphrase(elem.value, elem.value_nonce, registrationStore.passPhrase, registrationStore.pubKey).split(':')[1]
}))
}
encryptAccountData(data) {
return data.map((item) => {
let value = cryptography.encryptMessageWithPassphrase(cryptography.getRandomBytes(32).toString('hex') + ":" + item.value, this.passPhrase, this.pubKey);