85 lines
1.9 KiB
TypeScript
85 lines
1.9 KiB
TypeScript
import axios from "axios";
|
|
import { getAuthHeader } from "@utils/auth";
|
|
import { Tag } from "./Tag";
|
|
import qs from "query-string";
|
|
import { SignupForm } from "./SignupForm";
|
|
const url = `${process.env.API_URL}/events/`;
|
|
|
|
export interface Event {
|
|
id: number;
|
|
title_fi: string;
|
|
title_en: string;
|
|
description_fi: string;
|
|
description_en: string;
|
|
content_fi: string;
|
|
content_en: string;
|
|
start_time: string;
|
|
end_time: string;
|
|
tags: Tag[];
|
|
tag_id?: number[];
|
|
visible: boolean;
|
|
signup_id: number[];
|
|
signupForm: SignupForm[];
|
|
}
|
|
|
|
export async function getEvents(options: any = {}): Promise<Event[]> {
|
|
const { onlyNonPast, limit, auth } = options;
|
|
try {
|
|
const params = {
|
|
since: onlyNonPast ? (new Date()).toISOString() : undefined,
|
|
limit,
|
|
};
|
|
const search = qs.stringify(params);
|
|
const headers = auth ? { "Authorization": getAuthHeader() } : null;
|
|
const resp = await axios.get(`${url}?${search}`, {
|
|
headers
|
|
});
|
|
return resp.data["results"];
|
|
} catch (err) {
|
|
console.error(err);
|
|
throw err;
|
|
}
|
|
}
|
|
|
|
export async function getEvent(id: number, auth = false): Promise<Event> {
|
|
try {
|
|
const headers = auth ? { "Authorization": getAuthHeader() } : null;
|
|
const resp = await axios.get(`${url}${id}/`, {
|
|
headers
|
|
});
|
|
return resp.data;
|
|
} catch (err) {
|
|
console.error(err);
|
|
throw err;
|
|
}
|
|
}
|
|
|
|
export async function createEvent(data): Promise<Event> {
|
|
try {
|
|
const resp = await axios.post(url, data, {
|
|
headers: {
|
|
"Authorization": getAuthHeader(),
|
|
},
|
|
});
|
|
return resp.data;
|
|
} catch (err) {
|
|
console.error(err);
|
|
throw err;
|
|
}
|
|
}
|
|
|
|
export async function updateEvent(data): Promise<Event> {
|
|
try {
|
|
const putUrl = `${url}${data.id}/`;
|
|
const resp = await axios.put(putUrl, data, {
|
|
headers: {
|
|
"Authorization": getAuthHeader(),
|
|
},
|
|
});
|
|
return resp.data;
|
|
} catch (err) {
|
|
console.error(err);
|
|
throw err;
|
|
}
|
|
}
|