Add, list, getbyid
working via zustand now + adding image from camera
This commit is contained in:
@@ -1,47 +1,65 @@
|
||||
import { FlatList, Text, ActivityIndicator, RefreshControl } from "react-native";
|
||||
import { useState } from "react";
|
||||
import { listNotices } from "@/api/notices";
|
||||
import { useQuery } from "@tanstack/react-query";
|
||||
import { NoticeCard } from "@/components/NoticeCard";
|
||||
import {FlatList, Text, ActivityIndicator, RefreshControl} from "react-native";
|
||||
import {useState, useEffect} from "react";
|
||||
import {useNoticesStore} from "@/store/noticesStore";
|
||||
import {NoticeCard} from "@/components/NoticeCard";
|
||||
|
||||
export default function Notices() {
|
||||
const [refreshing, setRefreshing] = useState(false);
|
||||
const { data, isLoading, error, refetch } = useQuery({
|
||||
queryKey: ["notices"],
|
||||
queryFn: listNotices,
|
||||
});
|
||||
const {notices, fetchNotices} = useNoticesStore();
|
||||
const [refreshing, setRefreshing] = useState(false);
|
||||
const [isLoading, setIsLoading] = useState(true);
|
||||
const [error, setError] = useState(null);
|
||||
|
||||
if (isLoading) {
|
||||
return <ActivityIndicator />;
|
||||
}
|
||||
useEffect(() => {
|
||||
loadData();
|
||||
}, []);
|
||||
|
||||
if (error) {
|
||||
console.log(error.message);
|
||||
return <Text>Nie udało sie pobrać listy. {error.message}</Text>;
|
||||
}
|
||||
const loadData = async () => {
|
||||
setIsLoading(true);
|
||||
try {
|
||||
await fetchNotices();
|
||||
setError(null);
|
||||
} catch (err) {
|
||||
setError(err);
|
||||
} finally {
|
||||
setIsLoading(false);
|
||||
}
|
||||
};
|
||||
|
||||
const onRefresh = async () => {
|
||||
setRefreshing(true);
|
||||
await refetch();
|
||||
setRefreshing(false);
|
||||
};
|
||||
const onRefresh = async () => {
|
||||
setRefreshing(true);
|
||||
try {
|
||||
await fetchNotices();
|
||||
} catch (err) {
|
||||
setError(err);
|
||||
} finally {
|
||||
setRefreshing(false);
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
<FlatList
|
||||
key={2}
|
||||
data={data}
|
||||
numColumns={2}
|
||||
columnContainerClassName="m-2"
|
||||
columnWrapperClassName="gap-2 m-2"
|
||||
renderItem={({ item }) => <NoticeCard notice={item} />}
|
||||
refreshControl={
|
||||
<RefreshControl
|
||||
refreshing={refreshing || isLoading}
|
||||
onRefresh={onRefresh}
|
||||
colors={["#3b82f6"]}
|
||||
tintColor="#3b82f6"
|
||||
if (isLoading && !refreshing) {
|
||||
return <ActivityIndicator/>;
|
||||
}
|
||||
|
||||
if (error) {
|
||||
return <Text>Nie udało sie pobrać listy. {error.message}</Text>;
|
||||
}
|
||||
|
||||
return (
|
||||
<FlatList
|
||||
key={2}
|
||||
data={notices}
|
||||
numColumns={2}
|
||||
columnContainerClassName="m-2"
|
||||
columnWrapperClassName="gap-2 m-2"
|
||||
renderItem={({item}) => <NoticeCard notice={item}/>}
|
||||
refreshControl={
|
||||
<RefreshControl
|
||||
refreshing={refreshing}
|
||||
onRefresh={onRefresh}
|
||||
colors={["#3b82f6"]}
|
||||
tintColor="#3b82f6"
|
||||
/>
|
||||
}
|
||||
/>
|
||||
}
|
||||
/>
|
||||
);
|
||||
}
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user