81 lines
2.4 KiB
TypeScript
81 lines
2.4 KiB
TypeScript
import { useDispatch } from "react-redux";
|
|
import { BASE_URL } from "./ApiUrls";
|
|
import * as Keychain from "react-native-keychain";
|
|
import { setUserData } from "../redux/userSlice";
|
|
import { Alert } from "react-native";
|
|
|
|
export const fetchUserProfile = async () => {
|
|
const dispatch = useDispatch();
|
|
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();
|
|
console.log("user api data", result)
|
|
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");
|
|
}
|
|
};
|
|
|
|
// src/utils/dateFormatter.ts
|
|
|
|
export const formatDate = (dateString: string): string => {
|
|
const date = new Date(dateString);
|
|
|
|
if (isNaN(date.getTime())) return "Invalid Date";
|
|
|
|
const options: Intl.DateTimeFormatOptions = {
|
|
day: "2-digit",
|
|
month: "short",
|
|
year: "numeric",
|
|
hour: "2-digit",
|
|
minute: "2-digit",
|
|
hour12: true,
|
|
};
|
|
|
|
return new Intl.DateTimeFormat("en-GB", options).format(date);
|
|
};
|