From 45dcfba95b1bead49c4b9089f6699785c8119739 Mon Sep 17 00:00:00 2001 From: sosokker Date: Sat, 11 Nov 2023 23:18:20 +0700 Subject: [PATCH] Add funtion fetch tasks by ID and all tags --- frontend/src/api/AuthenticationApi.jsx | 92 ++++++++++++-------------- frontend/src/api/TagApi.jsx | 12 ++++ frontend/src/api/TaskApi.jsx | 32 ++++++--- 3 files changed, 77 insertions(+), 59 deletions(-) create mode 100644 frontend/src/api/TagApi.jsx diff --git a/frontend/src/api/AuthenticationApi.jsx b/frontend/src/api/AuthenticationApi.jsx index dc2de0c..d4b6486 100644 --- a/frontend/src/api/AuthenticationApi.jsx +++ b/frontend/src/api/AuthenticationApi.jsx @@ -1,61 +1,59 @@ -import axios from 'axios'; -import axiosInstance from './configs/AxiosConfig'; +import axios from "axios"; +import axiosInstance from "./configs/AxiosConfig"; // Function for user login -const apiUserLogin = (data) => { - return axiosInstance - .post('token/obtain/', data) - .then((response) => { - console.log(response.statusText); - return response; - }).catch(error => { - console.log('apiUserLogin error: ', error); - return error; - }); +const apiUserLogin = data => { + return axiosInstance + .post("token/obtain/", data) + .then(response => { + console.log(response.statusText); + return response; + }) + .catch(error => { + console.log("apiUserLogin error: ", error); + return error; + }); }; // Function for user logout const apiUserLogout = () => { - axiosInstance.defaults.headers['Authorization'] = ""; // Clear authorization header - localStorage.removeItem('access_token'); // Remove access token - localStorage.removeItem('refresh_token'); // Remove refresh token -} - -// Function for Google login -const googleLogin = async (token) => { - axios.defaults.withCredentials = true - let res = await axios.post( - "http://localhost:8000/api/auth/google/", - { - code: token, - } - ); - // console.log('service google login res: ', res); - return await res; + axiosInstance.defaults.headers["Authorization"] = ""; // Clear authorization header + localStorage.removeItem("access_token"); // Remove access token + localStorage.removeItem("refresh_token"); // Remove refresh token }; +// Function for Google login +const googleLogin = async token => { + axios.defaults.withCredentials = true; + let res = await axios.post("http://localhost:8000/api/auth/google/", { + code: token, + }); + // console.log('service google login res: ', res); + return await res; +}; // Function to get 'hello' data const getGreeting = () => { - return axiosInstance - .get('hello') - .then((response) => { - return response; - }).catch(error => { - return error; - }); -} + return axiosInstance + .get("hello") + .then(response => { + return response; + }) + .catch(error => { + return error; + }); +}; const config = { - headers: { - "Content-Type": "application/json" - } + headers: { + "Content-Type": "application/json", + }, }; // Function to register -const createUser = async (formData) => { +const createUser = async formData => { try { - axios.defaults.withCredentials = true + axios.defaults.withCredentials = true; const resposne = axios.post("http://localhost:8000/api/user/create/", formData); // const response = await axiosInstance.post('/user/create/', formData); return response.data; @@ -64,13 +62,11 @@ const createUser = async (formData) => { } }; - // Export the functions and Axios instance export default { - axiosInstance, - apiUserLogin, - apiUserLogout, - getGreeting: getGreeting, - googleLogin, - createUser + apiUserLogin, + apiUserLogout, + getGreeting: getGreeting, + googleLogin, + createUser, }; diff --git a/frontend/src/api/TagApi.jsx b/frontend/src/api/TagApi.jsx new file mode 100644 index 0000000..deea971 --- /dev/null +++ b/frontend/src/api/TagApi.jsx @@ -0,0 +1,12 @@ +import axiosInstance from "./configs/AxiosConfig"; + +export const fetchTags = () => { + return axiosInstance + .get("tags/") + .then(response => { + return response.data; + }) + .catch(error => { + throw error; + }); +}; diff --git a/frontend/src/api/TaskApi.jsx b/frontend/src/api/TaskApi.jsx index f4c3ef2..96f24f6 100644 --- a/frontend/src/api/TaskApi.jsx +++ b/frontend/src/api/TaskApi.jsx @@ -1,13 +1,23 @@ -import axios from 'axios'; -import axiosInstance from './configs/AxiosConfig'; +import axiosInstance from "./configs/AxiosConfig"; export const fetchTodoTasks = () => { - return axiosInstance - .get('todo/') - .then((response) => { - return response.data; - }) - .catch(error => { - throw error; - }); -}; \ No newline at end of file + return axiosInstance + .get("todo/") + .then(response => { + return response.data; + }) + .catch(error => { + throw error; + }); +}; + +export const fetchTodoTasksID = id => { + return axiosInstance + .get(`todo/${id}/`) + .then(response => { + return response.data; + }) + .catch(error => { + throw error; + }); +};