Mail sender is working
This commit is contained in:
30
ArtisanConnect/api/email.jsx
Normal file
30
ArtisanConnect/api/email.jsx
Normal 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 };
|
||||||
|
}
|
||||||
|
};
|
||||||
@@ -13,7 +13,7 @@ import {
|
|||||||
FlatList,
|
FlatList,
|
||||||
View,
|
View,
|
||||||
TextInput,
|
TextInput,
|
||||||
SafeAreaView,
|
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";
|
||||||
@@ -21,6 +21,8 @@ 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";
|
import * as ScreenOrientation from "expo-screen-orientation";
|
||||||
|
import { useAuthStore } from "@/store/authStore";
|
||||||
|
import { sendEmail } from "@/api/email";
|
||||||
|
|
||||||
export default function NoticeDetails() {
|
export default function NoticeDetails() {
|
||||||
const { id } = useLocalSearchParams();
|
const { id } = useLocalSearchParams();
|
||||||
@@ -36,16 +38,69 @@ export default function NoticeDetails() {
|
|||||||
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 [isSending, setIsSending] = useState(false);
|
||||||
|
|
||||||
const handleSendMessage = () => {
|
|
||||||
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) => {
|
||||||
@@ -358,13 +413,10 @@ export default function NoticeDetails() {
|
|||||||
<Text className="bg-gray-100 p-3 rounded text-gray-500">
|
<Text className="bg-gray-100 p-3 rounded text-gray-500">
|
||||||
{user?.email || "Brak adresu e-mail"}
|
{user?.email || "Brak adresu e-mail"}
|
||||||
</Text>
|
</Text>
|
||||||
<Text className="text-sm font-medium mb-1">Twój e-mail:</Text>
|
<Text className="text-sm font-medium mb-1">Temat:</Text>
|
||||||
<TextInput
|
<Text className="bg-gray-100 p-3 rounded text-gray-500">
|
||||||
className="border border-gray-300 p-2 rounded mb-4"
|
Zapytanie o ogłoszenie '{notice.title || "Brak nazwy ogłoszenia"}'
|
||||||
placeholder="Wpisz swój adres e-mail"
|
</Text>
|
||||||
value={Email}
|
|
||||||
onChangeText={setEmail}
|
|
||||||
/>
|
|
||||||
<Text className="text-sm font-medium mb-1">Treść:</Text>
|
<Text className="text-sm font-medium mb-1">Treść:</Text>
|
||||||
<TextInput
|
<TextInput
|
||||||
className="border border-gray-300 rounded-md p-2 mb-4 h-32 text-left"
|
className="border border-gray-300 rounded-md p-2 mb-4 h-32 text-left"
|
||||||
@@ -384,8 +436,13 @@ export default function NoticeDetails() {
|
|||||||
<Pressable
|
<Pressable
|
||||||
onPress={handleSendMessage}
|
onPress={handleSendMessage}
|
||||||
className="bg-blue-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>
|
||||||
</View>
|
</View>
|
||||||
|
|||||||
Reference in New Issue
Block a user