Compare commits
11 Commits
b34ce7fd20
...
fixowanie
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
a04ef906cd | ||
|
|
a51345fd93 | ||
|
|
c2d4f5fb79 | ||
| 7ec883100f | |||
|
|
207f8f7161 | ||
|
|
27175ffa91 | ||
| c25495ba3f | |||
| be51f1e9cc | |||
| 14bd178f84 | |||
| 7fc1312ddc | |||
| 1d3cbeef3a |
@@ -8,9 +8,11 @@ export async function listCategories() {
|
||||
const headers = token ? { Authorization: `Bearer ${token}` } : {};
|
||||
|
||||
try {
|
||||
const response = await axios.get(`${API_URL}/vars/categories`, { headers: headers });
|
||||
const response = await axios.get(`${API_URL}/vars/categories`, {
|
||||
headers: headers,
|
||||
});
|
||||
return response.data;
|
||||
} catch (err) {
|
||||
console.error("Nie udało się pobrać listy kategorii.", err.response.status);
|
||||
// console.error("Nie udało się pobrać listy kategorii.", err.response.status);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,10 +7,9 @@ export async function getUserById(userId) {
|
||||
const { token } = useAuthStore.getState();
|
||||
const headers = token ? { Authorization: `Bearer ${token}` } : {};
|
||||
try {
|
||||
const response = await axios.get(
|
||||
`${API_URL}/clients/get/${userId}`,
|
||||
headers
|
||||
);
|
||||
const response = await axios.get(`${API_URL}/clients/get/${userId}`, {
|
||||
headers: headers,
|
||||
});
|
||||
return response.data;
|
||||
} catch (err) {
|
||||
console.error(
|
||||
|
||||
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 przy wysyłaniu maila", errorMessage);
|
||||
return { success: false, error: errorMessage };
|
||||
}
|
||||
|
||||
const result = await response.text();
|
||||
return { success: true, result };
|
||||
} catch (error) {
|
||||
console.error("Error przy wysyłaniu maila:", error.message);
|
||||
return { success: false, error: error.message };
|
||||
}
|
||||
};
|
||||
@@ -2,7 +2,7 @@ import axios from "axios";
|
||||
import FormData from "form-data";
|
||||
import { useAuthStore } from "@/store/authStore";
|
||||
|
||||
// const API_URL = "https://testowe.zikor.pl/api/v1";
|
||||
// const API_URL = "https://hopp.zikor.pl/api/v1";
|
||||
|
||||
const API_URL = "https://hopp.zikor.pl/api/v1";
|
||||
|
||||
@@ -36,7 +36,9 @@ export async function createNotice(notice) {
|
||||
const { token } = useAuthStore.getState();
|
||||
const headers = token ? { Authorization: `Bearer ${token}` } : {};
|
||||
try {
|
||||
const response = await axios.post(`${API_URL}/notices/add`, notice, {headers: headers});
|
||||
const response = await axios.post(`${API_URL}/notices/add`, notice, {
|
||||
headers: headers,
|
||||
});
|
||||
|
||||
if (response.data.noticeId !== null) {
|
||||
for (const imageUri of notice.image) {
|
||||
@@ -71,7 +73,9 @@ export async function getAllImagesByNoticeId(noticeId) {
|
||||
const { token } = useAuthStore.getState();
|
||||
const headers = token ? { Authorization: `Bearer ${token}` } : {};
|
||||
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) {
|
||||
return listResponse.data.map((imageName) => ({
|
||||
|
||||
@@ -1,6 +1,4 @@
|
||||
import axios from "axios";
|
||||
import FormData from "form-data";
|
||||
|
||||
import { useAuthStore } from "@/store/authStore";
|
||||
|
||||
const API_URL = "https://hopp.zikor.pl/api/v1/orders";
|
||||
@@ -9,17 +7,16 @@ export async function createOrder(noticeId, orderType) {
|
||||
const { token } = useAuthStore.getState();
|
||||
const headers = token ? { Authorization: `Bearer ${token}` } : {};
|
||||
const clientId = 1;
|
||||
|
||||
try {
|
||||
const response = await axios.post(
|
||||
`${API_URL}/add`,
|
||||
{ clientId, noticeId, orderType },
|
||||
{ clientId: clientId, noticeId: noticeId, orderType: orderType },
|
||||
{
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
...headers,
|
||||
},
|
||||
headers: headers,
|
||||
}
|
||||
);
|
||||
|
||||
return response.data;
|
||||
} catch (error) {
|
||||
console.log("Error", error.response?.data, error.response?.status);
|
||||
@@ -33,13 +30,10 @@ export async function createPayment(orderId) {
|
||||
const clientId = 1;
|
||||
try {
|
||||
const response = await axios.post(
|
||||
`${API_URL}/token`,
|
||||
`${API_URL}/token?orderId=${orderId}`,
|
||||
{},
|
||||
{
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
...headers,
|
||||
},
|
||||
headers: headers,
|
||||
}
|
||||
);
|
||||
return response.data;
|
||||
@@ -49,13 +43,30 @@ export async function createPayment(orderId) {
|
||||
}
|
||||
}
|
||||
|
||||
export async function getOrder(orderId) {
|
||||
const { token } = useAuthStore.getState();
|
||||
const headers = token ? { Authorization: `Bearer ${token}` } : {};
|
||||
|
||||
try {
|
||||
const response = await axios.get(`${API_URL}/get/${orderId}`, { headers });
|
||||
return response.data;
|
||||
} catch (error) {
|
||||
console.error(
|
||||
"Error fetching order:",
|
||||
error.response?.data,
|
||||
error.response?.status
|
||||
);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
export async function listOrders() {
|
||||
const { token } = useAuthStore.getState();
|
||||
const headers = token ? { Authorization: `Bearer ${token}` } : {};
|
||||
|
||||
try {
|
||||
const response = await axios.get(`${API_URL}/get/all`, { headers });
|
||||
return response.data; // to będzie tablica OrderWithPaymentsDTO
|
||||
return response.data;
|
||||
} catch (error) {
|
||||
console.error(
|
||||
"Error fetching orders:",
|
||||
|
||||
@@ -28,11 +28,10 @@ export async function getWishlist() {
|
||||
|
||||
try {
|
||||
const response = await axios.get(`${API_URL}/`, { headers });
|
||||
console.log("Wishlist response:", response.data);
|
||||
return response.data;
|
||||
} catch (error) {
|
||||
console.error("Error fetching wishlist:", error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
``
|
||||
``;
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
"scheme": "com.hamx.artisanconnect",
|
||||
"version": "1.0.0",
|
||||
"orientation": "portrait",
|
||||
"icon": "./assets/icon.png",
|
||||
"icon": "./assets/AppIco.png",
|
||||
"userInterfaceStyle": "light",
|
||||
"newArchEnabled": true,
|
||||
"splash": {
|
||||
|
||||
@@ -3,11 +3,12 @@ import { Ionicons } from "@expo/vector-icons";
|
||||
import { useAuthStore } from "@/store/authStore";
|
||||
|
||||
export default function TabLayout() {
|
||||
const token = useAuthStore((state) => state.token);
|
||||
const { token } = useAuthStore.getState();
|
||||
|
||||
if (!token) {
|
||||
return <Redirect href="/login" />;
|
||||
}
|
||||
|
||||
return (
|
||||
<Tabs
|
||||
screenOptions={{
|
||||
|
||||
@@ -7,7 +7,7 @@ import { Text } from "@/components/ui/text";
|
||||
import { VStack } from "@/components/ui/vstack";
|
||||
import { ActivityIndicator, FlatList } from "react-native";
|
||||
import { useEffect, useState, useRef } from "react";
|
||||
import { createOrder, createPayment } from "@/api/order";
|
||||
import { createOrder, createPayment, getOrder } from "@/api/order";
|
||||
import { Linking } from "react-native";
|
||||
import { Ionicons } from "@expo/vector-icons";
|
||||
import { useToast, Toast, ToastTitle } from "@/components/ui/toast";
|
||||
@@ -22,14 +22,21 @@ export default function UserNotices() {
|
||||
const appState = useRef(AppState.currentState);
|
||||
const [toastId, setToastId] = useState(0);
|
||||
const { user_id } = useAuthStore.getState();
|
||||
const currentUserId= user_id;
|
||||
const currentUserId = user_id;
|
||||
const [orderId, setOrderId] = useState(null);
|
||||
|
||||
useEffect(() => {
|
||||
if (!isRedirecting) return;
|
||||
const subscription = AppState.addEventListener("change", (state) => {
|
||||
if (state === "active") {
|
||||
(async () => {
|
||||
const lastOrder = await getOrder(orderId);
|
||||
const lastPayments = lastOrder.payments;
|
||||
const paymentStatus =
|
||||
lastPayments.length > 0
|
||||
? lastPayments[lastPayments.length - 1].status
|
||||
: null;
|
||||
setIsRedirecting(false);
|
||||
const paymentStatus = "CORRECT";
|
||||
if (paymentStatus === "INCORRECT") {
|
||||
showNewToast("Płatność została anulowana.");
|
||||
} else if (paymentStatus === "CORRECT") {
|
||||
@@ -37,12 +44,12 @@ export default function UserNotices() {
|
||||
} else {
|
||||
showNewToast("Płatność jeszcze nie wpłynęła.");
|
||||
}
|
||||
})();
|
||||
}
|
||||
|
||||
appState.current = state;
|
||||
});
|
||||
return () => subscription.remove();
|
||||
}, [isRedirecting, toast]);
|
||||
}, [isRedirecting, toast, orderId]);
|
||||
|
||||
useEffect(() => {
|
||||
const loadNotices = async () => {
|
||||
@@ -56,7 +63,7 @@ export default function UserNotices() {
|
||||
}
|
||||
};
|
||||
loadNotices();
|
||||
}, []);
|
||||
}, [fetchNotices]);
|
||||
|
||||
const showNewToast = (title) => {
|
||||
const newId = Math.random();
|
||||
@@ -81,23 +88,23 @@ export default function UserNotices() {
|
||||
try {
|
||||
const result = await createOrder(noticeId, type);
|
||||
if (result) {
|
||||
setOrderId(result);
|
||||
try {
|
||||
const paymentResult = await createPayment(); //trzeba dodać orderId
|
||||
const paymentResult = await createPayment(result);
|
||||
if (paymentResult) {
|
||||
setIsRedirecting(true);
|
||||
await Linking.openURL(paymentResult);
|
||||
setWaitingForPayment(true);
|
||||
} else {
|
||||
console.log(`Nie udało się aktywować ogłoszenia ${noticeId}.`);
|
||||
console.log(`Nie udało się aktywować ogłoszenia 4 ${noticeId}.`);
|
||||
}
|
||||
} catch (err) {
|
||||
console.log("Błąd podczas aktywacji ogłoszenia:", err);
|
||||
// console.log("Błąd podczas aktywacji ogłoszenia 3:", err);
|
||||
}
|
||||
} else {
|
||||
console.log(`Nie udało się aktywować ogłoszenia ${noticeId}.`);
|
||||
// console.log(`Nie udało się aktywować ogłoszenia 2 ${noticeId}.`);
|
||||
}
|
||||
} catch (err) {
|
||||
console.log("Błąd podczas aktywacji ogłoszenia:", err);
|
||||
console.log("Błąd podczas aktywacji ogłoszenia 1:", err);
|
||||
}
|
||||
}
|
||||
};
|
||||
@@ -117,7 +124,7 @@ export default function UserNotices() {
|
||||
if (isLoading) {
|
||||
return (
|
||||
<Box className="items-center justify-center flex-1">
|
||||
<ActivityIndicator size="large" color="#787878"/>
|
||||
<ActivityIndicator size="large" color="#787878" />
|
||||
</Box>
|
||||
);
|
||||
}
|
||||
@@ -168,10 +175,14 @@ export default function UserNotices() {
|
||||
size="md"
|
||||
variant="solid"
|
||||
action="primary"
|
||||
onPress={() => handleOrder(item.noticeId, "ACTIVATE")}
|
||||
onPress={() => handleOrder(item.noticeId, "ACTIVATION")}
|
||||
>
|
||||
<ButtonText>Aktywuj</ButtonText>
|
||||
<Ionicons name="arrow-redo-outline" size={14} color="#fff" />
|
||||
<Ionicons
|
||||
name="arrow-redo-outline"
|
||||
size={14}
|
||||
color="#fff"
|
||||
/>
|
||||
</Button>
|
||||
)}
|
||||
</Box>
|
||||
|
||||
@@ -15,15 +15,15 @@ export default function Home() {
|
||||
const [isReady, setIsReady] = useState(false);
|
||||
const fetchNotices = useNoticesStore((state) => state.fetchNotices);
|
||||
|
||||
useEffect(() => {
|
||||
setIsReady(true);
|
||||
}, []);
|
||||
// useEffect(() => {
|
||||
// setIsReady(true);
|
||||
// }, []);
|
||||
|
||||
useEffect(() => {
|
||||
if (isReady && !token) {
|
||||
router.replace("/login");
|
||||
}
|
||||
}, [isReady, token, router]);
|
||||
// useEffect(() => {
|
||||
// if (isReady && !token) {
|
||||
// router.replace("/login");
|
||||
// }
|
||||
// }, [isReady, token, router]);
|
||||
|
||||
useEffect(() => {
|
||||
if (token) {
|
||||
@@ -32,11 +32,8 @@ export default function Home() {
|
||||
}, [token, fetchNotices]);
|
||||
|
||||
const notices = useNoticesStore((state) => state.notices);
|
||||
// console.log("Notices:", notices);
|
||||
// console.log("Notices length:", notices.length);
|
||||
|
||||
const activeNotices = notices.filter((notice) => notice.status === "ACTIVE");
|
||||
// console.log("Activer Notices:", activeNotices.length);
|
||||
const latestNotices = [...activeNotices]
|
||||
.sort((a, b) => new Date(b.publishDate) - new Date(a.publishDate))
|
||||
.slice(0, 6);
|
||||
|
||||
@@ -4,10 +4,9 @@ import { NoticeCard } from "@/components/NoticeCard";
|
||||
import { Ionicons } from "@expo/vector-icons";
|
||||
import { Box } from "@/components/ui/box";
|
||||
import { Text } from "@/components/ui/text";
|
||||
import {useCallback} from "react";
|
||||
import { useCallback } from "react";
|
||||
import { useFocusEffect } from "@react-navigation/native";
|
||||
|
||||
|
||||
export default function Wishlist() {
|
||||
const wishlistNotices = useWishlist((state) => state.wishlistNotices);
|
||||
const fetchWishlist = useWishlist((state) => state.fetchWishlist);
|
||||
@@ -21,10 +20,9 @@ export default function Wishlist() {
|
||||
const styles = {
|
||||
container: {
|
||||
margin: 10,
|
||||
}
|
||||
}
|
||||
},
|
||||
};
|
||||
|
||||
// console.log("Wishlist notices:", wishlistNotices);
|
||||
if (wishlistNotices.length === 0) {
|
||||
return (
|
||||
<Box style={styles.container} className="flex-row flex-1 justify-center">
|
||||
|
||||
@@ -17,6 +17,7 @@ return (
|
||||
}}
|
||||
>
|
||||
<Stack.Screen name="(tabs)" options={{ headerShown: false }} />
|
||||
<Stack.Screen name="user" options={{ headerShown: false }} />
|
||||
<Stack.Screen
|
||||
name="(auth)/login"
|
||||
options={{ headerShown: false }}/>
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
import {Link, Stack, useLocalSearchParams} from "expo-router";
|
||||
import {Box} from "@/components/ui/box";
|
||||
import {Card} from "@/components/ui/card";
|
||||
import {Heading} from "@/components/ui/heading";
|
||||
import {Image} from "@/components/ui/image";
|
||||
import {Text} from "@/components/ui/text";
|
||||
import {VStack} from "@/components/ui/vstack";
|
||||
import {Ionicons} from "@expo/vector-icons";
|
||||
import { Link, Stack, useLocalSearchParams } from "expo-router";
|
||||
import { Box } from "@/components/ui/box";
|
||||
import { Card } from "@/components/ui/card";
|
||||
import { Heading } from "@/components/ui/heading";
|
||||
import { Image } from "@/components/ui/image";
|
||||
import { Text } from "@/components/ui/text";
|
||||
import { VStack } from "@/components/ui/vstack";
|
||||
import { Ionicons } from "@expo/vector-icons";
|
||||
import {
|
||||
ActivityIndicator,
|
||||
Dimensions,
|
||||
@@ -13,16 +13,16 @@ import {
|
||||
View,
|
||||
TextInput,
|
||||
} from "react-native";
|
||||
import {useEffect, useState, useRef} from "react";
|
||||
import {useNoticesStore} from "@/store/noticesStore";
|
||||
import {useWishlist} from "@/store/wishlistStore";
|
||||
import {Pressable, ScrollView} from "react-native";
|
||||
import {getUserById} from "@/api/client";
|
||||
import { useEffect, useState, useRef } from "react";
|
||||
import { useNoticesStore } from "@/store/noticesStore";
|
||||
import { useWishlist } from "@/store/wishlistStore";
|
||||
import { Pressable, ScrollView } from "react-native";
|
||||
import { getUserById } from "@/api/client";
|
||||
|
||||
const {width} = Dimensions.get("window");
|
||||
const { width } = Dimensions.get("window");
|
||||
|
||||
export default function NoticeDetails() {
|
||||
const {id} = useLocalSearchParams();
|
||||
const { id } = useLocalSearchParams();
|
||||
const [images, setImages] = useState([]);
|
||||
const [isImageLoading, setIsImageLoading] = useState(true);
|
||||
const [isLoading, setIsLoading] = useState(true);
|
||||
@@ -55,7 +55,7 @@ export default function NoticeDetails() {
|
||||
});
|
||||
};
|
||||
|
||||
const {getNoticeById, getAllImagesByNoticeId} = useNoticesStore();
|
||||
const { getNoticeById, getAllImagesByNoticeId } = useNoticesStore();
|
||||
const toggleNoticeInWishlist = useWishlist(
|
||||
(state) => state.toggleNoticeInWishlist
|
||||
);
|
||||
@@ -63,7 +63,7 @@ export default function NoticeDetails() {
|
||||
const isInWishlist = useWishlist((state) =>
|
||||
id ? state.wishlistNotices.some((item) => item.noticeId === id) : false
|
||||
);
|
||||
const onViewableItemsChanged = useRef(({viewableItems}) => {
|
||||
const onViewableItemsChanged = useRef(({ viewableItems }) => {
|
||||
if (viewableItems.length > 0) {
|
||||
setCurrentIndex(viewableItems[0].index);
|
||||
}
|
||||
@@ -103,11 +103,11 @@ export default function NoticeDetails() {
|
||||
setImages(
|
||||
fetchedImages && fetchedImages.length > 0
|
||||
? fetchedImages
|
||||
: {uri: "https://http.cat/404.jpg"}
|
||||
: { uri: "https://http.cat/404.jpg" }
|
||||
);
|
||||
} catch (err) {
|
||||
console.error("Error while loading images:", err);
|
||||
setImage({uri: "https://http.cat/404.jpg"});
|
||||
setImage({ uri: "https://http.cat/404.jpg" });
|
||||
} finally {
|
||||
setIsImageLoading(false);
|
||||
}
|
||||
@@ -138,7 +138,7 @@ export default function NoticeDetails() {
|
||||
}, [notice]);
|
||||
|
||||
if (isLoading) {
|
||||
return <ActivityIndicator/>;
|
||||
return <ActivityIndicator />;
|
||||
}
|
||||
|
||||
if (error) {
|
||||
@@ -158,7 +158,7 @@ export default function NoticeDetails() {
|
||||
/>
|
||||
{isImageLoading ? (
|
||||
<Box className="h-auto w-full rounded-md aspect-[1/1] bg-gray-100 items-center justify-center">
|
||||
<ActivityIndicator size="large" color="#3b82f6"/>
|
||||
<ActivityIndicator size="large" color="#3b82f6" />
|
||||
</Box>
|
||||
) : (
|
||||
<Box className="sticky top-0 z-10 bg-white">
|
||||
@@ -173,8 +173,8 @@ export default function NoticeDetails() {
|
||||
pagingEnabled
|
||||
onViewableItemsChanged={onViewableItemsChanged}
|
||||
viewabilityConfig={viewabilityConfig}
|
||||
renderItem={({item, index}) => (
|
||||
<View style={{width: width}} className="p-1">
|
||||
renderItem={({ item, index }) => (
|
||||
<View style={{ width: width }} className="p-1">
|
||||
<Image
|
||||
source={item}
|
||||
className="h-auto w-auto rounded-md aspect-[1/1]"
|
||||
@@ -244,7 +244,7 @@ export default function NoticeDetails() {
|
||||
<Box className="mt-4 bg-gray-50 p-3 rounded-lg shadow-sm">
|
||||
<Text className="text-sm text-typography-500">Uzytkownik:</Text>
|
||||
{isUserLoading ? (
|
||||
<ActivityIndicator/>
|
||||
<ActivityIndicator />
|
||||
) : user ? (
|
||||
<>
|
||||
<Box className="mr-4">
|
||||
|
||||
11
ArtisanConnect/app/user/_layout.jsx
Normal file
11
ArtisanConnect/app/user/_layout.jsx
Normal file
@@ -0,0 +1,11 @@
|
||||
import { Stack } from 'expo-router';
|
||||
|
||||
export default function UserLayout() {
|
||||
return (
|
||||
<Stack
|
||||
screenOptions={{
|
||||
headerTitle: 'Ogłoszenia użytkownika',
|
||||
}}
|
||||
/>
|
||||
);
|
||||
}
|
||||
BIN
ArtisanConnect/assets/AppIco.png
Normal file
BIN
ArtisanConnect/assets/AppIco.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 304 KiB |
@@ -1,6 +1,5 @@
|
||||
import { View, FlatList } from "react-native";
|
||||
import { useEffect, useState } from "react";
|
||||
import { useAuthStore } from "@/store/authStore";
|
||||
import { Heading } from "@/components/ui/heading";
|
||||
import { Text } from "@/components/ui/text";
|
||||
import { Link } from "expo-router";
|
||||
@@ -17,9 +16,8 @@ export function CategorySection({ notices, title }) {
|
||||
setCategoryMap(data);
|
||||
}
|
||||
};
|
||||
|
||||
fetchCategories();
|
||||
});
|
||||
}, []);
|
||||
|
||||
const categories = Array.from(
|
||||
new Set(notices.map((notice) => notice.category))
|
||||
|
||||
11
ArtisanConnect/package-lock.json
generated
11
ArtisanConnect/package-lock.json
generated
@@ -43,6 +43,7 @@
|
||||
"expo-image-picker": "~16.1.4",
|
||||
"expo-linking": "~7.1.4",
|
||||
"expo-router": "~5.0.5",
|
||||
"expo-screen-orientation": "~8.1.7",
|
||||
"expo-status-bar": "~2.2.3",
|
||||
"expo-web-browser": "~14.1.6",
|
||||
"form-data": "^4.0.2",
|
||||
@@ -7012,6 +7013,16 @@
|
||||
"node": ">=10"
|
||||
}
|
||||
},
|
||||
"node_modules/expo-screen-orientation": {
|
||||
"version": "8.1.7",
|
||||
"resolved": "https://registry.npmjs.org/expo-screen-orientation/-/expo-screen-orientation-8.1.7.tgz",
|
||||
"integrity": "sha512-nYwadYtdU6mMDk0MCHMPPPQtBoeFYJ2FspLRW+J35CMLqzE4nbpwGeiImfXzkvD94fpOCfI4KgLj5vGauC3pfA==",
|
||||
"license": "MIT",
|
||||
"peerDependencies": {
|
||||
"expo": "*",
|
||||
"react-native": "*"
|
||||
}
|
||||
},
|
||||
"node_modules/expo-status-bar": {
|
||||
"version": "2.2.3",
|
||||
"license": "MIT",
|
||||
|
||||
@@ -62,7 +62,8 @@
|
||||
"react-native-svg": "15.11.2",
|
||||
"react-native-web": "~0.20.0",
|
||||
"tailwindcss": "^3.4.17",
|
||||
"zustand": "^5.0.3"
|
||||
"zustand": "^5.0.3",
|
||||
"expo-screen-orientation": "~8.1.7"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@babel/core": "^7.20.0",
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
import {create} from "zustand";
|
||||
import {createJSONStorage, persist} from "zustand/middleware";
|
||||
import { create } from "zustand";
|
||||
import { createJSONStorage, persist } from "zustand/middleware";
|
||||
import AsyncStorage from "@react-native-async-storage/async-storage";
|
||||
import axios from "axios";
|
||||
import { router } from "expo-router";
|
||||
|
||||
const API_URL = "https://hopp.zikor.pl/api/v1";
|
||||
|
||||
@@ -18,8 +19,9 @@ export const useAuthStore = create(
|
||||
(error.response && error.response.status === 401) ||
|
||||
error.response.status === 403
|
||||
) {
|
||||
set({user_id: null, token: null, isLoading: false});
|
||||
set({ user_id: null, token: null, isLoading: false });
|
||||
delete axios.defaults.headers.common["Authorization"];
|
||||
router.replace("/login");
|
||||
}
|
||||
return Promise.reject(error);
|
||||
}
|
||||
@@ -33,7 +35,7 @@ export const useAuthStore = create(
|
||||
error: null,
|
||||
|
||||
signIn: async (email, password) => {
|
||||
set({isLoading: true, error: null});
|
||||
set({ isLoading: true, error: null });
|
||||
try {
|
||||
const response = await axios.post(`${API_URL}/auth/login`, {
|
||||
email,
|
||||
@@ -42,7 +44,7 @@ export const useAuthStore = create(
|
||||
|
||||
const user_id = response.data.user_id;
|
||||
const token = response.data.token;
|
||||
set({user_id: user_id, token: token, isLoading: false});
|
||||
set({ user_id: user_id, token: token, isLoading: false });
|
||||
|
||||
axios.defaults.headers.common["Authorization"] = `Bearer ${token}`;
|
||||
} catch (error) {
|
||||
@@ -55,19 +57,19 @@ export const useAuthStore = create(
|
||||
},
|
||||
|
||||
signUp: async (userData) => {
|
||||
set({isLoading: true, error: null});
|
||||
set({ isLoading: true, error: null });
|
||||
try {
|
||||
const response = await axios.post(
|
||||
`${API_URL}/auth/register`,
|
||||
userData,
|
||||
{
|
||||
headers: {"Content-Type": "application/json"},
|
||||
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});
|
||||
set({ user_id: user_id, token: token, isLoading: false });
|
||||
|
||||
axios.defaults.headers.common["Authorization"] = `Bearer ${token}`;
|
||||
} catch (error) {
|
||||
@@ -80,18 +82,18 @@ export const useAuthStore = create(
|
||||
},
|
||||
|
||||
signInWithGoogle: async (googleToken) => {
|
||||
set({isLoading: true, error: null});
|
||||
set({ isLoading: true, error: null });
|
||||
try {
|
||||
const response = await axios.post(
|
||||
`${API_URL}/auth/google`,
|
||||
{googleToken: googleToken},
|
||||
{ googleToken: googleToken },
|
||||
{
|
||||
headers: {"Content-Type": "application/json"},
|
||||
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});
|
||||
set({ user_id: user_id, token: token, isLoading: false });
|
||||
|
||||
axios.defaults.headers.common["Authorization"] = `Bearer ${token}`;
|
||||
} catch (error) {
|
||||
@@ -104,34 +106,24 @@ export const useAuthStore = create(
|
||||
},
|
||||
|
||||
signOut: async () => {
|
||||
const { token } = get();
|
||||
const headers = token ? { Authorization: `Bearer ${token}` } : {};
|
||||
try {
|
||||
await axios.post(`${API_URL}/auth/logout`);
|
||||
await axios.post(
|
||||
`${API_URL}/auth/logout`,
|
||||
{},
|
||||
{
|
||||
headers: headers,
|
||||
}
|
||||
);
|
||||
} catch (error) {
|
||||
console.error("Logout error:", error);
|
||||
} finally {
|
||||
delete axios.defaults.headers.common["Authorization"];
|
||||
set({user_id: null, token: null});
|
||||
set({ user_id: null, token: null });
|
||||
router.replace("/login");
|
||||
}
|
||||
},
|
||||
|
||||
// checkAuth: async () => {
|
||||
// const { token } = useAuthStore.getState();
|
||||
// if (!token) return null;
|
||||
|
||||
// set({ isLoading: true });
|
||||
// try {
|
||||
// axios.defaults.headers.common["Authorization"] = `Bearer ${token}`;
|
||||
|
||||
// const response = await axios.get(`${API_URL}/auth/me`);
|
||||
|
||||
// set({ user_id: response.data, isLoading: false });
|
||||
// return response.data;
|
||||
// } catch (error) {
|
||||
// delete axios.defaults.headers.common["Authorization"];
|
||||
// set({ user_id: null, token: null, isLoading: false });
|
||||
// return null;
|
||||
// }
|
||||
// },
|
||||
};
|
||||
},
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user