diff --git a/src/screens/GroupScreen.tsx b/src/screens/GroupScreen.tsx index 4d49234..ae662c8 100644 --- a/src/screens/GroupScreen.tsx +++ b/src/screens/GroupScreen.tsx @@ -20,12 +20,12 @@ import { StackNavigationProp } from "@react-navigation/stack"; import { RootStackParamList } from "../navigation/AppNavigator"; import AppView from "../components/AppView"; import { apiCall } from "../utils/ApiHelper"; -import { BASE_URL, JOIN_GROUP_URL } from "../utils/ApiUrls"; +import { BASE_URL, JOIN_GROUP_URL, PUBLIC_GROUPS_URL } from "../utils/ApiUrls"; import colors from "../components/colors"; import { ApiResponse } from "./home/UserProfileStatus"; import * as Keychain from "react-native-keychain"; const { width } = Dimensions.get('window'); -const PUBLIC_GROUPS_URL = "https://api.godemo.in/FundCollect/api/fund/user/public"; + // --- Types --- diff --git a/src/screens/SplashScreen.tsx b/src/screens/SplashScreen.tsx index 44371f8..96d0bd9 100644 --- a/src/screens/SplashScreen.tsx +++ b/src/screens/SplashScreen.tsx @@ -1,92 +1,101 @@ -import React, { useEffect, useRef } from "react"; -import { View, Text, StyleSheet, ActivityIndicator, Animated, Alert } from "react-native"; +import React, { useEffect, useRef, useState } from "react"; +import { View, StyleSheet, ActivityIndicator, Animated, Alert } from "react-native"; import * as Keychain from "react-native-keychain"; -import { StackNavigationProp } from "@react-navigation/stack"; import { useNavigation } from "@react-navigation/native"; -import { RootStackParamList } from "../navigation/AppNavigator"; // adjust path +import { StackNavigationProp } from "@react-navigation/stack"; +import { useDispatch } from "react-redux"; + +// --- Import your project files --- +import { RootStackParamList } from "../navigation/AppNavigator"; // Adjust path import { BASE_URL } from "../utils/ApiUrls"; -import { useDispatch, useSelector } from "react-redux"; +import { fetchUserProfile } from "../utils/HelperFunction"; import { setUserData } from "../redux/userSlice"; import colors from "../components/colors"; import { ApiResponse } from "./home/UserProfileStatus"; -import { fetchUserProfile } from "../utils/HelperFunction"; const SplashScreen = () => { - - const user = useSelector((state: any) => state.user); const navigation = useNavigation>(); + const dispatch = useDispatch(); const fadeAnim = useRef(new Animated.Value(1)).current; - - - - - const userStatus = async () => { - try { - const credentials = await Keychain.getGenericPassword(); - if (!credentials) { - Alert.alert("Error", "You are not logged in"); - return; - } - const token = credentials.password; - - const response = await fetch(`${BASE_URL}user/profile/status`, { - method: "GET", - headers: { - Accept: "*/*", - Authorization: `Bearer ${token}`, - }, - }); - - const json: ApiResponse = await response.json(); - - console.log("👤 Profile Status Response:", json); - if (json.status === "success") { - - } else { - // setError(json.message || "Failed to fetch status"); - } - } catch (err) { - // setError("Something went wrong. Please try again."); - } - }; - - - console.log("redux user", user); + // State to track the outcome of initialization + const [isReady, setIsReady] = useState(false); + const [isLoggedIn, setIsLoggedIn] = useState(false); useEffect(() => { - const checkLogin = async () => { + // A single function to handle all startup tasks + const initializeApp = async () => { try { - const creds = await Keychain.getGenericPassword(); - if (creds) { - console.log("🔑 Found token:", creds.password); - navigation.replace("Home"); // go to bottom tabs if logged in - } else { - navigation.replace("Login"); + // 1. Check for stored credentials + const credentials = await Keychain.getGenericPassword(); + if (!credentials) { + console.log("No credentials found. User needs to log in."); + setIsLoggedIn(false); + return; // Stop here if not logged in } + + console.log("🔑 Credentials found. Proceeding with user data fetches."); + const { password: token } = credentials; + + // 2. Fetch all necessary data in parallel for efficiency + const [statusResponse, userProfile] = await Promise.all([ + fetch(`${BASE_URL}user/profile/status`, { + method: "GET", + headers: { + Accept: "*/*", + Authorization: `Bearer ${token}`, + }, + }), + fetchUserProfile(dispatch), // Assuming fetchUserProfile handles its own errors and dispatches to Redux + ]); + + // 3. Process the profile status response + if (!statusResponse.ok) { + throw new Error(`Server responded with status: ${statusResponse.status}`); + } + + const statusJson: ApiResponse = await statusResponse.json(); + console.log("👤 Profile Status Response:", statusJson); + + if (statusJson.status === "success") { + // You could dispatch more data here if needed + // dispatch(setUserData(statusJson.data)); + setIsLoggedIn(true); + } else { + throw new Error(statusJson.message || "Failed to verify user status."); + } + } catch (error) { - console.error("Error checking login:", error); - navigation.replace("Login"); + console.error("Initialization failed:", error); + Alert.alert("Error", "Your session may have expired. Please log in again."); + setIsLoggedIn(false); // Ensure login status is false on any error + } finally { + // 4. Signal that all tasks are done, whether they succeeded or failed + setIsReady(true); } }; - // Start fade out animation + // Trigger the animation, and run initialization logic after it completes Animated.timing(fadeAnim, { toValue: 0, - duration: 1500, // fade out in 1.5s + duration: 1500, useNativeDriver: true, - delay: 1500, // wait before fading + delay: 1500, }).start(() => { - checkLogin(); // only navigate after animation + initializeApp(); }); - }, [navigation, fadeAnim]); + }, [fadeAnim, dispatch]); + // This effect handles the final navigation step once everything is ready useEffect(() => { - const fetchProfile = async () => { - await fetchUserProfile(); - }; - fetchProfile(); - }, []) + if (isReady) { + if (isLoggedIn) { + navigation.replace("Home"); + } else { + navigation.replace("Login"); + } + } + }, [isReady, isLoggedIn, navigation]); return ( @@ -105,7 +114,7 @@ const styles = StyleSheet.create({ flex: 1, justifyContent: "center", alignItems: "center", - backgroundColor: "#FFFFFF", // white background + backgroundColor: "#FFFFFF", }, title: { fontSize: 28, diff --git a/src/screens/management/ManagementScreen.tsx b/src/screens/management/ManagementScreen.tsx index f3421ca..92f2b22 100644 --- a/src/screens/management/ManagementScreen.tsx +++ b/src/screens/management/ManagementScreen.tsx @@ -62,7 +62,8 @@ const ManagementScreen: FC = () => { console.log("👤 Profile Status Response:", json.data.role); if (json.status === "success") { - setUserRole(json.data.role); + console.log("User Role:", json.data); + setUserRole(json.data.role || user.role); if (json.data.profileStatus === false) { @@ -88,7 +89,8 @@ const ManagementScreen: FC = () => { setLoading(true); setError(null); try { - console.log(" abin user : ", userRole) + console.log(" abin user: ", userRole) + console.log('----',user) if (userRole === "ORGANIZER") { // Fetch all data concurrently const [myGroupsRes, joinedGroupsRes, requestsRes] = await Promise.all([ diff --git a/src/utils/ApiHelper.tsx b/src/utils/ApiHelper.tsx index 897d140..84552fa 100644 --- a/src/utils/ApiHelper.tsx +++ b/src/utils/ApiHelper.tsx @@ -17,7 +17,7 @@ export async function apiCall( try { // Get saved credentials from Keychain const creds = await Keychain.getGenericPassword(); - + console.log("hihihi", creds); const headers = { "Content-Type": "application/json", ...(creds ? { Authorization: `Bearer ${creds.password}` } : {}), diff --git a/src/utils/ApiUrls.tsx b/src/utils/ApiUrls.tsx index 8e62644..1862450 100644 --- a/src/utils/ApiUrls.tsx +++ b/src/utils/ApiUrls.tsx @@ -1,4 +1,5 @@ -export const BASE_URL = "https://api.godemo.in/FundCollect/api/" +// export const BASE_URL = "https://api.godemo.in/FundCollect/api/" +export const BASE_URL = "https://exotic-humpback-mistakenly.ngrok-free.app/api/" export const LOGIN_URL = `${BASE_URL}user/generate/otp?mobile=` export const VARIFY_OTP_URL = `${BASE_URL}user/otp/verify` export const ORGRAGISTRATION = `${BASE_URL}organizer/registration/apply` @@ -6,11 +7,11 @@ export const GROUP_CREATION_URL = `${BASE_URL}fund/groups/{organizerId}` export const PROFILE_URL =`${BASE_URL}user/profile/view` export const PUBLIC_GROUPS_URL =`${BASE_URL}fund/user/public` export const JOIN_GROUP_URL = `${BASE_URL}fund/`; -export const MY_GROUPS_URL =`https://api.godemo.in/FundCollect/api/fund/getAll/my/groups` +export const MY_GROUPS_URL =`${BASE_URL}fund/getAll/my/groups` -export const REQUESTS_URL =`https://api.godemo.in/FundCollect/api/fund/group/pending/request` +export const REQUESTS_URL =`${BASE_URL}fund/group/pending/request` //https://api.godemo.in/FundCollect/api/fund/groups/2a80cdaa-8532-4b8a-87f0-9c1023d18a7f/join/64 -export const APPROVE_REQUEST_URL =`https://api.godemo.in/FundCollect/api/fund/requests/` -export const DECLINE_REQUEST_URL =`https://api.godemo.in/FundCollect/api/fund/user/my/groups` +export const APPROVE_REQUEST_URL =`${BASE_URL}fund/requests/` +export const DECLINE_REQUEST_URL =`${BASE_URL}fund/user/my/groups` // https://api.godemo.in/FundCollect/api/fund/groups/b89f56a1-952d-41b3-ab8d-3c28c91697f5/join/100005 -export const GET_JOINED_GROUP_URL = `https://api.godemo.in/FundCollect/api/fund/my/join/groups` \ No newline at end of file +export const GET_JOINED_GROUP_URL = `${BASE_URL}fund/my/join/groups` \ No newline at end of file