Browse Source

App: improved fetching

Joystream Stats 3 years ago
parent
commit
36c7e41854
1 changed files with 49 additions and 17 deletions
  1. 49 17
      src/App.tsx

+ 49 - 17
src/App.tsx

@@ -4,7 +4,7 @@ import "./index.css";
 import { Modals, Routes, Loading, Footer, Status } from "./components";
 import { Modals, Routes, Loading, Footer, Status } from "./components";
 
 
 import * as get from "./lib/getters";
 import * as get from "./lib/getters";
-import { domain, wsLocation } from "./config";
+import { domain, apiLocation, wsLocation } from "./config";
 import axios from "axios";
 import axios from "axios";
 
 
 import { Api, IState } from "./types";
 import { Api, IState } from "./types";
@@ -153,6 +153,7 @@ class App extends React.Component<IProps, IState> {
     if (id / 50 === Math.floor(id / 50)) {
     if (id / 50 === Math.floor(id / 50)) {
       this.updateStatus(api, id);
       this.updateStatus(api, id);
       this.fetchTokenomics();
       this.fetchTokenomics();
+      this.updateActiveProposals();
     }
     }
     if (!status.lastReward) this.fetchLastReward(api);
     if (!status.lastReward) this.fetchLastReward(api);
   }
   }
@@ -182,6 +183,19 @@ class App extends React.Component<IProps, IState> {
     this.save("status", status);
     this.save("status", status);
   }
   }
 
 
+  updateActiveProposals() {
+    const active = this.state.proposals.filter((p) => p.result === "Pending");
+    console.log(`Updating ${active.length} active proposals`);
+    active.forEach(async (a) => {
+      const { data } = await axios.get(`${apiLocation}/proposals/${a.id}`);
+      if (!data || data.error) return console.error(`failed to fetch from API`);
+      this.save(
+        "proposals",
+        this.state.proposals.map((p) => (p.id === a.id ? data : p))
+      );
+    });
+  }
+
   async updateEra(api: Api) {
   async updateEra(api: Api) {
     const era = Number(await api.query.staking.currentEra());
     const era = Number(await api.query.staking.currentEra());
     this.fetchEraRewardPoints(api, era);
     this.fetchEraRewardPoints(api, era);
@@ -222,9 +236,7 @@ class App extends React.Component<IProps, IState> {
   }
   }
 
 
   async fetchCouncils() {
   async fetchCouncils() {
-    const { data } = await axios.get(
-      `https://api.joystreamstats.live/api/v1/councils`
-    );
+    const { data } = await axios.get(`${apiLocation}/councils`);
     if (!data || data.error) return console.error(`failed to fetch from API`);
     if (!data || data.error) return console.error(`failed to fetch from API`);
     console.debug(`councils`, data);
     console.debug(`councils`, data);
     this.save("councils", data);
     this.save("councils", data);
@@ -239,25 +251,39 @@ class App extends React.Component<IProps, IState> {
     this.save("status", status);
     this.save("status", status);
   }
   }
   async fetchProposals() {
   async fetchProposals() {
-    const { data } = await axios.get(
-      `https://api.joystreamstats.live/api/v1/proposals`
-    );
+    const { data } = await axios.get(`${apiLocation}/proposals`);
     if (!data || data.error) return console.error(`failed to fetch from API`);
     if (!data || data.error) return console.error(`failed to fetch from API`);
     console.debug(`proposals`, data);
     console.debug(`proposals`, data);
     this.save("proposals", data);
     this.save("proposals", data);
   }
   }
+
+  // forum
+  updateForum() {
+    this.fetchPosts();
+    this.fetchThreads();
+    this.fetchCategories();
+  }
   async fetchPosts() {
   async fetchPosts() {
-    const { data } = await axios.get(
-      `https://api.joystreamstats.live/api/v1/posts`
-    );
+    const { data } = await axios.get(`${apiLocation}/posts`);
     if (!data || data.error) return console.error(`failed to fetch from API`);
     if (!data || data.error) return console.error(`failed to fetch from API`);
     console.debug(`posts`, data);
     console.debug(`posts`, data);
     this.save("posts", data);
     this.save("posts", data);
   }
   }
+  async fetchThreads() {
+    const { data } = await axios.get(`${apiLocation}/threads`);
+    if (!data || data.error) return console.error(`failed to fetch from API`);
+    console.debug(`threads`, data);
+    this.save("threads", data);
+  }
+  async fetchCategories() {
+    const { data } = await axios.get(`${apiLocation}/categories`);
+    if (!data || data.error) return console.error(`failed to fetch from API`);
+    console.debug(`categories`, data);
+    this.save("categories", data);
+  }
+
   async fetchMembers() {
   async fetchMembers() {
-    const { data } = await axios.get(
-      `https://api.joystreamstats.live/api/v1/members`
-    );
+    const { data } = await axios.get(`${apiLocation}/members`);
     if (!data || data.error) return console.error(`failed to fetch from API`);
     if (!data || data.error) return console.error(`failed to fetch from API`);
     console.debug(`members`, data);
     console.debug(`members`, data);
     this.save("members", data);
     this.save("members", data);
@@ -479,6 +505,7 @@ class App extends React.Component<IProps, IState> {
           toggleStar={this.toggleStar}
           toggleStar={this.toggleStar}
           getMember={this.getMember}
           getMember={this.getMember}
           fetchProposals={this.fetchProposals}
           fetchProposals={this.fetchProposals}
+          updateForum={this.updateForum}
           {...this.state}
           {...this.state}
         />
         />
 
 
@@ -511,13 +538,17 @@ class App extends React.Component<IProps, IState> {
     );
     );
   }
   }
 
 
-  componentDidMount() {
-    this.loadData();
-    this.connectEndpoint();
+  fetchFromApi() {
     this.fetchProposals();
     this.fetchProposals();
-    this.fetchPosts();
+    this.updateForum();
     this.fetchMembers();
     this.fetchMembers();
     this.fetchCouncils();
     this.fetchCouncils();
+  }
+
+  componentDidMount() {
+    this.loadData();
+    this.connectEndpoint();
+    this.fetchFromApi();
     this.fetchStorageProviders();
     this.fetchStorageProviders();
     this.fetchAssets();
     this.fetchAssets();
     setTimeout(() => this.fetchTokenomics(), 30000);
     setTimeout(() => this.fetchTokenomics(), 30000);
@@ -534,6 +565,7 @@ class App extends React.Component<IProps, IState> {
     this.toggleShowStatus = this.toggleShowStatus.bind(this);
     this.toggleShowStatus = this.toggleShowStatus.bind(this);
     this.getMember = this.getMember.bind(this);
     this.getMember = this.getMember.bind(this);
     this.fetchProposals = this.fetchProposals.bind(this);
     this.fetchProposals = this.fetchProposals.bind(this);
+    this.updateForum = this.updateForum.bind(this);
   }
   }
 }
 }