Refactor Filament and Printer Profiles components to streamline state management and remove unused context

- Removed unnecessary useContext for ApiServerContext in both FilamentProfiles and PrinterProfiles components.
- Simplified modal state handling by consolidating profile modal logic into a single state variable for new profiles.
- Cleaned up the handleRowAction function in both components, focusing on new profile creation and removing delete functionality for improved clarity.
This commit is contained in:
Tom Butcher 2026-08-01 14:09:16 +01:00
parent e4657cc692
commit ddd48660e0
2 changed files with 15 additions and 76 deletions

View File

@ -1,14 +1,11 @@
import { useContext, useRef, useState } from 'react'
import { useRef, useState } from 'react'
import { Button, Dropdown, Flex, Modal, Space } from 'antd'
import NewFilamentProfile, {
EditFilamentProfile
} from './FilamentProfiles/NewFilamentProfile'
import NewFilamentProfile from './FilamentProfiles/NewFilamentProfile'
import ColumnViewButton from '../common/ColumnViewButton'
import ExportListButton from '../common/ExportListButton'
import FilterSidebarButton from '../common/FilterSidebarButton'
import ObjectTable from '../common/ObjectTable'
import ObjectTableViewButton from '../common/ObjectTableViewButton'
import { ApiServerContext } from '../context/ApiServerContext'
import PlusIcon from '../../Icons/PlusIcon'
import ReloadIcon from '../../Icons/ReloadIcon'
import useColumnVisibility from '../hooks/useColumnVisibility'
@ -16,11 +13,7 @@ import useFilterSidebarVisibility from '../hooks/useFilterSidebarVisibility'
import useViewMode from '../hooks/useViewMode'
const FilamentProfiles = () => {
const { deleteObject } = useContext(ApiServerContext)
const [profileModal, setProfileModal] = useState({
open: false,
profileId: null
})
const [newProfileOpen, setNewProfileOpen] = useState(false)
const tableRef = useRef()
const [viewMode, setViewMode] = useViewMode('FilamentProfiles')
const [columnVisibility, setColumnVisibility] =
@ -46,34 +39,11 @@ const FilamentProfiles = () => {
if (key === 'reloadList') {
tableRef.current?.reload()
} else if (key === 'newFilamentProfile') {
setProfileModal({ open: true, profileId: null })
setNewProfileOpen(true)
}
}
}
const handleRowAction = (action, profile) => {
if (action.name === 'edit') {
setProfileModal({ open: true, profileId: profile._id })
return true
}
if (action.name === 'delete') {
Modal.confirm({
title: 'Delete filament profile?',
content: `This will permanently delete ${profile.name || profile._reference}.`,
okText: 'Delete',
okType: 'danger',
onOk: async () => {
await deleteObject(profile._id, 'filamentProfile')
await tableRef.current?.reload()
}
})
return true
}
return false
}
return (
<>
<Flex vertical={'true'} gap='large' className='h-100'>
@ -108,34 +78,24 @@ const FilamentProfiles = () => {
visibleColumns={columnVisibility}
showFilterSidebar={showFilterSidebar}
expandHeight={true}
onRowAction={handleRowAction}
/>
</Flex>
<Modal
open={profileModal.open}
open={newProfileOpen}
footer={null}
width={800}
onCancel={() => setProfileModal({ open: false, profileId: null })}
onCancel={() => setNewProfileOpen(false)}
destroyOnHidden
>
{profileModal.open &&
(profileModal.profileId ? (
<EditFilamentProfile
profileId={profileModal.profileId}
onOk={() => {
setProfileModal({ open: false, profileId: null })
tableRef.current?.reload()
}}
/>
) : (
<NewFilamentProfile
onOk={() => {
setProfileModal({ open: false, profileId: null })
tableRef.current?.reload()
}}
/>
))}
{newProfileOpen && (
<NewFilamentProfile
onOk={() => {
setNewProfileOpen(false)
tableRef.current?.reload()
}}
/>
)}
</Modal>
</>
)

View File

@ -1,4 +1,4 @@
import { useContext, useRef, useState } from 'react'
import { useRef, useState } from 'react'
import { Button, Dropdown, Flex, Modal, Space } from 'antd'
import NewPrinterProfile from './PrinterProfiles/NewPrinterProfile'
import ColumnViewButton from '../common/ColumnViewButton'
@ -6,14 +6,12 @@ import ExportListButton from '../common/ExportListButton'
import FilterSidebarButton from '../common/FilterSidebarButton'
import ObjectTable from '../common/ObjectTable'
import ObjectTableViewButton from '../common/ObjectTableViewButton'
import { ApiServerContext } from '../context/ApiServerContext'
import PlusIcon from '../../Icons/PlusIcon'
import ReloadIcon from '../../Icons/ReloadIcon'
import useColumnVisibility from '../hooks/useColumnVisibility'
import useFilterSidebarVisibility from '../hooks/useFilterSidebarVisibility'
import useViewMode from '../hooks/useViewMode'
const PrinterProfiles = () => {
const { deleteObject } = useContext(ApiServerContext)
const [newProfileOpen, setNewProfileOpen] = useState(false)
const tableRef = useRef()
const [viewMode, setViewMode] = useViewMode('PrinterProfiles')
@ -45,24 +43,6 @@ const PrinterProfiles = () => {
}
}
const handleRowAction = (action, profile) => {
if (action.name === 'delete') {
Modal.confirm({
title: 'Delete printer profile?',
content: `This will permanently delete ${profile.name || profile._reference}.`,
okText: 'Delete',
okType: 'danger',
onOk: async () => {
await deleteObject(profile._id, 'printerProfile')
await tableRef.current?.reload()
}
})
return true
}
return false
}
return (
<>
<Flex vertical={'true'} gap='large' className='h-100'>
@ -97,7 +77,6 @@ const PrinterProfiles = () => {
visibleColumns={columnVisibility}
showFilterSidebar={showFilterSidebar}
expandHeight={true}
onRowAction={handleRowAction}
/>
</Flex>