Skip to content

Commit

Permalink
move function in utils (#32)
Browse files Browse the repository at this point in the history
  • Loading branch information
maximallain authored Nov 9, 2023
1 parent 16a8cb3 commit 08b4db2
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/api/cosiaApi.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import axios from "axios";
import applyCaseMiddleware from "axios-case-converter";
import { getCookie } from "../utils";

const cosiaApiAxiosInstance = applyCaseMiddleware(
axios.create({
Expand Down Expand Up @@ -58,5 +59,13 @@ type DepartmentDataDownload = DepartementDataDownloadPayload;
export const createDepartementDataDownload = (
payload: DepartementDataDownloadPayload,
): Promise<{ data: DepartmentDataDownload }> => {
return cosiaApiAxiosInstance.post("department-data-downloads/", payload);
const csrftoken = getCookie("csrftoken");
const config = {
headers: {
"content-type": "application/json",
"X-CSRFToken": csrftoken,
},
};

return cosiaApiAxiosInstance.post("department-data-downloads/", payload, config);
};
13 changes: 13 additions & 0 deletions src/utils.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
//TODO to test

export const isCorrectEmail = (email: string) => /^[\w\-.]+@[\w-]+\.[\w-]{2,}$/.test(email);

export const getCookie = (name: string) => {
if (!document.cookie || document.cookie === "") return;

const cookies = document.cookie.split(";");
for (let i = 0; i < cookies.length; i++) {
const cookie = cookies[i].trim();
// Does this cookie string begin with the name we want?
if (cookie.substring(0, name.length + 1) === name + "=") {
return decodeURIComponent(cookie.substring(name.length + 1));
}
}
};

0 comments on commit 08b4db2

Please sign in to comment.