Add, list, getbyid

working via zustand now + adding image from camera
This commit is contained in:
2025-05-20 15:19:29 +02:00
parent 0295386dac
commit 532183b305
10 changed files with 1199 additions and 865 deletions

View File

@@ -21,17 +21,20 @@ import {
} from "@/components/ui/select";
import {ChevronDownIcon} from "@/components/ui/icon";
import {useMutation} from "@tanstack/react-query";
import {createNotice} from "@/api/notices";
import {useNoticesStore} from "@/store/noticesStore";
import {listCategories} from "@/api/categories";
import {useRouter} from "expo-router";
export default function CreateNotice() {
const router = useRouter();
const {addNotice} = useNoticesStore();
const [title, setTitle] = useState("");
const [description, setDescription] = useState("");
const [price, setPrice] = useState("");
const [category, setCategory] = useState("");
const [image, setImage] = useState([]);
const [selectItems, setSelectItems] = useState([]);
const [isLoading, setIsLoading] = useState(false);
useEffect(() => {
let isMounted = true;
@@ -73,26 +76,7 @@ export default function CreateNotice() {
},
});
const noticeMutation = useMutation({
mutationFn: () =>
createNotice({
title: title,
clientId: 1,
description: description,
price: parseFloat(price),
category: category,
status: "ACTIVE",
image: image,
}),
onSuccess: () => {
console.log("Notice created successfully");
},
onError: (error) => {
console.error("Error creating notice. Erroe message: ", error.message);
},
});
const addNotice = () => {
const handleAddNotice = async () => {
setError({
title: !title,
description: !description,
@@ -104,13 +88,47 @@ export default function CreateNotice() {
console.log("Error in form");
return;
}
noticeMutation.mutate();
setIsLoading(true);
try {
const result = await addNotice({
title: title,
clientId: 1,
description: description,
price: price,
category: category,
status: "ACTIVE",
image: image
});
if (result) {
console.log("Notice created successfully with ID: ", result.noticeId);
router.push("/(tabs)/notices");
}
} catch (error) {
console.error("Error creating notice. Error message: ", error.message);
} finally {
setIsLoading(false);
}
};
const takePicture = async () => {
const {status} = await ImagePicker.requestCameraPermissionsAsync();
if (status !== 'granted') {
return;
}
const result = await ImagePicker.launchCameraAsync({
allowsEditing: false,
});
if (!result.canceled && result.assets) {
setImage(result.assets.map(asset => asset.uri));
}
}
const pickImage = async () => {
// No permissions request is necessary for launching the image library
let result = await ImagePicker.launchImageLibraryAsync({
mediaTypes: ['images'],
mediaTypes: 'images',
selectionLimit: 8,
allowsEditing: false,
allowsMultipleSelection: true,
@@ -119,7 +137,6 @@ export default function CreateNotice() {
});
if (!result.canceled) {
// await uploadImage(1, result.assets[0].uri);
setImage(result.assets.map(asset => asset.uri));
}
};
@@ -135,10 +152,13 @@ export default function CreateNotice() {
Wybierz zdjęcia
</ButtonText>
</Button>
<Text size="sm"
bold="true"
>
Pierwsze zdjęcie będzie zdjęciem głównym</Text>
<Button onPress={takePicture}>
<ButtonText>Zrób zdjęcie</ButtonText>
</Button>
<Text size="sm"
bold="true"
>
Pierwsze zdjęcie będzie zdjęciem głównym</Text>
{image && image.length > 0 && (
<VStack space="xs" className="flex-row flex-wrap">
{image.map((img, index) => (
@@ -208,8 +228,8 @@ export default function CreateNotice() {
</VStack>
<Button
className="mt-5 w-full"
onPress={() => addNotice()}
disabled={noticeMutation.isLoading}
onPress={handleAddNotice}
disabled={isLoading}
>
<ButtonText className="text-typography-0">Dodaj</ButtonText>
</Button>
@@ -217,4 +237,4 @@ export default function CreateNotice() {
</FormControl>
</ScrollView>
);
}
}