diff --git a/ArtisanConnect/api/email.jsx b/ArtisanConnect/api/email.jsx
new file mode 100644
index 0000000..3de50a5
--- /dev/null
+++ b/ArtisanConnect/api/email.jsx
@@ -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 };
+ }
+};
\ No newline at end of file
diff --git a/ArtisanConnect/app/notice/[id].jsx b/ArtisanConnect/app/notice/[id].jsx
index e1b6e49..678605c 100644
--- a/ArtisanConnect/app/notice/[id].jsx
+++ b/ArtisanConnect/app/notice/[id].jsx
@@ -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() {
{user?.email || "Brak adresu e-mail"}
- Twój e-mail:
-
+ Temat:
+
+ Zapytanie o ogłoszenie '{notice.title || "Brak nazwy ogłoszenia"}'
+
Treść:
- Wyślij
+ {isSending ? (
+
+ ) : (
+ Wyślij
+ )}