72 lines
1.5 KiB
TypeScript
72 lines
1.5 KiB
TypeScript
import axios from "axios";
|
|
import { getAuthHeader } from "../auth";
|
|
import * as qs from "query-string";
|
|
const url = `${process.env.API_URL}/events/`;
|
|
|
|
export interface Event {
|
|
id: number;
|
|
title: string;
|
|
description: string;
|
|
content: string;
|
|
start_time: string;
|
|
end_time: string;
|
|
tags: number[];
|
|
tag_id: number[];
|
|
visible: boolean;
|
|
}
|
|
|
|
export async function getEvents(options: any = {}): Promise<Event[]> {
|
|
const { onlyNonPast, limit } = options;
|
|
try {
|
|
const params = {
|
|
since: onlyNonPast ? (new Date()).toISOString() : undefined,
|
|
limit,
|
|
};
|
|
const search = qs.stringify(params);
|
|
const resp = await axios.get(`${url}?${search}`);
|
|
return resp.data["results"];
|
|
} catch (err) {
|
|
console.error(err);
|
|
throw err;
|
|
}
|
|
}
|
|
|
|
export async function getEvent(id: number): Promise<Event> {
|
|
try {
|
|
const resp = await axios.get(`${url}${id}/`);
|
|
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;
|
|
}
|
|
}
|