Skip to content

Commit 884162f

Browse files
committed
saga integrated with redux store
1 parent 060c23b commit 884162f

File tree

12 files changed

+341
-39
lines changed

12 files changed

+341
-39
lines changed

App.tsx

Lines changed: 16 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { View, Text,Button } from 'react-native'
1+
import { View, Text, Button } from 'react-native'
22
import React, { useEffect } from 'react'
33
import FirstComp from './components/FirstComp'
44
import ButtonEvent from './components/ButtonEvent'
@@ -29,27 +29,19 @@ import PostApiComp from './components/apiInNative/PostApiComp'
2929
import APIListComp from './components/apiInNative/APIListComp'
3030
import HeaderComp from './components/reduxComponentUiTest/HeaderComp'
3131
import ProductComp from './components/reduxComponentUiTest/ProductComp'
32+
import { NavigationContainer } from '@react-navigation/native';
33+
import { createNativeStackNavigator } from '@react-navigation/native-stack';
34+
import HeaderAndProduct from './HeaderAndProduct'
35+
import UserListNavigationRedux from './components/reduxComponentUiTest/UserListNavigationRedux'
36+
37+
38+
const Stack = createNativeStackNavigator();
3239

3340
const App = () => {
3441
useEffect(() => {
3542
console.log("hello")
3643
}, [])
37-
const products = [{
38-
name: 'POCO M2 Pro',
39-
price: '20000',
40-
color: 'Navy Blue'
41-
},
42-
{
43-
name: 'POCO M3 Pro',
44-
price: '20000',
45-
color: 'Navy Blue'
46-
},
47-
{
48-
name: 'Mi 11 Pro',
49-
price: '20000',
50-
color: 'Navy Blue'
51-
}
52-
]
44+
5345
return (
5446
<View style={{ flex: 1 }}>
5547
<Text style={{ fontSize: 30, textAlign: 'center', color: "blue", marginTop: 10, fontWeight: "bold" }}>
@@ -85,12 +77,13 @@ const App = () => {
8577
{/* <SimpleFreeApi/> */}
8678
{/* <PostApiComp/> */}
8779
{/* <APIListComp/> */}
88-
<HeaderComp />
89-
<View style={{ flex: 13, backgroundColor: 'pink' }}>
90-
{products.map((item) =>
91-
<ProductComp item={item}/>
92-
)}
93-
</View>
80+
81+
<NavigationContainer>
82+
<Stack.Navigator>
83+
<Stack.Screen name="HeaderAndProduct" component={HeaderAndProduct} />
84+
<Stack.Screen name="UserListNavigationRedux" component={UserListNavigationRedux} />
85+
</Stack.Navigator>
86+
</NavigationContainer>
9487
</View>
9588
)
9689
}

HeaderAndProduct.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import { View, Text,Button } from 'react-native'
2+
import React from 'react'
3+
import HeaderComp from './components/reduxComponentUiTest/HeaderComp'
4+
import ProductComp from './components/reduxComponentUiTest/ProductComp'
5+
6+
7+
const HeaderAndProduct = ({navigation}) => {
8+
const products = [{
9+
name: 'POCO M2 Pro',
10+
price: '20000',
11+
color: 'Navy Blue'
12+
},
13+
{
14+
name: 'POCO M3 Pro',
15+
price: '20000',
16+
color: 'Navy Blue'
17+
},
18+
{
19+
name: 'Mi 11 Pro',
20+
price: '20000',
21+
color: 'Navy Blue'
22+
}
23+
]
24+
return (
25+
<View style={{flex:1}}>
26+
<Button title='Move to user List' onPress={()=>{navigation.navigate("UserListNavigationRedux")}}/>
27+
<HeaderComp />
28+
<View style={{ flex: 13, backgroundColor: 'pink' }}>
29+
{products.map((item) =>
30+
<ProductComp item={item} key={item.name}/>
31+
)}
32+
</View>
33+
</View>
34+
)
35+
}
36+
37+
export default HeaderAndProduct

components/reduxComponentUiTest/ProductComp.js

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,39 @@
11
import { View, Text,Button } from 'react-native'
2-
import React from 'react'
3-
import { addToCart } from '../../redux/action';
4-
import { useDispatch } from 'react-redux';
2+
import React, { useEffect, useState } from 'react'
3+
import { addToCart,removeFromCart } from '../../redux/action';
4+
import { useDispatch,useSelector } from 'react-redux';
55

66
const ProductComp = (props) => {
77
const item=props.item;
88
const dispatch = useDispatch();
9+
const cartItem=useSelector((state)=>state.reducer);
10+
11+
const[isAdded,setIsAdded]=useState(true);
912

1013
const handleAddToCart=(item)=>{
1114
//console.warn("Added" ,item);
1215
dispatch(addToCart(item))
1316
}
17+
const handleRemoveFRomCart=(item)=>{
18+
//console.warn(item);
19+
dispatch(removeFromCart(item.name))
20+
}
21+
useEffect(()=>{
22+
let result=cartItem.filter(element=>{
23+
return element.name===item.name
24+
})
25+
if(result.length){
26+
setIsAdded(false)
27+
}else{
28+
setIsAdded(true)
29+
}
30+
},[cartItem])
1431
return (
1532
<View key={item.name} style={{ alignItems: 'center', margin: 20 }}>
1633
<Text style={{ fontSize: 18 }}>{item.name}</Text>
1734
<Text style={{ fontSize: 18 }}>{item.price}</Text>
1835
<Text style={{ fontSize: 18 }}>{item.color}</Text>
19-
<Button title='Add to Cart' onPress={()=>{handleAddToCart(item)}} />
36+
<Button title={isAdded?'Add To Cart':"Remove from Cart"} onPress={()=>{isAdded?handleAddToCart(item):handleRemoveFRomCart(item)}} />
2037
</View>
2138
)
2239
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { View, Text } from 'react-native'
2+
import React, { useEffect } from 'react'
3+
import { useSelector, useDispatch } from 'react-redux'
4+
import { fetchUserList } from '../../redux/action'
5+
6+
const UserListNavigationRedux = () => {
7+
const userListArray = useSelector((state) => state.reducer);
8+
const userList = userListArray.length ? userListArray[0] : [];
9+
console.warn(userList)
10+
11+
console.log(userList)
12+
const dispatch = useDispatch();
13+
useEffect(() => {
14+
dispatch(fetchUserList())
15+
},[])
16+
return (
17+
<View style={{ flex: 1 }}>
18+
{
19+
userList.length ?
20+
userList.map((item) => {
21+
console.log("users names :",item.name);
22+
return (
23+
<Text style={{ fontSize: 18 }} key={item.id}>{item.name}</Text>
24+
);
25+
}) :
26+
<Text>No Data</Text>
27+
}
28+
</View>
29+
)
30+
}
31+
32+
export default UserListNavigationRedux

package-lock.json

Lines changed: 99 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
"@react-navigation/material-bottom-tabs": "^6.2.15",
1515
"@react-navigation/material-top-tabs": "^6.6.2",
1616
"@react-navigation/native": "^6.1.6",
17+
"@react-navigation/native-stack": "^6.9.12",
1718
"@react-navigation/stack": "^6.3.16",
1819
"@reduxjs/toolkit": "^1.9.5",
1920
"react": "18.2.0",
@@ -25,7 +26,8 @@
2526
"react-native-screens": "^3.20.0",
2627
"react-native-vector-icons": "^9.2.0",
2728
"react-redux": "^8.0.5",
28-
"redux": "^4.2.1"
29+
"redux": "^4.2.1",
30+
"redux-saga": "^1.2.3"
2931
},
3032
"devDependencies": {
3133
"@babel/core": "^7.20.0",

redux/action.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,19 @@
1-
import { ADD_TO_CART } from "./constants";
1+
import { ADD_TO_CART,REMOVE_FROM_CART,USER_LIST } from "./constants";
22

33
export function addToCart(item){
44
return {
55
type:ADD_TO_CART,
66
data:item
77
}
8-
}
8+
}
9+
export const removeFromCart=(item)=>{
10+
return{
11+
type:REMOVE_FROM_CART,
12+
data:item
13+
}
14+
}
15+
16+
export const fetchUserList = () => ({
17+
type: USER_LIST,
18+
});
19+

redux/constants.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,4 @@
1-
export const ADD_TO_CART="add_to_cart";
1+
export const ADD_TO_CART="add_to_cart";
2+
export const REMOVE_FROM_CART="remove_from_cart";
3+
export const USER_LIST ="user_list";
4+
export const SET_USER_DATA="set_user_data";

0 commit comments

Comments
 (0)