62 lines
No EOL
1.9 KiB
TypeScript
62 lines
No EOL
1.9 KiB
TypeScript
import {BASE_URL, createRequestConfig, handleResponse, handleUnauthorizedError} from "./BaseApi.ts";
|
|
export interface ICompletionRequest {
|
|
answers: Array<{
|
|
questionId: number;
|
|
answerText: string;
|
|
}>;
|
|
}
|
|
|
|
export const getAllCompletions = async (surveyId: number) => {
|
|
const token = localStorage.getItem("token");
|
|
if (!token) {
|
|
throw new Error('Токен отсутствует');
|
|
}
|
|
try{
|
|
const response = await fetch(`${BASE_URL}/surveys/${surveyId}/completions`, {
|
|
...createRequestConfig('GET'),
|
|
})
|
|
return await handleResponse(response);
|
|
}
|
|
catch (error) {
|
|
handleUnauthorizedError(error);
|
|
console.error(`Error when receiving all selected responses: ${error}`);
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
export const addNewCompletion = async (surveyId: number, data: ICompletionRequest) => {
|
|
try{
|
|
const response = await fetch(`${BASE_URL}/surveys/${surveyId}/completions`, {
|
|
...createRequestConfig('POST'),
|
|
body: JSON.stringify(data)
|
|
})
|
|
if (!response.ok) {
|
|
throw new Error(`Ошибка: ${response.status}`);
|
|
}
|
|
return await handleResponse(response)
|
|
}
|
|
catch (error) {
|
|
handleUnauthorizedError(error);
|
|
console.error(`Error when adding a new survey passage: ${error}`);
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
export const getCompletionById = async (id: number) => {
|
|
const token = localStorage.getItem("token");
|
|
if (!token) {
|
|
throw new Error('Токен отсутствует');
|
|
}
|
|
|
|
try{
|
|
const response = await fetch(`${BASE_URL}/completions/${id}`, {
|
|
...createRequestConfig('GET'),
|
|
})
|
|
return await handleResponse(response);
|
|
}
|
|
catch (error) {
|
|
handleUnauthorizedError(error);
|
|
console.error(`Error when receiving a completed survey by id: ${error}`);
|
|
throw error;
|
|
}
|
|
} |