AnswerApi and CompletionApi
This commit is contained in:
parent
2d129c0493
commit
88dcb63232
9 changed files with 195 additions and 102 deletions
60
SurveyFrontend/src/api/CompletionApi.ts
Normal file
60
SurveyFrontend/src/api/CompletionApi.ts
Normal 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;
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue