user data
This commit is contained in:
@@ -20,12 +20,12 @@ import { StackNavigationProp } from "@react-navigation/stack";
|
|||||||
import { RootStackParamList } from "../navigation/AppNavigator";
|
import { RootStackParamList } from "../navigation/AppNavigator";
|
||||||
import AppView from "../components/AppView";
|
import AppView from "../components/AppView";
|
||||||
import { apiCall } from "../utils/ApiHelper";
|
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 colors from "../components/colors";
|
||||||
import { ApiResponse } from "./home/UserProfileStatus";
|
import { ApiResponse } from "./home/UserProfileStatus";
|
||||||
import * as Keychain from "react-native-keychain";
|
import * as Keychain from "react-native-keychain";
|
||||||
const { width } = Dimensions.get('window');
|
const { width } = Dimensions.get('window');
|
||||||
const PUBLIC_GROUPS_URL = "https://api.godemo.in/FundCollect/api/fund/user/public";
|
|
||||||
|
|
||||||
|
|
||||||
// --- Types ---
|
// --- Types ---
|
||||||
|
|||||||
@@ -1,92 +1,101 @@
|
|||||||
import React, { useEffect, useRef } from "react";
|
import React, { useEffect, useRef, useState } from "react";
|
||||||
import { View, Text, StyleSheet, ActivityIndicator, Animated, Alert } from "react-native";
|
import { View, StyleSheet, ActivityIndicator, Animated, Alert } from "react-native";
|
||||||
import * as Keychain from "react-native-keychain";
|
import * as Keychain from "react-native-keychain";
|
||||||
import { StackNavigationProp } from "@react-navigation/stack";
|
|
||||||
import { useNavigation } from "@react-navigation/native";
|
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 { BASE_URL } from "../utils/ApiUrls";
|
||||||
import { useDispatch, useSelector } from "react-redux";
|
import { fetchUserProfile } from "../utils/HelperFunction";
|
||||||
import { setUserData } from "../redux/userSlice";
|
import { setUserData } from "../redux/userSlice";
|
||||||
import colors from "../components/colors";
|
import colors from "../components/colors";
|
||||||
import { ApiResponse } from "./home/UserProfileStatus";
|
import { ApiResponse } from "./home/UserProfileStatus";
|
||||||
import { fetchUserProfile } from "../utils/HelperFunction";
|
|
||||||
|
|
||||||
const SplashScreen = () => {
|
const SplashScreen = () => {
|
||||||
|
|
||||||
const user = useSelector((state: any) => state.user);
|
|
||||||
const navigation = useNavigation<StackNavigationProp<RootStackParamList>>();
|
const navigation = useNavigation<StackNavigationProp<RootStackParamList>>();
|
||||||
|
const dispatch = useDispatch();
|
||||||
const fadeAnim = useRef(new Animated.Value(1)).current;
|
const fadeAnim = useRef(new Animated.Value(1)).current;
|
||||||
|
|
||||||
|
// State to track the outcome of initialization
|
||||||
|
const [isReady, setIsReady] = useState(false);
|
||||||
|
const [isLoggedIn, setIsLoggedIn] = useState(false);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
// A single function to handle all startup tasks
|
||||||
|
const initializeApp = async () => {
|
||||||
const userStatus = async () => {
|
|
||||||
try {
|
try {
|
||||||
|
// 1. Check for stored credentials
|
||||||
const credentials = await Keychain.getGenericPassword();
|
const credentials = await Keychain.getGenericPassword();
|
||||||
if (!credentials) {
|
if (!credentials) {
|
||||||
Alert.alert("Error", "You are not logged in");
|
console.log("No credentials found. User needs to log in.");
|
||||||
return;
|
setIsLoggedIn(false);
|
||||||
|
return; // Stop here if not logged in
|
||||||
}
|
}
|
||||||
const token = credentials.password;
|
|
||||||
|
|
||||||
const response = await fetch(`${BASE_URL}user/profile/status`, {
|
console.log("🔑 Credentials found. Proceeding with user data fetches.");
|
||||||
method: "GET",
|
const { password: token } = credentials;
|
||||||
headers: {
|
|
||||||
Accept: "*/*",
|
|
||||||
Authorization: `Bearer ${token}`,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
const json: ApiResponse = await response.json();
|
// 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
|
||||||
|
]);
|
||||||
|
|
||||||
console.log("👤 Profile Status Response:", json);
|
// 3. Process the profile status response
|
||||||
if (json.status === "success") {
|
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 {
|
} else {
|
||||||
// setError(json.message || "Failed to fetch status");
|
throw new Error(statusJson.message || "Failed to verify user status.");
|
||||||
}
|
}
|
||||||
} catch (err) {
|
|
||||||
// setError("Something went wrong. Please try again.");
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
console.log("redux user", user);
|
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
const checkLogin = 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");
|
|
||||||
}
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("Error checking login:", error);
|
console.error("Initialization failed:", error);
|
||||||
navigation.replace("Login");
|
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, {
|
Animated.timing(fadeAnim, {
|
||||||
toValue: 0,
|
toValue: 0,
|
||||||
duration: 1500, // fade out in 1.5s
|
duration: 1500,
|
||||||
useNativeDriver: true,
|
useNativeDriver: true,
|
||||||
delay: 1500, // wait before fading
|
delay: 1500,
|
||||||
}).start(() => {
|
}).start(() => {
|
||||||
checkLogin(); // only navigate after animation
|
initializeApp();
|
||||||
});
|
});
|
||||||
}, [navigation, fadeAnim]);
|
}, [fadeAnim, dispatch]);
|
||||||
|
|
||||||
|
// This effect handles the final navigation step once everything is ready
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const fetchProfile = async () => {
|
if (isReady) {
|
||||||
await fetchUserProfile();
|
if (isLoggedIn) {
|
||||||
};
|
navigation.replace("Home");
|
||||||
fetchProfile();
|
} else {
|
||||||
}, [])
|
navigation.replace("Login");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}, [isReady, isLoggedIn, navigation]);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<View style={styles.container}>
|
<View style={styles.container}>
|
||||||
@@ -105,7 +114,7 @@ const styles = StyleSheet.create({
|
|||||||
flex: 1,
|
flex: 1,
|
||||||
justifyContent: "center",
|
justifyContent: "center",
|
||||||
alignItems: "center",
|
alignItems: "center",
|
||||||
backgroundColor: "#FFFFFF", // white background
|
backgroundColor: "#FFFFFF",
|
||||||
},
|
},
|
||||||
title: {
|
title: {
|
||||||
fontSize: 28,
|
fontSize: 28,
|
||||||
|
|||||||
@@ -62,7 +62,8 @@ const ManagementScreen: FC = () => {
|
|||||||
|
|
||||||
console.log("👤 Profile Status Response:", json.data.role);
|
console.log("👤 Profile Status Response:", json.data.role);
|
||||||
if (json.status === "success") {
|
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) {
|
if (json.data.profileStatus === false) {
|
||||||
|
|
||||||
@@ -88,7 +89,8 @@ const ManagementScreen: FC = () => {
|
|||||||
setLoading(true);
|
setLoading(true);
|
||||||
setError(null);
|
setError(null);
|
||||||
try {
|
try {
|
||||||
console.log(" abin user : ", userRole)
|
console.log(" abin user: ", userRole)
|
||||||
|
console.log('----',user)
|
||||||
if (userRole === "ORGANIZER") {
|
if (userRole === "ORGANIZER") {
|
||||||
// Fetch all data concurrently
|
// Fetch all data concurrently
|
||||||
const [myGroupsRes, joinedGroupsRes, requestsRes] = await Promise.all([
|
const [myGroupsRes, joinedGroupsRes, requestsRes] = await Promise.all([
|
||||||
|
|||||||
@@ -17,7 +17,7 @@ export async function apiCall<T>(
|
|||||||
try {
|
try {
|
||||||
// Get saved credentials from Keychain
|
// Get saved credentials from Keychain
|
||||||
const creds = await Keychain.getGenericPassword();
|
const creds = await Keychain.getGenericPassword();
|
||||||
|
console.log("hihihi", creds);
|
||||||
const headers = {
|
const headers = {
|
||||||
"Content-Type": "application/json",
|
"Content-Type": "application/json",
|
||||||
...(creds ? { Authorization: `Bearer ${creds.password}` } : {}),
|
...(creds ? { Authorization: `Bearer ${creds.password}` } : {}),
|
||||||
|
|||||||
@@ -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 LOGIN_URL = `${BASE_URL}user/generate/otp?mobile=`
|
||||||
export const VARIFY_OTP_URL = `${BASE_URL}user/otp/verify`
|
export const VARIFY_OTP_URL = `${BASE_URL}user/otp/verify`
|
||||||
export const ORGRAGISTRATION = `${BASE_URL}organizer/registration/apply`
|
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 PROFILE_URL =`${BASE_URL}user/profile/view`
|
||||||
export const PUBLIC_GROUPS_URL =`${BASE_URL}fund/user/public`
|
export const PUBLIC_GROUPS_URL =`${BASE_URL}fund/user/public`
|
||||||
export const JOIN_GROUP_URL = `${BASE_URL}fund/`;
|
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
|
//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 APPROVE_REQUEST_URL =`${BASE_URL}fund/requests/`
|
||||||
export const DECLINE_REQUEST_URL =`https://api.godemo.in/FundCollect/api/fund/user/my/groups`
|
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
|
// 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`
|
export const GET_JOINED_GROUP_URL = `${BASE_URL}fund/my/join/groups`
|
||||||
Reference in New Issue
Block a user