205 lines
9.9 KiB
TypeScript
205 lines
9.9 KiB
TypeScript
import {create} from "zustand";
|
|
import {produce} from "immer";
|
|
import {persist, createJSONStorage} from "zustand/middleware";
|
|
import AsynStorage from "@react-native-async-storage/async-storage";
|
|
import DonutData from "../data/DonutData";
|
|
import DrinkData from "../data/DrinkData";
|
|
|
|
export const useStore = create(
|
|
persist(
|
|
(set, get) => ({
|
|
AllDonutMenu: DonutData,
|
|
AllDrinkMenu: DrinkData,
|
|
CartPrice: 0,
|
|
FavoriteList: [],
|
|
CartList: [],
|
|
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.AllDonutMenu.length; i++){
|
|
if(state.AllDonutMenu[i].id == id){
|
|
if(state.AllDonutMenu[i].favourite == false){
|
|
state.AllDonutMenu[i].favourite = true;
|
|
state.FavoriteList.unshift(state.AllDonutMenu[i]);
|
|
} else{
|
|
state.AllDonutMenu[i].favourite = false;
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
} else if(type == 'Drink'){
|
|
for(let i = 0; i < state.AllDrinkMenu.length; i++){
|
|
if(state.AllDrinkMenu[i].id == id){
|
|
if(state.AllDrinkMenu[i].favourite == false){
|
|
state.AllDrinkMenu[i].favourite = true;
|
|
state.FavoriteList.unshift(state.AllDrinkMenu[i]);
|
|
} else{
|
|
state.AllDrinkMenu[i].favourite = false;
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
})
|
|
),
|
|
deleteFromFavoriteList:(type: string, id: string) =>
|
|
set(
|
|
produce(state => {
|
|
if(type = 'Donut'){
|
|
for(let i = 0; i < state.AllDonutMenu.length; i++){
|
|
if(state.AllDonutMenu[i].id == id){
|
|
if(state.AllDonutMenu[i].favourite == true){
|
|
state.AllDonutMenu[i].favourite = false;
|
|
} else{
|
|
state.AllDonutMenu[i].favourite = true;
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
} else if(type = 'Drink'){
|
|
for(let i = 0; i < state.AllDrinkMenu.length; i++){
|
|
if(state.AllDrinkMenu[i].id == id){
|
|
if(state.AllDrinkMenu[i].favourite == true){
|
|
state.AllDrinkMenu[i].favourite = false;
|
|
} else{
|
|
state.AllDrinkMenu[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);
|
|
}),
|
|
),
|
|
addCartItemQuantity:(id: string, size: string) =>
|
|
set(
|
|
produce(state => {
|
|
for(let i = 0; i < state.CartList.length; i++){
|
|
if(state.CartList[i].id == id){
|
|
for (let j = 0; j < state.CartList[i].prices.length; j++) {
|
|
if (state.CartList[i].prices[j].size == size) {
|
|
state.CartList[i].prices[j].quantity++;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}),
|
|
),
|
|
removeCartItemQuantity:(id: string, size: string) =>
|
|
set(
|
|
produce(state => {
|
|
for (let i = 0; i < state.CartList.length; i++) {
|
|
if(state.CartList[i].id == id){
|
|
for (let j = 0; j < state.CartList[i].prices.length; j++) {
|
|
if (state.CartList[i].prices[j].size == size) {
|
|
if(state.CartList[i].prices.length > 1){
|
|
if (state.CartList[i].prices[j].quantity > 1) {
|
|
state.CartList[i].prices[j].quantity--;
|
|
} else{
|
|
state.CartList[i].prices.splice(j, 1);
|
|
}
|
|
} else{
|
|
if (state.CartList[i].prices[j].quantity > 1) {
|
|
state.CartList[i].prices[j].quantity--;
|
|
} else {
|
|
state.CartList.splice(i, 1);
|
|
}
|
|
}
|
|
break
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}),
|
|
),
|
|
addToReceiptList:() =>
|
|
set(
|
|
produce(state => {
|
|
let temp = state.CartList.reduce(
|
|
(accumulator: number, currentValue: any) =>
|
|
accumulator + parseFloat(currentValue.ItemPrice),
|
|
0,
|
|
);
|
|
if(state.ReceiptList.length > 0){
|
|
state.ReceiptList.unshift({
|
|
OrderDate: new Date().toDateString() + ' ' + new Date().toLocaleTimeString(),
|
|
CartList: state.CartList,
|
|
CartListPrice: temp.toFixed(2).toString(),
|
|
})
|
|
} else{
|
|
state.ReceiptList.push({
|
|
OrderDate: new Date().toDateString() + ' ' + new Date().toLocaleTimeString(),
|
|
CartList: state.CartList,
|
|
CartListPrice: temp.toFixed(2).toString(),
|
|
});
|
|
}
|
|
state.CartList = [];
|
|
}),
|
|
),
|
|
}),
|
|
{
|
|
name: 'donutshop',
|
|
storage: createJSONStorage(() => AsynStorage),
|
|
}
|
|
),
|
|
)
|