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

@@ -26,7 +26,9 @@ 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 <ActivityIndicator />;
@@ -34,25 +36,37 @@ export default function UserNotices() {
return (
<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 ? (
<FlatList
data={userNotices}
numColumns={2}
columnWrapperStyle={{ marginBottom: 10, justifyContent: "space-between" }}
// numColumns={1}
// columnWrapperStyle={{
// marginBottom: 10,
// justifyContent: "space-between",
// }}
renderItem={({ item }) => (
<Box className="flex-1">
<Box className="flex-1 mb-4 pb-2 bg-white rounded-lg">
<NoticeCard notice={item} />
<Box className="flex-row justify-between mt-2">
{item.status === "ACTIVE" ? (
<Button
title="Promuj"
title="Usuń"
onPress={() => {
// TODO: Implementacja promocji ogłoszenia
console.log(`Promuj ogłoszenie ${item.noticeId}`);
}}
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
title="Podbij"
onPress={() => {
@@ -60,8 +74,7 @@ export default function UserNotices() {
console.log(`Podbij ogłoszenie ${item.noticeId}`);
}}
className="bg-primary-500 py-2 px-4 rounded-md"
>
</Button>
></Button>
</Box>
</Box>
)}

View File

@@ -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() {
{/* <View> */}
<SearchSection />
<ScrollView showsVerticalScrollIndicator={false}>
<CategorySection title="Polecane kategorie" notices={notices} />
<CategorySection title="Polecane kategorie" notices={activeNotices} />
<NoticeSection
title="Najnowsze ogłoszenia"
notices={latestNotices}
ctaLink="/notices?sort=latest"
/>
<UserSection title="Popularni sprzedawcy" notices={notices} />
<UserSection title="Popularni sprzedawcy" notices={activeNotices} />
<NoticeSection
title="Proponowane ogłoszenia"
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 { Ionicons, MaterialCommunityIcons } from "@expo/vector-icons";
import { useNoticesStore } from "@/store/noticesStore";
@@ -11,7 +17,7 @@ import { listCategories } from "@/api/categories";
import { FormControl, FormControlLabel } from "@/components/ui/form-control";
import { Input, InputField } from "@/components/ui/input";
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 {
Actionsheet,
@@ -56,11 +62,11 @@ export default function Notices() {
if (Array.isArray(data)) {
setCategories(data);
} else {
console.error('listCategories did not return an array:', data);
setError(new Error('Invalid categories data'));
console.error("listCategories did not return an array:", data);
setError(new Error("Invalid categories data"));
}
} catch (error) {
console.error('Error fetching select items:', error);
console.error("Error fetching select items:", error);
setError(error);
}
};
@@ -72,10 +78,10 @@ export default function Notices() {
}, []);
useEffect(() => {
let result = notices;
let result = notices.filter((notice) => notice.status === "ACTIVE");
if (params.category) {
result = result.filter(notice => notice.category === params.category);
result = result.filter((notice) => notice.category === params.category);
}
if (params.sort) {
@@ -100,11 +106,10 @@ export default function Notices() {
return isNaN(priceA) || isNaN(priceB) ? 0 : priceB - priceA;
});
}
}
if (params.priceFrom) {
result = result.filter(notice => {
result = result.filter((notice) => {
const price = parseFloat(notice.price);
const priceFrom = parseFloat(params.priceFrom);
return !isNaN(price) && price >= priceFrom;
@@ -112,32 +117,36 @@ export default function Notices() {
}
if (params.priceTo) {
result = result.filter(notice => {
result = result.filter((notice) => {
const price = parseFloat(notice.price);
const priceTo = parseFloat(params.priceTo);
return !isNaN(price) && price <= priceTo;
});
}
if (params.search) {
const searchTerm = params.search.toLowerCase();
result = result.filter(notice => {
result = result.filter((notice) => {
return notice.title.toLowerCase().includes(searchTerm);
});
}
setFilteredNotices(result);
}, [notices,
}, [
notices,
params.category,
params.sort,
params.priceFrom,
params.priceTo,
params.search]);
let filterActive = !!params.category || !!params.sort || !!params.priceFrom || !!params.priceTo || !!params.search;
params.search,
]);
let filterActive =
!!params.category ||
!!params.sort ||
!!params.priceFrom ||
!!params.priceTo ||
!!params.search;
const loadData = async () => {
setIsLoading(true);
@@ -154,33 +163,33 @@ export default function Notices() {
const handleCategorySelect = (value) => {
router.replace({
pathname: "/notices",
params: { ...params, category: value }
params: { ...params, category: value },
});
};
const handlePriceFrom = (value) => {
router.replace({
pathname: "/notices",
params: { ...params, priceFrom: value }
params: { ...params, priceFrom: value },
});
}
};
const handlePriceTo = (value) => {
router.replace({
pathname: "/notices",
params: { ...params, priceTo: value }
params: { ...params, priceTo: value },
});
}
};
const handleClose = () => setShowActionsheet(false);
const handleSort = (value) => {
router.replace({
pathname: "/notices",
params: { ...params, sort: value }
params: { ...params, sort: value },
});
setShowSortSheet(false);
}
};
const onRefresh = async () => {
setRefreshing(true);
@@ -201,15 +210,26 @@ export default function Notices() {
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(
(cat) => cat.value === params.category
) || null;
const selectedCategory =
(params.category &&
categories?.find((cat) => cat.value === params.category)) ||
null;
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 }}>
<Button variant="outline" onPress={() => setShowActionsheet(true)}>
<ButtonText>Filtruj</ButtonText>
@@ -227,9 +247,11 @@ export default function Notices() {
</Box>
<Actionsheet isOpen={showActionsheet} onClose={handleClose}>
<ActionsheetBackdrop />
<ActionsheetContent style={{ maxHeight: SCREEN_HEIGHT * 0.6, width: '100%' }} >
<ActionsheetContent
style={{ maxHeight: SCREEN_HEIGHT * 0.6, width: "100%" }}
>
<KeyboardAwareScrollView
contentContainerStyle={{ flexGrow: 1, width: '100%' }}
contentContainerStyle={{ flexGrow: 1, width: "100%" }}
enableOnAndroid={true}
extraScrollHeight={40}
>
@@ -238,24 +260,22 @@ export default function Notices() {
</ActionsheetDragIndicatorWrapper>
<Box className="mb-4" style={{ width: "100%" }}>
<HStack space="md" style={{ width: "100%" }}>
<FormControl
style={{ flex: 1 }}>
<FormControl style={{ flex: 1 }}>
<Input>
<InputField
keyboardType="numeric"
placeholder="Od:"
value={params.priceFrom || ''}
value={params.priceFrom || ""}
onChangeText={handlePriceFrom}
/>
</Input>
</FormControl>
<FormControl
style={{ flex: 1 }}>
<FormControl style={{ flex: 1 }}>
<Input>
<InputField
keyboardType="numeric"
placeholder="Do:"
value={params.priceTo || ''}
value={params.priceTo || ""}
onChangeText={handlePriceTo}
/>
</Input>
@@ -265,7 +285,7 @@ export default function Notices() {
<Box className="mb-4" style={{ flex: 1 }}>
<Select
style={{ flex: 1 }}
selectedValue={params.category || ''}
selectedValue={params.category || ""}
onValueChange={handleCategorySelect}
>
<SelectTrigger variant="outline" size="md">
@@ -274,25 +294,27 @@ export default function Notices() {
placeholder="Wybierz kategorię"
value={selectedCategory ? selectedCategory.label : ""}
/>
<SelectIcon style={{ marginRight: 12 }} as={ChevronDownIcon} />
<SelectIcon
style={{ marginRight: 12 }}
as={ChevronDownIcon}
/>
</SelectTrigger>
<SelectPortal>
<SelectBackdrop />
<SelectContent
style={{ maxHeight: SCREEN_HEIGHT * 0.6}}
>
<SelectContent style={{ maxHeight: SCREEN_HEIGHT * 0.6 }}>
<SelectDragIndicatorWrapper>
<SelectDragIndicator />
</SelectDragIndicatorWrapper>
<FlatList
style={{ width: '100%' }}
style={{ width: "100%" }}
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 }) => (
<SelectItem
label={item.label}
value={item.value}
/>
<SelectItem label={item.label} value={item.value} />
)}
/>
</SelectContent>
@@ -302,35 +324,43 @@ export default function Notices() {
</KeyboardAwareScrollView>
</ActionsheetContent>
</Actionsheet>
<Actionsheet isOpen={showSortSheet} onClose={() => setShowSortSheet(false)}>
<Actionsheet
isOpen={showSortSheet}
onClose={() => setShowSortSheet(false)}
>
<ActionsheetBackdrop />
<ActionsheetContent>
<ActionsheetDragIndicatorWrapper>
<ActionsheetDragIndicator />
</ActionsheetDragIndicatorWrapper>
<ActionsheetItem
className={ !params.sort ? 'bg-gray-200' : ''}
onPress={() => handleSort()}>
className={!params.sort ? "bg-gray-200" : ""}
onPress={() => handleSort()}
>
<ActionsheetItemText>Trafność</ActionsheetItemText>
</ActionsheetItem>
<ActionsheetItem
className={ params.sort == 'latest' ? 'bg-gray-200' : ''}
onPress={() => handleSort('latest')}>
className={params.sort == "latest" ? "bg-gray-200" : ""}
onPress={() => handleSort("latest")}
>
<ActionsheetItemText>Najnowsze</ActionsheetItemText>
</ActionsheetItem>
<ActionsheetItem
className={ params.sort == 'oldest' ? 'bg-gray-200' : ''}
onPress={() => handleSort('oldest')}>
className={params.sort == "oldest" ? "bg-gray-200" : ""}
onPress={() => handleSort("oldest")}
>
<ActionsheetItemText>Najstarsze</ActionsheetItemText>
</ActionsheetItem>
<ActionsheetItem
className={ params.sort == 'cheapest' ? 'bg-gray-200' : ''}
onPress={() => handleSort('cheapest')}>
className={params.sort == "cheapest" ? "bg-gray-200" : ""}
onPress={() => handleSort("cheapest")}
>
<ActionsheetItemText>Najtańsze</ActionsheetItemText>
</ActionsheetItem>
<ActionsheetItem
className={ params.sort == 'expensive' ? 'bg-gray-200' : ''}
onPress={() => handleSort('expensive')}>
className={params.sort == "expensive" ? "bg-gray-200" : ""}
onPress={() => handleSort("expensive")}
>
<ActionsheetItemText>Najdroższe</ActionsheetItemText>
</ActionsheetItem>
</ActionsheetContent>

View File

@@ -14,9 +14,13 @@ import {useEffect, useState} from "react";
export function NoticeCard({ notice }) {
const noticeId = notice?.noticeId;
const toggleNoticeInWishlist = useWishlist((state) => state.toggleNoticeInWishlist);
const toggleNoticeInWishlist = useWishlist(
(state) => state.toggleNoticeInWishlist
);
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);
@@ -40,7 +44,9 @@ export function NoticeCard({notice}) {
try {
const images = await getAllImagesByNoticeId(noticeId);
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) {
console.error(`Error while loading image: ${error}`);