Requeste api implelented
This commit is contained in:
@@ -1,13 +1,75 @@
|
||||
import React, { useEffect, useRef } from "react";
|
||||
import { View, Text, StyleSheet, ActivityIndicator, Animated } from "react-native";
|
||||
import { View, Text, 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 { BASE_URL } from "../utils/ApiUrls";
|
||||
import { useDispatch, useSelector } from "react-redux";
|
||||
import { setUserData } from "../redux/userSlice";
|
||||
import colors from "../components/colors";
|
||||
|
||||
const SplashScreen = () => {
|
||||
const dispatch = useDispatch();
|
||||
const user = useSelector((state: any) => state.user);
|
||||
const navigation = useNavigation<StackNavigationProp<RootStackParamList>>();
|
||||
const fadeAnim = useRef(new Animated.Value(1)).current; // opacity starts at 1
|
||||
const fadeAnim = useRef(new Animated.Value(1)).current;
|
||||
|
||||
const fetchUserProfile = 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/view`,
|
||||
{
|
||||
method: "GET",
|
||||
headers: {
|
||||
Accept: "*/*",
|
||||
Authorization: `Bearer ${token}`,
|
||||
},
|
||||
}
|
||||
);
|
||||
|
||||
const result = await response.json();
|
||||
if (result.status === "success") {
|
||||
dispatch(setUserData({
|
||||
id: result?.data.id,
|
||||
userId: result?.data.userId,
|
||||
username: result?.data.username,
|
||||
firstName: result?.data.firstName,
|
||||
lastName: result?.data.lastName,
|
||||
gender: result?.data.gender,
|
||||
mobile: result?.data.mobile,
|
||||
email: result?.data.email,
|
||||
address: result?.data.address,
|
||||
bankAccountNumber: result?.data.bankAccountNumber,
|
||||
bankName: result?.data.bankName,
|
||||
branchCode: result?.data.branchCode,
|
||||
profilePicture: result?.data.filePath, // or keep as `filePath`
|
||||
filePath: result?.data.filePath,
|
||||
imageBlob: result?.data.imageBlob,
|
||||
role: result?.data.role,
|
||||
organizerId: result?.data.organizerId,
|
||||
organizerStatus: result?.data.organizerStatus,
|
||||
profileStatus: true, // set true once profile is fetched
|
||||
}));
|
||||
|
||||
console.log("splash user data:", result.data);
|
||||
} else {
|
||||
Alert.alert("Error", "Failed to fetch user profile");
|
||||
}
|
||||
} catch (error) {
|
||||
console.error("❌ Error fetching profile:", error);
|
||||
Alert.alert("Error", "Could not connect to server");
|
||||
}
|
||||
};
|
||||
|
||||
console.log("redux user", user);
|
||||
|
||||
useEffect(() => {
|
||||
const checkLogin = async () => {
|
||||
@@ -36,12 +98,16 @@ const SplashScreen = () => {
|
||||
});
|
||||
}, [navigation, fadeAnim]);
|
||||
|
||||
useEffect(() => {
|
||||
fetchUserProfile();
|
||||
}, [])
|
||||
|
||||
return (
|
||||
<View style={styles.container}>
|
||||
<Animated.Text style={[styles.title, { opacity: fadeAnim }]}>
|
||||
Circle Collect
|
||||
</Animated.Text>
|
||||
<ActivityIndicator size="large" color="#FF7A00" style={{ marginTop: 20 }} />
|
||||
<ActivityIndicator size="large" color={colors.Home} style={{ marginTop: 20 }} />
|
||||
</View>
|
||||
);
|
||||
};
|
||||
@@ -58,7 +124,7 @@ const styles = StyleSheet.create({
|
||||
title: {
|
||||
fontSize: 28,
|
||||
fontWeight: "800",
|
||||
color: "#FF7A00",
|
||||
color: colors.Home,
|
||||
letterSpacing: 1,
|
||||
},
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user