refactor: clean up API_URL definition and improve error handling in authStore
This commit is contained in:
@@ -2,15 +2,16 @@ import axios from "axios";
|
|||||||
import FormData from "form-data";
|
import FormData from "form-data";
|
||||||
import { useAuthStore } from "@/store/authStore";
|
import { useAuthStore } from "@/store/authStore";
|
||||||
|
|
||||||
const API_URL = "https://hopp.zikor.pl/api/v1";
|
// const API_URL = "https://testowe.zikor.pl/api/v1";
|
||||||
|
|
||||||
|
const API_URL = "https://hopp.zikor.pl/api/v1";
|
||||||
|
|
||||||
export async function listNotices() {
|
export async function listNotices() {
|
||||||
const { token } = useAuthStore.getState();
|
const { token } = useAuthStore.getState();
|
||||||
const headers = token ? { Authorization: `Bearer ${token}` } : {};
|
const headers = token ? { Authorization: `Bearer ${token}` } : {};
|
||||||
|
|
||||||
const response = await fetch(`${API_URL}/notices/get/all`, {
|
const response = await fetch(`${API_URL}/notices/get/all`, {
|
||||||
headers: headers
|
headers: headers,
|
||||||
});
|
});
|
||||||
const data = await response.json();
|
const data = await response.json();
|
||||||
if (!response.ok) {
|
if (!response.ok) {
|
||||||
@@ -71,25 +72,21 @@ export async function getAllImagesByNoticeId(noticeId) {
|
|||||||
const listResponse = await axios.get(`${API_URL}/images/list/${noticeId}`);
|
const listResponse = await axios.get(`${API_URL}/images/list/${noticeId}`);
|
||||||
|
|
||||||
if (listResponse.data && listResponse.data.length > 0) {
|
if (listResponse.data && listResponse.data.length > 0) {
|
||||||
return listResponse.data.map(imageName =>
|
return listResponse.data.map(
|
||||||
`${API_URL}/images/get/${imageName}`
|
(imageName) => `${API_URL}/images/get/${imageName}`
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
return ["https://http.cat/404.jpg"];
|
return ["https://http.cat/404.jpg"];
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
if(err.response.status === 404) {
|
if (err.response.status === 404) {
|
||||||
console.info(`Ogłoszenie o id: ${noticeId} nie posiada zdjęć.`);
|
// console.info(`Ogłoszenie o id: ${noticeId} nie posiada zdjęć.`);
|
||||||
return ["https://http.cat/404.jpg"];
|
return ["https://http.cat/404.jpg"];
|
||||||
}
|
}
|
||||||
console.warn(`Nie udało się pobrać listy zdjęć dla ogłoszenia o id: ${noticeId}`, err);
|
console.warn(
|
||||||
return ["https://http.cat/404.jpg"];
|
`Nie udało się pobrać listy zdjęć dla ogłoszenia o id: ${noticeId}`,
|
||||||
}
|
err
|
||||||
|
);
|
||||||
// console.log(`Brak zdjęć dla ogłoszenia o id: ${noticeId}`);
|
|
||||||
return ["https://http.cat/404.jpg"];
|
|
||||||
} catch (err) {
|
|
||||||
// console.log(`Błąd podczas pobierania listy zdjęć dla ogłoszenia o id: ${noticeId}`, err);
|
|
||||||
return ["https://http.cat/404.jpg"];
|
return ["https://http.cat/404.jpg"];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,7 +7,25 @@ const API_URL = "https://hopp.zikor.pl/api/v1";
|
|||||||
|
|
||||||
export const useAuthStore = create(
|
export const useAuthStore = create(
|
||||||
persist(
|
persist(
|
||||||
(set) => ({
|
(set) => {
|
||||||
|
// Dodaj interceptor tylko raz
|
||||||
|
if (!axios.interceptors.response.handlers.length) {
|
||||||
|
axios.interceptors.response.use(
|
||||||
|
(response) => response,
|
||||||
|
(error) => {
|
||||||
|
if (
|
||||||
|
(error.response && error.response.status === 401) ||
|
||||||
|
error.response.status === 403
|
||||||
|
) {
|
||||||
|
set({ user_id: null, token: null, isLoading: false });
|
||||||
|
delete axios.defaults.headers.common["Authorization"];
|
||||||
|
}
|
||||||
|
return Promise.reject(error);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
user_id: null,
|
user_id: null,
|
||||||
token: null,
|
token: null,
|
||||||
isLoading: false,
|
isLoading: false,
|
||||||
@@ -113,7 +131,8 @@ export const useAuthStore = create(
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
}),
|
};
|
||||||
|
},
|
||||||
{
|
{
|
||||||
name: "auth-storage",
|
name: "auth-storage",
|
||||||
storage: createJSONStorage(() => AsyncStorage),
|
storage: createJSONStorage(() => AsyncStorage),
|
||||||
|
|||||||
Reference in New Issue
Block a user