Commit 835df838 authored by Nguyen Danh Khanh's avatar Nguyen Danh Khanh

config blacklist whitelist to persist

parent da2c496c
...@@ -2,9 +2,10 @@ import axios from 'axios' ...@@ -2,9 +2,10 @@ import axios from 'axios'
import config from '../configs' import config from '../configs'
import { store } from '../redux/store' import { store } from '../redux/store'
const token = store.getState().auth.token
export function getListProducts() {
export function getListProducts() {
const token = store.getState().auth.token
console.log('API: ', token)
const body = { const body = {
"drug_barcode": "", "drug_barcode": "",
"drug_name": "", "drug_name": "",
......
...@@ -21,7 +21,7 @@ const styles = StyleSheet.create({ ...@@ -21,7 +21,7 @@ const styles = StyleSheet.create({
fontWeight: 'bold' fontWeight: 'bold'
}, },
textInfo: { textInfo: {
fontSize: 16, fontSize: 14,
} }
}); });
......
type Data = { type Data = {
} }
export function loginSuccess(token: string) { export function loginSuccess(token: string | null) {
return { return {
type: 'LOGIN_SUCCESS', type: 'LOGIN_SUCCESS',
payload: token payload: token
......
...@@ -3,36 +3,33 @@ import Types from "../types"; ...@@ -3,36 +3,33 @@ import Types from "../types";
type ActionType = { type ActionType = {
type: string type: string
payload: AnyAction payload: string| null
}; };
export interface InitStateAuth { export type InitStateAuth = {
username: string // username: string
password: string // password: string
token: string | null token: string | null
} }
const initialState : InitStateAuth = { const initialState : InitStateAuth = {
username: "", // username: "",
password: "", // password: "",
token: null token: null
} }
function authReducer(state = initialState, action: ActionType) {
function productReducer(state = initialState, action: ActionType) {
const payload = action.payload const payload = action.payload
switch (action.type) { switch (action.type) {
case Types.LOGIN_SUCCESS: case Types.LOGIN_SUCCESS:
return { return {
...state, ...state,
auth: payload, token: payload,
}; };
default: default:
return state return state
} }
} }
// export default authReducer // export default authReducer
export default productReducer export default authReducer
...@@ -6,6 +6,7 @@ import invoice, { InitStateInvoice } from './invoice' ...@@ -6,6 +6,7 @@ import invoice, { InitStateInvoice } from './invoice'
import cart from './cart' import cart from './cart'
import { Cart } from "../../model/cart"; import { Cart } from "../../model/cart";
import product, { InitStateProduct } from "./product"; import product, { InitStateProduct } from "./product";
import { authPersistedReducer } from "../store";
export type RootState = { export type RootState = {
auth: InitStateAuth, auth: InitStateAuth,
...@@ -14,6 +15,7 @@ export type RootState = { ...@@ -14,6 +15,7 @@ export type RootState = {
product: InitStateProduct product: InitStateProduct
} }
const rootReducer = combineReducers({ const rootReducer = combineReducers({
auth, auth,
invoice, invoice,
......
...@@ -68,7 +68,7 @@ const initState: InitStateProduct = { ...@@ -68,7 +68,7 @@ const initState: InitStateProduct = {
data: [] data: []
} }
function invoiceReducer(state: InitStateProduct = initState, action: AnyAction) { function productReducer(state: InitStateProduct = initState, action: AnyAction) {
const payload = action.payload const payload = action.payload
switch (action.type) { switch (action.type) {
...@@ -88,4 +88,4 @@ function invoiceReducer(state: InitStateProduct = initState, action: AnyAction) ...@@ -88,4 +88,4 @@ function invoiceReducer(state: InitStateProduct = initState, action: AnyAction)
// export default authReducer // export default authReducer
export default invoiceReducer export default productReducer
\ No newline at end of file \ No newline at end of file
...@@ -5,15 +5,41 @@ import logger from 'redux-logger'; ...@@ -5,15 +5,41 @@ import logger from 'redux-logger';
import createSagaMiddleware from 'redux-saga'; import createSagaMiddleware from 'redux-saga';
import rootSaga from '../sagas' import rootSaga from '../sagas'
import { persistStore, persistReducer } from 'redux-persist' import { persistStore, persistReducer } from 'redux-persist'
// import storage from 'redux-persist/lib/storage' // defaults to localStorage
import AsyncStorage from '@react-native-async-storage/async-storage'; import AsyncStorage from '@react-native-async-storage/async-storage';
import authReducer from '../reducers/auth';
import productReducer from '../reducers/product';
import cartReducer from '../reducers/cart';
import invoiceReducer from '../reducers/invoice';
const sagaMiddleware = createSagaMiddleware() const sagaMiddleware = createSagaMiddleware()
const persistConfig = { const persistConfig = {
key: 'root', key: 'root',
storage: AsyncStorage storage: AsyncStorage,
whitelist: ['auth', 'invoice', 'cart', 'product']
} }
const authPersistConfig = {
key: 'auth',
storage: AsyncStorage,
whitelist: ['token']
}
const productPersistConfig = {
key: 'product',
storage: AsyncStorage,
}
const cartPersistConfig = {
key: 'cart',
storage: AsyncStorage,
}
const invoicePersistConfig = {
key: 'invoice',
storage: AsyncStorage,
}
export const authPersistedReducer = persistReducer(authPersistConfig, authReducer)
export const productPersistedReducer = persistReducer(productPersistConfig, productReducer)
export const cartPersistedReducer = persistReducer(cartPersistConfig, cartReducer)
export const invoicePersistedReducer = persistReducer(invoicePersistConfig, invoiceReducer)
const persistedReducer = persistReducer(persistConfig, rootReducer) const persistedReducer = persistReducer(persistConfig, rootReducer)
// const middleware = logger, sagaMiddleware); // const middleware = logger, sagaMiddleware);
const store = createStore( const store = createStore(
......
...@@ -6,10 +6,11 @@ import { ...@@ -6,10 +6,11 @@ import {
Image Image
} from 'react-native'; } from 'react-native';
import styles from './index.style'; import styles from './index.style';
import { RootState } from '../../redux/reducers';
const App = () => { const App = () => {
const navigation = useNavigation() const navigation = useNavigation()
const token = useSelector(state => state.auth.token) const token = useSelector((state:RootState) => state.auth.token)
console.log('Flat: ', token, '\n') console.log('Flat: ', token, '\n')
if(token == null) navigation.navigate('AuthLogin') if(token == null) navigation.navigate('AuthLogin')
else navigation.navigate('Main') else navigation.navigate('Main')
......
...@@ -27,7 +27,7 @@ const App = () => { ...@@ -27,7 +27,7 @@ const App = () => {
//------------------------------------------------------ //------------------------------------------------------
//------------------------------------------------------------------------------------ //------------------------------------------------------------------------------------
const token = useSelector((state:RootState) => state.auth.token) const token = useSelector((state: RootState) => state.auth.token)
console.log('Home:Ahihi ', token, '\n') console.log('Home:Ahihi ', token, '\n')
//------------------------------------------------------------------------------------ //------------------------------------------------------------------------------------
...@@ -131,7 +131,7 @@ const App = () => { ...@@ -131,7 +131,7 @@ const App = () => {
) )
} }
const _renderButtonBill = () => { const _renderButtonBill = () => {
return ( return (
<View style={styles.billButtonContainer}> <View style={styles.billButtonContainer}>
<TouchableOpacity style={styles.billButton} <TouchableOpacity style={styles.billButton}
......
...@@ -8,6 +8,7 @@ const styles = StyleSheet.create({ ...@@ -8,6 +8,7 @@ const styles = StyleSheet.create({
height: 40, height: 40,
}, },
container: { container: {
height: '80%',
marginBottom: 60, marginBottom: 60,
}, },
buttonAdd: { buttonAdd: {
......
...@@ -112,12 +112,13 @@ const App = () => { ...@@ -112,12 +112,13 @@ const App = () => {
); );
}; };
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
const [amount, setAmount] = useState(35264) const [amount, setAmount] = useState(28598)
const [amountPaid, setAmountPaid] = useState('0') const [amountPaid, setAmountPaid] = useState('0')
const [amountVat, setAmountVat] = useState('') const [amountVat, setAmountVat] = useState('')
const [discount, setDiscount] = useState(0) const [discount, setDiscount] = useState(0)
const [note, setNote] = useState('') const [note, setNote] = useState('')
const [currentPaid, setCurrentPaid] = useState(0) const [currentPaid, setCurrentPaid] = useState(0)
const _renderBillDate = () => ( const _renderBillDate = () => (
<View style={styles.billDateContainer}> <View style={styles.billDateContainer}>
<View style={styles.lineContainer}> <View style={styles.lineContainer}>
...@@ -130,7 +131,7 @@ const App = () => { ...@@ -130,7 +131,7 @@ const App = () => {
</View> </View>
<View style={styles.lineContainer}> <View style={styles.lineContainer}>
<Text>Tổng tiền</Text> <Text>Tổng tiền</Text>
<Text>{`35264 VNĐ`}</Text> <Text>{`${amount} VNĐ`}</Text>
</View> </View>
<View style={styles.lineContainer}> <View style={styles.lineContainer}>
<Text>VAT</Text> <Text>VAT</Text>
...@@ -235,7 +236,8 @@ const App = () => { ...@@ -235,7 +236,8 @@ const App = () => {
return ( return (
<SafeAreaView > <SafeAreaView >
{_renderHeader()} {_renderHeader()}
<ScrollView style={styles.container}> <ScrollView style={styles.container}
>
{_renderButtonAdd()} {_renderButtonAdd()}
<FlatList <FlatList
data={cartProducts} data={cartProducts}
...@@ -248,9 +250,7 @@ const App = () => { ...@@ -248,9 +250,7 @@ const App = () => {
{_renderNote()} {_renderNote()}
<Button onPress={() => submit()} title='SEND'></Button> <Button onPress={() => submit()} title='SEND'></Button>
</ScrollView> </ScrollView>
<TouchableOpacity style={styles.buttonSend} onPress={() => submit()}>
<Text>SENDDDDDDDDDDDDDDDDDD</Text>
</TouchableOpacity>
</SafeAreaView> </SafeAreaView>
); );
}; };
......
...@@ -9,8 +9,7 @@ const styles = StyleSheet.create({ ...@@ -9,8 +9,7 @@ const styles = StyleSheet.create({
color: 'white', color: 'white',
}, },
container: { container: {
alignItems: 'center',
justifyContent: 'center'
}, },
containerInput: { containerInput: {
backgroundColor: 'white', backgroundColor: 'white',
......
...@@ -21,6 +21,7 @@ import { Cart, Product } from "../../model/cart"; ...@@ -21,6 +21,7 @@ import { Cart, Product } from "../../model/cart";
import { Data } from '../../model/product'; import { Data } from '../../model/product';
import { RootState } from '../../redux/reducers'; import { RootState } from '../../redux/reducers';
import IconBack from '../../components/IconBack'; import IconBack from '../../components/IconBack';
import { RefreshControl } from 'react-native';
const App = () => { const App = () => {
...@@ -123,18 +124,34 @@ const App = () => { ...@@ -123,18 +124,34 @@ const App = () => {
return () => { } return () => { }
}, []) }, [])
const wait = (timeout: number) => {
return new Promise(resolve => setTimeout(resolve, timeout));
}
const [refreshing, setRefreshing] = React.useState(false);
const onRefresh = React.useCallback(() => {
setRefreshing(true);
wait(500).then(() => setRefreshing(false));
}, []);
//------------------------------------------------------------ //------------------------------------------------------------
return ( return (
<SafeAreaView> <SafeAreaView>
{_renderHeader()} {_renderHeader()}
<View > <ScrollView style={styles.container}
refreshControl={
<RefreshControl
refreshing={refreshing}
onRefresh={onRefresh}
/>
}>
<FlatList <FlatList
data={products} data={products}
ItemSeparatorComponent={ItemSeparatorView} ItemSeparatorComponent={ItemSeparatorView}
renderItem={({ item }) => _renderListProduct(item)} renderItem={({ item }) => _renderListProduct(item)}
/> />
{/* {_renderButtonSubmit()} */} {/* {_renderButtonSubmit()} */}
</View> </ScrollView>
</SafeAreaView> </SafeAreaView>
); );
} }
......
...@@ -7,7 +7,6 @@ import { ...@@ -7,7 +7,6 @@ import {
import styles from './index.style'; import styles from './index.style';
import Header from '../../components/Header'; import Header from '../../components/Header';
import IconMedlink from '../../components/IconMedlink';
import HeaderTitle from '../../components/HeaderTitle'; import HeaderTitle from '../../components/HeaderTitle';
import HomeRepositoryItem from '../../components/HomeRepositoryItem' import HomeRepositoryItem from '../../components/HomeRepositoryItem'
import { useNavigation } from '@react-navigation/core'; import { useNavigation } from '@react-navigation/core';
......
...@@ -35,7 +35,7 @@ const App = () => { ...@@ -35,7 +35,7 @@ const App = () => {
const dispatch = useDispatch() const dispatch = useDispatch()
const navigation = useNavigation() const navigation = useNavigation()
const logOut = () => { const logOut = () => {
const token = '' const token = null
dispatch(loginSuccess(token)) dispatch(loginSuccess(token))
navigation.navigate('AuthLogin') navigation.navigate('AuthLogin')
} }
......
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