fix login check and urls
This commit is contained in:
@@ -1,129 +1,133 @@
|
||||
import axios from "axios";
|
||||
import FormData from 'form-data'
|
||||
import {useAuthStore} from "@/store/authStore";
|
||||
import FormData from "form-data";
|
||||
import { useAuthStore } from "@/store/authStore";
|
||||
|
||||
const API_URL = "https://testowe.zikor.pl/api/v1";
|
||||
const API_URL = "https://hopp.zikor.pl/api/v1";
|
||||
|
||||
//const API_URL = "http://10.0.2.2:8080/api/v1";
|
||||
|
||||
export async function listNotices() {
|
||||
const { token } = useAuthStore.getState();
|
||||
const headers = token ? { 'Authorization': `Bearer ${token}` } : {};
|
||||
const { token } = useAuthStore.getState();
|
||||
const headers = token ? { Authorization: `Bearer ${token}` } : {};
|
||||
|
||||
console.log(token);
|
||||
console.log(token);
|
||||
|
||||
const response = await fetch(`${API_URL}/notices/get/all`, {
|
||||
headers: headers
|
||||
});
|
||||
console.log(response);
|
||||
const data = await response.json();
|
||||
if (!response.ok) {
|
||||
throw new Error(response.toString());
|
||||
}
|
||||
return data;
|
||||
const response = await fetch(`${API_URL}/notices/get/all`, {
|
||||
headers: headers,
|
||||
});
|
||||
// console.log(response);r
|
||||
const data = await response.json();
|
||||
if (!response.ok) {
|
||||
throw new Error(response.toString());
|
||||
}
|
||||
return data;
|
||||
}
|
||||
|
||||
export async function getNoticeById(noticeId) {
|
||||
const response = await fetch(`${API_URL}/notices/get/${noticeId}`);
|
||||
const response = await fetch(`${API_URL}/notices/get/${noticeId}`);
|
||||
|
||||
const data = await response.json();
|
||||
if (!response.ok) {
|
||||
throw new Error("Error");
|
||||
}
|
||||
return data;
|
||||
const data = await response.json();
|
||||
if (!response.ok) {
|
||||
throw new Error("Error");
|
||||
}
|
||||
return data;
|
||||
}
|
||||
|
||||
export async function createNotice(notice) {
|
||||
try {
|
||||
const response = await axios.post(`${API_URL}/notices/add`, notice, {
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
});
|
||||
try {
|
||||
const response = await axios.post(`${API_URL}/notices/add`, notice, {
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
});
|
||||
|
||||
if (response.data.noticeId !== null) {
|
||||
for (const imageUri of notice.image) {
|
||||
await uploadImage(response.data.noticeId, imageUri);
|
||||
}
|
||||
}
|
||||
|
||||
return response.data;
|
||||
} catch (error) {
|
||||
console.log("Error", error.response.data, error.response.status);
|
||||
return null;
|
||||
if (response.data.noticeId !== null) {
|
||||
for (const imageUri of notice.image) {
|
||||
await uploadImage(response.data.noticeId, imageUri);
|
||||
}
|
||||
}
|
||||
|
||||
return response.data;
|
||||
} catch (error) {
|
||||
console.log("Error", error.response.data, error.response.status);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
export async function getImageByNoticeId(noticeId) {
|
||||
let imageUrl;
|
||||
try {
|
||||
const listResponse = await axios.get(`${API_URL}/images/list/${noticeId}`);
|
||||
let imageUrl;
|
||||
try {
|
||||
const listResponse = await axios.get(`${API_URL}/images/list/${noticeId}`);
|
||||
|
||||
const imageName = listResponse.data[0];
|
||||
imageUrl = `${API_URL}/images/get/${imageName}`;
|
||||
const imageName = listResponse.data[0];
|
||||
imageUrl = `${API_URL}/images/get/${imageName}`;
|
||||
|
||||
console.log(`Pobrano zdjęcie o nazwie: ${imageName}`);
|
||||
console.log(`Pobrano zdjęcie o nazwie: ${imageName}`);
|
||||
|
||||
return imageUrl;
|
||||
} catch (err) {
|
||||
console.log(`Zdjęcie nie istnieje dla notice o id: ${noticeId}`);
|
||||
imageUrl = "https://http.cat/404.jpg";
|
||||
return imageUrl;
|
||||
}
|
||||
return imageUrl;
|
||||
} catch (err) {
|
||||
console.log(`Zdjęcie nie istnieje dla notice o id: ${noticeId}`);
|
||||
imageUrl = "https://http.cat/404.jpg";
|
||||
return imageUrl;
|
||||
}
|
||||
}
|
||||
|
||||
export async function getAllImagesByNoticeId(noticeId) {
|
||||
try {
|
||||
const listResponse = await axios.get(`${API_URL}/images/list/${noticeId}`);
|
||||
try {
|
||||
const listResponse = await axios.get(`${API_URL}/images/list/${noticeId}`);
|
||||
|
||||
if (listResponse.data && listResponse.data.length > 0) {
|
||||
const imageUrls = listResponse.data.map(imageName =>
|
||||
`${API_URL}/images/get/${imageName}`
|
||||
);
|
||||
if (listResponse.data && listResponse.data.length > 0) {
|
||||
const imageUrls = listResponse.data.map(
|
||||
(imageName) => `${API_URL}/images/get/${imageName}`
|
||||
);
|
||||
|
||||
// console.log(`Pobrano ${imageUrls.length} zdjęć dla ogłoszenia o id: ${noticeId}`);
|
||||
return imageUrls;
|
||||
}
|
||||
|
||||
// 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"];
|
||||
// console.log(`Pobrano ${imageUrls.length} zdjęć dla ogłoszenia o id: ${noticeId}`);
|
||||
return imageUrls;
|
||||
}
|
||||
|
||||
// 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"];
|
||||
}
|
||||
}
|
||||
|
||||
export const uploadImage = async (noticeId, imageUri) => {
|
||||
console.log(imageUri);
|
||||
console.log(imageUri);
|
||||
|
||||
const formData = new FormData();
|
||||
const formData = new FormData();
|
||||
|
||||
const filename = imageUri.split('/').pop();
|
||||
const filename = imageUri.split("/").pop();
|
||||
|
||||
const match = /\.(\w+)$/.exec(filename);
|
||||
const type = match ? `image/${match[1]}` : 'image/jpeg';
|
||||
const match = /\.(\w+)$/.exec(filename);
|
||||
const type = match ? `image/${match[1]}` : "image/jpeg";
|
||||
|
||||
formData.append('file', {
|
||||
uri: imageUri,
|
||||
name: filename,
|
||||
type: type,
|
||||
});
|
||||
formData.append("file", {
|
||||
uri: imageUri,
|
||||
name: filename,
|
||||
type: type,
|
||||
});
|
||||
|
||||
try {
|
||||
const response = await axios.post(
|
||||
`${API_URL}/images/upload/${noticeId}`,
|
||||
formData,
|
||||
{
|
||||
headers: {
|
||||
'Content-Type': 'multipart/form-data',
|
||||
},
|
||||
}
|
||||
);
|
||||
console.info('Upload successful:', response.data);
|
||||
return response.data;
|
||||
} catch (error) {
|
||||
console.log("imageURI:", imageUri);
|
||||
console.error('Error uploading image:', error.response.data, error.response.status);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
try {
|
||||
const response = await axios.post(
|
||||
`${API_URL}/images/upload/${noticeId}`,
|
||||
formData,
|
||||
{
|
||||
headers: {
|
||||
"Content-Type": "multipart/form-data",
|
||||
},
|
||||
}
|
||||
);
|
||||
console.info("Upload successful:", response.data);
|
||||
return response.data;
|
||||
} catch (error) {
|
||||
console.log("imageURI:", imageUri);
|
||||
console.error(
|
||||
"Error uploading image:",
|
||||
error.response.data,
|
||||
error.response.status
|
||||
);
|
||||
throw error;
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user