add edit notice and clean code
This commit is contained in:
@@ -31,7 +31,7 @@ import { ChevronDownIcon } from "@/components/ui/icon";
|
||||
import { useNoticesStore } from "@/store/noticesStore";
|
||||
import { listCategories } from "@/api/categories";
|
||||
import { useRouter } from "expo-router";
|
||||
import { attributes } from "@/data/attributesData"; // Assuming you have a separate file for attributes data}
|
||||
import { attributes } from "@/data/attributesData";
|
||||
import { useLocalSearchParams } from "expo-router";
|
||||
|
||||
export default function EditNotice() {
|
||||
@@ -43,7 +43,6 @@ export default function EditNotice() {
|
||||
const [price, setPrice] = useState("");
|
||||
const [category, setCategory] = useState("");
|
||||
const [image, setImage] = useState([]);
|
||||
const [images, setImages] = useState([]);
|
||||
const [isImageLoading, setIsImageLoading] = useState(true);
|
||||
const [selectItems, setSelectItems] = useState([]);
|
||||
const [isLoading, setIsLoading] = useState(false);
|
||||
@@ -84,7 +83,6 @@ export default function EditNotice() {
|
||||
attributesObj[attr.name] = attr.value;
|
||||
});
|
||||
setSelectedAttributes(attributesObj);
|
||||
// console.log("Attributes loaded:", attributesObj);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -92,20 +90,13 @@ export default function EditNotice() {
|
||||
setIsImageLoading(true);
|
||||
try {
|
||||
const fetchedImages = await getAllImagesByNoticeId(notice.noticeId);
|
||||
console.log("Fetched images:", fetchedImages);
|
||||
|
||||
if (fetchedImages && fetchedImages.length > 0) {
|
||||
const imageUris = fetchedImages.map((img) => img.uri);
|
||||
setImages(fetchedImages);
|
||||
setImage(imageUris);
|
||||
console.log("Image URIs set:", imageUris);
|
||||
setImage(fetchedImages);
|
||||
} else {
|
||||
setImages([]);
|
||||
setImage([]);
|
||||
}
|
||||
} catch (err) {
|
||||
console.error("Error while loading images:", err);
|
||||
setImages([]);
|
||||
setImage([]);
|
||||
} finally {
|
||||
setIsImageLoading(false);
|
||||
@@ -115,7 +106,7 @@ export default function EditNotice() {
|
||||
if (notice) {
|
||||
fetchImage();
|
||||
}
|
||||
}, [notices]);
|
||||
}, [notices, id]);
|
||||
|
||||
const [error, setError] = useState({
|
||||
title: false,
|
||||
@@ -154,10 +145,9 @@ export default function EditNotice() {
|
||||
value: value,
|
||||
})
|
||||
);
|
||||
// console.log("Selected attributes:", formattedAttributes);
|
||||
setIsLoading(true);
|
||||
try {
|
||||
const result = await editNotice({
|
||||
const result = await editNotice(id, {
|
||||
title: title,
|
||||
description: description,
|
||||
price: price,
|
||||
@@ -168,14 +158,12 @@ export default function EditNotice() {
|
||||
});
|
||||
|
||||
if (result) {
|
||||
console.log("Notice created successfully with ID: ", result.noticeId);
|
||||
await fetchNotices();
|
||||
clearForm();
|
||||
|
||||
router.push("/(tabs)/dashboard/userNotices");
|
||||
}
|
||||
} catch (error) {
|
||||
console.error("Error creating notice. Error message: ", error.message);
|
||||
console.error("Error editing notice. Error message: ", error.message);
|
||||
} finally {
|
||||
setIsLoading(false);
|
||||
}
|
||||
@@ -210,27 +198,12 @@ export default function EditNotice() {
|
||||
}
|
||||
};
|
||||
|
||||
const clearForm = () => {
|
||||
setTitle("");
|
||||
setDescription("");
|
||||
setPrice("");
|
||||
setCategory("");
|
||||
setImage([]);
|
||||
setSelectedAttributes({});
|
||||
setError({
|
||||
title: false,
|
||||
description: false,
|
||||
price: false,
|
||||
category: false,
|
||||
});
|
||||
};
|
||||
|
||||
if (isLoading) {
|
||||
return (
|
||||
<Box className="items-center justify-center flex-1">
|
||||
<ActivityIndicator size="large" color="#787878" />
|
||||
<Text size="md" bold="true" className="mt-5">
|
||||
Dodajemy ogłoszenie...
|
||||
Edytuj ogłoszenie...
|
||||
</Text>
|
||||
</Box>
|
||||
);
|
||||
@@ -258,14 +231,22 @@ export default function EditNotice() {
|
||||
</Text>
|
||||
{image && image.length > 0 && (
|
||||
<VStack space="xs" className="flex-row flex-wrap">
|
||||
{image.map((img, index) => (
|
||||
<Image
|
||||
key={index}
|
||||
source={{ uri: img }}
|
||||
style={styles.image}
|
||||
className="m-1"
|
||||
/>
|
||||
))}
|
||||
{image.map((img, index) => {
|
||||
const imageSource =
|
||||
typeof img === "string" ? { uri: img } : img;
|
||||
|
||||
return (
|
||||
<Image
|
||||
key={index}
|
||||
source={imageSource}
|
||||
style={styles.image}
|
||||
className="m-1"
|
||||
onError={(error) =>
|
||||
console.log(`Image ${index} error:`, error)
|
||||
}
|
||||
/>
|
||||
);
|
||||
})}
|
||||
</VStack>
|
||||
)}
|
||||
</VStack>
|
||||
@@ -381,7 +362,7 @@ export default function EditNotice() {
|
||||
onPress={handleEditNotice}
|
||||
disabled={isLoading}
|
||||
>
|
||||
<ButtonText className="text-typography-0">Dodaj</ButtonText>
|
||||
<ButtonText className="text-typography-0">Edytuj</ButtonText>
|
||||
</Button>
|
||||
</VStack>
|
||||
</FormControl>
|
||||
|
||||
Reference in New Issue
Block a user