-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgivehub-sdk-example.js
163 lines (147 loc) · 4.08 KB
/
givehub-sdk-example.js
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
// Initialize the SDK
const givehub = new GiveHub({
baseUrl: 'https://api.thegivehub.com',
version: 'v1',
apiKey: 'your-api-key'
});
// Authentication Examples
async function authExamples() {
try {
// Login
const loginResult = await givehub.auth.login('[email protected]', 'password123');
console.log('Logged in as:', loginResult.user.username);
// Register new user
const userData = {
email: '[email protected]',
password: 'securepass123',
firstName: 'John',
lastName: 'Doe'
};
await givehub.auth.register(userData);
} catch (error) {
console.error('Auth error:', error);
}
}
// Campaign Examples
async function campaignExamples() {
try {
// Create campaign
const campaign = await givehub.campaigns.create({
title: 'Clean Water Project',
description: 'Providing clean water access to remote communities',
targetAmount: 50000,
category: 'water',
milestones: [
{
description: 'Phase 1: Survey',
amount: 10000
}
]
});
// Upload campaign media
const mediaFile = new File(['...'], 'campaign-photo.jpg', { type: 'image/jpeg' });
await givehub.campaigns.uploadMedia(campaign.id, mediaFile);
// Get campaign list
const campaigns = await givehub.campaigns.list({
category: 'water',
status: 'active',
page: 1,
limit: 10
});
} catch (error) {
console.error('Campaign error:', error);
}
}
// Donation Examples
async function donationExamples() {
try {
// Create one-time donation
const donation = await givehub.donations.create({
campaignId: 'campaign-id',
amount: {
value: 100,
currency: 'USD'
},
type: 'one-time'
});
// Create recurring donation
const recurring = await givehub.donations.createRecurring({
campaignId: 'campaign-id',
amount: {
value: 50,
currency: 'USD'
},
frequency: 'monthly'
});
} catch (error) {
console.error('Donation error:', error);
}
}
// Impact Tracking Examples
async function impactExamples() {
try {
// Create impact metrics
const metrics = await givehub.impact.createMetrics('campaign-id', {
metrics: [
{
name: 'People Helped',
value: 500,
unit: 'individuals'
},
{
name: 'Water Access',
value: 1000,
unit: 'liters/day'
}
]
});
// Get impact metrics
const impact = await givehub.impact.getMetrics('campaign-id', {
from: '2024-01-01',
to: '2024-12-31'
});
} catch (error) {
console.error('Impact error:', error);
}
}
// Update Examples
async function updateExamples() {
try {
// Create campaign update
const update = await givehub.updates.create({
campaignId: 'campaign-id',
title: 'Construction Progress',
content: 'We have completed the first phase of well construction.',
type: 'milestone'
});
// Upload update media
const mediaFile = new File(['...'], 'progress-photo.jpg', { type: 'image/jpeg' });
await givehub.updates.uploadMedia(update.id, mediaFile);
} catch (error) {
console.error('Update error:', error);
}
}
// Notification Examples
function notificationExamples() {
// Connect to real-time notifications
givehub.notifications.connect();
// Listen for donation notifications
givehub.notifications.on('donation_received', (notification) => {
console.log('New donation:', notification.amount);
updateDonationCounter(notification);
});
// Listen for milestone completions
givehub.notifications.on('milestone_completed', (notification) => {
console.log('Milestone completed:', notification.milestone);
updateCampaignProgress(notification);
});
// Listen for impact updates
givehub.notifications.on('impact_verified', (notification) => {
console.log('Impact verified:', notification.metrics);
updateImpactDisplay(notification);
});
// Clean up on page unload
window.addEventListener('unload', () => {
givehub.notifications.disconnect();
});
}