// Create models using typesrcipt
// User SignIn
// Food Model
export interface FoodModel {
_id: string;
name: string;
description: string;
category: string;
price: number;
readyTime: number;
images: [string];
unit: number;
}
export interface UserLogin {
email: string;
password: string;
}
// User SignUp
export interface UserSignUp {
email: string;
phone: string;
password: string;
}
// User Model
export interface UserModel {
firstName: string;
lastName: string;
contactNumber: string;
token: string;
verified: boolean;
}
Actions:
// Actions
export const onUserLogin = (userData: UserLogin) => {
return async (dispatch: Dispatch) => {
try {
const res = await axios.post(`${BASE_URL}user/login`, {
userData,
});
console.log(res);
if (!res) {
dispatch({
type: 'ON_USER_ERROR',
payload: 'Login Error',
});
} else {
dispatch({
type: 'ON_USER_LOGIN',
payload: res.data,
});
}
} catch (error) {
dispatch({
type: 'ON_USER_ERROR',
payload: 'Login Error',
});
}
};
};
import { UserModel, UserState, FoodModel, UserAction } from '../models'
const initialState: UserState = {
user: {} as UserModel,
location: {} as Location,
error: {} as string | undefined,
Cart: {} as [FoodModel],
};
import { UserModel, UserState, FoodModel, UserAction } from '../models'
const initialState: UserState = {
user: {} as UserModel,
location: {} as Location,
error: {} as string | undefined,
Cart: {} as [FoodModel],
};
const UserReducer = (state: UserState = initialState, action: UserAction) => {
const { type, payload } = action;
switch (action.type) {
case 'ON_UPDATE_LOCATION':
return {
...state,
location: payload,
};
case 'ON_UPDATE_CART':
if (!Array.isArray(state.Cart)) {
return {
...state,
Cart: [payload],
};
}
const exisitngFoods = state.Cart.filter(
(item) => item._id == action.payload._id
);
//Check for Existing Product to update unit
if (exisitngFoods.length > 0) {
let updateCart = state.Cart.map((food) => {
if (food._id === action.payload._id) {
food.unit = action.payload.unit;
}
return food;
});
return {
...state,
Cart: updateCart.filter((item) => item.unit > 0),
};
} else {
return {
...state,
Cart: [...state.Cart, action.payload],
};
}
case 'ON_USER_LOGIN':
return {
...state,
user: action.payload,
};
case 'ON_USER_SIGNUP':
return {
...state,
user: action.payload,
};
default:
return state;
}
};
export { UserReducer };
import { combineReducers } from 'redux'
import { CartReducer } from './CartReducer'
import { UserReducer } from './userReducer'
const rootReducer = combineReducers({
userReducer: UserReducer,
cartReducer: CartReducer,
});
export type ApplicationState = ReturnType<typeof rootReducer>;
export { rootReducer };
import { applyMiddleware, createStore } from 'redux';
import { thunk } from 'redux-thunk';
import { rootReducer } from './reducers';
const store = createStore(rootReducer, applyMiddleware(thunk));
export { store };
import 'react-native-gesture-handler';
import React from 'react';
import { NavigationContainer } from '@react-navigation/native';
import { RouteNavigation } from 'navigation';
import { Provider } from 'react-redux';
import { store } from './src/reduxStore';
export default function App() {
return (
<Provider store={store}>
<NavigationContainer>
<RouteNavigation />
</NavigationContainer>
</Provider>
);
}
const store = createStore(mainReducer, applyMiddleware(Thunk));
const mapStateToProps = (state, props) => {
return {
name: state.name,
age: state.age
}
}
const mapDispatchToProps = (dispatch) => {
return {
fetchData: () => dispatch(fetchData())
}
}
export default connect(mapStateToProps, mapDispatchToProps)(UserComponent);
export default connect(mapStateToProps, mapDispatchToProps)(UserComponent);