Mail sender is working

This commit is contained in:
2025-06-09 20:38:13 +02:00
parent 7fc1312ddc
commit 14bd178f84
2 changed files with 104 additions and 17 deletions

View File

@@ -0,0 +1,30 @@
import { useAuthStore } from "@/store/authStore";
const API_URL = "https://hopp.zikor.pl/api/v1";
export const sendEmail = async (emailData) => {
const token = useAuthStore.getState().token;
try {
const response = await fetch(`${API_URL}/email/send`, {
method: "POST",
headers: {
"Content-Type": "application/json",
...(token && { Authorization: `Bearer ${token}` }),
},
body: JSON.stringify(emailData),
});
if (!response.ok) {
const errorMessage = `HTTP error! Status: ${response.status}`;
console.error("Error sending email:", errorMessage);
return { success: false, error: errorMessage };
}
const result = await response.text();
return { success: true, result };
} catch (error) {
console.error("Error sending email:", error.message);
return { success: false, error: error.message };
}
};

View File

@@ -13,7 +13,7 @@ import {
FlatList,
View,
TextInput,
SafeAreaView,
SafeAreaView, Alert,
} from "react-native";
import { useEffect, useState, useRef } from "react";
import { useNoticesStore } from "@/store/noticesStore";
@@ -21,6 +21,8 @@ import { useWishlist } from "@/store/wishlistStore";
import { Pressable, ScrollView } from "react-native";
import { getUserById } from "@/api/client";
import * as ScreenOrientation from "expo-screen-orientation";
import { useAuthStore } from "@/store/authStore";
import { sendEmail } from "@/api/email";
export default function NoticeDetails() {
const { id } = useLocalSearchParams();
@@ -36,16 +38,69 @@ export default function NoticeDetails() {
const [currentIndex, setCurrentIndex] = useState(0);
const [isMessageFormVisible, setIsMessageFormVisible] = useState(false);
const [message, setMessage] = useState("");
const [Email, setEmail] = useState("");
const [isSending, setIsSending] = useState(false);
const handleSendMessage = () => {
console.log("Wiadomość do:", user?.email);
console.log("Email nadawcy:", Email);
console.log("Treść:", message);
setIsMessageFormVisible(false);
setMessage("");
setEmail("");
const handleSendMessage = async () => {
setIsSending(true);
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) => {
@@ -358,13 +413,10 @@ export default function NoticeDetails() {
<Text className="bg-gray-100 p-3 rounded text-gray-500">
{user?.email || "Brak adresu e-mail"}
</Text>
<Text className="text-sm font-medium mb-1">Twój e-mail:</Text>
<TextInput
className="border border-gray-300 p-2 rounded mb-4"
placeholder="Wpisz swój adres e-mail"
value={Email}
onChangeText={setEmail}
/>
<Text className="text-sm font-medium mb-1">Temat:</Text>
<Text className="bg-gray-100 p-3 rounded text-gray-500">
Zapytanie o ogłoszenie '{notice.title || "Brak nazwy ogłoszenia"}'
</Text>
<Text className="text-sm font-medium mb-1">Treść:</Text>
<TextInput
className="border border-gray-300 rounded-md p-2 mb-4 h-32 text-left"
@@ -384,8 +436,13 @@ export default function NoticeDetails() {
<Pressable
onPress={handleSendMessage}
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>
</View>
</View>