From 3c042d2cfbe2ca38d312df91c793e59357f2823c Mon Sep 17 00:00:00 2001 From: Patryk Date: Wed, 11 Jun 2025 20:56:39 +0200 Subject: [PATCH] add edit notice and clean code --- ArtisanConnect/api/notices.jsx | 76 ++++++++++++++++--- .../app/(tabs)/dashboard/notice/edit/[id].jsx | 65 ++++++---------- .../app/(tabs)/dashboard/userNotices.jsx | 1 - ArtisanConnect/app/(tabs)/notice/create.jsx | 2 - ArtisanConnect/app/(tabs)/notices.jsx | 4 - ArtisanConnect/app/notice/[id].jsx | 9 --- ArtisanConnect/store/noticesStore.jsx | 28 +++++++ 7 files changed, 118 insertions(+), 67 deletions(-) diff --git a/ArtisanConnect/api/notices.jsx b/ArtisanConnect/api/notices.jsx index ab61fe0..4a7190d 100644 --- a/ArtisanConnect/api/notices.jsx +++ b/ArtisanConnect/api/notices.jsx @@ -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; + } +}; diff --git a/ArtisanConnect/app/(tabs)/dashboard/notice/edit/[id].jsx b/ArtisanConnect/app/(tabs)/dashboard/notice/edit/[id].jsx index 5cc59dd..b78aa73 100644 --- a/ArtisanConnect/app/(tabs)/dashboard/notice/edit/[id].jsx +++ b/ArtisanConnect/app/(tabs)/dashboard/notice/edit/[id].jsx @@ -31,7 +31,7 @@ import { ChevronDownIcon } from "@/components/ui/icon"; import { useNoticesStore } from "@/store/noticesStore"; import { listCategories } from "@/api/categories"; import { useRouter } from "expo-router"; -import { attributes } from "@/data/attributesData"; // Assuming you have a separate file for attributes data} +import { attributes } from "@/data/attributesData"; import { useLocalSearchParams } from "expo-router"; export default function EditNotice() { @@ -43,7 +43,6 @@ export default function EditNotice() { const [price, setPrice] = useState(""); const [category, setCategory] = useState(""); const [image, setImage] = useState([]); - const [images, setImages] = useState([]); const [isImageLoading, setIsImageLoading] = useState(true); const [selectItems, setSelectItems] = useState([]); const [isLoading, setIsLoading] = useState(false); @@ -84,7 +83,6 @@ export default function EditNotice() { attributesObj[attr.name] = attr.value; }); setSelectedAttributes(attributesObj); - // console.log("Attributes loaded:", attributesObj); } } @@ -92,20 +90,13 @@ export default function EditNotice() { setIsImageLoading(true); try { const fetchedImages = await getAllImagesByNoticeId(notice.noticeId); - console.log("Fetched images:", fetchedImages); - if (fetchedImages && fetchedImages.length > 0) { - const imageUris = fetchedImages.map((img) => img.uri); - setImages(fetchedImages); - setImage(imageUris); - console.log("Image URIs set:", imageUris); + setImage(fetchedImages); } else { - setImages([]); setImage([]); } } catch (err) { console.error("Error while loading images:", err); - setImages([]); setImage([]); } finally { setIsImageLoading(false); @@ -115,7 +106,7 @@ export default function EditNotice() { if (notice) { fetchImage(); } - }, [notices]); + }, [notices, id]); const [error, setError] = useState({ title: false, @@ -154,10 +145,9 @@ export default function EditNotice() { value: value, }) ); - // console.log("Selected attributes:", formattedAttributes); setIsLoading(true); try { - const result = await editNotice({ + const result = await editNotice(id, { title: title, description: description, price: price, @@ -168,14 +158,12 @@ export default function EditNotice() { }); if (result) { - console.log("Notice created successfully with ID: ", result.noticeId); await fetchNotices(); - clearForm(); router.push("/(tabs)/dashboard/userNotices"); } } catch (error) { - console.error("Error creating notice. Error message: ", error.message); + console.error("Error editing notice. Error message: ", error.message); } finally { setIsLoading(false); } @@ -210,27 +198,12 @@ export default function EditNotice() { } }; - const clearForm = () => { - setTitle(""); - setDescription(""); - setPrice(""); - setCategory(""); - setImage([]); - setSelectedAttributes({}); - setError({ - title: false, - description: false, - price: false, - category: false, - }); - }; - if (isLoading) { return ( - Dodajemy ogłoszenie... + Edytuj ogłoszenie... ); @@ -258,14 +231,22 @@ export default function EditNotice() { {image && image.length > 0 && ( - {image.map((img, index) => ( - - ))} + {image.map((img, index) => { + const imageSource = + typeof img === "string" ? { uri: img } : img; + + return ( + + console.log(`Image ${index} error:`, error) + } + /> + ); + })} )} @@ -381,7 +362,7 @@ export default function EditNotice() { onPress={handleEditNotice} disabled={isLoading} > - Dodaj + Edytuj diff --git a/ArtisanConnect/app/(tabs)/dashboard/userNotices.jsx b/ArtisanConnect/app/(tabs)/dashboard/userNotices.jsx index 7da0398..ee2d5d1 100644 --- a/ArtisanConnect/app/(tabs)/dashboard/userNotices.jsx +++ b/ArtisanConnect/app/(tabs)/dashboard/userNotices.jsx @@ -162,7 +162,6 @@ export default function UserNotices() {