-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathSCEvents.js
More file actions
149 lines (135 loc) · 4.13 KB
/
SCEvents.js
File metadata and controls
149 lines (135 loc) · 4.13 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
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
import { ApiResponse } from './ApiResponses';
const SCEVENTS_API_URL = 'http://localhost:8002';
// ======== ORIGINAL BACKEND API REQUESTS ========
export async function getAllSCEvents() {
const status = new ApiResponse();
try {
const res = await fetch(`${SCEVENTS_API_URL}/events/`);
const result = await res.json();
status.responseData = result;
if (!res.ok) {
status.error = true;
}
} catch (err) {
status.responseData = err;
status.error = true;
}
return status;
}
export async function getEventByID(id) {
const status = new ApiResponse();
try {
const res = await fetch(`${SCEVENTS_API_URL}/events/${id}`);
const result = await res.json();
status.responseData = result;
if (!res.ok) {
status.error = true;
}
} catch (err) {
status.error = true;
status.responseData = err;
}
return status;
}
export async function createSCEvent(eventBody) {
const status = new ApiResponse();
try {
const res = await fetch(`${SCEVENTS_API_URL}/events/`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(eventBody),
});
const body = await res.json();
status.responseData = body;
if (!res.ok) {
status.error = true;
}
} catch (err) {
status.error = true;
status.responseData = err;
}
return status;
}
export async function updateSCEvent(id, userId, eventUpdates) {
const status = new ApiResponse();
try {
const res = await fetch(`${SCEVENTS_API_URL}/events/${id}?user_id=${userId}`, {
method: 'PATCH',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(eventUpdates),
});
const body = await res.json();
status.responseData = body;
if (!res.ok) {
status.error = true;
}
} catch (err) {
status.error = true;
status.responseData = err;
}
return status;
}
// =================================================
// ======== MOCKED LOCAL STORAGE VERSIONS ========
// const getMockEvents = () => {
// const data = localStorage.getItem('mockSCEvents');
// // Initialize with empty array if null
// return data ? JSON.parse(data) : [];
// };
// const setMockEvents = (events) => {
// localStorage.setItem('mockSCEvents', JSON.stringify(events));
// };
// export async function getAllSCEvents() {
// const status = new ApiResponse();
// // Simulated network delay
// await new Promise(resolve => setTimeout(resolve, 300));
// status.responseData = getMockEvents();
// status.error = false;
// return status;
// }
// export async function getEventByID(id) {
// const status = new ApiResponse();
// await new Promise(resolve => setTimeout(resolve, 300));
// const events = getMockEvents();
// const event = events.find(e => e.id === id || e._id === id);
// if (event) {
// status.responseData = event;
// status.error = false;
// } else {
// status.error = true;
// status.responseData = 'Event not found in mock local storage.';
// }
// return status;
// }
// export async function createSCEvent(eventBody) {
// const status = new ApiResponse();
// await new Promise(resolve => setTimeout(resolve, 300));
// const events = getMockEvents();
// // Simulate backend appending the event
// events.push(eventBody);
// setMockEvents(events);
// status.responseData = eventBody;
// status.error = false;
// return status;
// }
// export async function updateSCEvent(id, userId, eventUpdates) {
// const status = new ApiResponse();
// await new Promise(resolve => setTimeout(resolve, 300));
// const events = getMockEvents();
// const index = events.findIndex(e => e.id === id || e._id === id);
// if (index !== -1) {
// // Note: A real backend would verify `userId` here, but for simple mocked testing:
// events[index] = { ...events[index], ...eventUpdates };
// setMockEvents(events);
// status.responseData = { message: 'Updated successfully in mock local storage' };
// status.error = false;
// } else {
// status.error = true;
// status.responseData = 'Event not found in mock local storage.';
// }
// return status;
// }