-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathOrganization.ts
More file actions
99 lines (87 loc) · 2.84 KB
/
Organization.ts
File metadata and controls
99 lines (87 loc) · 2.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import { observable } from 'mobx';
import { HTTPClient } from 'koajax';
import { StrapiListModel, Base } from 'mobx-strapi';
// Define the organization data structure similar to China NGO database
export interface Organization extends Base {
name: string;
description?: string;
type?: string;
city?: string;
province?: string;
tags?: string[];
website?: string;
logo?: {
data?: {
attributes: {
url: string;
};
};
};
year?: number;
}
export interface OrganizationStatistic {
year: Array<{ label: string; count: number }>;
city: Array<{ label: string; count: number }>;
type: Array<{ label: string; count: number }>;
tag: Array<{ label: string; count: number }>;
}
// Strapi client configuration
const strapiClient = new HTTPClient({
baseURI: 'https://china-ngo-db.onrender.com/api/',
responseType: 'json',
});
export class OrganizationModel extends StrapiListModel<Organization> {
baseURI = '/organizations';
client = strapiClient;
@observable
accessor tagMap: Record<string, Organization[]> = {};
async groupAllByTags(): Promise<Record<string, Organization[]>> {
try {
const allData = await this.getAll();
const tagMap: Record<string, Organization[]> = {};
for (const org of allData) {
const tags = org.tags || [];
for (const tag of tags) {
if (!tagMap[tag]) {
tagMap[tag] = [];
}
tagMap[tag].push(org);
}
}
this.tagMap = tagMap;
return tagMap;
} catch (error) {
console.error('Failed to fetch organizations:', error);
return {};
}
}
}
export class OrganizationStatisticModel {
private client: HTTPClient<any>;
private collection: string;
constructor(baseId: string, collectionId: string) {
this.client = new HTTPClient({
baseURI: 'https://china-ngo-db.onrender.com/api/',
responseType: 'json',
});
this.collection = collectionId;
}
async countAll(): Promise<Array<{ label: string; count: number }>> {
try {
// This would need to be adapted based on the actual Strapi API structure
const response = await this.client.get(`${this.collection}`);
// Handle potential different response structures
const data = response.body?.data || response.body || [];
return Array.isArray(data) ? data : [];
} catch (error) {
console.error(`Failed to fetch statistics for ${this.collection}:`, error);
return [];
}
}
}
// Mock constants for now - these would be configured based on the actual Strapi setup
export const COMMUNITY_BASE_ID = 'community';
export const OSC_YEAR_STATISTIC_TABLE_ID = 'organization-year-stats';
export const OSC_CITY_STATISTIC_TABLE_ID = 'organization-city-stats';
export const OSC_TYPE_STATISTIC_TABLE_ID = 'organization-type-stats';
export const OSC_TAG_STATISTIC_TABLE_ID = 'organization-tag-stats';