add edit notice and clean code
This commit is contained in:
@@ -40,12 +40,9 @@ export async function createNotice(notice) {
|
||||
|
||||
if (response.data.noticeId !== null) {
|
||||
for (const image of notice.image) {
|
||||
console.log("image", image);
|
||||
if(notice.image.indexOf(image) === 0) {
|
||||
console.log("Image is first:", image);
|
||||
if (notice.image.indexOf(image) === 0) {
|
||||
await uploadImage(response.data.noticeId, image, true);
|
||||
}
|
||||
console.log("image", image);
|
||||
await uploadImage(response.data.noticeId, image, false);
|
||||
}
|
||||
}
|
||||
@@ -67,7 +64,6 @@ export async function getImageByNoticeId(noticeId) {
|
||||
|
||||
return imageUrl;
|
||||
} catch (err) {
|
||||
console.log(`Zdjęcie nie istnieje dla notice o id: ${noticeId}`);
|
||||
imageUrl = "https://http.cat/404.jpg";
|
||||
return imageUrl;
|
||||
}
|
||||
@@ -77,7 +73,9 @@ export async function getAllImagesByNoticeId(noticeId) {
|
||||
const { token } = useAuthStore.getState();
|
||||
const headers = token ? { Authorization: `Bearer ${token}` } : {};
|
||||
try {
|
||||
const listResponse = await axios.get(`${API_URL}/images/list/${noticeId}`, {headers: headers});
|
||||
const listResponse = await axios.get(`${API_URL}/images/list/${noticeId}`, {
|
||||
headers: headers,
|
||||
});
|
||||
|
||||
if (listResponse.data && listResponse.data.length > 0) {
|
||||
return listResponse.data.map((imageName) => ({
|
||||
@@ -104,7 +102,7 @@ export const uploadImage = async (noticeId, imageObj, isFirst) => {
|
||||
const { token } = useAuthStore.getState();
|
||||
const headers = {
|
||||
...(token ? { Authorization: `Bearer ${token}` } : {}),
|
||||
'Content-Type': 'multipart/form-data'
|
||||
"Content-Type": "multipart/form-data",
|
||||
};
|
||||
const formData = new FormData();
|
||||
|
||||
@@ -130,8 +128,6 @@ export const uploadImage = async (noticeId, imageObj, isFirst) => {
|
||||
console.info("Upload successful:", response.data);
|
||||
return response.data;
|
||||
} catch (error) {
|
||||
console.log("imageURI:", imageObj);
|
||||
|
||||
console.error(
|
||||
"Error uploading image:",
|
||||
error.response.data,
|
||||
@@ -160,3 +156,65 @@ export const deleteNotice = async (noticeId) => {
|
||||
throw error;
|
||||
}
|
||||
};
|
||||
|
||||
export const editNotice = async (noticeId, notice) => {
|
||||
const { token } = useAuthStore.getState();
|
||||
const headers = token ? { Authorization: `Bearer ${token}` } : {};
|
||||
|
||||
try {
|
||||
const response = await axios.put(
|
||||
`${API_URL}/notices/edit/${noticeId}`,
|
||||
{
|
||||
title: notice.title,
|
||||
description: notice.description,
|
||||
price: notice.price,
|
||||
category: notice.category,
|
||||
status: notice.status,
|
||||
attributes: notice.attributes,
|
||||
},
|
||||
{
|
||||
headers: headers,
|
||||
}
|
||||
);
|
||||
|
||||
if (response.data && notice.image && notice.image.length > 0) {
|
||||
for (let i = 0; i < notice.image.length; i++) {
|
||||
const image = notice.image[i];
|
||||
const isFirst = i == 0;
|
||||
|
||||
if (typeof image === "string" && !image.startsWith("http")) {
|
||||
await uploadImage(noticeId, image, isFirst);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return response.data;
|
||||
} catch (error) {
|
||||
console.error(
|
||||
"Error editing notice:",
|
||||
error.response?.data,
|
||||
error.response?.status
|
||||
);
|
||||
throw error;
|
||||
}
|
||||
};
|
||||
|
||||
export const deleteImage = async (filename) => {
|
||||
const { token } = useAuthStore.getState();
|
||||
const headers = token ? { Authorization: `Bearer ${token}` } : {};
|
||||
|
||||
try {
|
||||
const response = await axios.delete(
|
||||
`${API_URL}/images/delete/${filename}`,
|
||||
{ headers: headers }
|
||||
);
|
||||
return response.data;
|
||||
} catch (error) {
|
||||
console.error(
|
||||
"Error deleting image:",
|
||||
error.response?.data,
|
||||
error.response?.status
|
||||
);
|
||||
throw error;
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user