separate data from component

This commit is contained in:
Mohammed Saud 2019-10-06 19:26:18 +05:30
parent 17f608d626
commit 653d977130
2 changed files with 50 additions and 26 deletions

View File

@ -1,6 +1,7 @@
let m = require('mithril');
let rs = require('rswebui');
let widget = require('widgets');
let data = require('network_data');
const ConfirmRemove = () => {
@ -103,36 +104,14 @@ const SearchBar = () => {
const Friends = () => {
return {
oninit: () => {
FriendNodes = {};
rs.rsJsonApiRequest(
'/rsPeers/getFriendList', {},
(friendListIds) => {
friendListIds.sslIds.map(
(sslId) => rs.rsJsonApiRequest(
'/rsPeers/getPeerDetails', {
sslId
},
(details) => {
// Store nodes obj with gpg id as key
// single node can have multiple identities
if(FriendNodes[details.det.gpg_id] === undefined)
FriendNodes[details.det.gpg_id] = {
locations: [details.det],
isSearched: true
}
else
FriendNodes[details.det.gpg_id].locations.push(
details.det);
})
);
});
data.refreshGpgDetails();
},
view: () => m('.widget', [
m('h3', 'Friend nodes'),
m('hr'),
Object.keys(FriendNodes).map((id) => m(Node, {
data: FriendNodes[id].locations,
isSearched: FriendNodes[id].isSearched,
Object.keys(data.gpgDetails).map((id) => m(Node, {
data: data.gpgDetails[id].locations,
isSearched: data.gpgDetails[id].isSearched,
})),
]),
};

View File

@ -0,0 +1,45 @@
let rs = require('rswebui');
module.exports = {
sslIds: [],
sslDetails: [],
gpgDetails: {},
refreshIds() {
return rs.rsJsonApiRequest(
'/rsPeers/getFriendList',
{},
data => (this.sslIds = data.sslIds),
);
},
loadSslDetails() {
this.sslDetails = [];
return Promise.all(
this.sslIds.map(sslId =>
rs.rsJsonApiRequest('/rsPeers/getPeerDetails', {sslId}, data =>
this.sslDetails.push(data.det),
),
),
);
},
refreshGpgDetails() {
let details = {};
this.refreshIds()
.then(() => this.loadSslDetails())
.then(() => {
this.sslDetails.map(data => {
if (details[data.gpg_id] === undefined) {
details[data.gpg_id] = {isSearched: true, locations: [data]};
console.log(data);
} else {
details[data.gpg_id].locations.push(data);
}
});
this.gpgDetails = details;
});
},
};