Generateurv2/frontend/requests/requests.exos.js

211 lines
4.5 KiB
JavaScript
Raw Normal View History

2022-05-18 10:15:54 +02:00
import { exoInstance } from "../apis/exoInstance.instance.js";
2022-06-11 23:39:03 +02:00
import { csvExtParse, isBrowser } from "../utils/utils.js";
2022-05-18 10:15:54 +02:00
2022-06-24 13:42:16 +02:00
export const getExos = async (codes=[]) => {
2022-06-11 23:39:03 +02:00
return await exoInstance
.request({
method: "GET",
url: "exercices/",
2022-06-24 13:42:16 +02:00
params: { id: codes },
2022-06-11 23:39:03 +02:00
headers: {
Authorization:
isBrowser ?
localStorage.getItem("token") !== null
? `Token ${localStorage.getItem("token")}`
: null: null
},
})
.then((res) => res.data.data);
2022-05-18 10:15:54 +02:00
};
2022-06-11 23:39:03 +02:00
export const getPublicExos = async (code='all', page=1, filter) => {
return await exoInstance
.request({
method: "GET",
url: "exercices/public",
params: { code: code, p : page == 0 ? 1: page, ...filter },
headers: {
Authorization:
isBrowser ?
localStorage.getItem("token") !== null
? `Token ${localStorage.getItem("token")}`
: null: null
},
})
.then((res) => res.data);
};
export const getUserExos = async (code='all', page=1, filters) => {
return await exoInstance
.request({
method: "GET",
url: "exercices/user",
params: { code: code, p : page == 0 ? 1: page, ...filters },
headers: {
Authorization:
isBrowser ?
localStorage.getItem("token") !== null
? `Token ${localStorage.getItem("token")}`
: null: null
},
})
.then((res) => res.data);
};
2022-05-18 10:15:54 +02:00
export const createExo = async (data) => {
2022-06-11 23:39:03 +02:00
return await exoInstance
.request({
url: `exercices/`,
data: data,
method: "POST",
headers: {
Authorization:
localStorage.getItem("token") !== null
? `Token ${localStorage.getItem("token")}`
: null,
},
})
2022-05-18 10:15:54 +02:00
.then((res) => {
2022-06-11 23:39:03 +02:00
return res.data;
2022-05-18 10:15:54 +02:00
})
.catch((err) => {
2022-06-11 23:39:03 +02:00
throw err;
2022-05-18 10:15:54 +02:00
});
//return response.data.data;
};
export const delExo = async (id_code) => {
return await exoInstance.request({
url: `exercices/`,
data: { 'id_code': id_code},
method: "DELETE",
})
.then((res) => res.data)
.catch((err) => {throw err});
}
export const editExo = async (data) => {
return await exoInstance.request({
url: `exercices/`,
data: data,
method: "PUT",
})
.then((res) => res.data)
.catch((err) => {
throw err
});
}
export const getTags = async () => {
const response = await exoInstance.request({
method: "GET",
url: "/tags/",
});
return await exoInstance.request({
method: "GET",
url: "/tags/",
}).then(res=>res.data.data);
}
export const setTags = async (data) => {
return await exoInstance.request({
method: 'POST',
url: '/tags/',
data: {...data}
})
}
export const delTag = async (data) => {
return await exoInstance.request({
method: 'DELETE',
url: '/tags/',
data: {...data}
2022-06-11 23:39:03 +02:00
})
2022-05-18 10:15:54 +02:00
}
export const editorOut = async (data) => {
return await exoInstance
.request({
method: "post",
url: "editor/",
data: { code: data },
})
.then((res) => res.data);
};
export const csvGen = async (code, name) => {
return await exoInstance
.request({
method: "get",
url: `csv/${code}`,
})
.then((res) => {
let blob = new Blob([res.data], {
type: "text/csv",
});
const downloadUrl = window.URL.createObjectURL(blob);
const link = document.createElement("a");
link.href = downloadUrl;
link.setAttribute("download", csvExtParse(name));
document.body.appendChild(link);
link.click();
link.remove();
});
};
export const pdfGen = async (data) => {
return await exoInstance
.request({
method: "post",
url: `pdf/`,
data: {...data}
})
}
2022-06-11 23:39:03 +02:00
export const favExo = async (code) => {
2022-05-18 10:15:54 +02:00
return await exoInstance
.request({
2022-06-11 23:39:03 +02:00
method: "POST",
url: `fav/`,
data: { code: code },
})
.then((res) => res.data.data);
}
export const getExoModelFile = async (code) => {
return await exoInstance
.request({
method: "GET",
2022-05-18 10:15:54 +02:00
url: `model/`,
2022-06-11 23:39:03 +02:00
params: { id_code: code },
2022-05-18 10:15:54 +02:00
})
2022-06-11 23:39:03 +02:00
.then((res) => {
var name = res.headers['content-disposition'].split('filename=')[1]
let blob = new Blob([res.data], {
type: "text/x-python",
});
const downloadUrl = window.URL.createObjectURL(blob);
const link = document.createElement("a");
link.href = downloadUrl;
link.setAttribute("download", name);
document.body.appendChild(link);
link.click();
link.remove();
});
2022-05-18 10:15:54 +02:00
}
2022-06-11 23:39:03 +02:00