Generateurv2/frontend/requests/requests.exos.js
2022-06-24 13:42:16 +02:00

211 lines
4.5 KiB
JavaScript

import { exoInstance } from "../apis/exoInstance.instance.js";
import { csvExtParse, isBrowser } from "../utils/utils.js";
export const getExos = async (codes=[]) => {
return await exoInstance
.request({
method: "GET",
url: "exercices/",
params: { id: codes },
headers: {
Authorization:
isBrowser ?
localStorage.getItem("token") !== null
? `Token ${localStorage.getItem("token")}`
: null: null
},
})
.then((res) => res.data.data);
};
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);
};
export const createExo = async (data) => {
return await exoInstance
.request({
url: `exercices/`,
data: data,
method: "POST",
headers: {
Authorization:
localStorage.getItem("token") !== null
? `Token ${localStorage.getItem("token")}`
: null,
},
})
.then((res) => {
return res.data;
})
.catch((err) => {
throw err;
});
//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}
})
}
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}
})
}
export const favExo = async (code) => {
return await exoInstance
.request({
method: "POST",
url: `fav/`,
data: { code: code },
})
.then((res) => res.data.data);
}
export const getExoModelFile = async (code) => {
return await exoInstance
.request({
method: "GET",
url: `model/`,
params: { id_code: code },
})
.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();
});
}