Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use ArmaEvents API for event display #45

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 66 additions & 4 deletions frontend/src/services/events.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { API_URI } from '.';
import { fetchJSON } from './utils';
import { ARMA_EVENTS_GRUPPE_ADLER_ID, ARMA_EVENTS_URL } from '.';

export interface ArmaEvent {
date: Date;
Expand Down Expand Up @@ -28,7 +27,70 @@ export function isInFuture (date: Date): boolean {
}

export async function fetchEvents (): Promise<ArmaEvent[]> {
const events = await fetchJSON<Array<Omit<ArmaEvent, 'date'> & { date: string }>>(`${API_URI}/api/v1/events`);
const query = `
query GetCommunityEventsWithDetails($communityId: UUID!) {
events(
where: {
community: {
id: { eq: $communityId }
}
}
order: [
{ date: DESC } # Sort by date in descending order
]
) {
nodes {
id
title
slug
date
eventUsers {
nodes {
status
}
}
}
}
}
`;

return events.map(e => ({ ...e, date: new Date(e.date) }));
const communityId = ARMA_EVENTS_GRUPPE_ADLER_ID;

const variables = { communityId };
try {
const response = await fetch(ARMA_EVENTS_URL, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ query, variables })
});

if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}

const result = await response.json();

if (result.errors) {
console.error(result.errors);
throw new Error('ArmaEvents GraphQL error occurred');
}

// Transform the data to fit ArmaEvent interface
return result.data.events.nodes.map((event: any): ArmaEvent => {
const attending = event.eventUsers.nodes.filter((user: any) => user.status === 'attending').length;
const notSureYet = event.eventUsers.nodes.filter((user: any) => user.status === 'potentiallyAttending').length;

return {
date: new Date(event.date),
title: event.title,
url: `/events/${event.slug}`, // Construct URL using slug
attendance: [attending, notSureYet]
};
});
} catch (error) {
console.error('Error fetching events:', error);
throw error;
}
}
2 changes: 2 additions & 0 deletions frontend/src/services/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
export const API_URI = process.env.NODE_ENV === 'production' ? '' : 'https://gruppe-adler.de';
export const API_URL = `${API_URI}/api/v1`;
export const ARMA_EVENTS_URL = 'https://api.arma.events/graphql/';
export const ARMA_EVENTS_GRUPPE_ADLER_ID = 'f52e5ce0-928d-45a9-9381-6f93015950ee';
Loading