Skip to content

Commit 0bb8204

Browse files
committed
Added SCEvents ability to create event
1 parent 41888d3 commit 0bb8204

3 files changed

Lines changed: 72 additions & 63 deletions

File tree

src/APIFunctions/SCEvents.js

Lines changed: 46 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,74 @@
11
import { ApiResponse } from './ApiResponses';
22

3-
export const SCEVENTS_API_URL = 'http://localhost:8002';
3+
export function getSCEventsBaseUrl() {
4+
return (
5+
(typeof process !== 'undefined' && process.env.REACT_APP_SCEVENTS_URL) ||
6+
'http://localhost:8002'
7+
);
8+
}
49

5-
export async function getAllSCEvents() {
6-
let status = new ApiResponse();
10+
function eventsUrl(path) {
11+
const base = getSCEventsBaseUrl().replace(/\/$/, '');
12+
const p = path.startsWith('/') ? path : `/${path}`;
13+
return `${base}${p}`;
14+
}
715

16+
async function readBodyAsJsonOrText(res) {
17+
const text = await res.text();
18+
if (!text) {
19+
return null;
20+
}
821
try {
9-
const url = new URL('/events/', SCEVENTS_API_URL);
10-
const res = await fetch(url.href, {
11-
method: 'GET',
12-
headers: {
13-
'Content-Type': 'application/json',
14-
},
15-
});
22+
return JSON.parse(text);
23+
} catch {
24+
return text;
25+
}
26+
}
1627

17-
if (res.ok) {
18-
const result = await res.json();
19-
status.responseData = result;
20-
} else {
28+
export async function getAllSCEvents() {
29+
const status = new ApiResponse();
30+
try {
31+
const res = await fetch(eventsUrl('/events/'));
32+
status.responseData = await readBodyAsJsonOrText(res);
33+
if (!res.ok) {
2134
status.error = true;
2235
}
2336
} catch (err) {
24-
status.error = true;
2537
status.responseData = err;
38+
status.error = true;
2639
}
40+
return status;
41+
}
2742

43+
export async function getEventByID(id) {
44+
const status = new ApiResponse();
45+
try {
46+
const res = await fetch(eventsUrl(`/events/${id}`));
47+
status.responseData = await readBodyAsJsonOrText(res);
48+
if (!res.ok) {
49+
status.error = true;
50+
}
51+
} catch (err) {
52+
status.responseData = err;
53+
status.error = true;
54+
}
2855
return status;
2956
}
3057

3158
export async function createSCEvent(eventBody) {
32-
let status = new ApiResponse();
59+
const status = new ApiResponse();
3360
status.statusCode = null;
3461
try {
35-
const url = new URL('/events/', SCEVENTS_API_URL);
36-
const res = await fetch(url.href, {
62+
const url = eventsUrl('/events/');
63+
const res = await fetch(url, {
3764
method: 'POST',
3865
headers: {
3966
'Content-Type': 'application/json',
4067
},
4168
body: JSON.stringify(eventBody),
4269
});
4370
status.statusCode = res.status;
44-
const body = await res.json();
71+
const body = await readBodyAsJsonOrText(res);
4572
if (res.ok) {
4673
status.responseData = body;
4774
} else {
@@ -56,29 +83,3 @@ export async function createSCEvent(eventBody) {
5683
}
5784
return status;
5885
}
59-
60-
export async function getEventByID(id) {
61-
let status = new ApiResponse();
62-
63-
try {
64-
const url = new URL(`/events/${id}`, SCEVENTS_API_URL);
65-
const res = await fetch(url.href, {
66-
method: 'GET',
67-
headers: {
68-
'Content-Type': 'application/json',
69-
},
70-
});
71-
72-
if (res.ok) {
73-
const result = await res.json();
74-
status.responseData = result;
75-
} else {
76-
status.error = true;
77-
}
78-
} catch (err) {
79-
status.error = true;
80-
status.responseData = err;
81-
}
82-
83-
return status;
84-
}

src/Components/Navbar/AdminNavbar.js

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -43,20 +43,29 @@ export default function UserNavBar(props) {
4343
},
4444
];
4545

46-
const sceventsAdminNavLinks =
47-
config.SCEvents?.ENABLED && user?.accessLevel >= membershipState.OFFICER
48-
? [
49-
{
50-
title: 'Create event',
51-
route: '/events/create',
52-
icon: (
53-
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" strokeWidth="1.5" stroke="currentColor" className="w-6 h-6">
54-
<path strokeLinecap="round" strokeLinejoin="round" d="M12 4.5v15m7.5-7.5h-15" />
55-
</svg>
56-
),
57-
},
58-
]
59-
: [];
46+
const sceventsAdminNavLinks = [];
47+
if (config.SCEvents?.ENABLED) {
48+
sceventsAdminNavLinks.push({
49+
title: 'SCEvents',
50+
route: '/events',
51+
icon: (
52+
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" strokeWidth="1.5" stroke="currentColor" className="w-6 h-6">
53+
<path strokeLinecap="round" strokeLinejoin="round" d="M6.75 3v2.25M17.25 3v2.25M3 18.75V7.5a2.25 2.25 0 0 1 2.25-2.25h13.5A2.25 2.25 0 0 1 21 7.5v11.25m-18 0A2.25 2.25 0 0 0 5.25 21h13.5a2.25 2.25 0 0 0 2.25-2.25m-18 0v-7.5A2.25 2.25 0 0 1 5.25 9h13.5a2.25 2.25 0 0 1 2.25 2.25v7.5" />
54+
</svg>
55+
),
56+
});
57+
if (user?.accessLevel >= membershipState.OFFICER) {
58+
sceventsAdminNavLinks.push({
59+
title: 'Create event',
60+
route: '/events/create',
61+
icon: (
62+
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" strokeWidth="1.5" stroke="currentColor" className="w-6 h-6">
63+
<path strokeLinecap="round" strokeLinejoin="round" d="M12 4.5v15m7.5-7.5h-15" />
64+
</svg>
65+
),
66+
});
67+
}
68+
}
6069

6170
const adminLinks = [
6271
{

src/Pages/Events/CreateEventPage.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
import React, { useMemo, useState } from 'react';
33
import { Link, useHistory } from 'react-router-dom';
44
import { useSCE } from '../../Components/context/SceContext.js';
5-
import { createSCEvent, SCEVENTS_API_URL } from '../../APIFunctions/SCEvents.js';
5+
import { createSCEvent, getSCEventsBaseUrl } from '../../APIFunctions/SCEvents.js';
66
import CreateEventFormQuestionBlock from './CreateEventFormQuestionBlock.js';
7-
const enums = require('../../Enums.js');
7+
import { membershipState } from '../../Enums';
88

99
/** Matches SCEvents `max_attendees` when there is no cap. */
1010
const UNLIMITED_ATTENDEES = -1;
@@ -70,7 +70,6 @@ function toApiRegistrationForm(questions) {
7070
export default function CreateEventPage() {
7171
const { user } = useSCE();
7272
const history = useHistory();
73-
const { membershipState } = enums;
7473

7574
const [eventId] = useState(() => crypto.randomUUID());
7675
const [eventName, setEventName] = useState('');
@@ -196,7 +195,7 @@ export default function CreateEventPage() {
196195
setSubmitting(false);
197196

198197
if (result.error) {
199-
const base = SCEVENTS_API_URL;
198+
const base = getSCEventsBaseUrl();
200199
let msg = '';
201200
const data = result.responseData;
202201
if (data && typeof data === 'object' && data.error) {

0 commit comments

Comments
 (0)