AnswerApi and CompletionApi

This commit is contained in:
Tatiana Nikolaeva 2025-06-02 13:08:23 +05:00
parent 2d129c0493
commit 88dcb63232
9 changed files with 195 additions and 102 deletions

View file

@ -0,0 +1,60 @@
import {BASE_URL, createRequestConfig, handleResponse} from "./BaseApi.ts";
export interface IAnswer{
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) {
console.error(`Error when receiving all selected responses: ${error}`);
throw error;
}
}
export const addNewCompletion = async (surveyId: number, answer: IAnswer) => {
try{
const response = await fetch(`${BASE_URL}/surveys/${surveyId}/completions`, {
...createRequestConfig('POST'),
body: JSON.stringify({
questionId: answer.questionId,
answerText: answer.answerText,
})
})
if (!response.ok) {
throw new Error(`Ошибка: ${response.status}`);
}
return await handleResponse(response)
}
catch (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) {
console.error(`Error when receiving a completed survey by id: ${error}`);
throw error;
}
}