|
1 | 1 | "use server"; |
| 2 | +import { revalidateTag } from "next/cache"; |
| 3 | +import { cookies } from "next/headers"; |
| 4 | +import { TAGS } from "@/lib/constants"; |
2 | 5 |
|
3 | 6 | type FetchQueryProps = { |
4 | 7 | query: string; |
@@ -119,3 +122,107 @@ export async function generateSearchObjectFromLLM(text: string) { |
119 | 122 | return { data }; |
120 | 123 | } |
121 | 124 | } |
| 125 | + |
| 126 | +export async function addToCart(productId: string) { |
| 127 | + const cookieStore = cookies(); |
| 128 | + let cartId = cookieStore.get("cartId")?.value; |
| 129 | + if (!cartId) { |
| 130 | + cartId = Math.random().toString(36).substring(2, 15); |
| 131 | + cookies().set("cartId", cartId); |
| 132 | + } |
| 133 | + const graphqlQuery = ` |
| 134 | + query addToCart($cartId: String!, $productId: String!) { |
| 135 | + addToCart(cartId: $cartId, productId: $productId) |
| 136 | + } |
| 137 | + `; |
| 138 | + |
| 139 | + const { error, data } = await fetchQuery({ |
| 140 | + query: graphqlQuery, |
| 141 | + variables: { cartId, productId }, |
| 142 | + }); |
| 143 | + |
| 144 | + if (error) { |
| 145 | + return { error: Array.isArray(error) ? error[0] : error }; |
| 146 | + } else { |
| 147 | + revalidateTag(TAGS.cart); |
| 148 | + return { data }; |
| 149 | + } |
| 150 | +} |
| 151 | + |
| 152 | +export async function getCart(cartId: string) { |
| 153 | + const graphqlQuery = ` |
| 154 | + query getCart($cartId: String!) { |
| 155 | + getCart(cartId: $cartId) { |
| 156 | + cartId |
| 157 | + totalCartQuantity |
| 158 | + items { |
| 159 | + Product { |
| 160 | + name |
| 161 | + description |
| 162 | + stars |
| 163 | + price |
| 164 | + image |
| 165 | + } |
| 166 | + quantity |
| 167 | + cartItemID |
| 168 | + } |
| 169 | + } |
| 170 | + } |
| 171 | + `; |
| 172 | + |
| 173 | + const { error, data } = await fetchQuery({ |
| 174 | + query: graphqlQuery, |
| 175 | + variables: { cartId }, |
| 176 | + }); |
| 177 | + |
| 178 | + if (error) { |
| 179 | + return { error: Array.isArray(error) ? error[0] : error }; |
| 180 | + } else { |
| 181 | + return { data }; |
| 182 | + } |
| 183 | +} |
| 184 | + |
| 185 | +export async function removeFromCart(productId: string) { |
| 186 | + const cookieStore = cookies(); |
| 187 | + let cartId = cookieStore.get("cartId")?.value; |
| 188 | + |
| 189 | + const graphqlQuery = ` |
| 190 | + query removeFromCart($cartId: String!, $productId: String!) { |
| 191 | + removeFromCart(cartId: $cartId, productId: $productId) |
| 192 | + } |
| 193 | + `; |
| 194 | + |
| 195 | + const { error, data } = await fetchQuery({ |
| 196 | + query: graphqlQuery, |
| 197 | + variables: { cartId, productId }, |
| 198 | + }); |
| 199 | + |
| 200 | + if (error) { |
| 201 | + return { error: Array.isArray(error) ? error[0] : error }; |
| 202 | + } else { |
| 203 | + revalidateTag(TAGS.cart); |
| 204 | + return { data }; |
| 205 | + } |
| 206 | +} |
| 207 | + |
| 208 | +export async function decreaseItemQuantity(productId: string) { |
| 209 | + const cookieStore = cookies(); |
| 210 | + let cartId = cookieStore.get("cartId")?.value; |
| 211 | + const graphqlQuery = ` |
| 212 | + query decreaseQuantity($cartId: String!, $productId: String!) { |
| 213 | + decreaseQuantity(cartId: $cartId, productId: $productId) |
| 214 | + } |
| 215 | + `; |
| 216 | + |
| 217 | + const { error, data } = await fetchQuery({ |
| 218 | + query: graphqlQuery, |
| 219 | + variables: { cartId, productId }, |
| 220 | + }); |
| 221 | + |
| 222 | + if (error) { |
| 223 | + return { error: Array.isArray(error) ? error[0] : error }; |
| 224 | + } else { |
| 225 | + revalidateTag(TAGS.cart); |
| 226 | + return { data }; |
| 227 | + } |
| 228 | +} |
0 commit comments