summaryrefslogtreecommitdiff
path: root/src/clients/actions.ts
diff options
context:
space:
mode:
authorHombreLaser <sebastian-440@live.com>2023-05-26 18:57:36 -0600
committerHombreLaser <sebastian-440@live.com>2023-05-26 18:57:36 -0600
commite6fa7db87b5f879de71bfc9e4f8afea9d722ab95 (patch)
tree599188426b9ab131e09e6d2a5d79b486562a6899 /src/clients/actions.ts
parenta0318f76f40deaeba367badf18af6141325d29e9 (diff)
Añade creación de reseñas
Diffstat (limited to 'src/clients/actions.ts')
-rw-r--r--src/clients/actions.ts64
1 files changed, 43 insertions, 21 deletions
diff --git a/src/clients/actions.ts b/src/clients/actions.ts
index 447d054..852e473 100644
--- a/src/clients/actions.ts
+++ b/src/clients/actions.ts
@@ -79,27 +79,6 @@ export async function create({ request }) {
}
}
-export async function addToCart({ params, request }) {
- const client = new ApiClient();
- const product = await client.get(`/products/${ params.productId }`);
- const post_request_path = "/account/cart";
- const id = product.data.data.id;
- const form = await request.formData();
-
- form.append('product_id', id);
-
- if(form.get('quantity') == '')
- form.set('quantity', 1);
-
- try {
- await client.post(post_request_path, form, client.authorizationHeaders());
- } catch(error) {
- return error.response.status;
- }
-
- return 200;
-}
-
export async function deleteFromCart({ request }) {
const client = new ApiClient();
const form = await request.formData();
@@ -124,4 +103,47 @@ export async function placeOrder({ request }) {
} catch(error) {
return redirect('/account/cart')
}
+}
+
+export async function productAction({ params, request }) {
+ const client = new ApiClient();
+ const product = await client.get(`/products/${ params.productId }`);
+
+ if(!product)
+ redirect(`/products`);
+
+ const product_id = product.data.data.id;
+ const form = await request.formData();
+
+ if(form.get('intent') == 'create_review') {
+ return await createReview(params.productId, form, client);
+ }
+
+ return await addToCart(product_id, form, client);
+}
+
+async function addToCart(product_id: string, form: FormData, client: ApiClient) {
+ const post_request_path = "/account/cart";
+ form.append('product_id', product_id);
+
+ if(form.get('quantity') == '')
+ form.set('quantity', 1);
+
+ try {
+ await client.post(post_request_path, form, client.authorizationHeaders());
+ } catch(error) {
+ return error.response.status;
+ }
+
+ return 200;
+}
+
+async function createReview(product_id: string, form: FormData, client: ApiClient) {
+ try {
+ await client.post(`/products/${product_id}/reviews`, form, client.authorizationHeaders());
+
+ return null;
+ } catch(error) {
+ return error.response;
+ }
} \ No newline at end of file