implement redux toolkit for manage globle state

This commit is contained in:
Jaanu423
2025-09-16 18:30:30 +05:30
parent 6ed8163d25
commit a931f886b2
7 changed files with 317 additions and 160 deletions
+13
View File
@@ -0,0 +1,13 @@
// store.ts
import { configureStore } from "@reduxjs/toolkit";
import userReducer from "./userSlice"; // adjust the path
export const store = configureStore({
reducer: {
user: userReducer, // register user slice here
},
});
// ✅ Types for TypeScript
export type RootState = ReturnType<typeof store.getState>;
export type AppDispatch = typeof store.dispatch;
+46
View File
@@ -0,0 +1,46 @@
import { createSlice } from "@reduxjs/toolkit";
import type { PayloadAction } from "@reduxjs/toolkit";
export interface UserState {
id: string;
name: string;
email: string;
profilePicture: string;
role: string;
organizerStatus: string;
profileStatus: boolean;
}
const initialState: UserState = {
id: "",
name: "",
email: "",
profilePicture: "",
role: "",
organizerStatus: "",
profileStatus: false,
};
export const userSlice = createSlice({
name: "user",
initialState,
reducers: {
// ✅ set all user details at once (e.g. after login)
setUser: (state, action: PayloadAction<UserState>) => {
return { ...action.payload };
},
// ✅ update only selected fields (e.g. profile update)
updateUser: (state, action: PayloadAction<Partial<UserState>>) => {
return { ...state, ...action.payload };
},
// ✅ clear user data (e.g. on logout)
clearUser: () => initialState,
},
});
// Action creators
export const { setUser, updateUser, clearUser } = userSlice.actions;
export default userSlice.reducer;
-4
View File
@@ -24,17 +24,13 @@ const API_URL = "https://api.godemo.in/FundCollect/api/fund/getAll/my/groups";
const TOKEN =
"Bearer eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiI2MjY1MjI5MzcxIiwidXNlcklkIjoiMTAwMDAxIiwiaWF0IjoxNzU4MDAyOTkxLCJleHAiOjE3NTgwODkzOTF9.mMRPsULcvdfdCrJJBDjnT2okE3dJsTn42JBqi_vP0Xk";
// ✅ Group Card
const GroupCard = ({ item }: { item: any }) => {
const [expanded, setExpanded] = useState(false);
const toggleExpand = () => {
LayoutAnimation.easeInEaseOut();
setExpanded(!expanded);
};
const initial = item.groupName ? item.groupName.charAt(0).toUpperCase() : "?";
return (
<View style={styles.groupCard}>
<View style={styles.groupLeft}>
+17 -22
View File
@@ -12,32 +12,27 @@ import UserProfileStatus from "./UserProfileStatus";
import colors from "../../components/colors";
import { Ionicons } from "@react-native-vector-icons/ionicons";
import GroupsTabs from "./GroupsTabs";
// Enable animation for Android
if (Platform.OS === "android" && UIManager.setLayoutAnimationEnabledExperimental) {
UIManager.setLayoutAnimationEnabledExperimental(true);
}
const Home = () => {
return (
<AppView barStyle="dark-content">
{/* Header */}
<View style={styles.header}>
<Text style={styles.greeting}>Hello, Pooja 👋</Text>
<TouchableOpacity>
<Ionicons
name="person-circle-outline"
size={40}
color={colors.primary}
/>
</TouchableOpacity>
</View>
{/* Status */}
<UserProfileStatus />
<GroupsTabs />
{/* Header */}
<View style={styles.header}>
<Text style={styles.greeting}>Hello, Pooja 👋</Text>
<TouchableOpacity>
<Ionicons
name="person-circle-outline"
size={40}
color={colors.primary}
/>
</TouchableOpacity>
</View>
{/* Status */}
<UserProfileStatus />
<GroupsTabs />
</AppView>
);
};
@@ -45,7 +40,7 @@ const Home = () => {
export default Home;
const styles = StyleSheet.create({
container: { },
container: {},
header: {
flexDirection: "row",
justifyContent: "space-between",
@@ -53,6 +48,6 @@ const styles = StyleSheet.create({
marginBottom: 12,
},
greeting: { fontSize: 22, fontWeight: "600", color: colors.textDark },
});