api requests
This commit is contained in:
parent
fe74490440
commit
5a1cc7c43c
22 changed files with 665 additions and 133 deletions
|
|
@ -51,29 +51,56 @@ export const getAllSurveys = async (): Promise<ISurvey[]> => {
|
|||
* postNewSurvey - добавление нового опроса
|
||||
* @param survey
|
||||
*/
|
||||
export const postNewSurvey = async (survey: INewSurvey): Promise<INewSurvey> => {
|
||||
// export const postNewSurvey = async (survey: INewSurvey): Promise<ISurvey> => {
|
||||
// const token = localStorage.getItem("token");
|
||||
// if (!token) {
|
||||
// throw new Error("Токен отсутствует");
|
||||
// }
|
||||
//
|
||||
// try{
|
||||
// const response = await fetch(`${BASE_URL}/surveys`, {
|
||||
// ...createRequestConfig('POST'),
|
||||
// body: JSON.stringify(survey)
|
||||
// })
|
||||
//
|
||||
// // return await handleResponse(response);
|
||||
//
|
||||
// if (response.status === 200) {
|
||||
// return await handleResponse(response);
|
||||
// }
|
||||
// throw new Error(`Ожидался код 200, получен ${response.status}`);
|
||||
// } catch (error) {
|
||||
// console.error(`Error when adding a new survey: ${error}`);
|
||||
// throw error;
|
||||
// }
|
||||
// }
|
||||
|
||||
export const postNewSurvey = async (survey: INewSurvey): Promise<ISurvey> => {
|
||||
const token = localStorage.getItem("token");
|
||||
if (!token) {
|
||||
throw new Error("Токен отсутствует");
|
||||
}
|
||||
|
||||
try{
|
||||
try {
|
||||
const response = await fetch(`${BASE_URL}/surveys`, {
|
||||
...createRequestConfig('POST'),
|
||||
body: JSON.stringify(survey)
|
||||
})
|
||||
body: JSON.stringify(survey),
|
||||
});
|
||||
|
||||
// return await handleResponse(response);
|
||||
|
||||
if (response.status === 201) {
|
||||
return await handleResponse(response);
|
||||
if (!response.ok) {
|
||||
throw new Error(`Ошибка: ${response.status}`);
|
||||
}
|
||||
throw new Error(`Ожидался код 201, получен ${response.status}`);
|
||||
|
||||
const data = await response.json();
|
||||
if (!data.id) {
|
||||
throw new Error("Сервер не вернул ID опроса");
|
||||
}
|
||||
return data;
|
||||
} catch (error) {
|
||||
console.error(`Error when adding a new survey: ${error}`);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Запрос на получение опроса по заданному ID
|
||||
|
|
@ -95,7 +122,7 @@ export const getSurveyById = async (surveyId: number): Promise<ISurvey> => {
|
|||
* Запрос на удаление опроса
|
||||
* @param surveyId - ID выбранного опроса
|
||||
*/
|
||||
export const deleteSurvey = async (surveyId: string) => {
|
||||
export const deleteSurvey = async (surveyId: number) => {
|
||||
const token = localStorage.getItem("token");
|
||||
if (!token) {
|
||||
throw new Error("Токен отсутствует");
|
||||
|
|
@ -114,4 +141,33 @@ export const deleteSurvey = async (surveyId: string) => {
|
|||
console.error(`Error deleting a survey: ${error}`);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Запрос на изменение опроса целиком
|
||||
* @param surveyId
|
||||
* @param survey
|
||||
*/
|
||||
export const updateSurvey = async (surveyId: number, survey: Partial<INewSurvey>): Promise<ISurvey> => {
|
||||
const token = localStorage.getItem("token");
|
||||
if (!token) {
|
||||
throw new Error('Токен отсутствует');
|
||||
}
|
||||
try{
|
||||
const response = await fetch(`${BASE_URL}/surveys/${surveyId}`, {
|
||||
...createRequestConfig('PUT'),
|
||||
body: JSON.stringify({
|
||||
title: survey.title,
|
||||
description: survey.description,
|
||||
})
|
||||
})
|
||||
if (response.status === 200) {
|
||||
return await handleResponse(response);
|
||||
}
|
||||
throw new Error(`Ожидался код 200, получен ${response.status}`);
|
||||
}
|
||||
catch (error){
|
||||
console.error(`Error updating survey: ${error}`);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue