fix notice page
This commit is contained in:
@@ -7,20 +7,28 @@ import { Image } from "@/components/ui/image";
|
|||||||
import { Text } from "@/components/ui/text";
|
import { Text } from "@/components/ui/text";
|
||||||
import { VStack } from "@/components/ui/vstack";
|
import { VStack } from "@/components/ui/vstack";
|
||||||
import { Ionicons } from "@expo/vector-icons";
|
import { Ionicons } from "@expo/vector-icons";
|
||||||
|
import {
|
||||||
|
Avatar,
|
||||||
|
AvatarImage,
|
||||||
|
AvatarFallbackText,
|
||||||
|
} from "@/components/ui/avatar";
|
||||||
import {
|
import {
|
||||||
ActivityIndicator,
|
ActivityIndicator,
|
||||||
Dimensions,
|
Dimensions,
|
||||||
FlatList,
|
FlatList,
|
||||||
View,
|
View,
|
||||||
TextInput,
|
TextInput,
|
||||||
|
SafeAreaView,
|
||||||
|
Alert,
|
||||||
} from "react-native";
|
} from "react-native";
|
||||||
import { useEffect, useState, useRef } from "react";
|
import { useEffect, useState, useRef } from "react";
|
||||||
import { useNoticesStore } from "@/store/noticesStore";
|
import { useNoticesStore } from "@/store/noticesStore";
|
||||||
import { useWishlist } from "@/store/wishlistStore";
|
import { useWishlist } from "@/store/wishlistStore";
|
||||||
import { Pressable, ScrollView } from "react-native";
|
import { Pressable, ScrollView } from "react-native";
|
||||||
import { getUserById } from "@/api/client";
|
import { getUserById } from "@/api/client";
|
||||||
|
import * as ScreenOrientation from "expo-screen-orientation";
|
||||||
const { width } = Dimensions.get("window");
|
import { useAuthStore } from "@/store/authStore";
|
||||||
|
import { sendEmail } from "@/api/email";
|
||||||
|
|
||||||
export default function NoticeDetails() {
|
export default function NoticeDetails() {
|
||||||
const { id } = useLocalSearchParams();
|
const { id } = useLocalSearchParams();
|
||||||
@@ -31,20 +39,85 @@ export default function NoticeDetails() {
|
|||||||
const [notice, setNotice] = useState(null);
|
const [notice, setNotice] = useState(null);
|
||||||
const [user, setUser] = useState(null);
|
const [user, setUser] = useState(null);
|
||||||
const [isUserLoading, setIsUserLoading] = useState(true);
|
const [isUserLoading, setIsUserLoading] = useState(true);
|
||||||
|
const [isLandscape, setIsLandscape] = useState(false);
|
||||||
const flatListRef = useRef(null);
|
const flatListRef = useRef(null);
|
||||||
const [currentIndex, setCurrentIndex] = useState(0);
|
const [currentIndex, setCurrentIndex] = useState(0);
|
||||||
|
|
||||||
const [isMessageFormVisible, setIsMessageFormVisible] = useState(false);
|
const [isMessageFormVisible, setIsMessageFormVisible] = useState(false);
|
||||||
const [message, setMessage] = useState("");
|
const [message, setMessage] = useState("");
|
||||||
const [Email, setEmail] = useState("");
|
const [Email, setEmail] = useState("");
|
||||||
const handleSendMessage = () => {
|
const [isSending, setIsSending] = useState(false);
|
||||||
console.log("Wiadomość do:", user?.email);
|
|
||||||
console.log("Email nadawcy:", Email);
|
|
||||||
console.log("Treść:", message);
|
|
||||||
|
|
||||||
setIsMessageFormVisible(false);
|
const handleSendMessage = async () => {
|
||||||
setMessage("");
|
setIsSending(true);
|
||||||
setEmail("");
|
console.log("Rozpoczynanie procesu wysyłania wiadomości...");
|
||||||
|
|
||||||
|
const { user_id, token } = useAuthStore.getState();
|
||||||
|
console.log("Dane z authStore:", { user_id, token });
|
||||||
|
|
||||||
|
if (!user_id || !token) {
|
||||||
|
console.error("Brak danych zalogowanego użytkownika.");
|
||||||
|
Alert.alert("Błąd", "Zaloguj się, aby wysłać wiadomość.");
|
||||||
|
setIsSending(false);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
let currentUserEmail = "";
|
||||||
|
try {
|
||||||
|
console.log(`Pobieranie danych użytkownika dla user_id : $ { user_id }`);
|
||||||
|
const currentUser = await getUserById(user_id);
|
||||||
|
console.log("Dane zalogowanego użytkownika:", currentUser);
|
||||||
|
currentUserEmail = currentUser?.email;
|
||||||
|
if (!currentUserEmail) {
|
||||||
|
console.error("Nie znaleziono adresu email zalogowanego użytkownika.");
|
||||||
|
Alert.alert(
|
||||||
|
"Błąd",
|
||||||
|
"Nie znaleziono adresu email zalogowanego użytkownika."
|
||||||
|
);
|
||||||
|
setIsSending(false);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
console.log(`Pobrano email zalogowanego użytkownika
|
||||||
|
: $ { currentUserEmail }`);
|
||||||
|
} catch (error) {
|
||||||
|
console.error("Błąd podczas pobierania danych użytkownika:", error);
|
||||||
|
Alert.alert(
|
||||||
|
"Błąd",
|
||||||
|
"Nie udało się pobrać danych użytkownika. Spróbuj ponownie później."
|
||||||
|
);
|
||||||
|
setIsSending(false);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const emailData = {
|
||||||
|
to: user?.email || "",
|
||||||
|
subject: `Zapytanie ${currentUserEmail} o ogłoszenie ${notice.title}`,
|
||||||
|
body: message,
|
||||||
|
};
|
||||||
|
console.log("Dane emaila do wysyłki:", emailData);
|
||||||
|
|
||||||
|
if (!emailData.to || !emailData.subject || !emailData.body) {
|
||||||
|
console.error("Walidacja nieudana: brakujące pola w emailData.");
|
||||||
|
Alert.alert("Błąd", "Wszystkie pola są wymagane!");
|
||||||
|
setIsSending(false);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const result = await sendEmail(emailData);
|
||||||
|
if (result.success) {
|
||||||
|
console.log("Wiadomość wysłana pomyślnie!", result.result);
|
||||||
|
setIsMessageFormVisible(false);
|
||||||
|
setMessage("");
|
||||||
|
Alert.alert("Sukces", "Wiadomość została wysłana!");
|
||||||
|
} else {
|
||||||
|
console.error("Błąd podczas wysyłania wiadomości:", result.error);
|
||||||
|
Alert.alert(
|
||||||
|
"Błąd",
|
||||||
|
`Nie udało się wysłać wiadomości
|
||||||
|
: $ { result.error }`
|
||||||
|
);
|
||||||
|
}
|
||||||
|
setIsSending(false);
|
||||||
|
console.log("Zakończono proces wysyłania wiadomości.");
|
||||||
};
|
};
|
||||||
|
|
||||||
const formatDate = (dateString) => {
|
const formatDate = (dateString) => {
|
||||||
@@ -74,6 +147,51 @@ export default function NoticeDetails() {
|
|||||||
itemVisiblePercentThreshold: 70,
|
itemVisiblePercentThreshold: 70,
|
||||||
}).current;
|
}).current;
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
const unlockOrientation = async () => {
|
||||||
|
try {
|
||||||
|
await ScreenOrientation.unlockAsync();
|
||||||
|
} catch (err) {
|
||||||
|
console.error("Error unlocking orientation:", err);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const getInitialOrientation = async () => {
|
||||||
|
try {
|
||||||
|
const orientation = await ScreenOrientation.getOrientationAsync();
|
||||||
|
setIsLandscape(
|
||||||
|
orientation === ScreenOrientation.Orientation.LANDSCAPE_LEFT ||
|
||||||
|
orientation === ScreenOrientation.Orientation.LANDSCAPE_RIGHT
|
||||||
|
);
|
||||||
|
} catch (err) {
|
||||||
|
console.error("Error getting initial orientation:", err);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
unlockOrientation();
|
||||||
|
getInitialOrientation();
|
||||||
|
|
||||||
|
const subscription = ScreenOrientation.addOrientationChangeListener(
|
||||||
|
({ orientationInfo }) => {
|
||||||
|
const isLandscapeMode =
|
||||||
|
orientationInfo.orientation ===
|
||||||
|
ScreenOrientation.Orientation.LANDSCAPE_LEFT ||
|
||||||
|
orientationInfo.orientation ===
|
||||||
|
ScreenOrientation.Orientation.LANDSCAPE_RIGHT;
|
||||||
|
setIsLandscape(isLandscapeMode);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
return () => {
|
||||||
|
ScreenOrientation.removeOrientationChangeListener(subscription);
|
||||||
|
ScreenOrientation.lockAsync(
|
||||||
|
ScreenOrientation.OrientationLock.PORTRAIT_UP
|
||||||
|
).catch((err) =>
|
||||||
|
console.error("Error locking orientation on unmount:", err)
|
||||||
|
);
|
||||||
|
};
|
||||||
|
}, []);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const fetchNotice = async () => {
|
const fetchNotice = async () => {
|
||||||
setIsLoading(true);
|
setIsLoading(true);
|
||||||
@@ -167,7 +285,6 @@ export default function NoticeDetails() {
|
|||||||
ref={flatListRef}
|
ref={flatListRef}
|
||||||
data={images}
|
data={images}
|
||||||
horizontal
|
horizontal
|
||||||
snapToInterval={width}
|
|
||||||
snapToAlignment="start"
|
snapToAlignment="start"
|
||||||
decelerationRate="fast"
|
decelerationRate="fast"
|
||||||
showsHorizontalScrollIndicator={false}
|
showsHorizontalScrollIndicator={false}
|
||||||
@@ -175,12 +292,15 @@ export default function NoticeDetails() {
|
|||||||
onViewableItemsChanged={onViewableItemsChanged}
|
onViewableItemsChanged={onViewableItemsChanged}
|
||||||
viewabilityConfig={viewabilityConfig}
|
viewabilityConfig={viewabilityConfig}
|
||||||
renderItem={({ item, index }) => (
|
renderItem={({ item, index }) => (
|
||||||
<View style={{ width: width }} className="p-1">
|
<View
|
||||||
|
style={{ width: Dimensions.get("window").width }}
|
||||||
|
className="p-1"
|
||||||
|
>
|
||||||
<Image
|
<Image
|
||||||
source={item}
|
source={item}
|
||||||
className="h-auto w-auto rounded-md aspect-[1/1]"
|
className="h-auto w-auto rounded-md aspect-[1/1]"
|
||||||
alt={`Zdjęcie ${index + 1}`}
|
alt={`Zdjęcie ${index + 1}`}
|
||||||
resizeMode="cover"
|
resizeMode={isLandscape ? "cover" : "contain"}
|
||||||
/>
|
/>
|
||||||
</View>
|
</View>
|
||||||
)}
|
)}
|
||||||
@@ -376,9 +496,14 @@ export default function NoticeDetails() {
|
|||||||
|
|
||||||
<Pressable
|
<Pressable
|
||||||
onPress={handleSendMessage}
|
onPress={handleSendMessage}
|
||||||
className="bg-primary-500 py-2 px-4 rounded-md"
|
className="bg-blue-500 py-2 px-4 rounded-md"
|
||||||
|
disabled={isSending}
|
||||||
>
|
>
|
||||||
<Text className="text-white">Wyślij</Text>
|
{isSending ? (
|
||||||
|
<ActivityIndicator color="#fff" />
|
||||||
|
) : (
|
||||||
|
<Text className="text-white">Wyślij</Text>
|
||||||
|
)}
|
||||||
</Pressable>
|
</Pressable>
|
||||||
</View>
|
</View>
|
||||||
</ScrollView>
|
</ScrollView>
|
||||||
|
|||||||
Reference in New Issue
Block a user