Best way to implement a cart?

I've implemented a cart using jotai's atomWithStorage to save the items in lcoalStorage, but I was wondering if I should store the cart in the server instead? In this specific app only logged in users can access the store. Are there other better ways of implementing a cart?
25 Replies
Keef
Keefβ€’2y ago
Server side is probably the better way plus you can have the user save their carts across devices and also it'll let you solve a super super common race condition in ecommerce where you sell more items than you have quantity for If this is a project for fun you'd get a lot of clout for talking about this during an interview Just moving to serverside isnt enough to solve the problem but its a start, i just wasn't spoiling it since its kinda fun to discover on your own
hyperzone
hyperzoneOPβ€’2y ago
It's actually a real project πŸ˜† I've implemented carts before but on a different framework but I guess the requirement that only logged in users can add to cart really helps me here
Keef
Keefβ€’2y ago
Oh yeah then i'd definitely do serverside and to spill the beans you'd want some kind of way of knowing if you should proceed with the transaction or not Something like locks in a database, using redis etc Some ways people solve it is to hold stock for x minutes
hyperzone
hyperzoneOPβ€’2y ago
yeah that's interesting this specific app doesn't really care about stock so I guess I should just create a Cart model and associate it with the user
Keef
Keefβ€’2y ago
Cart in local storage works and if that meets your needs its fine. The sharing carts across device is a fluff thing but its kinda cute
hyperzone
hyperzoneOPβ€’2y ago
there is actually an argument I don't know how to counter, which is that developers tell me - "just fetch the items on initial render and store them in a global store" and same here - one can tell me "why send a request on each 'add to cart'"?
Keef
Keefβ€’2y ago
React Conferences by GitNation
YouTube
React Query: It’s Time to Break up with your "Global State”! –Tanne...
Find the latest React.js talks & workshops at https://portal.gitnation.org Β  Β  πŸ—“ Talk recording from React Summit Remote Edition 2020 Website - https://remote.reactsummit.com/ See other React conferences by GitNation React Summit – https://reactsummit.com React Advanced London – https://reactadvanced.com React Day Berlin – https://reactday.be...
hyperzone
hyperzoneOPβ€’2y ago
In my opinion it's a bad idea to hold items in a global store but dunno how to make the case that sending requests on each page load or 'add to cart' click isn't that bad
Keef
Keefβ€’2y ago
This is a really goooooooood talk and if you aren't familiar with it tanner is one of the people behind react-query
hyperzone
hyperzoneOPβ€’2y ago
yeah am familiar with this wizard ill watch it now thank you
Keef
Keefβ€’2y ago
Its just really about trade offs and how accurate you want to be Using react query makes it a non issue honestly since the api makes it easy to refetch/render and you'll also have the most recent fetch. Networking is also cheap now so I just think they are being stingy
hyperzone
hyperzoneOPβ€’2y ago
yeah that's a good point thanks for the help I really appreciate it πŸ™‚
Keef
Keefβ€’2y ago
np np gl!!
hyperzone
hyperzoneOPβ€’2y ago
@CFKeef Oh btw one last question πŸ˜† - say I have a Product model with all the relevant info, and I also have a Cart model now that I go with serverside Cart, I probably have to add a third CartProduct model that has a relation to a Product and a quantity property right? we're talking Prisma sql
Keef
Keefβ€’2y ago
It'd be a 1 to many from product -> cart so you can just do something like this and you don't need to write a bridge table
Keef
Keefβ€’2y ago
Keef
Keefβ€’2y ago
Have you worked with redis before? I'd just use redis for something like this since using the TTL lets you nicely clear up old data without explicitly having to do it
hyperzone
hyperzoneOPβ€’2y ago
so basically the quantity of a product is the num of times it's in the cart?
Keef
Keefβ€’2y ago
So I'd use a compound key of something like userId:cart and have a json blob of the relevant details with a time to live like idk a week or something Oh sorry yeah you'd prob need a bridge table if you want to "enhance" the relationship
hyperzone
hyperzoneOPβ€’2y ago
basically all i need is quantity so was wondering if i could get it done without a bridge
Keef
Keefβ€’2y ago
Redis πŸ™‚
hyperzone
hyperzoneOPβ€’2y ago
i'll look into it now
Keef
Keefβ€’2y ago
You could also use a metadata JSON field in your record if you want to get a little dirty about it but I think redis is the cleaner approach Cause using records in your database I'd bring up how would you deal with "ancient" carts?
hyperzone
hyperzoneOPβ€’2y ago
yeah that's right thank you again for the valuable info πŸ™‚
Keef
Keefβ€’2y ago
Redis is a REALLLLLY GOOOOD PIECE OF TECH So its well worth learning it np np np
Want results from more Discord servers?
Add your server