Compare commits
12 Commits
fd1c387cdb
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| 2d0f5b9e56 | |||
| dd73dc070d | |||
| 7efe9d91c3 | |||
| 67cf21230d | |||
| 945d225a9f | |||
|
|
0790285ae5 | ||
|
|
e1672ab319 | ||
|
|
2630b35afd | ||
|
|
3c042d2cfb | ||
| b323f02654 | |||
| 59db79eaf7 | |||
| 1d2a2420c2 |
@@ -0,0 +1,63 @@
|
|||||||
|
import axios from "axios";
|
||||||
|
|
||||||
|
const API_URL = "https://hopp.zikor.pl/api/v1";
|
||||||
|
|
||||||
|
export async function login(userData) {
|
||||||
|
try {
|
||||||
|
const response = await axios.post(`${API_URL}/auth/login`, userData, {
|
||||||
|
headers: {"Content-Type": "application/json"},
|
||||||
|
});
|
||||||
|
return response.data;
|
||||||
|
} catch (error) {
|
||||||
|
console.error("Login failed:", error);
|
||||||
|
throw error.response?.data?.message || "Login failed";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function register(userData) {
|
||||||
|
try {
|
||||||
|
const response = await axios.post(`${API_URL}/auth/register`, userData, {
|
||||||
|
headers: {"Content-Type": "application/json"},
|
||||||
|
});
|
||||||
|
|
||||||
|
return response.data;
|
||||||
|
} catch (error) {
|
||||||
|
console.error("Registration failed:", error);
|
||||||
|
throw error.response?.data?.message || "Registration failed";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function googleLogin(googleToken) {
|
||||||
|
try {
|
||||||
|
const response = await axios.post(
|
||||||
|
`${API_URL}/auth/google`,
|
||||||
|
{ googleToken: googleToken },
|
||||||
|
{
|
||||||
|
headers: { "Content-Type": "application/json" },
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
return response.data;
|
||||||
|
} catch (error) {
|
||||||
|
console.error("Google login failed:", error);
|
||||||
|
throw error.response?.data?.message || "Google login failed";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function logout(token) {
|
||||||
|
const headers = token ? { Authorization: `Bearer ${token}` } : {};
|
||||||
|
try {
|
||||||
|
const response = await axios.post(
|
||||||
|
`${API_URL}/auth/logout`,
|
||||||
|
{},
|
||||||
|
{
|
||||||
|
headers: headers,
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
return response.data;
|
||||||
|
} catch (error) {
|
||||||
|
console.error("Logout failed:", error);
|
||||||
|
throw error.response?.data?.message || "Logout failed";
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -12,10 +12,28 @@ export async function getUserById(userId) {
|
|||||||
});
|
});
|
||||||
return response.data;
|
return response.data;
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
// console.error(
|
console.error(
|
||||||
// `Nie udało się pobrać danych użytkownika o ID ${userId}.`,
|
`Nie udało się pobrać danych użytkownika o ID ${userId}.`,
|
||||||
// err.response.status
|
err.response.status
|
||||||
// );
|
);
|
||||||
|
throw err;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function getAllUsers() {
|
||||||
|
const { token } = useAuthStore.getState();
|
||||||
|
const headers = token ? { Authorization: `Bearer ${token}` } : {};
|
||||||
|
|
||||||
|
try {
|
||||||
|
const response = await axios.get(`${API_URL}/clients/get/all`, {
|
||||||
|
headers: headers,
|
||||||
|
});
|
||||||
|
return response.data;
|
||||||
|
} catch (err) {
|
||||||
|
console.error(
|
||||||
|
`Nie udało się pobrać danych o użytkownikach`,
|
||||||
|
err.response.status
|
||||||
|
);
|
||||||
throw err;
|
throw err;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,8 +2,6 @@ import axios from "axios";
|
|||||||
import FormData from "form-data";
|
import FormData from "form-data";
|
||||||
import { useAuthStore } from "@/store/authStore";
|
import { useAuthStore } from "@/store/authStore";
|
||||||
|
|
||||||
// const API_URL = "https://hopp.zikor.pl/api/v1";
|
|
||||||
|
|
||||||
const API_URL = "https://hopp.zikor.pl/api/v1";
|
const API_URL = "https://hopp.zikor.pl/api/v1";
|
||||||
|
|
||||||
export async function listNotices() {
|
export async function listNotices() {
|
||||||
@@ -41,8 +39,13 @@ export async function createNotice(notice) {
|
|||||||
});
|
});
|
||||||
|
|
||||||
if (response.data.noticeId !== null) {
|
if (response.data.noticeId !== null) {
|
||||||
for (const imageUri of notice.image) {
|
for (const image of notice.image) {
|
||||||
await uploadImage(response.data.noticeId, imageUri);
|
if (notice.image.indexOf(image) === 0) {
|
||||||
|
await uploadImage(response.data.noticeId, image, true);
|
||||||
|
} else {
|
||||||
|
await uploadImage(response.data.noticeId, image, false);
|
||||||
|
}
|
||||||
|
console.log("Image uploaded successfully");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -63,7 +66,6 @@ export async function getImageByNoticeId(noticeId) {
|
|||||||
|
|
||||||
return imageUrl;
|
return imageUrl;
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.log(`Zdjęcie nie istnieje dla notice o id: ${noticeId}`);
|
|
||||||
imageUrl = "https://http.cat/404.jpg";
|
imageUrl = "https://http.cat/404.jpg";
|
||||||
return imageUrl;
|
return imageUrl;
|
||||||
}
|
}
|
||||||
@@ -73,7 +75,9 @@ export async function getAllImagesByNoticeId(noticeId) {
|
|||||||
const { token } = useAuthStore.getState();
|
const { token } = useAuthStore.getState();
|
||||||
const headers = token ? { Authorization: `Bearer ${token}` } : {};
|
const headers = token ? { Authorization: `Bearer ${token}` } : {};
|
||||||
try {
|
try {
|
||||||
const listResponse = await axios.get(`${API_URL}/images/list/${noticeId}`, {headers: headers});
|
const listResponse = await axios.get(`${API_URL}/images/list/${noticeId}`, {
|
||||||
|
headers: headers,
|
||||||
|
});
|
||||||
|
|
||||||
if (listResponse.data && listResponse.data.length > 0) {
|
if (listResponse.data && listResponse.data.length > 0) {
|
||||||
return listResponse.data.map((imageName) => ({
|
return listResponse.data.map((imageName) => ({
|
||||||
@@ -96,38 +100,36 @@ export async function getAllImagesByNoticeId(noticeId) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export const uploadImage = async (noticeId, imageUri) => {
|
export const uploadImage = async (noticeId, imageObj, isFirst) => {
|
||||||
const { token } = useAuthStore.getState();
|
const { token } = useAuthStore.getState();
|
||||||
const headers = {
|
const headers = {
|
||||||
...(token ? { Authorization: `Bearer ${token}` } : {}),
|
...(token ? { Authorization: `Bearer ${token}` } : {}),
|
||||||
'Content-Type': 'multipart/form-data'
|
"Content-Type": "multipart/form-data",
|
||||||
};
|
};
|
||||||
const formData = new FormData();
|
const formData = new FormData();
|
||||||
|
|
||||||
const filename = imageUri.split("/").pop();
|
const filename = imageObj.split("/").pop();
|
||||||
|
|
||||||
const match = /\.(\w+)$/.exec(filename);
|
const match = /\.(\w+)$/.exec(filename);
|
||||||
const type = match ? `image/${match[1]}` : "image/jpeg";
|
const type = match ? `image/${match[1]}` : "image/jpeg";
|
||||||
|
|
||||||
formData.append("file", {
|
formData.append("file", {
|
||||||
uri: imageUri,
|
uri: imageObj,
|
||||||
name: filename,
|
name: filename,
|
||||||
type: type,
|
type: type,
|
||||||
});
|
});
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const response = await axios.post(
|
const response = await axios.post(
|
||||||
`${API_URL}/images/upload/${noticeId}`,
|
`${API_URL}/images/upload/${noticeId}?isMainImage=${isFirst}`,
|
||||||
formData,
|
formData,
|
||||||
{
|
{
|
||||||
headers: headers,
|
headers: headers,
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
console.info("Upload successful:", response.data);
|
console.log("Upload successful:", response.data);
|
||||||
return response.data;
|
return response.data;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.log("imageURI:", imageUri);
|
|
||||||
|
|
||||||
console.error(
|
console.error(
|
||||||
"Error uploading image:",
|
"Error uploading image:",
|
||||||
error.response.data,
|
error.response.data,
|
||||||
@@ -156,3 +158,65 @@ export const deleteNotice = async (noticeId) => {
|
|||||||
throw error;
|
throw error;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export const editNotice = async (noticeId, notice) => {
|
||||||
|
const { token } = useAuthStore.getState();
|
||||||
|
const headers = token ? { Authorization: `Bearer ${token}` } : {};
|
||||||
|
|
||||||
|
try {
|
||||||
|
const response = await axios.put(
|
||||||
|
`${API_URL}/notices/edit/${noticeId}`,
|
||||||
|
{
|
||||||
|
title: notice.title,
|
||||||
|
description: notice.description,
|
||||||
|
price: notice.price,
|
||||||
|
category: notice.category,
|
||||||
|
status: notice.status,
|
||||||
|
attributes: notice.attributes,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
headers: headers,
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
if (response.data && notice.image && notice.image.length > 0) {
|
||||||
|
for (let i = 0; i < notice.image.length; i++) {
|
||||||
|
const image = notice.image[i];
|
||||||
|
const isFirst = i === 0;
|
||||||
|
|
||||||
|
if (typeof image === "string" && !image.startsWith("http")) {
|
||||||
|
await uploadImage(noticeId, image, isFirst);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return response.data;
|
||||||
|
} catch (error) {
|
||||||
|
console.error(
|
||||||
|
"Error editing notice:",
|
||||||
|
error.response?.data,
|
||||||
|
error.response?.status
|
||||||
|
);
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
export const deleteImage = async (filename) => {
|
||||||
|
const { token } = useAuthStore.getState();
|
||||||
|
const headers = token ? { Authorization: `Bearer ${token}` } : {};
|
||||||
|
|
||||||
|
try {
|
||||||
|
const response = await axios.delete(
|
||||||
|
`${API_URL}/images/delete/${filename}`,
|
||||||
|
{ headers: headers }
|
||||||
|
);
|
||||||
|
return response.data;
|
||||||
|
} catch (error) {
|
||||||
|
console.error(
|
||||||
|
"Error deleting image:",
|
||||||
|
error.response?.data,
|
||||||
|
error.response?.status
|
||||||
|
);
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|||||||
@@ -44,7 +44,7 @@ export default function Account() {
|
|||||||
<Image
|
<Image
|
||||||
source={{
|
source={{
|
||||||
uri:
|
uri:
|
||||||
user.profileImage ||
|
user.image ||
|
||||||
"https://th.bing.com/th/id/OIP.3coo_N8sieled8QNroQmkgHaHa?rs=1&pid=ImgDetMain",
|
"https://th.bing.com/th/id/OIP.3coo_N8sieled8QNroQmkgHaHa?rs=1&pid=ImgDetMain",
|
||||||
}}
|
}}
|
||||||
className="h-24 w-24 rounded-full border-4 border-white shadow-md"
|
className="h-24 w-24 rounded-full border-4 border-white shadow-md"
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import { useNoticesStore } from "@/store/noticesStore";
|
import { useNoticesStore } from "@/store/noticesStore";
|
||||||
import { NoticeCard } from "@/components/NoticeCard";
|
import { NoticeCard } from "@/components/NoticeCard";
|
||||||
import { Button, ButtonText } from "@/components/ui/button";
|
import { Button, ButtonText } from "@/components/ui/button";
|
||||||
|
import { usePathname } from "expo-router";
|
||||||
import { Box } from "@/components/ui/box";
|
import { Box } from "@/components/ui/box";
|
||||||
import { Text } from "@/components/ui/text";
|
import { Text } from "@/components/ui/text";
|
||||||
import { VStack } from "@/components/ui/vstack";
|
import { VStack } from "@/components/ui/vstack";
|
||||||
@@ -16,6 +16,7 @@ import { useRouter } from "expo-router";
|
|||||||
|
|
||||||
export default function UserNotices() {
|
export default function UserNotices() {
|
||||||
const router = useRouter();
|
const router = useRouter();
|
||||||
|
const pathname = usePathname();
|
||||||
const { notices, fetchNotices, deleteNotice } = useNoticesStore();
|
const { notices, fetchNotices, deleteNotice } = useNoticesStore();
|
||||||
const [isLoading, setIsLoading] = useState(true);
|
const [isLoading, setIsLoading] = useState(true);
|
||||||
const [isRedirecting, setIsRedirecting] = useState(false);
|
const [isRedirecting, setIsRedirecting] = useState(false);
|
||||||
@@ -41,7 +42,7 @@ export default function UserNotices() {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
loadNotices();
|
loadNotices();
|
||||||
}, [fetchNotices]);
|
}, [pathname, fetchNotices]);
|
||||||
|
|
||||||
const showNewToast = (title) => {
|
const showNewToast = (title) => {
|
||||||
const newId = Math.random();
|
const newId = Math.random();
|
||||||
@@ -162,10 +163,7 @@ export default function UserNotices() {
|
|||||||
<Button
|
<Button
|
||||||
className="ml-2"
|
className="ml-2"
|
||||||
onPress={() => {
|
onPress={() => {
|
||||||
console.log("Edytuj notice");
|
router.replace(`notice/edit/${item.noticeId}`);
|
||||||
router.replace(
|
|
||||||
`dashboard/notice/edit/${item.noticeId}`
|
|
||||||
);
|
|
||||||
}}
|
}}
|
||||||
size="md"
|
size="md"
|
||||||
variant="outline"
|
variant="outline"
|
||||||
|
|||||||
@@ -15,21 +15,21 @@ export default function Home() {
|
|||||||
const [isReady, setIsReady] = useState(false);
|
const [isReady, setIsReady] = useState(false);
|
||||||
const fetchNotices = useNoticesStore((state) => state.fetchNotices);
|
const fetchNotices = useNoticesStore((state) => state.fetchNotices);
|
||||||
|
|
||||||
// useEffect(() => {
|
useEffect(() => {
|
||||||
// setIsReady(true);
|
setIsReady(true);
|
||||||
// }, []);
|
}, []);
|
||||||
|
|
||||||
// useEffect(() => {
|
useEffect(() => {
|
||||||
// if (isReady && !token) {
|
if (isReady && !token) {
|
||||||
// router.replace("/login");
|
router.replace("/login");
|
||||||
// }
|
}
|
||||||
// }, [isReady, token, router]);
|
}, [isReady, token, router]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (token) {
|
if (token) {
|
||||||
fetchNotices();
|
fetchNotices();
|
||||||
}
|
}
|
||||||
}, [token, fetchNotices]);
|
}, [token]);
|
||||||
|
|
||||||
const notices = useNoticesStore((state) => state.notices);
|
const notices = useNoticesStore((state) => state.notices);
|
||||||
|
|
||||||
|
|||||||
@@ -94,7 +94,6 @@ export default function CreateNotice() {
|
|||||||
});
|
});
|
||||||
|
|
||||||
if (!title || !description || !price || !category) {
|
if (!title || !description || !price || !category) {
|
||||||
console.log("Error in form");
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
const formattedAttributes = Object.entries(selectedAttributes).map(
|
const formattedAttributes = Object.entries(selectedAttributes).map(
|
||||||
@@ -103,7 +102,6 @@ export default function CreateNotice() {
|
|||||||
value: value,
|
value: value,
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
// console.log("Selected attributes:", formattedAttributes);
|
|
||||||
setIsLoading(true);
|
setIsLoading(true);
|
||||||
try {
|
try {
|
||||||
const result = await addNotice({
|
const result = await addNotice({
|
||||||
|
|||||||
@@ -134,7 +134,6 @@ export default function Notices() {
|
|||||||
|
|
||||||
Object.keys(params).forEach((key) => {
|
Object.keys(params).forEach((key) => {
|
||||||
if (key.startsWith("attribute_")) {
|
if (key.startsWith("attribute_")) {
|
||||||
// console.log("Filtering by attribute:", key);
|
|
||||||
const attributeName = key.replace("attribute_", "");
|
const attributeName = key.replace("attribute_", "");
|
||||||
const attributeValue = params[key];
|
const attributeValue = params[key];
|
||||||
|
|
||||||
@@ -207,7 +206,6 @@ export default function Notices() {
|
|||||||
newParams[`attribute_${attributeName}`] = value;
|
newParams[`attribute_${attributeName}`] = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
// console.log("New Params:", newParams);
|
|
||||||
router.replace({
|
router.replace({
|
||||||
pathname: "/notices",
|
pathname: "/notices",
|
||||||
params: newParams,
|
params: newParams,
|
||||||
@@ -250,8 +248,6 @@ export default function Notices() {
|
|||||||
categories?.find((cat) => cat.value === params.category)) ||
|
categories?.find((cat) => cat.value === params.category)) ||
|
||||||
null;
|
null;
|
||||||
|
|
||||||
// console.log("Filtered Notices:", filteredNotices);
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<Box
|
<Box
|
||||||
|
|||||||
@@ -54,10 +54,8 @@ export default function NoticeDetails() {
|
|||||||
|
|
||||||
const handleSendMessage = async () => {
|
const handleSendMessage = async () => {
|
||||||
setIsSending(true);
|
setIsSending(true);
|
||||||
console.log("Rozpoczynanie procesu wysyłania wiadomości...");
|
|
||||||
|
|
||||||
const { user_id, token } = useAuthStore.getState();
|
const { user_id, token } = useAuthStore.getState();
|
||||||
console.log("Dane z authStore:", { user_id, token });
|
|
||||||
|
|
||||||
if (!user_id || !token) {
|
if (!user_id || !token) {
|
||||||
console.error("Brak danych zalogowanego użytkownika.");
|
console.error("Brak danych zalogowanego użytkownika.");
|
||||||
@@ -68,9 +66,7 @@ export default function NoticeDetails() {
|
|||||||
|
|
||||||
let currentUserEmail = "";
|
let currentUserEmail = "";
|
||||||
try {
|
try {
|
||||||
console.log(`Pobieranie danych użytkownika dla user_id : $ { user_id }`);
|
|
||||||
const currentUser = await getUserById(user_id);
|
const currentUser = await getUserById(user_id);
|
||||||
console.log("Dane zalogowanego użytkownika:", currentUser);
|
|
||||||
currentUserEmail = currentUser?.email;
|
currentUserEmail = currentUser?.email;
|
||||||
if (!currentUserEmail) {
|
if (!currentUserEmail) {
|
||||||
console.error("Nie znaleziono adresu email zalogowanego użytkownika.");
|
console.error("Nie znaleziono adresu email zalogowanego użytkownika.");
|
||||||
@@ -81,8 +77,6 @@ export default function NoticeDetails() {
|
|||||||
setIsSending(false);
|
setIsSending(false);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
console.log(`Pobrano email zalogowanego użytkownika
|
|
||||||
: $ { currentUserEmail }`);
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("Błąd podczas pobierania danych użytkownika:", error);
|
console.error("Błąd podczas pobierania danych użytkownika:", error);
|
||||||
Alert.alert(
|
Alert.alert(
|
||||||
@@ -98,7 +92,6 @@ export default function NoticeDetails() {
|
|||||||
subject: `Zapytanie ${currentUserEmail} o ogłoszenie ${notice.title}`,
|
subject: `Zapytanie ${currentUserEmail} o ogłoszenie ${notice.title}`,
|
||||||
body: message,
|
body: message,
|
||||||
};
|
};
|
||||||
console.log("Dane emaila do wysyłki:", emailData);
|
|
||||||
|
|
||||||
if (!emailData.to || !emailData.subject || !emailData.body) {
|
if (!emailData.to || !emailData.subject || !emailData.body) {
|
||||||
console.error("Walidacja nieudana: brakujące pola w emailData.");
|
console.error("Walidacja nieudana: brakujące pola w emailData.");
|
||||||
@@ -109,7 +102,6 @@ export default function NoticeDetails() {
|
|||||||
|
|
||||||
const result = await sendEmail(emailData);
|
const result = await sendEmail(emailData);
|
||||||
if (result.success) {
|
if (result.success) {
|
||||||
console.log("Wiadomość wysłana pomyślnie!", result.result);
|
|
||||||
setIsMessageFormVisible(false);
|
setIsMessageFormVisible(false);
|
||||||
setMessage("");
|
setMessage("");
|
||||||
Alert.alert("Sukces", "Wiadomość została wysłana!");
|
Alert.alert("Sukces", "Wiadomość została wysłana!");
|
||||||
@@ -122,7 +114,6 @@ export default function NoticeDetails() {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
setIsSending(false);
|
setIsSending(false);
|
||||||
console.log("Zakończono proces wysyłania wiadomości.");
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const formatDate = (dateString) => {
|
const formatDate = (dateString) => {
|
||||||
|
|||||||
@@ -31,8 +31,9 @@ import { ChevronDownIcon } from "@/components/ui/icon";
|
|||||||
import { useNoticesStore } from "@/store/noticesStore";
|
import { useNoticesStore } from "@/store/noticesStore";
|
||||||
import { listCategories } from "@/api/categories";
|
import { listCategories } from "@/api/categories";
|
||||||
import { useRouter } from "expo-router";
|
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";
|
import { useLocalSearchParams, Stack } from "expo-router";
|
||||||
|
import { Ionicons } from "@expo/vector-icons";
|
||||||
|
|
||||||
export default function EditNotice() {
|
export default function EditNotice() {
|
||||||
const { id } = useLocalSearchParams();
|
const { id } = useLocalSearchParams();
|
||||||
@@ -43,7 +44,6 @@ export default function EditNotice() {
|
|||||||
const [price, setPrice] = useState("");
|
const [price, setPrice] = useState("");
|
||||||
const [category, setCategory] = useState("");
|
const [category, setCategory] = useState("");
|
||||||
const [image, setImage] = useState([]);
|
const [image, setImage] = useState([]);
|
||||||
const [images, setImages] = useState([]);
|
|
||||||
const [isImageLoading, setIsImageLoading] = useState(true);
|
const [isImageLoading, setIsImageLoading] = useState(true);
|
||||||
const [selectItems, setSelectItems] = useState([]);
|
const [selectItems, setSelectItems] = useState([]);
|
||||||
const [isLoading, setIsLoading] = useState(false);
|
const [isLoading, setIsLoading] = useState(false);
|
||||||
@@ -84,7 +84,6 @@ export default function EditNotice() {
|
|||||||
attributesObj[attr.name] = attr.value;
|
attributesObj[attr.name] = attr.value;
|
||||||
});
|
});
|
||||||
setSelectedAttributes(attributesObj);
|
setSelectedAttributes(attributesObj);
|
||||||
// console.log("Attributes loaded:", attributesObj);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -92,20 +91,13 @@ export default function EditNotice() {
|
|||||||
setIsImageLoading(true);
|
setIsImageLoading(true);
|
||||||
try {
|
try {
|
||||||
const fetchedImages = await getAllImagesByNoticeId(notice.noticeId);
|
const fetchedImages = await getAllImagesByNoticeId(notice.noticeId);
|
||||||
console.log("Fetched images:", fetchedImages);
|
|
||||||
|
|
||||||
if (fetchedImages && fetchedImages.length > 0) {
|
if (fetchedImages && fetchedImages.length > 0) {
|
||||||
const imageUris = fetchedImages.map((img) => img.uri);
|
setImage(fetchedImages);
|
||||||
setImages(fetchedImages);
|
|
||||||
setImage(imageUris);
|
|
||||||
console.log("Image URIs set:", imageUris);
|
|
||||||
} else {
|
} else {
|
||||||
setImages([]);
|
|
||||||
setImage([]);
|
setImage([]);
|
||||||
}
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error("Error while loading images:", err);
|
console.error("Error while loading images:", err);
|
||||||
setImages([]);
|
|
||||||
setImage([]);
|
setImage([]);
|
||||||
} finally {
|
} finally {
|
||||||
setIsImageLoading(false);
|
setIsImageLoading(false);
|
||||||
@@ -115,7 +107,7 @@ export default function EditNotice() {
|
|||||||
if (notice) {
|
if (notice) {
|
||||||
fetchImage();
|
fetchImage();
|
||||||
}
|
}
|
||||||
}, [notices]);
|
}, [notices, id]);
|
||||||
|
|
||||||
const [error, setError] = useState({
|
const [error, setError] = useState({
|
||||||
title: false,
|
title: false,
|
||||||
@@ -154,10 +146,9 @@ export default function EditNotice() {
|
|||||||
value: value,
|
value: value,
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
// console.log("Selected attributes:", formattedAttributes);
|
|
||||||
setIsLoading(true);
|
setIsLoading(true);
|
||||||
try {
|
try {
|
||||||
const result = await editNotice({
|
const result = await editNotice(id, {
|
||||||
title: title,
|
title: title,
|
||||||
description: description,
|
description: description,
|
||||||
price: price,
|
price: price,
|
||||||
@@ -168,14 +159,12 @@ export default function EditNotice() {
|
|||||||
});
|
});
|
||||||
|
|
||||||
if (result) {
|
if (result) {
|
||||||
console.log("Notice created successfully with ID: ", result.noticeId);
|
|
||||||
await fetchNotices();
|
await fetchNotices();
|
||||||
clearForm();
|
|
||||||
|
|
||||||
router.push("/(tabs)/dashboard/userNotices");
|
router.push("/(tabs)/dashboard/userNotices");
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("Error creating notice. Error message: ", error.message);
|
console.error("Error editing notice. Error message: ", error.message);
|
||||||
} finally {
|
} finally {
|
||||||
setIsLoading(false);
|
setIsLoading(false);
|
||||||
}
|
}
|
||||||
@@ -210,27 +199,12 @@ export default function EditNotice() {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const clearForm = () => {
|
|
||||||
setTitle("");
|
|
||||||
setDescription("");
|
|
||||||
setPrice("");
|
|
||||||
setCategory("");
|
|
||||||
setImage([]);
|
|
||||||
setSelectedAttributes({});
|
|
||||||
setError({
|
|
||||||
title: false,
|
|
||||||
description: false,
|
|
||||||
price: false,
|
|
||||||
category: false,
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
if (isLoading) {
|
if (isLoading) {
|
||||||
return (
|
return (
|
||||||
<Box className="items-center justify-center flex-1">
|
<Box className="items-center justify-center flex-1">
|
||||||
<ActivityIndicator size="large" color="#787878" />
|
<ActivityIndicator size="large" color="#787878" />
|
||||||
<Text size="md" bold="true" className="mt-5">
|
<Text size="md" bold="true" className="mt-5">
|
||||||
Dodajemy ogłoszenie...
|
Edytuj ogłoszenie...
|
||||||
</Text>
|
</Text>
|
||||||
</Box>
|
</Box>
|
||||||
);
|
);
|
||||||
@@ -242,6 +216,22 @@ export default function EditNotice() {
|
|||||||
style={{ flex: 1 }}
|
style={{ flex: 1 }}
|
||||||
keyboardVerticalOffset={Platform.OS === "ios" ? 64 : 0}
|
keyboardVerticalOffset={Platform.OS === "ios" ? 64 : 0}
|
||||||
>
|
>
|
||||||
|
<Stack.Screen
|
||||||
|
options={{
|
||||||
|
title: "Edycja",
|
||||||
|
headerLeft: () => (
|
||||||
|
<Button
|
||||||
|
variant="link"
|
||||||
|
size="sm"
|
||||||
|
onPress={() => router.replace("/(tabs)/dashboard/userNotices")}
|
||||||
|
className="mr-2"
|
||||||
|
>
|
||||||
|
<Ionicons name="arrow-back" size={24} color="#1c1c1e" />
|
||||||
|
<ButtonText className="text-typography-900">Wróć</ButtonText>
|
||||||
|
</Button>
|
||||||
|
),
|
||||||
|
}}
|
||||||
|
/>
|
||||||
<ScrollView h="$80" w="$80">
|
<ScrollView h="$80" w="$80">
|
||||||
<FormControl className="p-4 border rounded-lg border-outline-300">
|
<FormControl className="p-4 border rounded-lg border-outline-300">
|
||||||
<VStack space="xl">
|
<VStack space="xl">
|
||||||
@@ -258,14 +248,22 @@ export default function EditNotice() {
|
|||||||
</Text>
|
</Text>
|
||||||
{image && image.length > 0 && (
|
{image && image.length > 0 && (
|
||||||
<VStack space="xs" className="flex-row flex-wrap">
|
<VStack space="xs" className="flex-row flex-wrap">
|
||||||
{image.map((img, index) => (
|
{image.map((img, index) => {
|
||||||
<Image
|
const imageSource =
|
||||||
key={index}
|
typeof img === "string" ? { uri: img } : img;
|
||||||
source={{ uri: img }}
|
|
||||||
style={styles.image}
|
return (
|
||||||
className="m-1"
|
<Image
|
||||||
/>
|
key={index}
|
||||||
))}
|
source={imageSource}
|
||||||
|
style={styles.image}
|
||||||
|
className="m-1"
|
||||||
|
onError={(error) =>
|
||||||
|
console.log(`Image ${index} error:`, error)
|
||||||
|
}
|
||||||
|
/>
|
||||||
|
);
|
||||||
|
})}
|
||||||
</VStack>
|
</VStack>
|
||||||
)}
|
)}
|
||||||
</VStack>
|
</VStack>
|
||||||
@@ -381,7 +379,7 @@ export default function EditNotice() {
|
|||||||
onPress={handleEditNotice}
|
onPress={handleEditNotice}
|
||||||
disabled={isLoading}
|
disabled={isLoading}
|
||||||
>
|
>
|
||||||
<ButtonText className="text-typography-0">Dodaj</ButtonText>
|
<ButtonText className="text-typography-0">Edytuj</ButtonText>
|
||||||
</Button>
|
</Button>
|
||||||
</VStack>
|
</VStack>
|
||||||
</FormControl>
|
</FormControl>
|
||||||
@@ -2,30 +2,36 @@ import { View } from "react-native";
|
|||||||
import { useEffect, useState } from "react";
|
import { useEffect, useState } from "react";
|
||||||
import { Heading } from "@/components/ui/heading";
|
import { Heading } from "@/components/ui/heading";
|
||||||
import { FlatList } from "react-native";
|
import { FlatList } from "react-native";
|
||||||
import axios from "axios";
|
|
||||||
import UserBlock from "@/components/UserBlock";
|
import UserBlock from "@/components/UserBlock";
|
||||||
|
import { getAllUsers } from "@/api/client";
|
||||||
import { useAuthStore } from "@/store/authStore";
|
import { useAuthStore } from "@/store/authStore";
|
||||||
|
|
||||||
export function UserSection({ notices, title }) {
|
export function UserSection({ notices, title }) {
|
||||||
const token = useAuthStore((state) => state.token);
|
|
||||||
const headers = token ? { Authorization: `Bearer ${token}` } : {};
|
|
||||||
const [users, setUsers] = useState([]);
|
const [users, setUsers] = useState([]);
|
||||||
|
const { token } = useAuthStore.getState();
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
|
const fetchUsers = async () => {
|
||||||
|
try {
|
||||||
|
const data = await getAllUsers();
|
||||||
|
setUsers(data);
|
||||||
|
} catch (error) {
|
||||||
|
setUsers([]);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
if (token) {
|
if (token) {
|
||||||
axios
|
fetchUsers();
|
||||||
.get("https://hopp.zikor.pl/api/v1/clients/get/all", {
|
|
||||||
headers: headers,
|
|
||||||
})
|
|
||||||
.then((res) => setUsers(res.data))
|
|
||||||
.catch(() => setUsers([]));
|
|
||||||
}
|
}
|
||||||
}, [token]);
|
}, [token]);
|
||||||
|
|
||||||
const usersWithNoticeCount = users.map((user) => {
|
const usersWithNoticeCount =
|
||||||
const count = notices.filter((n) => n.clientId === user.id).length;
|
users && users.length > 0
|
||||||
return { ...user, noticeCount: count };
|
? users.map((user) => {
|
||||||
});
|
const count = notices.filter((n) => n.clientId === user.id).length;
|
||||||
|
return { ...user, noticeCount: count };
|
||||||
|
})
|
||||||
|
: [];
|
||||||
|
|
||||||
const topUsers = usersWithNoticeCount
|
const topUsers = usersWithNoticeCount
|
||||||
.sort((a, b) => b.noticeCount - a.noticeCount)
|
.sort((a, b) => b.noticeCount - a.noticeCount)
|
||||||
|
|||||||
@@ -2,10 +2,9 @@ import { create } from "zustand";
|
|||||||
import { createJSONStorage, persist } from "zustand/middleware";
|
import { createJSONStorage, persist } from "zustand/middleware";
|
||||||
import AsyncStorage from "@react-native-async-storage/async-storage";
|
import AsyncStorage from "@react-native-async-storage/async-storage";
|
||||||
import axios from "axios";
|
import axios from "axios";
|
||||||
|
import * as api from "@/api/auth";
|
||||||
import { router } from "expo-router";
|
import { router } from "expo-router";
|
||||||
|
|
||||||
const API_URL = "https://hopp.zikor.pl/api/v1";
|
|
||||||
|
|
||||||
let interceptorInitialized = false;
|
let interceptorInitialized = false;
|
||||||
|
|
||||||
export const useAuthStore = create(
|
export const useAuthStore = create(
|
||||||
@@ -38,16 +37,8 @@ export const useAuthStore = create(
|
|||||||
signIn: async (email, password) => {
|
signIn: async (email, password) => {
|
||||||
set({ isLoading: true, error: null });
|
set({ isLoading: true, error: null });
|
||||||
try {
|
try {
|
||||||
const response = await axios.post(`${API_URL}/auth/login`, {
|
const response = await api.login({email, password});
|
||||||
email,
|
set({ user_id: response.user_id, token: response.token, isLoading: false });
|
||||||
password,
|
|
||||||
});
|
|
||||||
|
|
||||||
const user_id = response.data.user_id;
|
|
||||||
const token = response.data.token;
|
|
||||||
set({ user_id: user_id, token: token, isLoading: false });
|
|
||||||
|
|
||||||
axios.defaults.headers.common["Authorization"] = `Bearer ${token}`;
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
set({
|
set({
|
||||||
error: error.response?.data?.message || error.message,
|
error: error.response?.data?.message || error.message,
|
||||||
@@ -60,19 +51,8 @@ export const useAuthStore = create(
|
|||||||
signUp: async (userData) => {
|
signUp: async (userData) => {
|
||||||
set({ isLoading: true, error: null });
|
set({ isLoading: true, error: null });
|
||||||
try {
|
try {
|
||||||
const response = await axios.post(
|
const response = await api.register(userData);
|
||||||
`${API_URL}/auth/register`,
|
set({ user_id: response.user_id, token: response.token, isLoading: false });
|
||||||
userData,
|
|
||||||
{
|
|
||||||
headers: { "Content-Type": "application/json" },
|
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
const user_id = response.data.user_id;
|
|
||||||
const token = response.data.token;
|
|
||||||
set({ user_id: user_id, token: token, isLoading: false });
|
|
||||||
|
|
||||||
axios.defaults.headers.common["Authorization"] = `Bearer ${token}`;
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
set({
|
set({
|
||||||
error: error.response?.data?.message || error.message,
|
error: error.response?.data?.message || error.message,
|
||||||
@@ -85,18 +65,8 @@ export const useAuthStore = create(
|
|||||||
signInWithGoogle: async (googleToken) => {
|
signInWithGoogle: async (googleToken) => {
|
||||||
set({ isLoading: true, error: null });
|
set({ isLoading: true, error: null });
|
||||||
try {
|
try {
|
||||||
const response = await axios.post(
|
const response = await api.googleLogin(googleToken);
|
||||||
`${API_URL}/auth/google`,
|
set({ user_id: response.user_id, token: response.token, isLoading: false });
|
||||||
{ googleToken: googleToken },
|
|
||||||
{
|
|
||||||
headers: { "Content-Type": "application/json" },
|
|
||||||
}
|
|
||||||
);
|
|
||||||
const user_id = response.data.user_id;
|
|
||||||
const token = response.data.token;
|
|
||||||
set({ user_id: user_id, token: token, isLoading: false });
|
|
||||||
|
|
||||||
axios.defaults.headers.common["Authorization"] = `Bearer ${token}`;
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
set({
|
set({
|
||||||
error: error.response?.data?.message || error.message,
|
error: error.response?.data?.message || error.message,
|
||||||
@@ -108,19 +78,11 @@ export const useAuthStore = create(
|
|||||||
|
|
||||||
signOut: async () => {
|
signOut: async () => {
|
||||||
const { token } = get();
|
const { token } = get();
|
||||||
const headers = token ? { Authorization: `Bearer ${token}` } : {};
|
|
||||||
try {
|
try {
|
||||||
await axios.post(
|
await api.logout(token);
|
||||||
`${API_URL}/auth/logout`,
|
|
||||||
{},
|
|
||||||
{
|
|
||||||
headers: headers,
|
|
||||||
}
|
|
||||||
);
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("Logout error:", error);
|
console.error("Logout error:", error);
|
||||||
} finally {
|
} finally {
|
||||||
delete axios.defaults.headers.common["Authorization"];
|
|
||||||
set({ user_id: null, token: null });
|
set({ user_id: null, token: null });
|
||||||
router.replace("/login");
|
router.replace("/login");
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -26,6 +26,34 @@ export const useNoticesStore = create((set, get) => ({
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
|
editNotice: async (noticeId, notice) => {
|
||||||
|
try {
|
||||||
|
if (notice.image.length > 0 && typeof notice.image[0] == "string") {
|
||||||
|
const currentImages = await api.getAllImagesByNoticeId(noticeId);
|
||||||
|
if (currentImages && currentImages.length > 0) {
|
||||||
|
for (const image of currentImages) {
|
||||||
|
const filename = image.uri
|
||||||
|
? image.uri.split("/").pop()
|
||||||
|
: image.split("/").pop();
|
||||||
|
|
||||||
|
await api.deleteImage(filename);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
const updatedNotice = await api.editNotice(noticeId, notice);
|
||||||
|
set((state) => ({
|
||||||
|
notices: state.notices.map((n) =>
|
||||||
|
n.noticeId == noticeId ? updatedNotice : n
|
||||||
|
),
|
||||||
|
}));
|
||||||
|
return updatedNotice;
|
||||||
|
} catch (error) {
|
||||||
|
console.error("Error editing notice:", error);
|
||||||
|
set({ error });
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
getNoticeById: (noticeId) => {
|
getNoticeById: (noticeId) => {
|
||||||
return get().notices.find(
|
return get().notices.find(
|
||||||
(notice) => String(notice.noticeId) === String(noticeId)
|
(notice) => String(notice.noticeId) === String(noticeId)
|
||||||
|
|||||||
Reference in New Issue
Block a user