Skip to content

Commit

Permalink
Merge pull request #1802 from yunchipang/fix/1801-404-error-on-top-page
Browse files Browse the repository at this point in the history
fix: handle api 404 error to display data properly
  • Loading branch information
dadiorchen authored Oct 26, 2024
2 parents d1a30e4 + e6851f0 commit c6b429e
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 21 deletions.
40 changes: 34 additions & 6 deletions src/models/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ export async function getFeaturedGrowers() {
const res = await axios.get(url);
const data = await res.data;
log.warn('url:', url, 'took:', Date.now() - begin);
return data.grower_accounts;
return data.grower_accounts || [];
} catch (err) {
log.error(err.message);
throw err;
log.error('Error in getFeaturedGrowers:', err.message);
return [];
}
}

Expand All @@ -24,10 +24,10 @@ export async function getCaptures() {
const res = await axios.get(url);
const data = await res.data;
log.warn('url:', url, 'took:', Date.now() - begin);
return data.captures;
return data.captures || [];
} catch (err) {
log.error(err.message);
throw err;
log.error('Error in getCaptures:', err.message);
return [];
}
}

Expand Down Expand Up @@ -73,6 +73,20 @@ export async function getCountryLeaderboard() {
}
}

export async function getOrganizations() {
try {
const url = apiPaths.featuredOrganizations;
const begin = Date.now();
const res = await axios.get(url);
const data = await res.data;
log.warn('url:', url, 'took:', Date.now() - begin);
return data.organizations || [];
} catch (err) {
log.error('Error in getOrganizations:', err.message);
return [];
}
}

export async function getOrganizationById(id) {
try {
const url = apiPaths.organization(id);
Expand Down Expand Up @@ -176,6 +190,20 @@ export async function getOrgLinks({
};
}

export async function getWallets() {
try {
const url = apiPaths.featuredWallets;
const begin = Date.now();
const res = await axios.get(url);
const data = await res.data;
log.warn('url:', url, 'took:', Date.now() - begin);
return data.wallets || [];
} catch (err) {
log.error('Error in getWallets:', err.message);
return [];
}
}

export async function getWalletById(id) {
try {
const url = apiPaths.wallets(id);
Expand Down
2 changes: 2 additions & 0 deletions src/models/apiPaths.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ const apiPaths = {
filterSpeciesByWalletId: (id = '') =>
urlJoin(host, `/species?wallet_id=${id}`),
tokens: (id = '') => urlJoin(host, `/tokens/${id}`),
featuredOrganizations: urlJoin(host, '/organizations/featured'),
featuredWallets: urlJoin(host, '/wallets/featured'),
};

export default apiPaths;
23 changes: 8 additions & 15 deletions src/pages/top.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ import {
getCaptures,
getCountryLeaderboard,
getFeaturedGrowers,
getFeaturedTrees,
getOrganizations,
getWallets,
} from 'models/api';
import * as utils from 'models/utils';

Expand Down Expand Up @@ -250,24 +251,16 @@ function Top(props) {
);
}

async function serverSideData(params) {
async function serverSideData() {
const [captures, countries, growers, organizations, wallets] =
await Promise.all([
getCaptures(), //
getCaptures(),
process.env.NEXT_PUBLIC_COUNTRY_LEADER_BOARD_DISABLED === 'true'
? []
: getCountryLeaderboard(),
getFeaturedGrowers(),
(async () => {
const data = await utils.requestAPI('/organizations/featured');
log.warn('organizations', data);
return data.organizations;
})(),
(async () => {
const data = await utils.requestAPI('/wallets/featured');
log.warn('wallets', data);
return data.wallets;
})(),
getOrganizations(),
getWallets(),
]);
return {
captures,
Expand All @@ -278,8 +271,8 @@ async function serverSideData(params) {
};
}

const getStaticProps = utils.wrapper(async ({ params }) => {
const props = await serverSideData(params);
const getStaticProps = utils.wrapper(async () => {
const props = await serverSideData();
return {
props,
revalidate: Number(process.env.NEXT_CACHE_REVALIDATION_OVERRIDE) || 30,
Expand Down

0 comments on commit c6b429e

Please sign in to comment.