From e2e5543e0d089e669ed3be0e1ce8e5679d1e4d0c Mon Sep 17 00:00:00 2001 From: Patryk Date: Sun, 8 Jun 2025 10:30:14 +0200 Subject: [PATCH] fix notice status --- ArtisanConnect/api/order.jsx | 12 + .../app/(tabs)/dashboard/userNotices.jsx | 103 +-- ArtisanConnect/app/(tabs)/index.jsx | 11 +- ArtisanConnect/app/(tabs)/notices.jsx | 650 +++++++++--------- ArtisanConnect/components/NoticeCard.jsx | 206 +++--- 5 files changed, 523 insertions(+), 459 deletions(-) create mode 100644 ArtisanConnect/api/order.jsx diff --git a/ArtisanConnect/api/order.jsx b/ArtisanConnect/api/order.jsx new file mode 100644 index 0000000..d460b3a --- /dev/null +++ b/ArtisanConnect/api/order.jsx @@ -0,0 +1,12 @@ +import axios from "axios"; +import FormData from "form-data"; + +const API_URL = "https://hopp.zikor.pl/api/v1"; +export async function listOrders() { + const response = await fetch(`${API_URL}/orders/get/all`); + const data = await response.json(); + if (!response.ok) { + throw new Error(response.toString()); + } + return data; +} diff --git a/ArtisanConnect/app/(tabs)/dashboard/userNotices.jsx b/ArtisanConnect/app/(tabs)/dashboard/userNotices.jsx index 7fe0f2b..c35e68d 100644 --- a/ArtisanConnect/app/(tabs)/dashboard/userNotices.jsx +++ b/ArtisanConnect/app/(tabs)/dashboard/userNotices.jsx @@ -1,11 +1,11 @@ import { useNoticesStore } from "@/store/noticesStore"; import { NoticeCard } from "@/components/NoticeCard"; -import {Button} from "react-native"; -import {Box} from "@/components/ui/box"; -import {Text} from "@/components/ui/text"; -import {VStack} from "@/components/ui/vstack"; -import {ActivityIndicator, FlatList } from "react-native"; -import {useEffect, useState} from "react"; +import { Button } from "react-native"; +import { Box } from "@/components/ui/box"; +import { Text } from "@/components/ui/text"; +import { VStack } from "@/components/ui/vstack"; +import { ActivityIndicator, FlatList } from "react-native"; +import { useEffect, useState } from "react"; export default function UserNotices() { const { notices, fetchNotices } = useNoticesStore(); @@ -26,50 +26,63 @@ export default function UserNotices() { loadNotices(); }, []); - const userNotices = notices.filter(notice => notice.clientId === currentUserId); + const userNotices = notices + .filter((notice) => notice.clientId === currentUserId) + .sort((a, b) => new Date(b.publishDate) - new Date(a.publishDate)); if (isLoading) { return ; } return ( - - Moje ogłoszenia - {userNotices.length > 0 ? ( - ( - - - - - - - + + {/* Moje ogłoszenia */} + {userNotices.length > 0 ? ( + ( + + + + {item.status === "ACTIVE" ? ( + + ) : ( + )} - keyExtractor={(item) => item.noticeId.toString()} - /> - ) : ( - Nie masz żadnych ogłoszeń. - )} - + + + + + )} + keyExtractor={(item) => item.noticeId.toString()} + /> + ) : ( + Nie masz żadnych ogłoszeń. + )} + ); -} \ No newline at end of file +} diff --git a/ArtisanConnect/app/(tabs)/index.jsx b/ArtisanConnect/app/(tabs)/index.jsx index 674865e..723bd8a 100644 --- a/ArtisanConnect/app/(tabs)/index.jsx +++ b/ArtisanConnect/app/(tabs)/index.jsx @@ -34,11 +34,14 @@ export default function Home() { const notices = useNoticesStore((state) => state.notices); // console.log("Notices:", notices); + // console.log("Notices length:", notices.length); - const latestNotices = [...notices] + const activeNotices = notices.filter((notice) => notice.status === "ACTIVE"); + // console.log("Activer Notices:", activeNotices.length); + const latestNotices = [...activeNotices] .sort((a, b) => new Date(b.publishDate) - new Date(a.publishDate)) .slice(0, 6); - const recomendedNotices = [...notices] + const recomendedNotices = [...activeNotices] .sort(() => Math.random() - 0.5) .slice(0, 6); @@ -47,13 +50,13 @@ export default function Home() { {/* */} - + - + { - const fetchSelectItems = async () => { - try { - const data = await listCategories(); - if (Array.isArray(data)) { - setCategories(data); - } else { - console.error('listCategories did not return an array:', data); - setError(new Error('Invalid categories data')); - } - } catch (error) { - console.error('Error fetching select items:', error); - setError(error); - } - }; - fetchSelectItems(); - }, []); - - useEffect(() => { - loadData(); - }, []); - - useEffect(() => { - let result = notices; - - if (params.category) { - result = result.filter(notice => notice.category === params.category); + useEffect(() => { + const fetchSelectItems = async () => { + try { + const data = await listCategories(); + if (Array.isArray(data)) { + setCategories(data); + } else { + console.error("listCategories did not return an array:", data); + setError(new Error("Invalid categories data")); } + } catch (error) { + console.error("Error fetching select items:", error); + setError(error); + } + }; + fetchSelectItems(); + }, []); - if (params.sort) { - if( params.sort == "latest"){ - result = [...result].sort( - (a, b) => new Date(b.publishDate) - new Date(a.publishDate) - ); - }else if (params.sort == "oldest") { - result = [...result].sort( - (a, b) => new Date(a.publishDate) - new Date(b.publishDate) - ); - }else if (params.sort == "cheapest") { - result = [...result].sort((a, b) => { - const priceA = parseFloat(a.price); - const priceB = parseFloat(b.price); - return isNaN(priceA) || isNaN(priceB) ? 0 : priceA - priceB; - }); - }else if (params.sort == "expensive") { - result = [...result].sort((a, b) => { - const priceA = parseFloat(a.price); - const priceB = parseFloat(b.price); - return isNaN(priceA) || isNaN(priceB) ? 0 : priceB - priceA; - }); - } + useEffect(() => { + loadData(); + }, []); - } + useEffect(() => { + let result = notices.filter((notice) => notice.status === "ACTIVE"); - if (params.priceFrom) { - result = result.filter(notice => { - const price = parseFloat(notice.price); - const priceFrom = parseFloat(params.priceFrom); - return !isNaN(price) && price >= priceFrom; - }); - } + if (params.category) { + result = result.filter((notice) => notice.category === params.category); + } - if (params.priceTo) { - result = result.filter(notice => { - const price = parseFloat(notice.price); - const priceTo = parseFloat(params.priceTo); - return !isNaN(price) && price <= priceTo; - }); - } + if (params.sort) { + if (params.sort == "latest") { + result = [...result].sort( + (a, b) => new Date(b.publishDate) - new Date(a.publishDate) + ); + } else if (params.sort == "oldest") { + result = [...result].sort( + (a, b) => new Date(a.publishDate) - new Date(b.publishDate) + ); + } else if (params.sort == "cheapest") { + result = [...result].sort((a, b) => { + const priceA = parseFloat(a.price); + const priceB = parseFloat(b.price); + return isNaN(priceA) || isNaN(priceB) ? 0 : priceA - priceB; + }); + } else if (params.sort == "expensive") { + result = [...result].sort((a, b) => { + const priceA = parseFloat(a.price); + const priceB = parseFloat(b.price); + return isNaN(priceA) || isNaN(priceB) ? 0 : priceB - priceA; + }); + } + } + if (params.priceFrom) { + result = result.filter((notice) => { + const price = parseFloat(notice.price); + const priceFrom = parseFloat(params.priceFrom); + return !isNaN(price) && price >= priceFrom; + }); + } - if (params.search) { - const searchTerm = params.search.toLowerCase(); - result = result.filter(notice => { - return notice.title.toLowerCase().includes(searchTerm); - }); - } + if (params.priceTo) { + result = result.filter((notice) => { + const price = parseFloat(notice.price); + const priceTo = parseFloat(params.priceTo); + return !isNaN(price) && price <= priceTo; + }); + } - setFilteredNotices(result); - }, [notices, + if (params.search) { + const searchTerm = params.search.toLowerCase(); + result = result.filter((notice) => { + return notice.title.toLowerCase().includes(searchTerm); + }); + } + + setFilteredNotices(result); + }, [ + notices, params.category, params.sort, params.priceFrom, params.priceTo, - params.search]); + params.search, + ]); + let filterActive = + !!params.category || + !!params.sort || + !!params.priceFrom || + !!params.priceTo || + !!params.search; - let filterActive = !!params.category || !!params.sort || !!params.priceFrom || !!params.priceTo || !!params.search; - - - const loadData = async () => { - setIsLoading(true); - try { - await fetchNotices(); - setError(null); - } catch (err) { - setError(err); - } finally { - setIsLoading(false); - } - }; - - const handleCategorySelect = (value) => { - router.replace({ - pathname: "/notices", - params: { ...params, category: value } - }); - }; - - const handlePriceFrom = (value) => { - router.replace({ - pathname: "/notices", - params: { ...params, priceFrom: value } - }); + const loadData = async () => { + setIsLoading(true); + try { + await fetchNotices(); + setError(null); + } catch (err) { + setError(err); + } finally { + setIsLoading(false); } + }; - const handlePriceTo = (value) => { - router.replace({ - pathname: "/notices", - params: { ...params, priceTo: value } - }); + const handleCategorySelect = (value) => { + router.replace({ + pathname: "/notices", + params: { ...params, category: value }, + }); + }; + + const handlePriceFrom = (value) => { + router.replace({ + pathname: "/notices", + params: { ...params, priceFrom: value }, + }); + }; + + const handlePriceTo = (value) => { + router.replace({ + pathname: "/notices", + params: { ...params, priceTo: value }, + }); + }; + + const handleClose = () => setShowActionsheet(false); + + const handleSort = (value) => { + router.replace({ + pathname: "/notices", + params: { ...params, sort: value }, + }); + setShowSortSheet(false); + }; + + const onRefresh = async () => { + setRefreshing(true); + try { + await fetchNotices(); + } catch (err) { + setError(err); + } finally { + setRefreshing(false); } + }; - const handleClose = () => setShowActionsheet(false); + if (isLoading && !refreshing) { + return ; + } - const handleSort = (value) => { - router.replace({ - pathname: "/notices", - params: { ...params, sort: value } - }); - setShowSortSheet(false); - } + if (error) { + return Nie udało się pobrać listy. {error.message}; + } - const onRefresh = async () => { - setRefreshing(true); - try { - await fetchNotices(); - } catch (err) { - setError(err); - } finally { - setRefreshing(false); - } - }; + const SCREEN_HEIGHT = Dimensions.get("window").height; - if (isLoading && !refreshing) { - return ; - } + const selectedCategory = + (params.category && + categories?.find((cat) => cat.value === params.category)) || + null; - if (error) { - return Nie udało się pobrać listy. {error.message}; - } - - const SCREEN_HEIGHT = Dimensions.get('window').height; - - const selectedCategory = params.category && categories?.find( - (cat) => cat.value === params.category - ) || null; - - return ( - <> - - - - - - {filterActive && ( - - )} - - - - - + - - - - - - - - - - - - - - - - - - - - - - - - setShowSortSheet(false)}> - - - - - - handleSort()}> - Trafność - - handleSort('latest')}> - Najnowsze - - handleSort('oldest')}> - Najstarsze - - handleSort('cheapest')}> - Najtańsze - - handleSort('expensive')}> - Najdroższe - - - - } - refreshControl={ - + + + + {filterActive && ( + + )} + + + + + + + + + + + + + - } - /> - - ); -} \ No newline at end of file + + + + + + + + + + + + + + + + setShowSortSheet(false)} + > + + + + + + handleSort()} + > + Trafność + + handleSort("latest")} + > + Najnowsze + + handleSort("oldest")} + > + Najstarsze + + handleSort("cheapest")} + > + Najtańsze + + handleSort("expensive")} + > + Najdroższe + + + + } + refreshControl={ + + } + /> + + ); +} diff --git a/ArtisanConnect/components/NoticeCard.jsx b/ArtisanConnect/components/NoticeCard.jsx index 3645430..9a72044 100644 --- a/ArtisanConnect/components/NoticeCard.jsx +++ b/ArtisanConnect/components/NoticeCard.jsx @@ -1,111 +1,117 @@ -import {Box} from "@/components/ui/box"; -import {Card} from "@/components/ui/card"; -import {Heading} from "@/components/ui/heading"; -import {Image} from "@/components/ui/image"; -import {Text} from "@/components/ui/text"; -import {VStack} from "@/components/ui/vstack"; -import {Link} from "expo-router"; -import {Pressable, ActivityIndicator, View} from "react-native"; -import {useWishlist} from "@/store/wishlistStore"; -import {useNoticesStore} from "@/store/noticesStore"; -import {Ionicons} from "@expo/vector-icons"; -import {useEffect, useState} from "react"; +import { Box } from "@/components/ui/box"; +import { Card } from "@/components/ui/card"; +import { Heading } from "@/components/ui/heading"; +import { Image } from "@/components/ui/image"; +import { Text } from "@/components/ui/text"; +import { VStack } from "@/components/ui/vstack"; +import { Link } from "expo-router"; +import { Pressable, ActivityIndicator, View } from "react-native"; +import { useWishlist } from "@/store/wishlistStore"; +import { useNoticesStore } from "@/store/noticesStore"; +import { Ionicons } from "@expo/vector-icons"; +import { useEffect, useState } from "react"; -export function NoticeCard({notice}) { - const noticeId = notice?.noticeId; +export function NoticeCard({ notice }) { + const noticeId = notice?.noticeId; - const toggleNoticeInWishlist = useWishlist((state) => state.toggleNoticeInWishlist); - const isInWishlist = useWishlist((state) => - noticeId ? state.wishlistNotices.some((item) => item.noticeId === noticeId) : false - ); + const toggleNoticeInWishlist = useWishlist( + (state) => state.toggleNoticeInWishlist + ); + const isInWishlist = useWishlist((state) => + noticeId + ? state.wishlistNotices.some((item) => item.noticeId === noticeId) + : false + ); - const [image, setImage] = useState(null); - const [isLoading, setIsLoading] = useState(true); + const [image, setImage] = useState(null); + const [isLoading, setIsLoading] = useState(true); - const {getAllImagesByNoticeId} = useNoticesStore(); + const { getAllImagesByNoticeId } = useNoticesStore(); - useEffect(() => { - let isMounted = true; + useEffect(() => { + let isMounted = true; - const fetchImage = async () => { - if (!noticeId) { - if (isMounted) { - setImage("https://http.cat/404.jpg"); - setIsLoading(false); - } - return; - } + const fetchImage = async () => { + if (!noticeId) { + if (isMounted) { + setImage("https://http.cat/404.jpg"); + setIsLoading(false); + } + return; + } - setIsLoading(true); - try { - const images = await getAllImagesByNoticeId(noticeId); - if (isMounted) { - setImage(images && images.length > 0 ? images[0] : "https://http.cat/404.jpg"); - } - } catch (error) { - console.error(`Error while loading image: ${error}`); - if (isMounted) { - setImage("https://http.cat/404.jpg"); - } - } finally { - if (isMounted) { - setIsLoading(false); - } - } - }; + setIsLoading(true); + try { + const images = await getAllImagesByNoticeId(noticeId); + if (isMounted) { + setImage( + images && images.length > 0 ? images[0] : "https://http.cat/404.jpg" + ); + } + } catch (error) { + console.error(`Error while loading image: ${error}`); + if (isMounted) { + setImage("https://http.cat/404.jpg"); + } + } finally { + if (isMounted) { + setIsLoading(false); + } + } + }; - fetchImage(); + fetchImage(); - return () => { - isMounted = false; - }; - }, [noticeId]); + return () => { + isMounted = false; + }; + }, [noticeId]); - if (!notice) { - return ; - } + if (!notice) { + return ; + } - return ( - - - - {isLoading ? ( - - - - ) : ( - image - )} - - - {notice.title} - - - - {notice.price}zł - - { - toggleNoticeInWishlist(noticeId); - }} - > - - - - - - - - ); -} \ No newline at end of file + return ( + + + + {isLoading ? ( + + + + ) : ( + image + )} + + + {notice.title} + + + + {notice.price}zł + + { + toggleNoticeInWishlist(noticeId); + }} + > + + + + + + + + ); +}