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.
Unstyled cart screen in client global state with AsyncStorage
- Loading branch information
Diego Romero
committed
Nov 25, 2023
1 parent
828a7df
commit adea84a
Showing
10 changed files
with
185 additions
and
25 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
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,37 @@ | ||
import { atom } from "jotai"; | ||
import { atomFamily, atomWithStorage, createJSONStorage } from "jotai/utils"; | ||
import { singleProductAtomFamily } from "./products"; | ||
import AsyncStorage from "@react-native-async-storage/async-storage"; | ||
|
||
export const cartAtom = atomWithStorage<Record<number, number>>( | ||
"cart", | ||
{}, | ||
createJSONStorage(() => AsyncStorage), | ||
); | ||
|
||
export const cartItemQuantityAtomFamily = atomFamily((id: number) => | ||
atom(async (get) => { | ||
return (await get(cartAtom))[id] ?? 0; | ||
}), | ||
); | ||
|
||
export const addItemToCartAtom = atomFamily( | ||
({ id, quantity }: { id: number; quantity: number }) => | ||
atom(null, async (get, set) => { | ||
const currentCart = await get(cartAtom); | ||
const currentQuantity = currentCart[id]; | ||
const currentItemStock = | ||
get(singleProductAtomFamily(id))?.stock.toNumber() ?? 0; | ||
|
||
const q = currentQuantity ? currentQuantity + quantity : quantity; | ||
|
||
if (q <= currentItemStock) { | ||
set(cartAtom, { ...currentCart, [id]: q }); | ||
} else if (q < 0) { | ||
// prevents negative quantity | ||
set(cartAtom, { ...currentCart, [id]: 0 }); | ||
} else { | ||
set(cartAtom, { ...currentCart, [id]: currentItemStock }); | ||
} | ||
}), | ||
); |
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,15 @@ | ||
import { useAtomValue } from "jotai"; | ||
import { singleProductAtomFamily } from "../atoms/products"; | ||
import { Text } from "react-native"; | ||
|
||
function CartItem({ id, quantity }: { id: number; quantity: number }) { | ||
const item = useAtomValue(singleProductAtomFamily(id)); | ||
|
||
return ( | ||
<Text> | ||
{item?.name} : {quantity} | ||
</Text> | ||
); | ||
} | ||
|
||
export default CartItem; |
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 |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { BottomTabScreenProps } from "@react-navigation/bottom-tabs"; | ||
import { | ||
CompositeScreenProps, | ||
NavigatorScreenParams, | ||
} from "@react-navigation/native"; | ||
import { StackScreenProps } from "@react-navigation/stack"; | ||
|
||
export type RootStackParamList = { | ||
HomeScreen: NavigatorScreenParams<HomeTabParamList>; | ||
SingleItemScreen: { id: number }; | ||
}; | ||
|
||
export type RootStackScreenProps<T extends keyof RootStackParamList> = | ||
StackScreenProps<RootStackParamList, T>; | ||
|
||
export type HomeTabParamList = { | ||
Search: undefined; | ||
Cart: undefined; | ||
"My Profile": undefined; | ||
}; | ||
|
||
export type HomeTabScreenProps<T extends keyof HomeTabParamList> = | ||
CompositeScreenProps< | ||
BottomTabScreenProps<HomeTabParamList, T>, | ||
RootStackScreenProps<keyof RootStackParamList> | ||
>; | ||
|
||
declare global { | ||
namespace ReactNavigation { | ||
// eslint-disable-next-line @typescript-eslint/no-empty-interface | ||
interface RootParamList extends RootStackParamList {} | ||
} | ||
} |
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,17 @@ | ||
import { useAtomValue } from "jotai"; | ||
import { cartAtom } from "../atoms/cart"; | ||
import CartItem from "../components/CartItem"; | ||
|
||
function CartScreen() { | ||
const cart = useAtomValue(cartAtom); | ||
|
||
return ( | ||
<> | ||
{Object.entries(cart).map(([key, value]) => ( | ||
<CartItem id={Number(key)} key={key} quantity={value} /> | ||
))} | ||
</> | ||
); | ||
} | ||
|
||
export default CartScreen; |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.