Skip to content

Commit 897aca3

Browse files
author
YONGJAE LEE
committed
[#58] Context API를 Redux로 바꾸는 작업을 하라
1 parent a6be1c0 commit 897aca3

20 files changed

Lines changed: 384 additions & 161 deletions

File tree

.eslintrc.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ module.exports = {
66
},
77
},
88
},
9+
globals: {
10+
context: 'readonly',
11+
},
912
env: {
1013
amd: true,
1114
browser: true,

App.jsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import RootStackNavigator from './src/components/navigation/RootStackNavigator';
1+
import App from './src/App';
22

33
const STORYBOOK_START = false;
44
/* eslint-disable global-require */
55
export default STORYBOOK_START ? require('./storybook').default
6-
: RootStackNavigator;
6+
: App;
77
/* eslint-enable global-require */

package.json

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
"@react-navigation/bottom-tabs": "^5.7.3",
2424
"@react-navigation/native": "^5.7.3",
2525
"@react-navigation/stack": "^5.8.0",
26+
"@reduxjs/toolkit": "^1.4.0",
2627
"@storybook/addon-actions": "^5.3.19",
2728
"@storybook/addon-links": "^5.3.19",
2829
"@storybook/addons": "^5.3.19",
@@ -51,6 +52,9 @@
5152
"react-native-web": "~0.11.7",
5253
"react-native-webview": "9.4.0",
5354
"react-naver-maps": "^0.0.13",
55+
"react-redux": "^7.2.1",
56+
"redux": "^4.0.5",
57+
"redux-thunk": "^2.3.0",
5458
"styled-components": "^5.1.1",
5559
"victory": "^35.0.8",
5660
"victory-native": "^35.0.1"
@@ -73,10 +77,12 @@
7377
"eslint-plugin-jsx-a11y": "^6.3.1",
7478
"eslint-plugin-react": "^7.20.3",
7579
"eslint-plugin-react-hooks": "^4.0.7",
80+
"flow-bin": "^0.134.0",
7681
"given2": "^2.1.7",
7782
"jest": "^26.1.0",
7883
"jest-expo": "^38.0.2",
79-
"jest-plugin-context": "^2.9.0"
84+
"jest-plugin-context": "^2.9.0",
85+
"redux-mock-store": "^1.5.4"
8086
},
8187
"private": true
8288
}

src/App.jsx

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import React from 'react';
2+
3+
import { Provider } from 'react-redux';
4+
5+
import store from './store';
6+
7+
import RootStackNavigator from './components/navigation/RootStackNavigator';
8+
9+
export default function App() {
10+
return (
11+
<Provider store={store}>
12+
<RootStackNavigator />
13+
</Provider>
14+
);
15+
}

src/__mocks__/react-redux.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export const useDispatch = jest.fn();
2+
3+
export const useSelector = jest.fn();

src/components/navigation/MainStackNavigator.js

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* eslint-disable react/jsx-boolean-value */
22
/* eslint-disable react/jsx-props-no-spreading */
3-
import React, { useContext, useEffect, useState } from 'react';
3+
import React, { useEffect, useState } from 'react';
44
import { View, Button, Platform } from 'react-native';
55

66
import { WebView } from 'react-native-webview';
@@ -11,9 +11,13 @@ import Icon from 'react-native-vector-icons/FontAwesome';
1111

1212
import { Fireworks } from 'fireworks/lib/react';
1313

14-
import HomeTabNavigator from './HomeTabNavigator';
14+
import { useDispatch, useSelector } from 'react-redux';
15+
16+
import { toggleAuth } from '../../slice';
1517

16-
import { AuthContext } from '../../contexts';
18+
import { get } from '../../utils';
19+
20+
import HomeTabNavigator from './HomeTabNavigator';
1721

1822
import { HeaderSide, HeaderTitle, HeaderTitleText } from './style';
1923

@@ -32,7 +36,8 @@ function Test({ navigation }) {
3236
}
3337

3438
export default function MainStackNavigator({ navigation }) {
35-
const { toggleAuth, location } = useContext(AuthContext);
39+
const dispatch = useDispatch();
40+
const location = useSelector(get('location'));
3641

3742
const [fireworks, setFireworks] = useState(false);
3843

@@ -84,7 +89,7 @@ export default function MainStackNavigator({ navigation }) {
8489
);
8590

8691
const headerRight = () => (
87-
<HeaderSide onPress={() => toggleAuth()}>
92+
<HeaderSide onPress={() => dispatch(toggleAuth())}>
8893
<Icon name="id-card" size={20} color={COLOR_PRIMARY} />
8994
</HeaderSide>
9095
);
Lines changed: 26 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1-
import React, { useState } from 'react';
1+
import React from 'react';
22

33
import { NavigationContainer } from '@react-navigation/native';
44
import { createStackNavigator } from '@react-navigation/stack';
5-
import { AuthContext } from '../../contexts';
5+
6+
import { useSelector } from 'react-redux';
7+
8+
import { get } from '../../utils';
69

710
import SharedStatusBar from '../shared/SharedStatusBar';
811
import MainStackNavigator from './MainStackNavigator';
@@ -13,53 +16,28 @@ import TermsScreen from '../screens/TermsScreen';
1316
const Stack = createStackNavigator();
1417

1518
export default function RootStackNavigator() {
16-
const [auth, setAuth] = useState(false);
17-
const [location, setLocation] = useState({ x: 127.1054221, y: 37.3591614 });
18-
19-
const [profile, setProfile] = useState({
20-
name: 'Test',
21-
picture:
22-
'https://encrypted-tbn0.gstatic.com/images?q=tbn%3AANd9GcRqj_WvMmoHaetb38rMQlE2wgH5Kj5ATh8XaA&usqp=CAU',
23-
});
24-
25-
function toggleAuth() {
26-
setAuth(!auth);
27-
}
19+
const auth = useSelector(get('auth'));
2820

2921
return (
30-
<AuthContext.Provider
31-
value={{
32-
auth,
33-
toggleAuth,
34-
location,
35-
setLocation,
36-
profile,
37-
setProfile,
38-
}}
39-
>
40-
<NavigationContainer>
41-
<SharedStatusBar />
42-
<Stack.Navigator
43-
initialRouteName="LoginScreen"
44-
screenOptions={{
45-
animationEnabled: false,
46-
headerShown: false,
47-
}}
48-
>
49-
{auth ? (
50-
<Stack.Screen name="Main" component={MainStackNavigator} />
51-
) : (
52-
<>
53-
<Stack.Screen name="LoginScreen" component={LoginScreen} />
54-
<Stack.Screen
55-
name="AuthCheckScreen"
56-
component={AuthCheckScreen}
57-
/>
58-
<Stack.Screen name="TermsScreen" component={TermsScreen} />
59-
</>
60-
)}
61-
</Stack.Navigator>
62-
</NavigationContainer>
63-
</AuthContext.Provider>
22+
<NavigationContainer>
23+
<SharedStatusBar />
24+
<Stack.Navigator
25+
initialRouteName="LoginScreen"
26+
screenOptions={{
27+
animationEnabled: false,
28+
headerShown: false,
29+
}}
30+
>
31+
{auth ? (
32+
<Stack.Screen name="Main" component={MainStackNavigator} />
33+
) : (
34+
<>
35+
<Stack.Screen name="LoginScreen" component={LoginScreen} />
36+
<Stack.Screen name="AuthCheckScreen" component={AuthCheckScreen} />
37+
<Stack.Screen name="TermsScreen" component={TermsScreen} />
38+
</>
39+
)}
40+
</Stack.Navigator>
41+
</NavigationContainer>
6442
);
6543
}

src/components/screens/HomeScreen/HomeAnimalEnrollment.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
/* eslint-disable jsx-a11y/accessible-emoji */
22
/* eslint-disable global-require */
3-
import React, { useContext } from 'react';
3+
import React from 'react';
44

55
import { TouchableOpacity } from 'react-native';
66

7-
import { AuthContext } from '../../../contexts';
7+
import { useDispatch } from 'react-redux';
8+
9+
import { toggleAuth } from '../../../slice';
810

911
import {
1012
AnimalEnrollmentContainter,
@@ -15,10 +17,10 @@ import {
1517
} from './style';
1618

1719
export default function HomeAnimalEnrollment() {
18-
const { toggleAuth } = useContext(AuthContext);
20+
const dispatch = useDispatch();
1921

2022
return (
21-
<TouchableOpacity onPress={() => toggleAuth()}>
23+
<TouchableOpacity onPress={() => dispatch(toggleAuth())}>
2224
<AnimalEnrollmentContainter>
2325
<AnimalEnrollImage source={require('../../assets/PetEnroll.png')} />
2426
<EnrollmentTextContainer>

src/components/screens/HomeScreen/index.js

Lines changed: 13 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
/* eslint-disable react/jsx-props-no-spreading */
22
/* eslint-disable global-require */
3-
// import React, { useState } from 'react';
4-
import React, { useEffect, useState, useContext } from 'react';
3+
import React, { useEffect, useState } from 'react';
54

6-
// import { View, ScrollView } from 'react-native';
75
import {
86
Text,
97
TouchableOpacity,
@@ -19,13 +17,14 @@ import Icon from 'react-native-vector-icons/FontAwesome';
1917

2018
import * as Location from 'expo-location';
2119

22-
import { AuthContext } from '../../../contexts';
20+
import { useDispatch, useSelector } from 'react-redux';
21+
22+
import { setLongtitude, setLatitude } from '../../../slice';
23+
import { get } from '../../../utils';
2324

2425
import HomeAnimalEnrollment from './HomeAnimalEnrollment';
25-
// import HomeHospital from './HomeHospital';
2626
import HomeSearch from './HomeSearch';
2727
import HomeBanner from './HomeBanner';
28-
// import { HomeContainer, HomeHospitalText, Beta } from './style';
2928

3029
import {
3130
NAVER_MAP_API_ID,
@@ -34,43 +33,11 @@ import {
3433

3534
import { HomeContainer } from './style';
3635

37-
// const DataOfNearbyHospital = [
38-
// {
39-
// name: '성심 동물 메디컬 센터',
40-
// adress: '봉명동 548-11',
41-
// phone: '042-719-7566',
42-
// key: '1',
43-
// },
44-
// {
45-
// name: '로얄 동물 펫병원',
46-
// adress: '봉명동 669',
47-
// phone: '042-823-7583',
48-
// key: '2',
49-
// },
50-
// {
51-
// name: '대전동물메디컬센터 숲',
52-
// adress: '봉명동 664-3단지',
53-
// phone: '042-826-7584',
54-
// key: '3',
55-
// },
56-
// {
57-
// name: '도안 ECO종합 동물 병원',
58-
// adress: '11-13, 봉명서로',
59-
// phone: '042-485-7582',
60-
// key: '4',
61-
// },
62-
// {
63-
// name: '피니펫 동물병원',
64-
// adress: '어은동 105-7',
65-
// phone: '042-862-7588',
66-
// key: '5',
67-
// },
68-
// ];
69-
7036
export default function HomeScreen({ navigation }) {
71-
const { location, setLocation, profile } = useContext(AuthContext);
37+
const dispatch = useDispatch();
38+
const location = useSelector(get('location'));
39+
const profile = useSelector(get('profile'));
7240

73-
// const [location, setLocation] = useState({ x: 127.1054221, y: 37.3591614 });
7441
const [errorMsg, setErrorMsg] = useState(null);
7542
const [level, setLevel] = useState(6);
7643

@@ -84,10 +51,12 @@ export default function HomeScreen({ navigation }) {
8451
const x = JSON.stringify(locationFrom.coords.longitude);
8552
const y = JSON.stringify(locationFrom.coords.latitude);
8653
if (x && y) {
87-
setLocation({
54+
dispatch(setLatitude({
8855
x,
56+
}));
57+
dispatch(setLongtitude({
8958
y,
90-
});
59+
}));
9160
}
9261
setLevel(13);
9362
}
@@ -149,7 +118,7 @@ export default function HomeScreen({ navigation }) {
149118
onPress={() => handleGetLocation()}
150119
style={{
151120
zIndex: 1,
152-
position: 'absolute',
121+
// position: 'absolute',
153122
paddingTop: 30,
154123
paddingRight: 20,
155124
}}

0 commit comments

Comments
 (0)