import { FlatList, Text, ActivityIndicator } from "react-native";
import { listNotices } from "@/api/notices";
import { useQuery } from "@tanstack/react-query";
import { NoticeCard } from "@/components/NoticeCard";

export default function Notices() {
  const { data, isLoading, error } = useQuery({
    queryKey: ["notices"],
    queryFn: listNotices,
  });

  if (isLoading) {
    return <ActivityIndicator />;
  }

  if (error) {
    console.log(error.message);
    return <Text>Nie udało sie pobrać listy. {error.message}</Text>;
  }

  return (
    <FlatList
      key={2}
      data={data}
      numColumns={2}
      columnContainerClassName="m-2"
      columnWrapperClassName="gap-2 m-2"
      renderItem={({ item }) => <NoticeCard notice={item} />}
    />
  );
}
