162 lines
3.5 KiB
TypeScript
162 lines
3.5 KiB
TypeScript
import type { Tag } from '..//types/exo.type';
|
|
import { exoInstance } from '../apis/exo.api';
|
|
import { stringify } from 'qs';
|
|
const formDataFromObj = (obj: object) => {
|
|
const form_data = new FormData();
|
|
|
|
for (const key in obj) {
|
|
form_data.append(key, obj[key as keyof typeof obj]);
|
|
}
|
|
return form_data;
|
|
};
|
|
|
|
export const createExo = (data: {
|
|
name: string;
|
|
consigne: string;
|
|
private: boolean;
|
|
file: File;
|
|
}) => {
|
|
return exoInstance({
|
|
url: '/exercices',
|
|
method: 'POST',
|
|
data: formDataFromObj(data),
|
|
headers: {
|
|
'Content-type': 'multipart/form-data'
|
|
}
|
|
});
|
|
};
|
|
|
|
export const delExo = (id_code: string) => {
|
|
return exoInstance({
|
|
url: '/exercice/' + id_code,
|
|
method: 'DELETE'
|
|
});
|
|
};
|
|
|
|
export const editExo = (
|
|
id_code: string,
|
|
data: {
|
|
name: string;
|
|
consigne: string;
|
|
private: boolean;
|
|
file?: File;
|
|
}
|
|
) => {
|
|
return exoInstance({
|
|
url: '/exercice/' + id_code,
|
|
method: 'PUT',
|
|
data: formDataFromObj(data),
|
|
headers: {
|
|
'Content-type': 'multipart/form-data'
|
|
}
|
|
});
|
|
};
|
|
|
|
export const getExos = (
|
|
category: 'public' | 'user',
|
|
data: {
|
|
search?: string;
|
|
type?: 'csv' | 'pdf' | 'web';
|
|
tags?: string[];
|
|
page?: number;
|
|
size?: number;
|
|
}
|
|
) => {
|
|
console.log('SENDINF', data, stringify(data, { arrayFormat: 'brackets' }));
|
|
return exoInstance({
|
|
url: '/exercices/' + category,
|
|
method: 'GET',
|
|
params: data
|
|
})
|
|
.then((r) => r.data)
|
|
.catch(console.log);
|
|
};
|
|
|
|
export const getExo = (id_code: string) => {
|
|
return exoInstance({
|
|
url: '/exercice/' + id_code,
|
|
method: 'GET'
|
|
})
|
|
.then((r) => r.data)
|
|
.catch(console.log);
|
|
};
|
|
|
|
export const addTags = (id_code: string, data: Tag[]) => {
|
|
return exoInstance({
|
|
url: `/exercice/${id_code}/tags`,
|
|
data,
|
|
method: 'POST'
|
|
}).then((r) => r.data);
|
|
};
|
|
export const delTags = (id_code: string, tag_id: string) => {
|
|
return exoInstance({
|
|
url: `/exercice/${id_code}/tags/${tag_id}`,
|
|
method: 'DELETE'
|
|
}).then((r) => r.data);
|
|
};
|
|
|
|
export const getTags = () => {
|
|
return exoInstance({
|
|
url: `/tags`,
|
|
method: 'Get'
|
|
}).then((r) => r.data).catch(console.log);
|
|
};
|
|
|
|
export const cloneExo = (id_code: string) => {
|
|
return exoInstance({
|
|
url: `/clone/${id_code}`,
|
|
method: 'POST'
|
|
}).then((r) => r.data);
|
|
};
|
|
|
|
export const getExoSource = (id_code: string,) => {
|
|
return exoInstance({
|
|
url: `/exercice/${id_code}/exo_source`,
|
|
method: 'Get'
|
|
}).then((r) => {
|
|
const contentDisposition = r.headers['content-disposition'] || "filename=untitled.py";
|
|
const splitted = contentDisposition.split('filename=')
|
|
let filename = "untitled.py"
|
|
if(splitted.length >= 1) {
|
|
filename = splitted[1]
|
|
}
|
|
const blob = new Blob([r.data], {
|
|
type: 'text/x-python'
|
|
});
|
|
const downloadUrl = window.URL.createObjectURL(blob);
|
|
const link = document.createElement('a');
|
|
link.href = downloadUrl;
|
|
link.setAttribute('download', filename);
|
|
document.body.appendChild(link);
|
|
link.click();
|
|
link.remove();
|
|
});
|
|
};
|
|
|
|
export const generateRequest = (id_code: string,filename: string) => {
|
|
return exoInstance({
|
|
url: `generator/csv/${id_code}`,
|
|
method: 'Get',
|
|
params: {filename}
|
|
}).then((r) => {
|
|
const contentDisposition = r.headers['content-disposition'] || "filename=untitled.csv";
|
|
const splitted = contentDisposition.split('filename=')
|
|
let filename = "untitled.csv"
|
|
if(splitted.length >= 1) {
|
|
filename = splitted[1]
|
|
}
|
|
const blob = new Blob([r.data], {
|
|
type: 'text/csv'
|
|
});
|
|
const downloadUrl = window.URL.createObjectURL(blob);
|
|
const link = document.createElement('a');
|
|
link.href = downloadUrl;
|
|
link.setAttribute('download', filename);
|
|
document.body.appendChild(link);
|
|
link.click();
|
|
link.remove();
|
|
});
|
|
};
|
|
|
|
|