diff --git a/donutshop/app/components/BackgroundInfo.tsx b/donutshop/app/components/BackgroundInfo.tsx new file mode 100644 index 0000000..cb5e26c --- /dev/null +++ b/donutshop/app/components/BackgroundInfo.tsx @@ -0,0 +1,76 @@ +import { ImageBackground, ImageProps, Pressable, StyleSheet, Text, View } from 'react-native' +import React from 'react' +import { Ionicons, AntDesign } from '@expo/vector-icons'; + + +interface BackgroundInfoProps{ + EnableBackHandler: boolean; + type: string, + id: string, + image_item: ImageProps, + favourite: boolean, + donutname: string, + calories: string, + average_rating: number, + ratings_count: string, + BackHandler?: any; + ToggleFavorite: any +} + + +const BackgroundInfo: React.FC = ({ + EnableBackHandler, + type, + id, + image_item, + favourite, + donutname, + calories, + average_rating, + ratings_count, + BackHandler, + ToggleFavorite, +}) => { + + + + return ( + + + {EnableBackHandler ? ( + + {BackHandler()}} style={{paddingHorizontal: 10, paddingVertical: 10}}> + + + {ToggleFavorite(favourite, type, id);}} style={{paddingHorizontal: 10, paddingVertical: 10}}> + + + + ) : ( + + {ToggleFavorite(favourite, type, id);}} style={{paddingHorizontal: 10, paddingVertical: 10}}> + + + + )} + + + {donutname} + + + {calories} calories + + + {average_rating} + ({ratings_count}) + + + + + + ) +} + +export default BackgroundInfo + +const styles = StyleSheet.create({}) \ No newline at end of file diff --git a/donutshop/app/screen/Detail.tsx b/donutshop/app/screen/Detail.tsx index 3d5adde..793c313 100644 --- a/donutshop/app/screen/Detail.tsx +++ b/donutshop/app/screen/Detail.tsx @@ -1,10 +1,48 @@ -import { View, Text } from 'react-native' -import React from 'react' +import { View, Text, ScrollView } from 'react-native'; +import React from 'react'; +import { useStore } from '../store/store'; +import { StatusBar } from 'expo-status-bar'; +import BackgroundInfo from "../components/BackgroundInfo"; -const Detail = () => { + + +const Detail = ({navigation, route}: any) => { + const ItemOfIndex = useStore((state: any) => + route.params.type == 'Donut' ? state.AllDonutList1 : state.AllDrinkList, + )[route.params.index]; + + const addToFavoriteList = useStore((state: any) => state.addToFavoriteList); + const deleteFromFavoriteList = useStore((state: any) => state.deleteFromFavoriteList); + + //favorite functionality + const ToggleFavorite = (favourite: boolean, type: string, id: string) => { + favourite ? deleteFromFavoriteList(type, id) : addToFavoriteList(type, id) + } + + //backhandler functionality + const BackHandler = () => { + navigation.pop(); + } + + return ( - - Detail + + + + + ) } diff --git a/donutshop/app/screen/Inbox.tsx b/donutshop/app/screen/Inbox.tsx index 51cd95d..f75df4d 100644 --- a/donutshop/app/screen/Inbox.tsx +++ b/donutshop/app/screen/Inbox.tsx @@ -9,7 +9,7 @@ const Inbox = () => { return ( - navigation.goBack()} style={{flexDirection: 'row', gap: 5, alignItems: 'flex-end'}}> + navigation.goBack()} > Inbox diff --git a/donutshop/app/screen/Menu.tsx b/donutshop/app/screen/Menu.tsx index a80ecbb..e92a10d 100644 --- a/donutshop/app/screen/Menu.tsx +++ b/donutshop/app/screen/Menu.tsx @@ -1,9 +1,9 @@ -import { View, Text, ScrollView, StyleSheet, Pressable } from 'react-native' +import { View, Text, ScrollView, StyleSheet, Pressable, Dimensions} from 'react-native' import React, { useState } from 'react' import { useStore } from '../store/store'; import { useBottomTabBarHeight } from '@react-navigation/bottom-tabs'; import { FlatList, TextInput } from 'react-native-gesture-handler'; -import { AntDesign } from '@expo/vector-icons'; +import { AntDesign, MaterialIcons} from '@expo/vector-icons'; import MenuCard from "../components/MenuCard"; import DrinkCard from "../components/DrinkCard"; @@ -31,8 +31,8 @@ const getDonutList = (category: string, data: any) => { }; -const Menu = () => { - const DonutList = useStore((state: any) => state.AllDonut); +const Menu = ({navigation}: any) => { + const DonutList = useStore((state: any) => state.AllDonutList1); const DrinkList = useStore((state: any) => state.AllDrinkList); const [categories, setCategories] = useState(getCategoriesFromData(DonutList)); @@ -43,23 +43,52 @@ const Menu = () => { const tabBar = useBottomTabBarHeight(); //console.log(sortItem.length) + const searchMenuItem = (search:string) => { + if(search != ''){ + setCategoryIndex({index: 0, category: categories[0]}); + setSortItem([...DonutList.filter((item: any) => + item.donutname.toLowerCase().includes(search.toLocaleLowerCase()),),] + ); + } + }; + + const resetSearch = () => { + setCategoryIndex({index: 0, category: categories[0]}); + setSortItem([...DonutList]); + setSearchMenu(''); + } return ( Menu + {/*===== search =====*/} + + {searchMenu.length == 0 ? ( + {searchMenuItem(searchMenu)}}> + + + ) : ( + {resetSearch()}}> + + + )} - - setSearchMenu(text)} placeholder='Search...' style={{fontSize: 18, flex:1}}/> - {}}> - - + {setSearchMenu(text); searchMenuItem(text)}} placeholder='Search...' style={{fontSize: 18, flex:1, marginLeft: 5}}/> + + {searchMenu.length > 0 ? ( + {searchMenuItem(searchMenu)}}> + + + ) : ( + <> + )} - - Donuts {/*===== Tab category =====*/} + Donuts + {categories.map((item, index) => ( @@ -79,8 +108,25 @@ const Menu = () => { {/*===== donuts=====*/} - item.id} renderItem={({item}) => { - return {}}> + item.id} + ListEmptyComponent={ + + Sorry, this item is not on our menu + + } + renderItem={({item}) => { + return { + navigation.push('detail', { + index: item.index, + id: item.id, + type: item.type, + }); + }}> { }}/> {/*===== drinks =====*/} - Drinks - item.id} renderItem={({item}) => { - return {}}> - {}} - /> - - }}/> + Drinks + item.id} + renderItem={({item}) => { + return { + navigation.push('detail', { + index: item.index, + id: item.id, + type: item.type, + }); + }}> + {}} + /> + + }}/> diff --git a/donutshop/app/store/store.ts b/donutshop/app/store/store.ts index 35700ad..e5c91e1 100644 --- a/donutshop/app/store/store.ts +++ b/donutshop/app/store/store.ts @@ -8,13 +8,130 @@ import DrinkData from "../data/DrinkData"; export const useStore = create( persist( (set, get) => ({ - AllDonut: DonutData, + AllDonutList1: DonutData, AllDrinkList: DrinkData, CartPrice: 0, FavoriteList: [], CartList: [], - ReceiptList: [] - }), { + ReceiptList: [], + addToCart:(cartItem: any) => + set( + produce(state => { + let found = false; + for(let i = 0; i < state.CartList.length; i++){ + if(state.CartList[i].id == cartItem.id){ + found = true; + let size = false; + for(let j = 0; j < state.CartList[i].prices.length; j++){ + if(state.CartList[i].prices[j].size == cartItem.prices[0].size){ + size = true; + state.CartList[i].prices[j].quantity++; + break; + } + } + if(size == false){ + state.CartList[i].prices.push(cartItem.prices[0]); + } + state.CartList[i].prices.sort((a: any, b: any) => { + if(a.size > b.size){ + return -1; + } + if(a.size < b.size) { + return 1; + } + return 0; + }); + break; + } + } + if(found == false){ + state.CartList.push(cartItem) + } + }), + ), + calculateCartPrice:() => + set( + produce(state => { + let totalPrice = 0; + for(let i = 0; i < state.CartList.length; i++){ + let tempPrice = 0; + for(let j = 0; j < state.CartList[i].prices.length; j++){ + tempPrice = tempPrice + parseFloat(state.CartList[i].prices[j].price) * state.CartList[i].prices[j].quantity; + } + state.CartList[i].ItemPrice = tempPrice.toFixed(2).toString(); + totalPrice = totalPrice + tempPrice; + } + state.CartPrice = totalPrice.toFixed(2).toString(); + }) + ), + addToFavoriteList:(type: string, id: string) => + set( + produce(state => { + if(type == 'Donut'){ + for(let i = 0; i < state.AllDonutList1.length; i++){ + if(state.AllDonutList1[i].id == id){ + if(state.AllDonutList1[i].favourite == false){ + state.AllDonutList1[i].favourite = true; + state.FavoriteList.unshift(state.AllDonutList1[i]); + } else{ + state.AllDonutList1[i].favourite = false; + } + break; + } + } + } else if(type == 'Drink'){ + for(let i = 0; i< state.AllDrinkList.length; i++){ + if(state.AllDrinkList[i].id == id){ + if(state.AllDrinkList[i].favourite == false){ + state.AllDrinkList[i].favourite = true; + state.FavoriteList.unshift(state.AllDrinkList[i]); + } else{ + state.AllDrinkList[i].favourite = false; + } + break; + } + } + } + }) + ), + deleteFromFavoriteList:(type: string, id: string) => + set( + produce(state => { + if(type = 'Donut'){ + for(let i = 0; i < state.AllDonutList1.length; i++){ + if(state.AllDonutList1[i].id == id){ + if(state.AllDonutList1[i].favourite == true){ + state.AllDonutList1[i].favourite = false; + } else{ + state.AllDonutList1[i].favourite = true; + } + break; + } + } + } else if(type = 'Drink'){ + for(let i = 0; i < state.AllDrinkList.length; i++){ + if(state.AllDrinkList[i].id == id){ + if(state.AllDrinkList[i].favourite == true){ + state.AllDrinkList[i].favourite = false; + } else{ + state.AllDrinkList[i].favourite = true; + } + break; + } + } + } + let spliceIndex = -1; + for(let i = 0; i < state.FavoriteList.length; i++){ + if (state.FavoriteList[i].id == id){ + spliceIndex = i; + break; + } + } + state.FavoriteList.splice(spliceIndex, 1); + }), + ), + }), + { name: 'donutshop', storage: createJSONStorage(() => AsynStorage), }