fix login check and urls

This commit is contained in:
Patryk
2025-06-07 23:08:21 +02:00
parent 472dcfc96a
commit c19333ad8b
10 changed files with 424 additions and 374 deletions

View File

@@ -1,32 +1,25 @@
import { View, FlatList} from 'react-native';
import { useEffect, useState } from 'react'
import { View, FlatList } from "react-native";
import { useEffect, useState } from "react";
import { useAuthStore } from "@/store/authStore";
import { Heading } from '@/components/ui/heading';
import { Text } from '@/components/ui/text';
import { Link } from 'expo-router';
import { Pressable } from '@/components/ui/pressable';
// import axios from 'axios';
import {listCategories} from "@/api/categories";
import { Heading } from "@/components/ui/heading";
import { Text } from "@/components/ui/text";
import { Link } from "expo-router";
import { Pressable } from "@/components/ui/pressable";
import { listCategories } from "@/api/categories";
export function CategorySection({notices, title}) {
const token = useAuthStore((state) => state.token);
export function CategorySection({ notices, title }) {
const [categoryMap, setCategoryMap] = useState({});
useEffect(() => {
if(token){
const fetchCategories = async () => {
let data = await listCategories();
if (Array.isArray(data)) {
setCategoryMap(data);
}
}
let data = await listCategories();
if (Array.isArray(data)) {
setCategoryMap(data);
}
};
fetchCategories();
}
}, [token]);
});
const categories = Array.from(
new Set(notices.map((notice) => notice.category))
@@ -35,13 +28,11 @@ export function CategorySection({notices, title}) {
const getCount = (category) =>
notices.filter((notice) => notice.category === category).length;
console.log("CategoryMap:", categoryMap);
if(!categoryMap) {
return null;
if (!categoryMap || Object.keys(categoryMap).length === 0) {
return null;
}
return (
return (
<View className="mb-6">
<Heading className="text-2xl font-bold mb-4 mt-4">{title}</Heading>
<FlatList
@@ -54,16 +45,15 @@ return (
const categoryObj = categoryMap.find((cat) => cat.value === item);
return (
<Link href={`/notices?category=${item}`} asChild>
<Pressable className="bg-gray-200 p-4 rounded-lg mr-2">
<Text>
{categoryObj ? categoryObj.label : item} ({getCount(item)})
</Text>
</Pressable>
<Pressable className="bg-gray-200 p-4 rounded-lg mr-2">
<Text>
{categoryObj ? categoryObj.label : item} ({getCount(item)})
</Text>
</Pressable>
</Link>
);
}}
/>
</View>
);
}
}

View File

@@ -1,27 +1,27 @@
import { View} from 'react-native';
import { useEffect, useState } from 'react'
import { Heading } from '@/components/ui/heading';
import { FlatList } from 'react-native';
import axios from 'axios';
import UserBlock from '@/components/UserBlock';
import {useAuthStore} from "@/store/authStore";
import { View } from "react-native";
import { useEffect, useState } from "react";
import { Heading } from "@/components/ui/heading";
import { FlatList } from "react-native";
import axios from "axios";
import UserBlock from "@/components/UserBlock";
import { useAuthStore } from "@/store/authStore";
export function UserSection({notices, title}) {
const token = useAuthStore((state) => state.token);
const headers = token ? { 'Authorization': `Bearer ${token}` } : {};
const [users, setUsers] = useState([]);
export function UserSection({ notices, title }) {
const token = useAuthStore((state) => state.token);
const headers = token ? { Authorization: `Bearer ${token}` } : {};
const [users, setUsers] = useState([]);
useEffect(() => {
if (token){
axios.get('https://testowe.zikor.pl/api/v1/clients/get/all', { headers })
.then(res => setUsers(res.data))
.catch(() => setUsers([]));
if (token) {
axios
.get("https://hopp.zikor.pl/api/v1/clients/get/all", { headers })
.then((res) => setUsers(res.data))
.catch(() => setUsers([]));
}
}, [token]);
const usersWithNoticeCount = users.map(user => {
const count = notices.filter(n => n.clientId === user.id).length;
const usersWithNoticeCount = users.map((user) => {
const count = notices.filter((n) => n.clientId === user.id).length;
return { ...user, noticeCount: count };
});
@@ -29,7 +29,7 @@ export function UserSection({notices, title}) {
.sort((a, b) => b.noticeCount - a.noticeCount)
.slice(0, 5);
return (
return (
<View className="mb-6">
<Heading className="text-2xl font-bold mb-4 mt-4">{title}</Heading>
<FlatList
@@ -38,12 +38,9 @@ return (
showsHorizontalScrollIndicator={false}
contentContainerStyle={{ paddingHorizontal: 8, gap: 12 }}
renderItem={({ item }) => {
return (
<UserBlock user={item} />
);
return <UserBlock user={item} />;
}}
/>
</View>
);
}
}