generated from clerk/t3-turbo-and-clerk
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Performace and styleized cart. Also crrate buttons to control quantity
- Loading branch information
Diego Romero
committed
Nov 26, 2023
1 parent
6a921aa
commit 326ecce
Showing
6 changed files
with
166 additions
and
73 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,63 @@ | ||
import { useAtomValue } from "jotai"; | ||
import { useAtomValue, useSetAtom } from "jotai"; | ||
import { singleProductAtomFamily } from "../atoms/products"; | ||
import { Text } from "react-native"; | ||
import { Image, Pressable, Text, View } from "react-native"; | ||
import Ionicons from "@expo/vector-icons/Ionicons"; | ||
import { addItemToCartAtom, deleteItemFromCartAtom } from "../atoms/cart"; | ||
import { memo } from "react"; | ||
|
||
function CartItem({ id, quantity }: { id: number; quantity: number }) { | ||
const item = useAtomValue(singleProductAtomFamily(id)); | ||
const deleteItemFromCart = useSetAtom(deleteItemFromCartAtom({ id })); | ||
const addItemToCart = useSetAtom(addItemToCartAtom({ id, quantity: 1 })); | ||
const removeItemToCart = useSetAtom(addItemToCartAtom({ id, quantity: -1 })); | ||
|
||
return ( | ||
<Text> | ||
{item?.name} : {quantity} | ||
</Text> | ||
<View className="flex flex-row items-center justify-between rounded border border-gray-200 bg-white shadow"> | ||
<View className="w-2/3 flex-row items-center p-2"> | ||
<Text className="mr-2 text-lg" numberOfLines={1}> | ||
{quantity} | ||
</Text> | ||
<Image | ||
source={{ | ||
uri: | ||
item?.image_url ?? | ||
"https://t4.ftcdn.net/jpg/04/00/24/31/360_F_400243185_BOxON3h9avMUX10RsDkt3pJ8iQx72kS3.jpg", | ||
}} | ||
className="h-12 w-12" | ||
resizeMode="contain" | ||
/> | ||
<View> | ||
<Text className="w-40 text-base font-medium" numberOfLines={1}> | ||
{item?.name} | ||
</Text> | ||
<Text className=" text-base font-normal" numberOfLines={1}> | ||
C$ {(item?.price.toNumber() ?? 0) * quantity} | ||
</Text> | ||
</View> | ||
</View> | ||
|
||
<View className="flex h-full w-1/3 flex-grow flex-row"> | ||
<Pressable | ||
className=" w-1/3 items-center justify-center bg-red-400 " | ||
onPress={deleteItemFromCart} | ||
> | ||
<Ionicons name="trash-outline" size={18} color={"white"} /> | ||
</Pressable> | ||
<Pressable | ||
className=" w-1/3 items-center justify-center bg-cyan-600 " | ||
onPress={removeItemToCart} | ||
> | ||
<Ionicons name="remove" size={20} color={"white"} /> | ||
</Pressable> | ||
<Pressable | ||
className=" w-1/3 items-center justify-center rounded-r bg-emerald-500" | ||
onPress={addItemToCart} | ||
> | ||
<Ionicons name="add" size={20} color={"white"} /> | ||
</Pressable> | ||
</View> | ||
</View> | ||
); | ||
} | ||
|
||
export default CartItem; | ||
export default memo(CartItem); |
This file was deleted.
Oops, something went wrong.
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { useAtomValue } from "jotai"; | ||
import { cartAtom, cartTotalAtom } from "../atoms/cart"; | ||
import CartItem from "../components/CartItem"; | ||
import { Text, View } from "react-native"; | ||
import { FlatList } from "react-native-gesture-handler"; | ||
import { useMemo } from "react"; | ||
|
||
function CartListScreen() { | ||
const cart = useAtomValue(cartAtom); | ||
|
||
const data = useMemo(() => Object.entries(cart), [cart]); | ||
const total = useAtomValue(cartTotalAtom); | ||
|
||
return ( | ||
<View className="flex-1 bg-gray-50"> | ||
<View className="mb-2.5 h-12 justify-center border-b border-b-gray-200 bg-white shadow-sm"> | ||
<Text className="ml-2.5 text-lg ">Shopping Cart</Text> | ||
</View> | ||
|
||
<View className="flex-1 px-2"> | ||
<View className=" flex flex-row items-center justify-between"> | ||
<Text className="text-lg"> | ||
<Text className="text-xl font-medium">Total: </Text>C${" "} | ||
{total.toFixed(2)} | ||
</Text> | ||
</View> | ||
|
||
<View className="mt-1.5 mb-2.5 border-b-[0.5px] border-b-gray-400" /> | ||
<FlatList | ||
data={data} | ||
renderItem={(item) => ( | ||
<CartItem | ||
id={Number(item.item[0])} | ||
key={item.item[0]} | ||
quantity={item.item[1]} | ||
/> | ||
)} | ||
ItemSeparatorComponent={() => <View className="mt-2.5" />} | ||
ListFooterComponent={() => <View className="mt-2.5" />} | ||
/> | ||
</View> | ||
</View> | ||
); | ||
} | ||
|
||
export default CartListScreen; |
This file contains 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
This file contains 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