From 35a46a93962774ccf5d1a219ec6989f2cf541d71 Mon Sep 17 00:00:00 2001 From: JaPatryk Date: Tue, 3 Jun 2025 21:16:05 +0200 Subject: [PATCH] add sort element --- ArtisanConnect/app/(tabs)/notices.jsx | 107 ++++++++++++++++++++++---- 1 file changed, 90 insertions(+), 17 deletions(-) diff --git a/ArtisanConnect/app/(tabs)/notices.jsx b/ArtisanConnect/app/(tabs)/notices.jsx index 739d660..1082cb4 100644 --- a/ArtisanConnect/app/(tabs)/notices.jsx +++ b/ArtisanConnect/app/(tabs)/notices.jsx @@ -1,6 +1,6 @@ import { FlatList, Text, ActivityIndicator, RefreshControl, Dimensions } from "react-native"; import { useState, useEffect } from "react"; -import { Ionicons } from "@expo/vector-icons"; +import { Ionicons, MaterialCommunityIcons } from "@expo/vector-icons"; import { useNoticesStore } from "@/store/noticesStore"; import { NoticeCard } from "@/components/NoticeCard"; import { useLocalSearchParams, useRouter } from "expo-router"; @@ -14,6 +14,8 @@ import { HStack } from "@/components/ui/hstack"; import { Actionsheet, ActionsheetContent, + ActionsheetItem, + ActionsheetItemText, ActionsheetDragIndicator, ActionsheetDragIndicatorWrapper, ActionsheetBackdrop, @@ -38,6 +40,7 @@ export default function Notices() { const [isLoading, setIsLoading] = useState(true); const [error, setError] = useState(null); const [showActionsheet, setShowActionsheet] = useState(false); + const [showSortSheet, setShowSortSheet] = useState(false); const [categories, setCategories] = useState([]); const [filteredNotices, setFilteredNotices] = useState([]); const params = useLocalSearchParams(); @@ -72,13 +75,32 @@ export default function Notices() { result = result.filter(notice => notice.category === params.category); } - if (params.sort === "latest") { - result = [...result].sort( - (a, b) => new Date(b.publishDate) - new Date(a.publishDate) - ); + 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) { + if (params.priceFrom) { result = result.filter(notice => { const price = parseFloat(notice.price); const priceFrom = parseFloat(params.priceFrom); @@ -94,16 +116,21 @@ export default function Notices() { }); } - + if (params.search) { const searchTerm = params.search.toLowerCase(); - result = result.filter(notice =>{ - return notice.title.toLowerCase().includes(searchTerm); + result = result.filter(notice => { + return notice.title.toLowerCase().includes(searchTerm); }); } setFilteredNotices(result); - }, ); + }, [notices, + params.category, + params.sort, + params.priceFrom, + params.priceTo, + params.search]); let filterActive = !!params.category || !!params.sort || !!params.priceFrom || !!params.priceTo || !!params.search; @@ -129,7 +156,7 @@ export default function Notices() { }; const handlePriceFrom = (value) => { - router.replace({ + router.replace({ pathname: "/notices", params: { ...params, priceFrom: value } }); @@ -144,6 +171,14 @@ export default function Notices() { const handleClose = () => setShowActionsheet(false); + const handleSort = (value) => { + router.replace({ + pathname: "/notices", + params: { ...params, sort: value } + }); + setShowSortSheet(false); + } + const onRefresh = async () => { setRefreshing(true); try { @@ -172,10 +207,15 @@ export default function Notices() { return ( <> - + + + + {filterActive && (