Commit 9bb2ff15 by OuiAtichat

demo redux

parent 2e7c2544
export function increment(){
return{
type: "Increment"
};
}
export function decrement(){
return{
type: "Decrement"
};
}
import { AsyncStorage } from 'react-native';
export const getToken = token => ({
type: 'GET_TOKEN',
token,
});
export const saveToken = token => ({
type: 'SAVE_TOKEN',
token,
});
export const removeToken = () => ({
type: 'REMOVE_TOKEN',
});
export const loading = bool => ({
type: 'LOADING',
isLoading: bool,
});
export const error = error => ({
type: 'ERROR',
error,
});
export const getUserToken = () => async dispatch => {
try {
await AsyncStorage.getItem('userToken');
dispatch(loading(false));
dispatch(getToken(data));
} catch (err) {
dispatch(loading(false));
dispatch(error(err.message || 'ERROR'));
}
};
export const saveUserToken = data => async dispatch => {
try {
await AsyncStorage.setItem('userToken', '');
dispatch(loading(false));
dispatch(saveToken(data));
} catch (err) {
dispatch(loading(false));
dispatch(error(err.message || 'ERROR'));
}
};
export const removeUserToken = () => async dispatch => {
try {
await AsyncStorage.removeItem('userToken');
dispatch(loading(false));
dispatch(removeToken(data));
} catch (err) {
dispatch(loading(false));
dispatch(error(err.message || 'ERROR'));
}
};
const initialState = {
token: {},
loading: true,
error: null,
};
const authReducer = (state = initialState, action) => {
switch (action.type) {
case 'GET_TOKEN':
return { ...state, token: action.token };
case 'SAVE_TOKEN':
return { ...state, token: action.token };
case 'REMOVE_TOKEN':
return { ...state, token: action.token };
case 'LOADING':
return { ...state, loading: action.isLoading };
case 'ERROR':
return { ...state, error: action.error };
default:
return state;
}
};
export default authReducer;
let count = 0;
export default function(state = count, action) {
switch (action.type) {
case 'Increment':
count++;
break;
case 'Decrement':
count--;
break;
}
return count;
}
import { combineReducers } from 'redux';
// import routes from './routes';
import countReducer from './countReducer';
import authReducer from './authReducer';
export default combineReducers({
// routes,
count: countReducer,
// ... other reducers
token: authReducer,
});
import { ActionConst } from 'react-native-router-flux'
const initialState = {
scene: {},
}
export default (state = initialState, action = {}) => {
switch (action.type) {
// focus action is dispatched when a new screen comes into focus
case ActionConst.FOCUS:
console.log(action)
return {
...state,
scene: action.scene,
}
// ...other actions
default:
return state
}
}
......@@ -4,7 +4,7 @@ import { HeaderButtons, Item } from 'react-navigation-header-buttons';
import IoniconsHeaderButton from '../../components/IoniconsHeaderButton';
import * as Permissions from 'expo-permissions';
import { Camera } from 'expo-camera';
import { width, height } from '../../constants/Layout';
import { width } from '../../constants/Layout';
export default class CameraScreen extends Component {
state = {
......@@ -68,7 +68,12 @@ export default class CameraScreen extends Component {
>
<Camera
onBarCodeScanned={scanned ? undefined : this.handleBarCodeScanned}
style={{ width: width / 1.45, height: width / 1.45, borderRadius: 12, overflow: 'hidden' }}
style={{
width: width / 1.45,
height: width / 1.45,
borderRadius: 12,
overflow: 'hidden',
}}
flashMode={this.state.flashMode}
/>
</View>
......
import React, { Component } from 'react';
import { Text, Footer } from 'native-base';
import { Image, FlatList, View } from 'react-native';
import { connect } from 'react-redux';
import { increment, decrement } from '../../../reduxStore/actions';
import { Text } from 'native-base';
import { FlatList, View } from 'react-native';
import { color } from '../../../constants/Styles';
import MeterCard from '../../../components/MeterCard';
import { HeaderButtons, Item } from 'react-navigation-header-buttons';
......@@ -101,15 +100,4 @@ class SmartMeterScreen extends Component {
}
}
const mapStateToProps = state => {
return {
count: state.count,
};
};
const mapDispatchToProps = { increment, decrement };
export default connect(
mapStateToProps,
mapDispatchToProps
)(SmartMeterScreen);
export default SmartMeterScreen;
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment