Skip to content

Commit

Permalink
refactor: reduce code duplication
Browse files Browse the repository at this point in the history
  • Loading branch information
gratestas authored and 0xferit committed Jun 12, 2023
1 parent 7259426 commit 43f4d1d
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 39 deletions.
1 change: 0 additions & 1 deletion src/data/ethereumProvider.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ export const networkMap = {

const LONGPOLLING_PERIOD_MS = 20000;

const isSupportedChain = (chainId) => Object.keys(networkMap).includes(chainId);
const initializeContract = (chainId) => new ethers.Contract(Object.keys(networkMap[chainId].deployments)[0], ABI);
const getDefaultNetwork = () => {
const defaultNetworkKeys = Object.keys(networkMap).filter((key) => networkMap[key].default);
Expand Down
21 changes: 2 additions & 19 deletions src/routes/account/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,11 @@ import * as styles from "./index.module.scss";

import ListArticles from "/src/components/others/listArticles";
import { getArticlesByAuthor } from "/src/data/ethereumProvider";
import { ipfsGateway } from "/src/utils/addToIPFS";
import populateArticleContents from "../../utils/populateArticleContents";

export async function loader({ params }) {
const articles = await getArticlesByAuthor(params.chain, params.id);
const fetchPromises = articles.map(async (article) => {
if (!article) return null;
try {
const response = await fetch(ipfsGateway + article?.articleID);
if (!response.ok) {
throw new Error("Network response was not OK");
}
const { title, description } = await response.json();
article.title = title;
article.description = description;
} catch (error) {
console.error(error);
throw new Error(error.message);
}
});

await Promise.all(fetchPromises);
return articles;
return await populateArticleContents(articles);
}

export default function Account() {
Expand Down
21 changes: 2 additions & 19 deletions src/routes/browse/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,11 @@ import ListArticles from "/src/components/others/listArticles";
import SyncStatus from "/src/components/presentational/syncStatus";

import { EthereumContext, networkMap, getAllArticles } from "/src/data/ethereumProvider";
import { ipfsGateway } from "/src/utils/addToIPFS";
import populateArticleContents from "../../utils/populateArticleContents";

export async function loader({ params }) {
const articles = await getAllArticles(params.chain);
const fetchPromises = articles.map(async (article) => {
if (!article) return null;
try {
const response = await fetch(ipfsGateway + article?.articleID);
if (!response.ok) {
throw new Error("Network response was not OK");
}
const { title, description } = await response.json();
article.title = title;
article.description = description;
} catch (error) {
console.error(error);
throw new Error(error.message);
}
});

await Promise.all(fetchPromises);
return articles;
return await populateArticleContents(articles);
}

export default function Browse() {
Expand Down
22 changes: 22 additions & 0 deletions src/utils/populateArticleContents.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { ipfsGateway } from "/src/utils/addToIPFS";

export default async function populateArticleContents(articles) {
const fetchPromises = articles.map(async (article) => {
if (!article) return null;
try {
const response = await fetch(ipfsGateway + article?.articleID);
if (!response.ok) {
throw new Error("Network response was not OK");
}
const { title, description } = await response.json();
article.title = title;
article.description = description;
} catch (error) {
console.error(error);
throw new Error(error.message);
}
});

await Promise.all(fetchPromises);
return articles;
}

0 comments on commit 43f4d1d

Please sign in to comment.