80 lines
1.7 KiB
TypeScript
80 lines
1.7 KiB
TypeScript
import axios from "axios";
|
|
import { getAuthHeader } from "@utils/auth";
|
|
const url = `${process.env.API_URL}/signupForm/`;
|
|
import { Question } from "@components/SignupQuestionsWidget";
|
|
|
|
export interface SignupForm {
|
|
id?: number;
|
|
title_fi: string;
|
|
title_en: string;
|
|
visible: boolean;
|
|
start_time: string;
|
|
end_time: string;
|
|
questions: Question[];
|
|
signups: string[];
|
|
quota: number;
|
|
schema: {
|
|
title?: string;
|
|
type: string;
|
|
required: string[];
|
|
properties: any;
|
|
minProperties?: number;
|
|
};
|
|
}
|
|
|
|
export async function getForms(auth = false): Promise<SignupForm[]> {
|
|
try {
|
|
const headers = auth ? { "Authorization": getAuthHeader() } : null;
|
|
const resp = await axios.get(url, {
|
|
headers
|
|
});
|
|
const { results } = resp.data;
|
|
return results;
|
|
} catch (err) {
|
|
console.error(err);
|
|
throw err;
|
|
}
|
|
}
|
|
|
|
export async function getForm(id: number, auth = false): Promise<SignupForm> {
|
|
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 createForm(data): Promise<SignupForm> {
|
|
try {
|
|
const resp = await axios.post(url, data, {
|
|
headers: {
|
|
"Authorization": getAuthHeader(),
|
|
},
|
|
});
|
|
return resp.data;
|
|
} catch (err) {
|
|
console.error(err);
|
|
throw err;
|
|
}
|
|
}
|
|
|
|
export async function updateForm(data): Promise<SignupForm> {
|
|
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;
|
|
}
|
|
}
|