Skip to content

Commit 0ab732e

Browse files
CopilotTechQuery
andcommitted
Fix getStaticProps serialization and add error handling for API failures
Co-authored-by: TechQuery <19969570+TechQuery@users.noreply.github.com>
1 parent 0d6041b commit 0ab732e

2 files changed

Lines changed: 79 additions & 57 deletions

File tree

pages/ngo/index.tsx

Lines changed: 58 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -7,71 +7,82 @@ import { ChinaPublicInterestMap } from '../../components/Organization/ChinaPubli
77
import { OrganizationModel, OrganizationStatistic } from '../../models/Organization';
88
import { I18nContext } from '../../models/Translation';
99

10-
export interface NGOPageProps extends OrganizationStatistic {
11-
store: OrganizationModel;
12-
}
10+
export interface NGOPageProps extends OrganizationStatistic {}
1311

1412
export const getStaticProps: GetStaticProps<NGOPageProps> = async () => {
15-
const store = new OrganizationModel();
16-
17-
const allData = await store.getAll();
18-
19-
// Generate statistics from the data
20-
const yearStats = allData.reduce((acc, org) => {
21-
if (org.year) {
22-
const yearKey = org.year.toString();
23-
acc[yearKey] = (acc[yearKey] || 0) + 1;
24-
}
13+
try {
14+
const store = new OrganizationModel();
15+
16+
const allData = await store.getAll();
17+
18+
// Generate statistics from the data
19+
const yearStats = allData.reduce((acc, org) => {
20+
if (org.year) {
21+
const yearKey = org.year.toString();
22+
acc[yearKey] = (acc[yearKey] || 0) + 1;
23+
}
2524

26-
return acc;
27-
}, {} as Record<string, number>);
25+
return acc;
26+
}, {} as Record<string, number>);
2827

29-
const cityStats = allData.reduce((acc, org) => {
30-
if (org.city) {
31-
acc[org.city] = (acc[org.city] || 0) + 1;
32-
}
28+
const cityStats = allData.reduce((acc, org) => {
29+
if (org.city) {
30+
acc[org.city] = (acc[org.city] || 0) + 1;
31+
}
3332

34-
return acc;
35-
}, {} as Record<string, number>);
33+
return acc;
34+
}, {} as Record<string, number>);
3635

37-
const typeStats = allData.reduce((acc, org) => {
38-
if (org.type) {
39-
acc[org.type] = (acc[org.type] || 0) + 1;
40-
}
36+
const typeStats = allData.reduce((acc, org) => {
37+
if (org.type) {
38+
acc[org.type] = (acc[org.type] || 0) + 1;
39+
}
4140

42-
return acc;
43-
}, {} as Record<string, number>);
41+
return acc;
42+
}, {} as Record<string, number>);
4443

45-
const tagStats = allData.reduce((acc, org) => {
46-
if (org.tags) {
47-
org.tags.forEach(tag => {
48-
acc[tag] = (acc[tag] || 0) + 1;
49-
});
50-
}
44+
const tagStats = allData.reduce((acc, org) => {
45+
if (org.tags) {
46+
org.tags.forEach(tag => {
47+
acc[tag] = (acc[tag] || 0) + 1;
48+
});
49+
}
5150

52-
return acc;
53-
}, {} as Record<string, number>);
51+
return acc;
52+
}, {} as Record<string, number>);
5453

55-
return {
56-
props: {
57-
year: Object.entries(yearStats).map(([label, count]) => ({ label, count })),
58-
city: Object.entries(cityStats).map(([label, count]) => ({ label, count })),
59-
type: Object.entries(typeStats).map(([label, count]) => ({ label, count })),
60-
tag: Object.entries(tagStats).map(([label, count]) => ({ label, count })),
61-
store,
62-
},
63-
revalidate: 60 * 60 * 24, // Revalidate once per day
64-
};
54+
return {
55+
props: {
56+
year: Object.entries(yearStats).map(([label, count]) => ({ label, count })),
57+
city: Object.entries(cityStats).map(([label, count]) => ({ label, count })),
58+
type: Object.entries(typeStats).map(([label, count]) => ({ label, count })),
59+
tag: Object.entries(tagStats).map(([label, count]) => ({ label, count })),
60+
},
61+
revalidate: 60 * 60 * 24, // Revalidate once per day
62+
};
63+
} catch (error) {
64+
console.error('Failed to load organization data:', error);
65+
// Return empty data structure when API is not available
66+
return {
67+
props: {
68+
year: [],
69+
city: [],
70+
type: [],
71+
tag: [],
72+
},
73+
revalidate: 60 * 60 * 24,
74+
};
75+
}
6576
};
6677

67-
const NGOPage: FC<NGOPageProps> = observer(({ store, year, city, type, tag }) => {
78+
const NGOPage: FC<NGOPageProps> = observer(({ year, city, type, tag }) => {
6879
const { t } = useContext(I18nContext);
6980

7081
return (
7182
<>
7283
<PageHead title={t('china_public_interest_map')} />
7384
<ChinaPublicInterestMap
74-
store={store}
85+
store={new OrganizationModel()}
7586
year={year}
7687
city={city}
7788
type={type}

pages/ngo/landscape.tsx

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,27 @@ export interface NGOLandscapePageProps {
1212
}
1313

1414
export const getStaticProps: GetStaticProps<NGOLandscapePageProps> = async () => {
15-
const store = new OrganizationModel();
16-
17-
const categoryMap = await store.groupAllByTags();
18-
19-
return {
20-
props: {
21-
categoryMap,
22-
},
23-
revalidate: 60 * 60 * 24, // Revalidate once per day
24-
};
15+
try {
16+
const store = new OrganizationModel();
17+
18+
const categoryMap = await store.groupAllByTags();
19+
20+
return {
21+
props: {
22+
categoryMap,
23+
},
24+
revalidate: 60 * 60 * 24, // Revalidate once per day
25+
};
26+
} catch (error) {
27+
console.error('Failed to load landscape data:', error);
28+
// Return empty data structure when API is not available
29+
return {
30+
props: {
31+
categoryMap: {},
32+
},
33+
revalidate: 60 * 60 * 24,
34+
};
35+
}
2536
};
2637

2738
const LandscapePage: FC<NGOLandscapePageProps> = ({ categoryMap }) => {

0 commit comments

Comments
 (0)