fix notice status

This commit is contained in:
Patryk
2025-06-08 10:30:14 +02:00
parent ca59c94783
commit e2e5543e0d
5 changed files with 523 additions and 459 deletions

View File

@@ -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;
}

View File

@@ -1,11 +1,11 @@
import { useNoticesStore } from "@/store/noticesStore"; import { useNoticesStore } from "@/store/noticesStore";
import { NoticeCard } from "@/components/NoticeCard"; import { NoticeCard } from "@/components/NoticeCard";
import {Button} from "react-native"; import { Button } from "react-native";
import {Box} from "@/components/ui/box"; import { Box } from "@/components/ui/box";
import {Text} from "@/components/ui/text"; import { Text } from "@/components/ui/text";
import {VStack} from "@/components/ui/vstack"; import { VStack } from "@/components/ui/vstack";
import {ActivityIndicator, FlatList } from "react-native"; import { ActivityIndicator, FlatList } from "react-native";
import {useEffect, useState} from "react"; import { useEffect, useState } from "react";
export default function UserNotices() { export default function UserNotices() {
const { notices, fetchNotices } = useNoticesStore(); const { notices, fetchNotices } = useNoticesStore();
@@ -26,7 +26,9 @@ export default function UserNotices() {
loadNotices(); 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) { if (isLoading) {
return <ActivityIndicator />; return <ActivityIndicator />;
@@ -34,25 +36,37 @@ export default function UserNotices() {
return ( return (
<VStack className="p-4"> <VStack className="p-4">
<Text className="text-2xl font-bold mb-4">Moje ogłoszenia</Text> {/* <Text className="text-2xl font-bold mb-4">Moje ogłoszenia</Text> */}
{userNotices.length > 0 ? ( {userNotices.length > 0 ? (
<FlatList <FlatList
data={userNotices} data={userNotices}
numColumns={2} // numColumns={1}
columnWrapperStyle={{ marginBottom: 10, justifyContent: "space-between" }} // columnWrapperStyle={{
// marginBottom: 10,
// justifyContent: "space-between",
// }}
renderItem={({ item }) => ( renderItem={({ item }) => (
<Box className="flex-1"> <Box className="flex-1 mb-4 pb-2 bg-white rounded-lg">
<NoticeCard notice={item} /> <NoticeCard notice={item} />
<Box className="flex-row justify-between mt-2"> <Box className="flex-row justify-between mt-2">
{item.status === "ACTIVE" ? (
<Button <Button
title="Promuj" title="Usuń"
onPress={() => { onPress={() => {
// TODO: Implementacja promocji ogłoszenia
console.log(`Promuj ogłoszenie ${item.noticeId}`); console.log(`Promuj ogłoszenie ${item.noticeId}`);
}} }}
className="bg-primary-500 py-2 px-4 rounded-md" className="bg-primary-500 py-2 px-4 rounded-md"
> ></Button>
</Button> ) : (
<Button
title="Aktywj"
onPress={() => {
console.log(`Promuj ogłoszenie ${item.noticeId}`);
}}
className="bg-primary-500 py-2 px-4 rounded-md"
></Button>
)}
<Button <Button
title="Podbij" title="Podbij"
onPress={() => { onPress={() => {
@@ -60,8 +74,7 @@ export default function UserNotices() {
console.log(`Podbij ogłoszenie ${item.noticeId}`); console.log(`Podbij ogłoszenie ${item.noticeId}`);
}} }}
className="bg-primary-500 py-2 px-4 rounded-md" className="bg-primary-500 py-2 px-4 rounded-md"
> ></Button>
</Button>
</Box> </Box>
</Box> </Box>
)} )}

View File

@@ -34,11 +34,14 @@ export default function Home() {
const notices = useNoticesStore((state) => state.notices); const notices = useNoticesStore((state) => state.notices);
// console.log("Notices:", 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)) .sort((a, b) => new Date(b.publishDate) - new Date(a.publishDate))
.slice(0, 6); .slice(0, 6);
const recomendedNotices = [...notices] const recomendedNotices = [...activeNotices]
.sort(() => Math.random() - 0.5) .sort(() => Math.random() - 0.5)
.slice(0, 6); .slice(0, 6);
@@ -47,13 +50,13 @@ export default function Home() {
{/* <View> */} {/* <View> */}
<SearchSection /> <SearchSection />
<ScrollView showsVerticalScrollIndicator={false}> <ScrollView showsVerticalScrollIndicator={false}>
<CategorySection title="Polecane kategorie" notices={notices} /> <CategorySection title="Polecane kategorie" notices={activeNotices} />
<NoticeSection <NoticeSection
title="Najnowsze ogłoszenia" title="Najnowsze ogłoszenia"
notices={latestNotices} notices={latestNotices}
ctaLink="/notices?sort=latest" ctaLink="/notices?sort=latest"
/> />
<UserSection title="Popularni sprzedawcy" notices={notices} /> <UserSection title="Popularni sprzedawcy" notices={activeNotices} />
<NoticeSection <NoticeSection
title="Proponowane ogłoszenia" title="Proponowane ogłoszenia"
notices={recomendedNotices} notices={recomendedNotices}

View File

@@ -1,4 +1,10 @@
import { FlatList, Text, ActivityIndicator, RefreshControl, Dimensions } from "react-native"; import {
FlatList,
Text,
ActivityIndicator,
RefreshControl,
Dimensions,
} from "react-native";
import { useState, useEffect } from "react"; import { useState, useEffect } from "react";
import { Ionicons, MaterialCommunityIcons } from "@expo/vector-icons"; import { Ionicons, MaterialCommunityIcons } from "@expo/vector-icons";
import { useNoticesStore } from "@/store/noticesStore"; import { useNoticesStore } from "@/store/noticesStore";
@@ -11,7 +17,7 @@ import { listCategories } from "@/api/categories";
import { FormControl, FormControlLabel } from "@/components/ui/form-control"; import { FormControl, FormControlLabel } from "@/components/ui/form-control";
import { Input, InputField } from "@/components/ui/input"; import { Input, InputField } from "@/components/ui/input";
import { HStack } from "@/components/ui/hstack"; import { HStack } from "@/components/ui/hstack";
import { KeyboardAwareScrollView } from 'react-native-keyboard-aware-scroll-view'; import { KeyboardAwareScrollView } from "react-native-keyboard-aware-scroll-view";
import { KeyboardAvoidingView, Platform } from "react-native"; import { KeyboardAvoidingView, Platform } from "react-native";
import { import {
Actionsheet, Actionsheet,
@@ -56,11 +62,11 @@ export default function Notices() {
if (Array.isArray(data)) { if (Array.isArray(data)) {
setCategories(data); setCategories(data);
} else { } else {
console.error('listCategories did not return an array:', data); console.error("listCategories did not return an array:", data);
setError(new Error('Invalid categories data')); setError(new Error("Invalid categories data"));
} }
} catch (error) { } catch (error) {
console.error('Error fetching select items:', error); console.error("Error fetching select items:", error);
setError(error); setError(error);
} }
}; };
@@ -72,39 +78,38 @@ export default function Notices() {
}, []); }, []);
useEffect(() => { useEffect(() => {
let result = notices; let result = notices.filter((notice) => notice.status === "ACTIVE");
if (params.category) { if (params.category) {
result = result.filter(notice => notice.category === params.category); result = result.filter((notice) => notice.category === params.category);
} }
if (params.sort) { if (params.sort) {
if( params.sort == "latest"){ if (params.sort == "latest") {
result = [...result].sort( result = [...result].sort(
(a, b) => new Date(b.publishDate) - new Date(a.publishDate) (a, b) => new Date(b.publishDate) - new Date(a.publishDate)
); );
}else if (params.sort == "oldest") { } else if (params.sort == "oldest") {
result = [...result].sort( result = [...result].sort(
(a, b) => new Date(a.publishDate) - new Date(b.publishDate) (a, b) => new Date(a.publishDate) - new Date(b.publishDate)
); );
}else if (params.sort == "cheapest") { } else if (params.sort == "cheapest") {
result = [...result].sort((a, b) => { result = [...result].sort((a, b) => {
const priceA = parseFloat(a.price); const priceA = parseFloat(a.price);
const priceB = parseFloat(b.price); const priceB = parseFloat(b.price);
return isNaN(priceA) || isNaN(priceB) ? 0 : priceA - priceB; return isNaN(priceA) || isNaN(priceB) ? 0 : priceA - priceB;
}); });
}else if (params.sort == "expensive") { } else if (params.sort == "expensive") {
result = [...result].sort((a, b) => { result = [...result].sort((a, b) => {
const priceA = parseFloat(a.price); const priceA = parseFloat(a.price);
const priceB = parseFloat(b.price); const priceB = parseFloat(b.price);
return isNaN(priceA) || isNaN(priceB) ? 0 : priceB - priceA; return isNaN(priceA) || isNaN(priceB) ? 0 : priceB - priceA;
}); });
} }
} }
if (params.priceFrom) { if (params.priceFrom) {
result = result.filter(notice => { result = result.filter((notice) => {
const price = parseFloat(notice.price); const price = parseFloat(notice.price);
const priceFrom = parseFloat(params.priceFrom); const priceFrom = parseFloat(params.priceFrom);
return !isNaN(price) && price >= priceFrom; return !isNaN(price) && price >= priceFrom;
@@ -112,32 +117,36 @@ export default function Notices() {
} }
if (params.priceTo) { if (params.priceTo) {
result = result.filter(notice => { result = result.filter((notice) => {
const price = parseFloat(notice.price); const price = parseFloat(notice.price);
const priceTo = parseFloat(params.priceTo); const priceTo = parseFloat(params.priceTo);
return !isNaN(price) && price <= priceTo; return !isNaN(price) && price <= priceTo;
}); });
} }
if (params.search) { if (params.search) {
const searchTerm = params.search.toLowerCase(); const searchTerm = params.search.toLowerCase();
result = result.filter(notice => { result = result.filter((notice) => {
return notice.title.toLowerCase().includes(searchTerm); return notice.title.toLowerCase().includes(searchTerm);
}); });
} }
setFilteredNotices(result); setFilteredNotices(result);
}, [notices, }, [
notices,
params.category, params.category,
params.sort, params.sort,
params.priceFrom, params.priceFrom,
params.priceTo, 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 () => { const loadData = async () => {
setIsLoading(true); setIsLoading(true);
@@ -154,33 +163,33 @@ export default function Notices() {
const handleCategorySelect = (value) => { const handleCategorySelect = (value) => {
router.replace({ router.replace({
pathname: "/notices", pathname: "/notices",
params: { ...params, category: value } params: { ...params, category: value },
}); });
}; };
const handlePriceFrom = (value) => { const handlePriceFrom = (value) => {
router.replace({ router.replace({
pathname: "/notices", pathname: "/notices",
params: { ...params, priceFrom: value } params: { ...params, priceFrom: value },
}); });
} };
const handlePriceTo = (value) => { const handlePriceTo = (value) => {
router.replace({ router.replace({
pathname: "/notices", pathname: "/notices",
params: { ...params, priceTo: value } params: { ...params, priceTo: value },
}); });
} };
const handleClose = () => setShowActionsheet(false); const handleClose = () => setShowActionsheet(false);
const handleSort = (value) => { const handleSort = (value) => {
router.replace({ router.replace({
pathname: "/notices", pathname: "/notices",
params: { ...params, sort: value } params: { ...params, sort: value },
}); });
setShowSortSheet(false); setShowSortSheet(false);
} };
const onRefresh = async () => { const onRefresh = async () => {
setRefreshing(true); setRefreshing(true);
@@ -201,15 +210,26 @@ export default function Notices() {
return <Text>Nie udało się pobrać listy. {error.message}</Text>; return <Text>Nie udało się pobrać listy. {error.message}</Text>;
} }
const SCREEN_HEIGHT = Dimensions.get('window').height; const SCREEN_HEIGHT = Dimensions.get("window").height;
const selectedCategory = params.category && categories?.find( const selectedCategory =
(cat) => cat.value === params.category (params.category &&
) || null; categories?.find((cat) => cat.value === params.category)) ||
null;
return ( return (
<> <>
<Box style={{ flexDirection: "row", padding: 8, paddingTop: 16, paddingBottom: 16, backgroundColor: "white", alignItems: "center", justifyContent: "space-between" }}> <Box
style={{
flexDirection: "row",
padding: 8,
paddingTop: 16,
paddingBottom: 16,
backgroundColor: "white",
alignItems: "center",
justifyContent: "space-between",
}}
>
<Box style={{ flexDirection: "row", alignItems: "center", gap: 8 }}> <Box style={{ flexDirection: "row", alignItems: "center", gap: 8 }}>
<Button variant="outline" onPress={() => setShowActionsheet(true)}> <Button variant="outline" onPress={() => setShowActionsheet(true)}>
<ButtonText>Filtruj</ButtonText> <ButtonText>Filtruj</ButtonText>
@@ -227,9 +247,11 @@ export default function Notices() {
</Box> </Box>
<Actionsheet isOpen={showActionsheet} onClose={handleClose}> <Actionsheet isOpen={showActionsheet} onClose={handleClose}>
<ActionsheetBackdrop /> <ActionsheetBackdrop />
<ActionsheetContent style={{ maxHeight: SCREEN_HEIGHT * 0.6, width: '100%' }} > <ActionsheetContent
style={{ maxHeight: SCREEN_HEIGHT * 0.6, width: "100%" }}
>
<KeyboardAwareScrollView <KeyboardAwareScrollView
contentContainerStyle={{ flexGrow: 1, width: '100%' }} contentContainerStyle={{ flexGrow: 1, width: "100%" }}
enableOnAndroid={true} enableOnAndroid={true}
extraScrollHeight={40} extraScrollHeight={40}
> >
@@ -238,24 +260,22 @@ export default function Notices() {
</ActionsheetDragIndicatorWrapper> </ActionsheetDragIndicatorWrapper>
<Box className="mb-4" style={{ width: "100%" }}> <Box className="mb-4" style={{ width: "100%" }}>
<HStack space="md" style={{ width: "100%" }}> <HStack space="md" style={{ width: "100%" }}>
<FormControl <FormControl style={{ flex: 1 }}>
style={{ flex: 1 }}>
<Input> <Input>
<InputField <InputField
keyboardType="numeric" keyboardType="numeric"
placeholder="Od:" placeholder="Od:"
value={params.priceFrom || ''} value={params.priceFrom || ""}
onChangeText={handlePriceFrom} onChangeText={handlePriceFrom}
/> />
</Input> </Input>
</FormControl> </FormControl>
<FormControl <FormControl style={{ flex: 1 }}>
style={{ flex: 1 }}>
<Input> <Input>
<InputField <InputField
keyboardType="numeric" keyboardType="numeric"
placeholder="Do:" placeholder="Do:"
value={params.priceTo || ''} value={params.priceTo || ""}
onChangeText={handlePriceTo} onChangeText={handlePriceTo}
/> />
</Input> </Input>
@@ -265,7 +285,7 @@ export default function Notices() {
<Box className="mb-4" style={{ flex: 1 }}> <Box className="mb-4" style={{ flex: 1 }}>
<Select <Select
style={{ flex: 1 }} style={{ flex: 1 }}
selectedValue={params.category || ''} selectedValue={params.category || ""}
onValueChange={handleCategorySelect} onValueChange={handleCategorySelect}
> >
<SelectTrigger variant="outline" size="md"> <SelectTrigger variant="outline" size="md">
@@ -274,63 +294,73 @@ export default function Notices() {
placeholder="Wybierz kategorię" placeholder="Wybierz kategorię"
value={selectedCategory ? selectedCategory.label : ""} value={selectedCategory ? selectedCategory.label : ""}
/> />
<SelectIcon style={{ marginRight: 12 }} as={ChevronDownIcon} /> <SelectIcon
style={{ marginRight: 12 }}
as={ChevronDownIcon}
/>
</SelectTrigger> </SelectTrigger>
<SelectPortal> <SelectPortal>
<SelectBackdrop /> <SelectBackdrop />
<SelectContent <SelectContent style={{ maxHeight: SCREEN_HEIGHT * 0.6 }}>
style={{ maxHeight: SCREEN_HEIGHT * 0.6}}
>
<SelectDragIndicatorWrapper> <SelectDragIndicatorWrapper>
<SelectDragIndicator /> <SelectDragIndicator />
</SelectDragIndicatorWrapper> </SelectDragIndicatorWrapper>
<FlatList <FlatList
style={{ width: '100%' }} style={{ width: "100%" }}
data={categories} data={categories}
keyExtractor={(item) => item.value?.toString() || item.id?.toString() || Math.random().toString()} keyExtractor={(item) =>
item.value?.toString() ||
item.id?.toString() ||
Math.random().toString()
}
renderItem={({ item }) => ( renderItem={({ item }) => (
<SelectItem <SelectItem label={item.label} value={item.value} />
label={item.label}
value={item.value}
/>
)} )}
/> />
</SelectContent> </SelectContent>
</SelectPortal> </SelectPortal>
</Select> </Select>
</Box> </Box>
</KeyboardAwareScrollView> </KeyboardAwareScrollView>
</ActionsheetContent> </ActionsheetContent>
</Actionsheet> </Actionsheet>
<Actionsheet isOpen={showSortSheet} onClose={() => setShowSortSheet(false)}> <Actionsheet
isOpen={showSortSheet}
onClose={() => setShowSortSheet(false)}
>
<ActionsheetBackdrop /> <ActionsheetBackdrop />
<ActionsheetContent> <ActionsheetContent>
<ActionsheetDragIndicatorWrapper> <ActionsheetDragIndicatorWrapper>
<ActionsheetDragIndicator /> <ActionsheetDragIndicator />
</ActionsheetDragIndicatorWrapper> </ActionsheetDragIndicatorWrapper>
<ActionsheetItem <ActionsheetItem
className={ !params.sort ? 'bg-gray-200' : ''} className={!params.sort ? "bg-gray-200" : ""}
onPress={() => handleSort()}> onPress={() => handleSort()}
>
<ActionsheetItemText>Trafność</ActionsheetItemText> <ActionsheetItemText>Trafność</ActionsheetItemText>
</ActionsheetItem> </ActionsheetItem>
<ActionsheetItem <ActionsheetItem
className={ params.sort == 'latest' ? 'bg-gray-200' : ''} className={params.sort == "latest" ? "bg-gray-200" : ""}
onPress={() => handleSort('latest')}> onPress={() => handleSort("latest")}
>
<ActionsheetItemText>Najnowsze</ActionsheetItemText> <ActionsheetItemText>Najnowsze</ActionsheetItemText>
</ActionsheetItem> </ActionsheetItem>
<ActionsheetItem <ActionsheetItem
className={ params.sort == 'oldest' ? 'bg-gray-200' : ''} className={params.sort == "oldest" ? "bg-gray-200" : ""}
onPress={() => handleSort('oldest')}> onPress={() => handleSort("oldest")}
>
<ActionsheetItemText>Najstarsze</ActionsheetItemText> <ActionsheetItemText>Najstarsze</ActionsheetItemText>
</ActionsheetItem> </ActionsheetItem>
<ActionsheetItem <ActionsheetItem
className={ params.sort == 'cheapest' ? 'bg-gray-200' : ''} className={params.sort == "cheapest" ? "bg-gray-200" : ""}
onPress={() => handleSort('cheapest')}> onPress={() => handleSort("cheapest")}
>
<ActionsheetItemText>Najtańsze</ActionsheetItemText> <ActionsheetItemText>Najtańsze</ActionsheetItemText>
</ActionsheetItem> </ActionsheetItem>
<ActionsheetItem <ActionsheetItem
className={ params.sort == 'expensive' ? 'bg-gray-200' : ''} className={params.sort == "expensive" ? "bg-gray-200" : ""}
onPress={() => handleSort('expensive')}> onPress={() => handleSort("expensive")}
>
<ActionsheetItemText>Najdroższe</ActionsheetItemText> <ActionsheetItemText>Najdroższe</ActionsheetItemText>
</ActionsheetItem> </ActionsheetItem>
</ActionsheetContent> </ActionsheetContent>

View File

@@ -1,28 +1,32 @@
import {Box} from "@/components/ui/box"; import { Box } from "@/components/ui/box";
import {Card} from "@/components/ui/card"; import { Card } from "@/components/ui/card";
import {Heading} from "@/components/ui/heading"; import { Heading } from "@/components/ui/heading";
import {Image} from "@/components/ui/image"; import { Image } from "@/components/ui/image";
import {Text} from "@/components/ui/text"; import { Text } from "@/components/ui/text";
import {VStack} from "@/components/ui/vstack"; import { VStack } from "@/components/ui/vstack";
import {Link} from "expo-router"; import { Link } from "expo-router";
import {Pressable, ActivityIndicator, View} from "react-native"; import { Pressable, ActivityIndicator, View } from "react-native";
import {useWishlist} from "@/store/wishlistStore"; import { useWishlist } from "@/store/wishlistStore";
import {useNoticesStore} from "@/store/noticesStore"; import { useNoticesStore } from "@/store/noticesStore";
import {Ionicons} from "@expo/vector-icons"; import { Ionicons } from "@expo/vector-icons";
import {useEffect, useState} from "react"; import { useEffect, useState } from "react";
export function NoticeCard({notice}) { export function NoticeCard({ notice }) {
const noticeId = notice?.noticeId; const noticeId = notice?.noticeId;
const toggleNoticeInWishlist = useWishlist((state) => state.toggleNoticeInWishlist); const toggleNoticeInWishlist = useWishlist(
(state) => state.toggleNoticeInWishlist
);
const isInWishlist = useWishlist((state) => const isInWishlist = useWishlist((state) =>
noticeId ? state.wishlistNotices.some((item) => item.noticeId === noticeId) : false noticeId
? state.wishlistNotices.some((item) => item.noticeId === noticeId)
: false
); );
const [image, setImage] = useState(null); const [image, setImage] = useState(null);
const [isLoading, setIsLoading] = useState(true); const [isLoading, setIsLoading] = useState(true);
const {getAllImagesByNoticeId} = useNoticesStore(); const { getAllImagesByNoticeId } = useNoticesStore();
useEffect(() => { useEffect(() => {
let isMounted = true; let isMounted = true;
@@ -40,7 +44,9 @@ export function NoticeCard({notice}) {
try { try {
const images = await getAllImagesByNoticeId(noticeId); const images = await getAllImagesByNoticeId(noticeId);
if (isMounted) { if (isMounted) {
setImage(images && images.length > 0 ? images[0] : "https://http.cat/404.jpg"); setImage(
images && images.length > 0 ? images[0] : "https://http.cat/404.jpg"
);
} }
} catch (error) { } catch (error) {
console.error(`Error while loading image: ${error}`); console.error(`Error while loading image: ${error}`);
@@ -62,7 +68,7 @@ export function NoticeCard({notice}) {
}, [noticeId]); }, [noticeId]);
if (!notice) { if (!notice) {
return <View style={{flex: 1}} />; return <View style={{ flex: 1 }} />;
} }
return ( return (