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.
- Loading branch information
Showing
14 changed files
with
238 additions
and
135 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 |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import { atom } from "jotai"; | ||
import { atomFamily, atomWithStorage, createJSONStorage } from "jotai/utils"; | ||
import { productsAtom, singleProductAtomFamily } from "./products"; | ||
|
||
export const cartAtom = atomWithStorage<Record<number, number>>( | ||
"cart", | ||
{}, | ||
createJSONStorage(() => localStorage), | ||
); | ||
|
||
export const cartItemQuantityAtomFamily = atomFamily((id: number) => | ||
atom( | ||
(get) => { | ||
return get(cartAtom)[id] ?? 0; | ||
}, | ||
(get, set, newValue: number) => { | ||
const currentCart = get(cartAtom); | ||
set(cartAtom, { ...currentCart, [id]: newValue }); | ||
}, | ||
), | ||
); | ||
|
||
export const cartTotalAtom = atom((get) => { | ||
const currentCart = get(cartAtom); | ||
const products = get(productsAtom); | ||
|
||
const ids = Object.keys(currentCart); | ||
let total = 0; | ||
|
||
for (const pr of products) { | ||
if (ids.includes(pr.id.toString())) { | ||
total += (currentCart[pr.id] ?? 0) * pr.price.toNumber(); | ||
} | ||
} | ||
|
||
return total; | ||
}); | ||
|
||
export const addItemToCartAtom = atomFamily( | ||
({ id, quantity }: { id: number; quantity: number }) => | ||
atom(null, async (get, set) => { | ||
const currentCart = get(cartAtom); | ||
const currentQuantity = currentCart[id]; | ||
const currentItemStock = | ||
get(singleProductAtomFamily(id))?.stock.toNumber() ?? 0; | ||
|
||
const q = currentQuantity ? currentQuantity + quantity : quantity; | ||
|
||
if (q < 1) { | ||
set(cartAtom, { ...currentCart, [id]: 1 }); | ||
} else if (q <= currentItemStock) { | ||
set(cartAtom, { ...currentCart, [id]: q }); | ||
} else { | ||
set(cartAtom, { ...currentCart, [id]: currentItemStock }); | ||
} | ||
}), | ||
); | ||
|
||
export const deleteItemFromCartAtom = atomFamily(({ id }: { id: number }) => | ||
atom(null, async (_, set) => { | ||
set(cartAtom, async (prev) => { | ||
const { [id]: toDelete, ...rest } = await prev; | ||
return { ...rest }; | ||
}); | ||
}), | ||
); |
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,16 @@ | ||
import { AppRouter } from "@acme/api"; | ||
import { inferProcedureOutput } from "@trpc/server"; | ||
import { atom } from "jotai"; | ||
import { atomFamily } from "jotai/utils"; | ||
|
||
export const productsAtom = atom< | ||
inferProcedureOutput<AppRouter["item"]["all"]> | ||
>([]); | ||
|
||
export const singleProductAtomFamily = atomFamily((id: number) => | ||
atom((get) => { | ||
const product = get(productsAtom).filter((product) => product.id === id)[0]; | ||
if (!product) return null; | ||
return product; | ||
}), | ||
); |
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,5 @@ | ||
import { createStore } from "jotai"; | ||
|
||
const jotaiStore = createStore(); | ||
|
||
export default jotaiStore; |
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
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
Oops, something went wrong.