2026-01-27 18:27:29 +05:30
import { memo , useState , useEffect } from "react" ;
import { DropDowns } from "../../../Components/Forms/DropDown" ;
import { Checkbox , Popover } from "antd" ;
import { Settings , X } from "lucide-react" ;
2026-03-04 18:41:02 +05:30
import { PlusCircleOutlined } from '@ant-design/icons' ;
import { Tooltip } from 'antd' ;
import { getActiveProdSubCatData } from "../../../Features/ProductPage/ProductPage" ;
import { useDispatch } from "react-redux" ;
import { getSession } from "../../../Services/Others" ;
2026-01-27 18:27:29 +05:30
2026-03-04 18:41:02 +05:30
const DefaultSettingPopover = memo ( ( { field , options , onSubmit , currentDefault , popOverHeading , checkBoxHeading = 'Apply to all existing rows' , showField , Component = null , triggerModal = ( ) => { } , needAllSubCategories = false } ) => {
const dispatch = useDispatch ( ) ;
2026-01-27 18:27:29 +05:30
const [ selectedValue , setSelectedValue ] = useState ( currentDefault ) ;
const [ applyToAll , setApplyToAll ] = useState ( false ) ;
const [ open , setOpen ] = useState ( false ) ;
2026-03-04 18:41:02 +05:30
const [ fetchSubCategoriesData , setFetchSubCategoriesData ] = useState ( false ) ;
2026-03-04 19:13:48 +05:30
console . log ( fetchSubCategoriesData , "fetchSubCategoriesData" )
2026-01-27 18:27:29 +05:30
useEffect ( ( ) => {
if ( open ) {
setSelectedValue ( currentDefault ) ;
setApplyToAll ( false ) ;
}
} , [ open , currentDefault ] ) ;
2026-03-04 18:41:02 +05:30
useEffect ( ( ) => {
const fetchAllSubCategories = async ( ) => {
2026-03-04 19:13:48 +05:30
2026-03-04 18:41:02 +05:30
await ( dispatch ( getActiveProdSubCatData ( { AppId : getSession ( 'AppId' ) } ) ) ) ? . unwrap ( ) ;
2026-03-04 19:13:48 +05:30
setFetchSubCategoriesData ( false ) ;
2026-03-04 18:41:02 +05:30
}
if ( fetchSubCategoriesData ) {
fetchAllSubCategories ( ) ;
}
} , [ fetchSubCategoriesData ] ) ;
2026-01-27 18:27:29 +05:30
const handleSubmit = ( ) => {
onSubmit ( field , selectedValue , applyToAll ) ;
setOpen ( false ) ;
setApplyToAll ( false ) ;
setSelectedValue ( "" ) ;
} ;
const getFieldTitle = ( field ) => {
switch ( field ) {
case "uom" : return "UOM" ;
case "category" : return "Category" ;
case "subCategory" : return "Sub-Category" ;
case "tax" : return "Tax" ;
default : return field ;
}
} ;
const content = (
< div className = "popover-container" >
< div className = "popover-header" >
< span className = "popover-title" > { popOverHeading } < / span >
< X size = { 14 } className = "close-icon" onClick = { ( ) => setOpen ( false ) } / >
< / div >
2026-03-04 18:41:02 +05:30
{ showField && < div
style = { {
fontSize : '14px' , fontWeight : '500' , color : '#333' , marginBottom : '10px' , padding : '5px 10px' , backgroundColor : '#f0f0f0' , borderRadius : '4px' , textAlign : 'center'
} }
2026-01-27 18:27:29 +05:30
> { showField } < / div > }
2026-03-04 18:41:02 +05:30
< div className = "popover-section" style = { { display : 'flex' , alignItems : 'flex-start' , gap : '5px' } } >
2026-01-27 18:27:29 +05:30
< DropDowns
label = { ` Select default ${ getFieldTitle ( field ) . toLowerCase ( ) } ` }
valueData = { selectedValue }
onChangeFunction = { setSelectedValue }
options = { options }
/ >
2026-03-04 18:41:02 +05:30
{ Component && (
< Tooltip title = { ` Add ${ getFieldTitle ( field ) } ` } placement = "top" >
< PlusCircleOutlined
className = "product-add-iconMargin"
onClick = { ( ) => {
triggerModal ( true ) ;
if ( needAllSubCategories ) {
setFetchSubCategoriesData ( true ) ;
}
} }
/ >
< / Tooltip >
) }
2026-01-27 18:27:29 +05:30
< / div >
2026-03-04 18:41:02 +05:30
{ Component }
2026-01-27 18:27:29 +05:30
< div className = "popover-checkbox" >
< Checkbox
checked = { applyToAll }
onChange = { ( e ) => setApplyToAll ( e . target . checked ) }
>
{ checkBoxHeading }
< / Checkbox >
< / div >
< div className = "popover-actions" >
< button
className = "btn btn--primary"
onClick = { handleSubmit }
disabled = { ! selectedValue }
>
Submit
< / button >
< / div >
< / div >
) ;
return (
2026-03-04 18:41:02 +05:30
< >
< Popover
content = { content }
trigger = "click"
placement = "bottomLeft"
open = { open }
onOpenChange = { setOpen }
overlayClassName = "default-setting-popover"
>
< Settings size = { 14 } className = "setting-icon" / >
< / Popover >
< / >
2026-01-27 18:27:29 +05:30
) ;
} ) ;
export default DefaultSettingPopover ;