@@ -7,71 +7,82 @@ import { ChinaPublicInterestMap } from '../../components/Organization/ChinaPubli
77import { OrganizationModel , OrganizationStatistic } from '../../models/Organization' ;
88import { I18nContext } from '../../models/Translation' ;
99
10- export interface NGOPageProps extends OrganizationStatistic {
11- store : OrganizationModel ;
12- }
10+ export interface NGOPageProps extends OrganizationStatistic { }
1311
1412export 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 }
0 commit comments