// src/gcodefiles.js
import React, { useEffect, useState, useContext, useCallback } from 'react'
import { useNavigate } from 'react-router-dom'
import axios from 'axios'
import moment from 'moment'
import { Table, Button, Flex, Space, Modal, Dropdown, message } from 'antd'
import { createStyles } from 'antd-style'
import {
LoadingOutlined,
PlusOutlined,
DownloadOutlined,
ReloadOutlined,
InfoCircleOutlined
} from '@ant-design/icons'
import { AuthContext } from '../../Auth/AuthContext'
import IdText from '../common/IdText'
import NewProduct from './Products/NewProduct'
import ProductIcon from '../../Icons/ProductIcon'
const useStyle = createStyles(({ css, token }) => {
const { antCls } = token
return {
customTable: css`
${antCls}-table {
${antCls}-table-container {
${antCls}-table-body,
${antCls}-table-content {
scrollbar-width: thin;
scrollbar-color: #eaeaea transparent;
scrollbar-gutter: stable;
}
}
}
`
}
})
const Products = () => {
const [messageApi, contextHolder] = message.useMessage()
const navigate = useNavigate()
const { styles } = useStyle()
const [productsData, setProductsData] = useState([])
const [newProductOpen, setNewProductOpen] = useState(false)
const [loading, setLoading] = useState(true)
const { authenticated } = useContext(AuthContext)
const fetchProductsData = useCallback(async () => {
try {
const response = await axios.get('http://localhost:8080/products', {
params: {
page: 1,
limit: 25
},
headers: {
Accept: 'application/json'
},
withCredentials: true // Important for including cookies
})
setProductsData(response.data)
setLoading(false)
//setPagination({ ...pagination, total: response.data.totalItems }); // Update total count
} catch (error) {
if (error.response) {
messageApi.error(
'Error updating printer details:',
error.response.status
)
} else {
messageApi.error(
'An unexpected error occurred. Please try again later.'
)
}
}
}, [messageApi])
useEffect(() => {
if (authenticated) {
fetchProductsData()
}
}, [authenticated, fetchProductsData])
const getProductActionItems = (id) => {
return {
items: [
{
label: 'Info',
key: 'info',
icon:
},
{
label: 'Download',
key: 'download',
icon:
}
],
onClick: ({ key }) => {
if (key === 'info') {
navigate(`/management/products/info?productId=${id}`)
}
}
}
}
// Column definitions
const columns = [
{
title: '',
dataIndex: '',
key: '',
width: 40,
fixed: 'left',
render: () =>
},
{
title: 'Name',
dataIndex: 'name',
key: 'name',
width: 200,
fixed: 'left'
},
{
title: 'ID',
dataIndex: '_id',
key: 'id',
fixed: 'left',
width: 165,
render: (text) =>
},
{
title: 'Created At',
dataIndex: 'createdAt',
key: 'createdAt',
width: 180,
render: (createdAt) => {
if (createdAt) {
const formattedDate = moment(createdAt).format('YYYY-MM-DD HH:mm:ss')
return {formattedDate}
} else {
return 'n/a'
}
}
},
{
title: 'Actions',
key: 'actions',
fixed: 'right',
width: 150,
render: (text, record) => {
return (
}
onClick={() =>
navigate(`/management/products/info?productId=${record._id}`)
}
/>
)
}
}
]
const actionItems = {
items: [
{
label: 'New Product',
key: 'newProduct',
icon:
},
{ type: 'divider' },
{
label: 'Reload List',
key: 'reloadList',
icon:
}
],
onClick: ({ key }) => {
if (key === 'reloadList') {
fetchProductsData()
} else if (key === 'newProduct') {
setNewProductOpen(true)
}
}
}
return (
<>
{contextHolder}
}}
/>
{
setNewProductOpen(false)
}}
destroyOnClose
>
{
setNewProductOpen(false)
fetchProductsData()
}}
reset={newProductOpen}
/>
>
)
}
export default Products