with helper funtion

This commit is contained in:
Abin Toppo
2025-09-19 13:39:04 +05:30
parent 4de484c0a5
commit d4a1866f34
8 changed files with 314 additions and 316 deletions
+20 -15
View File
@@ -14,7 +14,7 @@ import {
import { useNavigation, useFocusEffect } from "@react-navigation/native";
import { StackNavigationProp } from "@react-navigation/stack";
import { RootStackParamList } from "../../navigation/AppNavigator";
import { APPROVE_REQUEST_URL, BASE_URL, DECLINE_REQUEST_URL, JOIN_GROUP_URL, MY_GROUPS_URL, REQUESTS_URL } from "../../utils/ApiUrls";
import { APPROVE_REQUEST_URL, BASE_URL, DECLINE_REQUEST_URL, GET_JOINED_GROUP_URL, JOIN_GROUP_URL, MY_GROUPS_URL, REQUESTS_URL } from "../../utils/ApiUrls";
import colors from "../../components/colors";
import AppView from "../../components/AppView";
import { apiCall } from "../../utils/ApiHelper";
@@ -23,11 +23,13 @@ import { MyGroup, JoinedGroup, JoinRequest, ApiResponse } from '../../types';
import { MyGroupCard } from "../../components/MyGroupCard";
import { JoinedGroupCard } from "../../components/JoinedGroupCard";
import { RequestCard } from "../../components/RequestCard";
import { useSelector } from "react-redux";
type GroupManagementNavProp = StackNavigationProp<RootStackParamList>;
type ActiveTab = "My Groups" | "Joined Groups" | "Requests";
type ActiveTab = "My Groups" | "Joined Groups" | "Requests" | "Your Requests";
const ManagementScreen: FC = () => {
const user = useSelector((state:any) => state.user)
const [activeTab, setActiveTab] = useState<ActiveTab>("My Groups");
const navigation = useNavigation<GroupManagementNavProp>();
const [myGroups, setMyGroups] = useState<MyGroup[]>([]);
@@ -35,8 +37,9 @@ const ManagementScreen: FC = () => {
const [requests, setRequests] = useState<JoinRequest[]>([]);
const [loading, setLoading] = useState<boolean>(false);
const [error, setError] = useState<string | null>(null);
const userRole = "ORGANIZER";
const userRole = user.role;
console.log(" abin user : ", user)
const fetchData = useCallback(async () => {
if (loading) return;
setLoading(true);
@@ -44,9 +47,9 @@ const ManagementScreen: FC = () => {
try {
if (userRole === "ORGANIZER") {
// Fetch all data concurrently
const [myGroupsRes,joinedGroupsRes,requestsRes] = await Promise.all([
const [myGroupsRes, joinedGroupsRes, requestsRes] = await Promise.all([
apiCall(MY_GROUPS_URL, "GET") as Promise<ApiResponse<MyGroup[]>>,
apiCall(JOIN_GROUP_URL, "GET") as Promise<ApiResponse<JoinedGroup[]>>, // Assuming this is also needed
apiCall(GET_JOINED_GROUP_URL, "GET") as Promise<ApiResponse<JoinedGroup[]>>, // Assuming this is also needed
apiCall(REQUESTS_URL, "GET") as Promise<ApiResponse<JoinRequest[]>>,
]);
@@ -58,8 +61,10 @@ const ManagementScreen: FC = () => {
setRequests(requestsRes.data || []);
} else {
// Fetch only joined groups for non-organizers
// const joinedGroupsRes = await apiCall(JOINED_GROUPS_URL, "GET") as Promise<ApiResponse<JoinedGroup[]>>;
// setJoinedGroups(joinedGroupsRes.data || []);
const [joinedGroupsRes] = await Promise.all([
apiCall(GET_JOINED_GROUP_URL, "GET") as Promise<ApiResponse<JoinedGroup[]>>, // Assuming this is also needed
]);
setJoinedGroups(joinedGroupsRes.data || []);
}
} catch (e) {
console.error("Fetch Data Error:", e);
@@ -74,7 +79,7 @@ const ManagementScreen: FC = () => {
const handleAction = async (url: string, body: object, successMessage: string, failureMessage: string, action: string) => {
// ... (logic remains the same)
console.log(url)
try {
try {
setLoading(true); // Optionally show a loading state during the action
// Make the POST request with an empty body
const res = await apiCall(url, "PUT", {}) as ApiResponse<null> | null;
@@ -93,15 +98,15 @@ const ManagementScreen: FC = () => {
}
};
const handleApprove = (req: JoinRequest) => handleAction(APPROVE_REQUEST_URL+req.requestId+"/approve", { requestId: req.requestId }, `${req.userName} has been added.`, "Failed to approve request.","Approve");
const handleDecline = (req: JoinRequest) => handleAction(DECLINE_REQUEST_URL+req.requestId+"/reject?reason=/"+" ", { requestId: req.requestId }, `Request from ${req.userName} has been declined.`, "Failed to decline request.","Decline");
const handleApprove = (req: JoinRequest) => handleAction(APPROVE_REQUEST_URL + req.requestId + "/approve", { requestId: req.requestId }, `${req.userName} has been added.`, "Failed to approve request.", "Approve");
const handleDecline = (req: JoinRequest) => handleAction(DECLINE_REQUEST_URL + req.requestId + "/reject?reason=/" + " ", { requestId: req.requestId }, `Request from ${req.userName} has been declined.`, "Failed to decline request.", "Decline");
const handleManageGroup = (group: MyGroup) => navigation.navigate('EditGroup', { groupId: group.id });
const renderContent = () => {
if (error) {
return <Text style={styles.emptyText}>{error}</Text>;
}
switch (activeTab) {
case "My Groups":
if (loading) {
@@ -151,9 +156,9 @@ const ManagementScreen: FC = () => {
default: return null;
}
};
const tabs: ActiveTab[] = userRole === 'ORGANIZER' ? ["My Groups", "Joined Groups", "Requests"] : ["Joined Groups"];
const tabs: ActiveTab[] = userRole === 'ORGANIZER' ? ["My Groups", "Joined Groups", "Requests"] : ["Joined Groups", "Your Requests"];
return (
<AppView style={styles.container} paddingHorizontal={0}>
<View style={styles.header}>
@@ -176,7 +181,7 @@ const ManagementScreen: FC = () => {
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#f5f6fa'
backgroundColor: '#f5f6fa'
},
header: {
paddingHorizontal: 20,