Created
October 31, 2020 21:00
-
-
Save paulonteri/5cdbb12a03fe8217510c67d17093edbb to your computer and use it in GitHub Desktop.
React Native (AsyncStorage) Add to Cart Functions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// @flow | |
// with flow types -> https://flow.org | |
import AsyncStorage from '@react-native-async-storage/async-storage'; | |
// common types | |
type CartItem = { itemId: number, quantity: number }; | |
type Cart = Array<CartItem>; | |
const CART: string = 'QUOTE_LIST'; | |
const getCartItems = async (): Promise<Cart> => { | |
const itemsInCart: string = await AsyncStorage.getItem(CART); | |
if (!itemsInCart) { | |
return []; | |
} | |
return JSON.parse(itemsInCart); | |
}; | |
// Delete old cart and create a new one | |
const updateCartFunc = async (newCartItems: Cart): Promise<boolean> => { | |
try { | |
await AsyncStorage.removeItem(CART); | |
} catch (e) { | |
return false; | |
} | |
// new cart | |
try { | |
await AsyncStorage.setItem(CART, JSON.stringify(newCartItems)); | |
} catch (e) { | |
return false; | |
} | |
return true; | |
}; | |
// Add item to cart | |
// check if there is a cart, if not, create one | |
// check if item in cart, if so update it, if not, append to cart | |
export const addItemToCartFunc = async ( | |
itemId: number, | |
quantity: number, | |
): Promise<boolean> => { | |
try { | |
let itemsInCart: ?Cart = await getCartItems(); | |
// cart is available --------------------> | |
if (itemsInCart) { | |
// check if in cart | |
const requiredItemFromCart: ?CartItem = itemsInCart.find( | |
(item) => item.itemId === itemId, | |
); | |
// item is already in cart | |
if (requiredItemFromCart) { | |
// remove form our items list | |
const newItemsInCart: Cart = itemsInCart.filter( | |
(itm) => itm !== requiredItemFromCart, | |
); | |
// update quentity | |
const updatedItem: CartItem = { | |
itemId: itemId, | |
quantity: requiredItemFromCart.quantity + quantity, | |
}; | |
newItemsInCart.push(updatedItem); | |
return await updateCartFunc(newItemsInCart); | |
} | |
// item was not in cart | |
else { | |
const newItem: CartItem = { | |
itemId: itemId, | |
quantity: quantity, | |
}; | |
itemsInCart.push(newItem); | |
return await updateCartFunc(itemsInCart); | |
} | |
} | |
// cart is not available --------------------> | |
else { | |
// create cart | |
const newItem: CartItem = { | |
itemId: itemId, | |
quantity: quantity, | |
}; | |
const newCart: Cart = [newItem]; | |
await AsyncStorage.setItem(CART, JSON.stringify(newCart)); | |
} | |
} catch (e) { | |
// error reading itemsInCart | |
return false; | |
} | |
return true; | |
}; | |
// reset cart | |
export const resetCartFunc = async (): Promise<boolean> => { | |
try { | |
await AsyncStorage.removeItem(CART); | |
} catch (e) { | |
return false; | |
} | |
return true; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment