import React from 'react' import { Box, Flex, Button, Divider, HStack, Text } from '@chakra-ui/react' import { formatCurrency } from '../utils/functions.js' import { FiTrash2 } from 'react-icons/fi' import { HiOutlineLocationMarker } from 'react-icons/hi' import { useEvaluate } from '@qorebase/app-component' const url = 'https://staging-qore-data-hair-353194.qore.dev' const headers = { accept: '*/*', 'x-qore-engine-admin-secret': 'cBfO0PFdM65CfHTknZT5oYxf3y9G4JCW', } function Item(props: any) { const { item, setAllSubtotal, setRows, setOnLoadProcess, onLoadProcess, } = props const title = useEvaluate(props.properties.title) const price = useEvaluate(props.properties.price) const location = useEvaluate(props.properties.location) const date = useEvaluate(props.properties.date) const [qty, setQty] = React.useState(item.qty) const client = props.hooks.useClient() const increment = async () => { try { setOnLoadProcess(true) //get cart by id const response = await client.project.axios({ method: 'post', headers, url: `${url}/v1/execute`, data: { operations: [ { operation: 'Select', instruction: { table: 'order_history', name: 'data', condition: { cart_id_items: item.id, }, }, }, ], }, }) if (!response.data.results.data.length) { throw new Error('Cart tidak ditemukan!') } const cart = response.data.results.data[0] //update cart summary, and update cart item await client.project.axios({ method: 'post', headers, url: `${url}/v1/execute`, data: { operations: [ { operation: 'Update', instruction: { table: 'order_history', name: 'UpdateCart', condition: { id: cart.id, }, set: { total_price: Number(cart.total_price) + Number(item.price), }, }, }, { operation: 'Update', instruction: { table: 'cart', name: 'UpdateCartItem', condition: { id: item.id, }, set: { qty: qty + 1, }, }, }, ], }, }) setAllSubtotal(Number(cart.total_price) + Number(item.price)) setQty(qty + 1) setOnLoadProcess(false) } catch (error) { throw new Error(error) } } const decrement = async () => { try { //get cart by id setOnLoadProcess(true) const response = await client.project.axios({ method: 'post', headers, url: `${url}/v1/execute`, data: { operations: [ { operation: 'Select', instruction: { table: 'order_history', name: 'data', condition: { cart_id_items: item.id, }, }, }, ], }, }) if (!response.data.results.data.length) { throw new Error('Cart tidak ditemukan!') } const cart = response.data.results.data[0] //update cart summary, and update cart item await client.project.axios({ method: 'post', headers, url: `${url}/v1/execute`, data: { operations: [ { operation: 'Update', instruction: { table: 'order_history', name: 'UpdateCart', condition: { id: cart.id, }, set: { total_price: Number(cart.total_price) - Number(item.price), }, }, }, { operation: 'Update', instruction: { table: 'cart', name: 'UpdateCartItem', condition: { id: item.id, }, set: { qty: qty - 1, }, }, }, ], }, }) setAllSubtotal(Number(cart.total_price) - Number(item.price)) setQty(qty - 1) setOnLoadProcess(false) } catch (error) { throw new Error(error) } } const remove = async () => { try { setOnLoadProcess(true) //get cart by id const response = await client.project.axios({ method: 'post', headers, url: `${url}/v1/execute`, data: { operations: [ { operation: 'Select', instruction: { table: 'order_history', name: 'data', condition: { cart_id_items: item.id, }, }, }, ], }, }) if (!response.data.results.data.length) { throw new Error('Cart tidak ditemukan!') } const cart = response.data.results.data[0] //update cart summary, and remove cart item await client.project.axios({ method: 'post', headers, url: `${url}/v1/execute`, data: { operations: [ { operation: 'Update', instruction: { table: 'order_history', name: 'UpdateCart', condition: { id: cart.id, }, set: { total_price: Number(cart.total_price) - Number(item.price) * qty, }, }, }, { operation: 'Delete', instruction: { table: 'cart', name: 'DeleteCartItem', condition: { id: item.id, }, }, }, ], }, }) const cartResp = await client.project.axios({ method: 'post', headers, url: `${url}/v1/execute`, data: { operations: [ { operation: 'Select', instruction: { table: 'cart', name: 'data', condition: { cart_id: cart.id, }, }, }, ], }, }) setRows(cartResp.data.results.data) setAllSubtotal(Number(cart.total_price) - Number(item.price) * qty) setQty(0) setOnLoadProcess(false) } catch (error) { throw new Error(error) } } if (qty <= 0) return null return ( { item?.isReachMaxedQuota ? ( {title} - This Class has Reach Maxed Quota ) : ( {title} ) } {formatCurrency(price)} x {qty} {date} {location} {qty} ) } export default Item